Commit Graph

7135 Commits

Author SHA1 Message Date
Akira Matsuda
2812af720e Suppress ⚠️s 2016-01-28 22:30:52 +09:00
Akira Matsuda
60b8ab710d scope needs the second argument 2016-01-28 22:29:55 +09:00
Akira Matsuda
dc925119a3 Revert "Remove valid_scope_name? check - use ruby"
This reverts commit f6db31ec16e42ee7713029f7120f0b011d1ddc6c.

Reason:
Scope names can very easily conflict, particularly when sharing Concerns
within the team, or using multiple gems that extend AR models.

It is true that Ruby has the ability to detect this with the -w option, but the
reality is that we are depending on too many gems that do not care about Ruby
warnings, therefore it might not be a realistic solution to turn this switch on
in our real-world apps.
2016-01-28 18:37:39 +09:00
Sean Griffin
74bee8353b Merge pull request #18928 from bogdan/unreversable-order
Raise AR::IrreversibleOrderError when #reverse_order can not do it's job
2016-01-27 23:26:34 -05:00
Sean Griffin
77383fc1e4 Do not use default attributes for STI when instantiating a subclass
The commit which originally added this behavior did not consider that
doing `Subclass.new` does not actually populate the `type` field in the
attributes (though perhaps it should). We simply need to not use the
defaults for STI related things unless we are instantiating the base
class.

Fixes #23285.
2016-01-27 13:26:20 -05:00
Bogdan Gusiev
14efb42a90 Introduce ActiveRecord::IrreversibleOrderError
Raises when #reverse_order can not process SQL order instead of making
invalid SQL before this patch
2016-01-27 12:53:51 +02:00
Aaron Patterson
6dfab475ca Merge branch '5-0-beta-sec'
* 5-0-beta-sec:
  bumping version
  fix version update task to deal with .beta1.1
  Eliminate instance level writers for class accessors
  allow :file to be outside rails root, but anything else must be inside the rails view directory
  Don't short-circuit reject_if proc
  stop caching mime types globally
  use secure string comparisons for basic auth username / password
2016-01-25 11:25:11 -08:00
Richard Schneeman
4962b44755 Merge pull request #23161 from schneems/schneems/fix-mysql-internalmetadata
[close #23009] Limit key length
2016-01-25 08:54:44 -06:00
Vipul A M
0eef20af66 When verifying size of relation, also verify count is ok. 2016-01-25 13:41:21 +05:30
Rafael França
5119171d39 Merge pull request #23221 from vipulnsward/23209-fix-missin_source_type
Add missing source_type if provided on hmt which belongs to an sti re…
2016-01-24 13:08:32 -05:00
Prathamesh Sonpatki
29cf0dd9f4 Fix AR::Relation#cache_key to remove select scope added by user
- We don't need the select scope added by user as we only want to max
  timestamp and size of the collection. So we already know which columns
  to select.
- Additionally having user defined columns in select scope blows the cache_key
  method with PostGreSQL because it needs all `selected` columns in the group_by
  clause or aggregate function.
- Fixes #23038.
2016-01-24 22:36:13 +05:30
Vipul A M
b334bc93c6 Add missing source_type if provided on hmt which belongs to an sti record
Fixes #23209
2016-01-24 22:25:12 +05:30
Kasper Timm Hansen
8c1f248c58 Merge pull request #23081 from prathamesh-sonpatki/fix-cache-key-for-queries-with-offset
Fix ActiveRecord::Relation#cache_key for relations with no results
2016-01-24 16:24:31 +01:00
Prathamesh Sonpatki
909818b93b Pare back default index option for the migration generator
- Using `references` or `belongs_to` in migrations will always add index
  for the referenced column by default, without adding `index:true` option
  to generated migration file.
- Users can opt out of this by passing `index: false`.
- Legacy migrations won't be affected by this change. They will continue
  to run as they were before.
- Fixes #18146
2016-01-24 18:48:15 +05:30
Sean Griffin
67c1719012 Use the database type to deserialize enum
This fixes incorrect assumptions made by e991c7b that we can assume the
DB is already casting the value for us. The enum type needs additional
information to perform casting, and needs a subtype.

I've opted not to call `super` in `cast`, as we have a known set of
types which we accept there, and the subtype likely doesn't accept them
(symbol -> integer doesn't make sense)

Close #23190
2016-01-23 08:44:16 -07:00
Andrew White
0fde6f554b Don't short-circuit reject_if proc
When updating an associated record via nested attribute hashes the
reject_if proc could be bypassed if the _destroy flag was set in the
attribute hash and allow_destroy was set to false.

The fix is to only short-circuit if the _destroy flag is set and the
option allow_destroy is set to true. It also fixes an issue where
a new record wasn't created if _destroy was set and the option
allow_destroy was set to false.

CVE-2015-7577
2016-01-22 15:00:50 -08:00
Prathamesh Sonpatki
66023eccb6 Fix ActiveRecord::Relation#cache_key for relations with no results
- When relations return no result or 0 result then cache_key should
  handle it gracefully instead of blowing up trying to access
  `result[:size]` and `result[:timestamp]`.
- Fixes #23063.
2016-01-22 11:19:10 +05:30
Matthew Draper
a688c0317d Merge pull request #23080 from prathamesh-sonpatki/fix-cache-key-for-loaded-empty-collection
Fix ActiveRecord::Relation#cache_key for loaded empty collection
2016-01-22 16:07:59 +10:30
schneems
8897e05ea3 [close #23009] Limit key length
Mysql has a weird bug where it cannot index a string column of utf8mb4 if it is over a certain character limit. To get compatibility with msql we can add a limit to the key column. 191 characters is a very long key, it seems reasonable to limit across all adapters since using a longer key wouldn't be supported in mysql.

Thanks to @kamipo for the original PR and the test refactoring.

Conversation: https://github.com/rails/rails/pull/23009#issuecomment-171416629
2016-01-21 13:19:33 -06:00
Prathamesh Sonpatki
e55ba530c1 Fix test failure on PostgreSQL by sorting the result before comparison 2016-01-19 22:45:25 +05:30
Yves Senn
3ea4476942 run type column through attribtues API type casting.
Closes #21986.

This makes it possible to write custom types that define a different
mapping for STI columns.
2016-01-19 17:31:16 +01:00
Vipul A M
da26934313 Changed options for find_each and variants to have options start/finish instead of start_at/end_at based on comments
at https://github.com/rails/rails/pull/12257#issuecomment-74688344
2016-01-18 12:40:30 +05:30
Prathamesh Sonpatki
8126925447 Fix ActiveRecord::Relation#cache_key for loaded empty collection
- Before this patch if we try to find cache_key of a loaded but empty
  collection it used to give error because of trying to call `updated_at`
  on `nil` value generated by
  `collection.max_by(&timestamp_column).public_send(timestamp_column)`.
- This commit fixes above error by checking if size is greater than zero
  or not.
2016-01-16 12:47:26 +05:30
Rafael França
de2259791c Merge pull request #20005 from kamipo/default_expression_support
Add `:expression` option support on the schema default
2016-01-16 04:18:21 -02:00
Rafael França
efe346f23a Merge pull request #23067 from kamipo/sql_for_insert_returns_values_for_passing_to_exec_insert
`sql_for_insert` returns values for passing to `exec_insert`
2016-01-16 03:42:55 -02:00
Ryuta Kamizono
89653edaf6 sql_for_insert returns values for passing to exec_insert 2016-01-15 20:52:36 +09:00
schneems
a95a554c7e Fix intermittent test failures
😳
2016-01-14 15:33:14 -06:00
schneems
4fc3074403 Revert "Revert "Set environment even when no migration runs""
This reverts commit 11e85b91731ca6125ee1db33553f984549a3bc2b.
2016-01-14 15:09:18 -06:00
Sean Griffin
11e85b9173 Revert "Set environment even when no migration runs" 2016-01-14 13:59:10 -07:00
Kasper Timm Hansen
1aa1cec777 Revert "Merge pull request #20835 from glittershark/if-and-unless-in-secure-token"
This reverts commit 224eddfc0eeff6555ae88691306e61c7a9e8b758, reversing
changes made to 9d681fc74c6251d5f2b93fa9576c9b2113116680.

When merging the pull request, I misunderstood `has_secure_token` as declaring a model
has a token from birth and through the rest of its lifetime.

Therefore, supporting conditional creation doesn't make sense. You should never mark a
model as having a secure token if there's a time when it shouldn't have it on creation.
2016-01-14 21:52:03 +01:00
Sean Griffin
434ec23714 Merge pull request #23048 from kamipo/substitute_at_is_no_longer_used
`substitute_at` is no longer used
2016-01-14 13:43:04 -07:00
Sean Griffin
f138bea5da Properly include the from clause when merging relations
The code that set the from clause was removed in
bdc5141652770fd227455681cde1f9899f55b0b9. I did not give any reason for
doing so. My assumption was that I intended to change it to use the
clause objects, but forgot. We appeared to not have test coverage for
this case.

Fixes #22996
2016-01-14 13:36:30 -07:00
Richard Schneeman
5854de3885 Merge pull request #23017 from schneems/schneems/migration-sets-environment-value
Set environment even when no migration runs
2016-01-14 13:07:35 -06:00
Ryuta Kamizono
a557ab00f5 substitute_at is no longer used
Arel handles substitution for bind parameters by now.
2016-01-14 09:59:26 +09:00
Ryuta Kamizono
744552f72d Fix extract default with CURRENT_TIMESTUMP 2016-01-13 22:03:56 +09:00
Ryuta Kamizono
5fd30ac52d Add expression support on the schema default
Example:

    create_table :posts do |t|
      t.datetime :published_at, default: -> { 'NOW()' }
    end
2016-01-13 22:03:56 +09:00
Yves Senn
ede2343031 fix regression when loading fixture files with symbol keys.
Closes #22584.
2016-01-13 10:11:54 +01:00
Rafael Mendonça França
f466cd7fc4 Improve error message for #or when it is structurally incompatible
When you are using scopes and you chaining these scopes it is hard to
know which are the values that are incompatible. This way you can read
the message and know for which values you need to look for.

[Herminio Torres]
2016-01-13 01:13:18 -02:00
Sean Griffin
b64b754510 Revert "Change WhereClause#merge to same named columns on diff tables"
This reverts commit 5d41cb3bfd6b19833261622ce5d339b1e580bd8b.

This implementation does not properly handle cases involving predicates
which are not associated with a bind param. I have the fix in mind, but
don't have time to implement just yet. It will be more similar to #22823
than not.
2016-01-12 15:48:00 -07:00
Sean Griffin
5d41cb3bfd Change WhereClause#merge to same named columns on diff tables
While the predicates are an arel equality node where the left side is a
full arel attribute, the binds just have the name of the column and
nothing else. This means that while splitting the predicates can include
the table as a factor, the binds cannot. It's entirely possible that we
might be able to have the bind params carry a bit more information (I
don't believe the name is used for anything but logging), and that is
probably a worthwhile change to make in the future.

However the simplest (and likely slightly faster) solution is to simply
use the indices of the conflicts in both cases. This means that we only
have to compute the collision space once, instead of twice even though
we're doing an additional array iteration. Regardless, this method isn't
a performance hotspot.

Close #22823.

[Ben Woosley & Sean Griffin]
2016-01-12 14:38:25 -07:00
Matthew Draper
5c6d365383 Skip the STI condition when evaluating a default scope
Given a default_scope on a parent of the current class, where that
parent is not the base class, the parent's STI condition would become
attached to the evaluated default scope, and then override the child's
own STI condition.

Instead, we can treat the STI condition as though it is a default scope,
and skip it in this situation: the scope will be merged into the base
relation, which already contains the correct STI condition.

Fixes #22426.
2016-01-12 05:43:58 +10:30
Matthew Draper
8ce0175d50 Active scopes apply to child classes, though not parents/siblings
While the commit message (and changelog example) in
5e0b555b453ea2ca36986c111512627d806101e7 talked about sibling classes,
the added test had a child ignore its parent's scoping, which seems less
reasonable.
2016-01-12 05:43:58 +10:30
schneems
4d60e93174 Set environment even when no migration runs
This PR addresses the issue described in https://github.com/rails/rails/pull/22967#issuecomment-170251635. If the database is non empty and has no new migrations than `db:migrate` will not set the environment. This PR works by always setting the environment value on successful `up` migration regardless of whether or not a migration was actually executed.
2016-01-11 11:58:12 -06:00
schneems
a703999c62 Clean up duplicate migration logic 2016-01-11 11:27:32 -06:00
Ryuta Kamizono
560bf1c939 Extract MySQL::TypeMetadata class to connection_adapters/mysql/type_metadata.rb 2016-01-11 05:03:31 +09:00
Ryuta Kamizono
83026cb15d Extract MySQL::Column class to connection_adapters/mysql/column.rb 2016-01-11 04:54:03 +09:00
Kasper Timm Hansen
492d203986 Split out token if tests to trigger before_create.
When running passing condition assertions in the same test the user had already
been saved at that point.

Split out so we have a not yet persisted user.

Rename condition tests to improve clarity a bit.
2016-01-09 23:33:57 +01:00
Kasper Timm Hansen
62abae9172 Add conditional_token to users.
Fix the NoMethodErrors introduced in 224eddf, when adding conditional token creation.

The model declarations but the column wasn't added to the schema.
2016-01-09 22:31:13 +01:00
Griffin Smith
21c0a1f301 Support :if and :unless in has_secure_token
Pass through :if and :unless options from has_secure_token to the
generated before_create callback
2016-01-09 14:49:00 -05:00
Sean Griffin
34ac8a1e98 Merge pull request #22976 from schneems/schneems/schema-migration-primary-key
Get update_attributes working with SchemaMigration
2016-01-08 17:44:26 -07:00