aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/test/schema/schema.rb
Commit message (Collapse)AuthorAgeFilesLines
* Only nullify persisted has_one target associationsAgis-2015-08-241-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | Since after 87d1aba3c `dependent: :destroy` callbacks on has_one assocations run *after* destroy, it is possible that a nullification is attempted on an already destroyed target: class Car < ActiveRecord::Base has_one :engine, dependent: :nullify end class Engine < ActiveRecord::Base belongs_to :car, dependent: :destroy end > car = Car.create! > engine = Engine.create!(car: car) > engine.destroy! # => ActiveRecord::ActiveRecordError: cannot update a > destroyed record In the above case, `engine.destroy!` deletes `engine` and *then* triggers the deletion of `car`, which in turn triggers a nullification of `engine.car_id`. However, `engine` is already destroyed at that point. Fixes #21223.
* Merge pull request #19683 from tristang/require-option-for-counter-cacheRafael Mendonça França2015-08-131-0/+2
|\ | | | | | | Require explicit counter_cache option for has_many
| * Add tests for associations without counter_cacheTristan Gamilis2015-04-091-0/+2
| | | | | | | | | | Assert that counter_cache behaviour is not used on belongs_to or has_many associations if the option is not given explicitly.
* | Skip statement cache on through association readerRafael Mendonça França2015-08-121-0/+12
| | | | | | | | | | | | | | If the through class has default scopes we should skip the statement cache. Closes #20745.
* | use correct DB connection for generated HABTM tableMatt Hanlon2015-08-071-0/+9
| |
* | Fix counter_cache for polymorphic associationsStefan Kanev2015-07-191-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | Also removes a false positive test that depends on the fixed bug: At this time, counter_cache does not work with polymorphic relationships (which is a bug). The test was added to make sure that no StaleObjectError is raised when the car is destroyed. No such error is currently raised because the lock version is not incremented by appending a wheel to the car. Furthermore, `assert_difference` succeeds because `car.wheels.count` does not check the counter cache, but the collection size. The test will fail if it is replaced with `car.wheels_count || 0`.
* | Ensure cyclic associations w/ autosave don't cause duplicate errorsSean Griffin2015-07-181-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | This code is so fucked. Things that cause this bug not to replicate: - Defining the validation before the association (we end up calling `uniq!` on the errors in the autosave validation) - Adding `accepts_nested_attributes_for` (I have no clue why. The only thing it does that should affect this is adds `autosave: true` to the inverse reflection, and doing that manually doesn't fix this). This solution is a hack, and I'm almost certain there's a better way to go about it, but this shouldn't cause a huge hit on validation times, and is the simplest way to get it done. Fixes #20874.
* | Add pending test for the great-grandparent touching bug from #19324David Heinemeier Hansson2015-06-251-0/+11
| |
* | Add enum prefix/suffix option to enum definitionIgor Kapkov2015-06-121-0/+4
| | | | | | | | Fixes #17511 and #17415
* | Fix crash when loading fixture with belongs_to association defined in ↵Victor Costan2015-06-041-0/+5
| | | | | | | | abstract base class.
* | Move PostgreSQL specific schema to postgresql_specific_schema.rbRyuta Kamizono2015-05-031-14/+0
|/
* Fix leaky chain on polymorphic associationeileencodes2015-03-151-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | If there was a polymorphic hm:t association with a scope AND second non-scoped hm:t association on a model the polymorphic scope would leak through into the call for the non-polymorhic hm:t association. This would only break if `hotel.drink_designers` was called before `hotel.recipes`. If `hotel.recipes` was called first there would be no problem with the SQL. Before (employable_type should not be here): ``` SELECT COUNT(*) FROM "drink_designers" INNER JOIN "chefs" ON "drink_designers"."id" = "chefs"."employable_id" INNER JOIN "departments" ON "chefs"."department_id" = "departments"."id" WHERE "departments"."hotel_id" = ? AND "chefs"."employable_type" = ? [["hotel_id", 1], ["employable_type", "DrinkDesigner"]] ``` After: ``` SELECT COUNT(*) FROM "recipes" INNER JOIN "chefs" ON "recipes"."chef_id" = "chefs"."id" INNER JOIN "departments" ON "chefs"."department_id" = "departments"."id" WHERE "departments"."hotel_id" = ? [["hotel_id", 1]] ``` From the SQL you can see that `employable_type` was leaking through when calling recipes. The solution is to dup the chain of the polymorphic association so it doesn't get cached. Additionally, this follows `scope_chain` which dup's the `source_reflection`'s `scope_chain`. This required another model/table/relationship because the leak only happens on a hm:t polymorphic that's called before another hm:t on the same model. I am specifically testing the SQL here instead of the number of records becasue the test could pass if there was 1 drink designer recipe for the drink designer chef even though the `employable_type` was leaking through. This needs to specifically check that `employable_type` is not in the SQL statement.
* Merge pull request #17297 from ↵Rafael Mendonça França2015-02-251-0/+1
|\ | | | | | | | | rebyn/fix/17161-remove-objs-from-has_many-updates-fields Add specs for adding-to/clear has_many collections’s behavior on `updated_at`
| * Add specs for adding-to/clear has_many collections’s behavior on `updated_at`Tu Hoang2014-10-301-0/+1
| | | | | | | | | | | | | | | | | | | | | | There are behaviors mentioned in #17161 that: 1. are not documented properly, and 2. don't have specs This commit addresses the spec absence. For has_many collections, 1. addition (<<) should update the associated object's updated_at (if any) 2. .clear, depending on options[:dependent], calls delete_all, destroy_all, or nullifies the associated object(s)' foreign key.
* | Add `ActiveRecord::Base.suppress`Michael Ryan2015-02-181-0/+4
| |
* | Removed magic comments # encoding: utf-8 , since its default from ruby 2.0 ↵Vipul A M2015-02-031-1/+0
| | | | | | | | onwards.
* | Merge pull request #18512 from vipulnsward/18492-fixtures-with-stiAaron Patterson2015-01-311-1/+11
|\ \ | | | | | | Fix STI for fixtures from multi-files
| * | Fixes #18492Vipul A M2015-01-141-1/+11
| | | | | | | | | | | | | | | | | | | | | - Add check for not deleting previously created fixtures, to overcome sti fixtures from multiple files - Added fixtures and fixtures test to verify the same - Fixed wrong fixtures duplicating data insertion in same table
* | | Don't redefine autosave association callbacks in nested attrsSean Griffin2015-01-281-0/+1
|/ / | | | | | | | | | | | | | | These callbacks will already have been defined when the association was built. The check against `reflection.autosave` happens at call time, not at define time, so simply modifying the reflection is sufficient. Fixes #18704
* | Add firebird support to test suiteRay Zane2015-01-051-0/+2
| |
* | Add has_secure_token to Active Recordrobertomiranda2015-01-041-0/+5
| | | | | | | | | | | | Update SecureToken Docs Add Changelog entry for has_secure_token [ci skip]
* | Allow precision option for MySQL datetimes.Ryuta Kamizono2015-01-021-1/+1
| |
* | Add foreign_type option for polymorphic has_one and has_many.Ulisses Almeida + Kassio Borges2014-12-081-0/+5
| | | | | | | | | | | | | | To be possible to use a custom column name to save/read the polymorphic associated type in a has_many or has_one polymorphic association, now users can use the option :foreign_type to inform in what column the associated object type will be saved.
* | Ensure HABTM relationships produce valid class names (Fixes #17119)Sammy Larbi2014-11-091-0/+5
| |
* | Use bind values for joined tables in where statementsSean Griffin2014-11-011-0/+5
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | In practical terms, this allows serialized columns and tz aware columns to be used in wheres that go through joins, where they previously would not behave correctly. Internally, this removes 1/3 of the cases where we rely on Arel to perform type casting for us. There were two non-obvious changes required for this. `update_all` on relation was merging its bind values with arel's in the wrong order. Additionally, through associations were assuming there would be no bind parameters in the preloader (presumably because the where would always be part of a join) [Melanie Gilman & Sean Griffin]
* | 💣Sean Griffin2014-10-281-1/+1
|/ | | | We were relying on hash inequality in tests
* Dynamically modified schema and association would not be correctly resetAkira Matsuda2014-09-061-0/+6
| | | | | This fixes <"SQLite3::SQLException: no such column: legacy_things.person_id: SELECT \"legacy_things\".* FROM \"legacy_things\" WHERE \"legacy_things\".\"person_id\" = ?"> in OptimisticLockingTest#test_lock_destroy
* Added enable_extension! to helperAbdelkader Boudih2014-09-051-1/+1
|
* Only merge scopes with zero arity in has_many throughAgis-2014-08-201-0/+1
| | | | | | | | | | | | | | | with dynamic conditions. Fixes #16128 This bug was introduced in https://github.com/rails/rails/commit/c35e438620f2d56562251571377995359546393d so it's present from 4.1.2-rc1 and after. https://github.com/rails/rails/commit/c35e438620f2d56562251571377995359546393d merges any relation scopes passed as proc objects to the relation, but does *not* take into account the arity of the lambda. To reproduce: https://gist.github.com/Agis-/5f1f0d664d2cd08dfb9b
* Change the default `null` value for timestampsSean Griffin2014-08-121-5/+5
| | | | | | | As per discussion, this changes the model generators to specify `null: false` for timestamp columns. A warning is now emitted if `timestamps` is called without a `null` option specified, so we can safely change the behavior when no option is specified in Rails 5.
* use foreign key DSL in our tests.Yves Senn2014-07-161-3/+2
|
* Address ORA-00972: identifier is too long when tested with OracleYasuo Honda2014-07-141-2/+2
| | | | by using shorter attribute names.
* Re-enable foriegn key tests on MySQLGodfrey Chan2014-07-051-3/+1
| | | | This reverts commit e84799d, e31104c and e6ca8e2
* MySQL doesn't work with foreign keysPhilippe Creux2014-06-271-1/+4
| | | | | | This was previously fixed in e84799d but broken in 3f596f8. This commit reintroduced the conditional that prevents the foreign keys from being added to MySQL databases.
* Fix test with add_foreign_key DSL in 9d21ef9Philippe Creux2014-06-271-1/+1
|
* Do not change the global state of the test suiteRafael Mendonça França2014-06-271-0/+1
|
* fk: use random digest namesYves Senn2014-06-261-2/+2
| | | | | | The name of the foreign key is not relevant from a users perspective. Using random names resolves the urge to rename the foreign key when the respective table or column is renamed.
* Deprecate automatic counter caches on has_many :throughSean Griffin2014-06-261-2/+1
| | | | | | | | | | | Reliant on https://github.com/rails/rails/pull/15747 but pulled to a separate PR to reduce noise. `has_many :through` associations have the undocumented behavior of automatically detecting counter caches. However, the way in which it does so is inconsistent with counter caches everywhere else, and doesn't actually work consistently. As with normal `has_many` associations, the user should specify the counter cache on the `belongs_to`, if they'd like it updated.
* Fix has_and_belongs_to_many in a namespaced model pointing to a non ↵Rafael Mendonça França2014-06-191-0/+5
| | | | | | | | | | | | | | | namespaced model Now the following case will work fine class Tag < ActiveRecord::Base end class Publisher::Article < ActiveRecord::Base has_and_belongs_to_many :tags end Fixes #15761
* Merge pull request #15343 from dontfidget/fix_polymorphic_automatic_inverse_ofRafael Mendonça França2014-06-131-0/+2
|\ | | | | prevent bad automatic inverse_of association
| * use name specified by 'as' for automatic inverse association to avoid ↵Andrew S. Brown2014-06-101-0/+2
| | | | | | | | reflecting on wrong association
* | Timestamp values should be present on callbacksRafael Mendonça França2014-06-091-0/+1
| | | | | | | | | | | | | | This reverts commit dd3ea17191e316aeebddaa7b176f6cfeee7a6365 and add a regression test. Fixes #15418
* | Allow specifying a default value in overloaded propertiesSean Griffin2014-05-301-0/+1
| |
* | Add a public API to allow users to specify column typesSean Griffin2014-05-261-0/+6
|/ | | | | | | | | | | | | | | | | | | | | | | | As a result of all of the refactoring that's been done, it's now possible for us to define a public API to allow users to specify behavior. This is an initial implementation so that I can work off of it in smaller pieces for additional features/refactorings. The current behavior will continue to stay the same, though I'd like to refactor towards the automatic schema detection being built off of this API, and add the ability to opt out of automatic schema detection. Use cases: - We can deprecate a lot of the edge cases around types, now that there is an alternate path for users who wish to maintain the same behavior. - I intend to refactor serialized columns to be built on top of this API. - Gem and library maintainers are able to interact with `ActiveRecord` at a slightly lower level in a more stable way. - Interesting ability to reverse the work flow of adding to the schema. Model can become the single source of truth for the structure. We can compare that to what the database says the schema is, diff them, and generate a migration.
* Fixed serialization for records with an attribute named `format`.Godfrey Chan2014-05-221-0/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | * * * This bug can be triggered when serializing record R (the instance) of type C (the class), provided that the following conditions are met: 1. The name of one or more columns/attributes on C/R matches an existing private method on C (e.g. those defined by `Kernel`, such as `format`). 2. The attribute methods have not yet been generated on C. In this case, the matching private methods will be called by the serialization code (with no arguments) and their return values will be serialized instead. If the method requires one or more arguments, it will result in an `ArgumentError`. This regression is introduced in d1316bb. * * * Attribute methods (e.g. `#name` and `#format`, assuming the class has columns named `name` and `format` in its database table) are lazily defined. Instead of defining them when a the class is defined (e.g. in the `inherited` hook on `ActiveRecord::Base`), this operation is deferred until they are first accessed. The reason behind this is that is defining those methods requires knowing what columns are defined on the database table, which usually requires a round-trip to the database. Deferring their definition until the last-possible moment helps reducing unnessary work, especially in development mode where classes are redefined and throw away between requests. Typically, when an attribute is first accessed (e.g. `a_book.format`), it will fire the `method_missing` hook on the class, which triggers the definition of the attribute methods. This even works for methods like `format`, because calling a private method with an explicit receiver will also trigger that hook. Unfortunately, `read_attribute_for_serialization` is simply an alias to `send`, which does not respect method visibility. As a result, when serializing a record with those conflicting attributes, the `method_missing` is not fired, and as a result the attribute methods are not defined one would expected. Before d1316bb, this is negated by the fact that calling the `run_callbacks` method will also trigger a call to `respond_to?`, which is another trigger point for the class to define its attribute methods. Therefore, when Active Record tries to run the `after_find` callbacks, it will also define all the attribute methods thus masking the problem. * * * The proper fix for this problem is probably to restrict `read_attribute_for_serialization` to call public methods only (i.e. alias `read_attribute_for_serialization` to `public_send` instead of `send`). This however would be quite risky to change in a patch release and would probably require a full deprecation cycle. Another approach would be to override `read_attribute_for_serialization` inside Active Record to force the definition of attribute methods: def read_attribute_for_serialization(attribute) self.class.define_attribute_methods send(attribute) end Unfortunately, this is quite likely going to cause a performance degradation. This patch therefore restores the behaviour from the 4-0-stable branch by explicitly forcing the class to define its attribute methods in a similar spot (when records are initialized). This should not cause any extra roundtrips to the database because the `@columns` should already be cached on the class. Fixes #15188.
* Revert "Merge pull request #14544 from jefflai2/named_scope_sti"Rafael Mendonça França2014-05-211-2/+0
| | | | | | | | | | | | This reverts commit 9a1abedcdeecd9464668695d4f9c1d55a2fd9332, reversing changes made to c72d6c91a7c0c2dc81cc857a1d6db496e84e0065. Conflicts: activerecord/CHANGELOG.md activerecord/test/models/comment.rb This change break integration with activerecord-deprecated_finders so I'm reverting until we find a way to make it work with this gem.
* Fix polymorphic eager load with foreign_key as String.Lauro Caetano2014-05-201-0/+3
| | | | | | | | | The foreign_key could be `String` and just doing `owners_map[owner_key]` could return `nil`. To prevent this bug, we should `to_s` both keys if their types are different. Fixes #14734.
* Merge pull request #14544 from jefflai2/named_scope_stiRafael Mendonça França2014-05-201-0/+2
|\ | | | | | | | | | | | | Fixes Issue #13466. Conflicts: activerecord/CHANGELOG.md
| * Fixes Issue #13466.Jefferson Lai2014-04-231-0/+2
| | | | | | | | | | | | Changed the call to a scope block to be evaluated with instance_eval. The result is that ScopeRegistry can use the actual class instead of base_class when caching scopes so queries made by classes with a common ancestor won't leak scopes.
* | Remove dead test code for unsupported adaptersSean Griffin2014-05-171-8/+0
| |