aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/fixtures.rb
Commit message (Collapse)AuthorAgeFilesLines
* Allow specify fixtures to be ignoredTongfei Gao2019-07-271-3/+42
| | | | | | | | | | | | | | | | | | | | | | | | | Allow specifying what fixtures can be ignored by setting `ignore` in fixtures YAML file: # users.yml _fixture: ignore: - base base: &base admin: false introduction: "This is a default description" admin: <<: *base admin: true visitor: <<: *base In the above example, "base" fixture will be ignored when creating users fixture. This is helpful when you want to inherit attributes and it makes your fixtures more "DRY".
* Enable `Layout/EmptyLinesAroundAccessModifier` copRyuta Kamizono2019-06-131-3/+0
| | | | | | | | | | | We sometimes say "✂️ newline after `private`" in a code review (e.g. https://github.com/rails/rails/pull/18546#discussion_r23188776, https://github.com/rails/rails/pull/34832#discussion_r244847195). Now `Layout/EmptyLinesAroundAccessModifier` cop have new enforced style `EnforcedStyle: only_before` (https://github.com/rubocop-hq/rubocop/pull/7059). That cop and enforced style will reduce the our code review cost.
* Enable `Style/RedundantBegin` cop to avoid newly adding redundant begin blockRyuta Kamizono2018-12-211-5/+3
| | | | | | | | | | Currently we sometimes find a redundant begin block in code review (e.g. https://github.com/rails/rails/pull/33604#discussion_r209784205). I'd like to enable `Style/RedundantBegin` cop to avoid that, since rescue/else/ensure are allowed inside do/end blocks in Ruby 2.5 (https://bugs.ruby-lang.org/issues/12906), so we'd probably meets with that situation than before.
* Upgrade Rubocop to 0.61.1 and fix offensesVinicius Stock2018-12-101-33/+33
|
* Don't pass useless `table_name` to `ModelMetadata.new`Ryuta Kamizono2018-11-031-3/+1
| | | | | | | The `model_metadata` is only used if `model_class` is given. If `model_class` is given, the `table_name` is always `model_class.table_name`.
* Don't pass unused `connection` to `FixtureSet.new`Ryuta Kamizono2018-11-031-5/+2
| | | | | | | | | | The `@connection` is no longer used since ee5ab22. Originally the `@connection` was useless because it is only used in `timestamp_column_names`, which is only used if `model_class` is given. If `model_class` is given, the `@connection` is always `model_class.connection`.
* Move FixtureSet::ReflectionProxy and FixtureSet::HasManyThroughProxy to ↵Gannon McGibbon2018-10-051-32/+0
| | | | FixtureSet::TableRows
* Introduce FixtureSet::TableRows and FixtureSet::TableRowGannon McGibbon2018-10-051-88/+7
|
* Introduce FixtureSet::ModelMetadataGannon McGibbon2018-10-051-29/+9
|
* Small refactors to FixtureSet::ClassCache and FixtureGannon McGibbon2018-10-051-9/+8
|
* Move FixtureSet.create_fixtures reading and inserting sections into private ↵Gannon McGibbon2018-10-051-41/+58
| | | | methods
* Organize FixtureSet class methodsGannon McGibbon2018-10-051-102/+104
|
* Move test_fixtures and render_context to separate filesGannon McGibbon2018-10-031-214/+2
|
* Raise an error when loading all fixtures from nil fixture_pathGannon McGibbon2018-09-261-0/+1
| | | | [Gannon McGibbon + Max Albrecht]
* [ci skip] Change references from Rake task to Rails commandAlberto Almagro2018-08-011-2/+2
| | | | | This commit follows the path we started at commit #ea4f0e2 and continued at PR #33229.
* Add test parallelization to Railseileencodes2018-02-151-2/+3
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Provides both a forked process and threaded parallelization options. To use add `parallelize` to your test suite. Takes a `workers` argument that controls how many times the process is forked. For each process a new database will be created suffixed with the worker number; test-database-0 and test-database-1 respectively. If `ENV["PARALLEL_WORKERS"]` is set the workers argument will be ignored and the environment variable will be used instead. This is useful for CI environments, or other environments where you may need more workers than you do for local testing. If the number of workers is set to `1` or fewer, the tests will not be parallelized. The default parallelization method is to fork processes. If you'd like to use threads instead you can pass `with: :threads` to the `parallelize` method. Note the threaded parallelization does not create multiple database and will not work with system tests at this time. parallelize(workers: 2, with: :threads) The threaded parallelization uses Minitest's parallel exector directly. The processes paralleliztion uses a Ruby Drb server. For parallelization via threads a setup hook and cleanup hook are provided. ``` class ActiveSupport::TestCase parallelize_setup do |worker| # setup databases end parallelize_teardown do |worker| # cleanup database end parallelize(workers: 2) end ``` [Eileen M. Uchitelle, Aaron Patterson]
* Use assert_predicate and assert_not_predicateDaniel Colson2018-01-251-3/+3
|
* Build a multi-statement query when inserting fixtures:Edouard CHIN2018-01-221-34/+25
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | - The `insert_fixtures` method can be optimized by making a single multi statement query for all fixtures having the same connection instead of doing a single query per table - The previous code was bulk inserting fixtures for a single table, making X query for X fixture files - This patch builds a single **multi statement query** for every tables. Given a set of 3 fixtures (authors, dogs, computers): ```ruby # before %w(authors dogs computers).each do |table| sql = build_sql(table) connection.query(sql) end # after sql = build_sql(authors, dogs, computers) connection.query(sql) ``` - `insert_fixtures` is now deprecated, `insert_fixtures_set` is the new way to go with performance improvement - My tests were done with an app having more than 700 fixtures, the time it takes to insert all of them was around 15s. Using a single multi statement query, it took on average of 8 seconds - In order for a multi statement to be executed, mysql needs to be connected with the `MULTI_STATEMENTS` [flag](https://dev.mysql.com/doc/refman/5.7/en/c-api-multiple-queries.html), which is done before inserting the fixtures by reconnecting to da the database with the flag declared. Reconnecting to the database creates some caveats: 1. We loose all open transactions; Inside the original code, when inserting fixtures, a transaction is open. Multple delete statements are [executed](https://github.com/rails/rails/blob/a681eaf22955734c142609961a6d71746cfa0583/activerecord/lib/active_record/fixtures.rb#L566) and finally the fixtures are inserted. The problem with this patch is that we need to open the transaction only after we reconnect to the DB otherwise reconnecting drops the open transaction which doesn't commit all delete statements and inserting fixtures doesn't work since we duplicated them (Primary key duplicate exception)... - In order to fix this problem, the transaction is now open directly inside the `insert_fixtures` method, right after we reconnect to the db - As an effect, since the transaction is open inside the `insert_fixtures` method, the DELETE statements need to be executed here since the transaction is open later 2. The same problem happens for the `disable_referential_integrity` since we reconnect, the `FOREIGN_KEY_CHECKS` is reset to the original value - Same solution as 1. , the disable_referential_integrity can be called after we reconnect to the transaction 3. When the multi statement query is executed, no other queries can be performed until we paginate over the set of results, otherwise mysql throws a "Commands out of sync" [Ref](https://dev.mysql.com/doc/refman/5.7/en/commands-out-of-sync.html) - Iterating over the set of results until `mysql_client.next_result` is false. [Ref](https://github.com/brianmario/mysql2#multiple-result-sets) - Removed the `active_record.sql "Fixture delete"` notification, the delete statements are now inside the INSERT's one - On mysql the `max_allowed_packet` is looked up: 1. Before executing the multi-statements query, we check the packet length of each statements, if the packet is bigger than the max_allowed_packet config, an `ActiveRecordError` is raised 2. Otherwise we concatenate the current sql statement into the previous and so on until the packet is `< max_allowed_packet`
* [Active Record] require => require_relativeAkira Matsuda2017-10-211-2/+2
| | | | This basically reverts 9d4f79d3d394edb74fa2192e5d9ad7b09ce50c6d
* Use tt in doc for ActiveRecord [ci skip]Yoshiyuki Hirano2017-08-271-1/+1
|
* Add `binary` helper method to fixtures.yalab2017-08-121-0/+4
|
* Use frozen-string-literal in ActiveRecordKir Shatrov2017-07-191-0/+2
|
* [Active Record] require => require_relativeAkira Matsuda2017-07-011-2/+2
|
* Use bulk INSERT to insert fixturesKir Shatrov2017-06-201-3/+1
| | | | | | | | Improves the performance from O(n) to O(1). Previously it would require 50 queries to insert 50 fixtures. Now it takes only one query. Disabled on sqlite which doesn't support multiple inserts.
* Use mattr_accessor default: option throughout the projectGenadi Samokovarov2017-06-031-2/+1
|
* Add option for class_attribute default (#29270)David Heinemeier Hansson2017-05-291-14/+6
| | | | | | | | | | | | * Allow a default value to be declared for class_attribute * Convert to using class_attribute default rather than explicit setter * Removed instance_accessor option by mistake * False is a valid default value * Documentation
* Passing in no arguments to the dynamic fixture accessor method returns all ↵Kevin McPhillips2017-04-071-4/+25
| | | | fixtures, not an empty array.
* Ensure test threads share a DB connectioneileencodes2017-02-201-0/+3
| | | | | | | | | | | | | | | | | | | | | | | This ensures multiple threads inside a transactional test to see consistent database state. When a system test starts Puma spins up one thread and Capybara spins up another thread. Because of this when tests are run the database cannot see what was inserted into the database on teardown. This is because there are two threads using two different connections. This change uses the statement cache to lock the threads to using a single connection ID instead of each not being able to see each other. This code only runs in the fixture setup and teardown so it does not affect real production databases. When a transaction is opened we set `lock_thread` to `Thread.current` so we can keep track of which connection the thread is using. When we rollback the transaction we unlock the thread and then there will be no left-over data in the database because the transaction will roll back the correct connections. [ Eileen M. Uchitelle, Matthew Draper ]
* `self.` is not needed when calling its own instance methodAkira Matsuda2017-01-051-1/+1
| | | | Actually, private methods cannot be called with `self.`, so it's not just redundant, it's a bad habit in Ruby
* Remove deprecated #use_transactional_fixtures configurationRafael Mendonça França2016-12-291-13/+1
|
* fix #create_fixtures when equal table names in different databasesJulia Lopez2016-12-211-3/+3
|
* Fix spelling in API docsDave Powers2016-11-121-2/+2
| | | | [ci skip]
* Add more rubocop rules about whitespacesRafael Mendonça França2016-10-291-7/+7
|
* remove duplicated fixture set namesyuuji.yaginuma2016-09-121-1/+1
| | | | | | | | | | | | If using namespaced fixtures, get following Ruby warning. ``` activerecord/lib/active_record/fixtures.rb:922: warning: method redefined; discarding old admin_foos activerecord/lib/active_record/fixtures.rb:922: warning: previous definition of admin_foos was here ``` This is happening because of the multiple set the same path when setting the fixture name. Fix to remove the duplicate path.
* Fix `ActiveRecord::FixtureSet` docs [ci skip]Ryuta Kamizono2016-08-151-4/+4
| | | | | | | `ActiveSupport::TestCase` was replaced `ActiveRecord::TestCase` in #26150. But this docs is for rails apps per se, it should be `ActiveSupport::TestCase`. See https://github.com/rails/rails/pull/26150#discussion_r74710989.
* Consolidate `ActiveRecord::TestCase` and `ActiveSupport::TestCase` in AR ↵Ryuta Kamizono2016-08-141-4/+4
| | | | test cases
* applies remaining conventions across the projectXavier Noria2016-08-061-1/+0
|
* normalizes indentation and whitespace across the projectXavier Noria2016-08-061-13/+13
|
* modernizes hash syntax in activerecordXavier Noria2016-08-061-2/+2
|
* applies new string literal convention in activerecord/libXavier Noria2016-08-061-16/+16
| | | | | The current code base is not uniform. After some discussion, we have chosen to go with double quotes by default.
* Fix `payload[:class_name]` to `payload[:spec_name]`Ryuta Kamizono2016-07-171-8/+3
| | | | | | Follow up to #20818. `retrieve_connection` is passed `spec_name` instead of `klass` since #24844.
* Create connection.active_record notification and use that to ensure that lazy-Jeremy Wadsack2016-07-121-0/+28
| | | | | | | | | | | | | | | | | | | loaded model classes have their connections wrapped in transactions. See #17776 In Rails 4 config.eager_load was changed to false in the test environment. This means that model classes that connect to alternate databases with establish_connection are not loaded at start up. If use_transactional_fixtures is enabled, transactions are wrapped around the connections that have been established only at the start of the test suite. So model classes loaded later don't have transactions causing data created in the alternate database not to be removed. This change resolves that by creating a new connection.active_record notification that gets fired whenever a connection is established. I then added a subscriber after we set up transactions in the test environment to listen for additional connections and wrap those in transactions as well.
* Fix typoFrancesco Rodriguez2016-06-121-1/+1
| | | [ci skip]
* Refer to rails command instead of rake in a bunch of placesDavid Heinemeier Hansson2015-12-181-1/+1
| | | | Still more to do. Please assist!
* FixtureSet.fixture_class_names should have no default valueJamis Buck2015-10-291-3/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Look at `TestFixtures.set_fixture_class`. As documented, it accepts a mapping of fixture identifiers (string or symbol) to Classes (the model classes that implement the named fixture). Look now at the initialization of `TestFixtures.fixture_class_names`. It defines a Hash, which will return a string by default (where the string is the estimated class name of the given fixture identifier). Now look at TestFixtures.load_fixtures. It calls `FixtureSet.create_fixtures`, passing in the mapping of `fixture_class_names`. Following this on to `FixtureSet.create_fixtures`, this instantiates a `FixtureSet::ClassCache`, passing in the map of class names. `ClassCache`, in turn, calls `insert_class` for each value in the cache. (Recall that `set_fixture_class` puts Class objects in there, while the default proc for the mapping puts String objects.) Look finally at `insert_class`. If the value is present, it checks to see if the value is a subclass of `AR::Base`. Fair enough...but wait! What if the value is a String? You get an exception, because a String instance cannot be compared with a Class. Judging from the implementation, it seems like the expected behavior here is for `fixture_class_names` to have no default proc. Look-ups are supposed to happen via `ClassCache`, with `fixture_class_names` existing solely as a repository for explicitly-registered class mappings. That is what this change does.
* applies new doc guidelines to Active Record.Yves Senn2015-10-141-2/+2
| | | | | | | | | | | | | | | | | | | The focus of this change is to make the API more accessible. References to method and classes should be linked to make it easy to navigate around. This patch makes exzessiv use of `rdoc-ref:` to provide more readable docs. This makes it possible to document `ActiveRecord::Base#save` even though the method is within a separate module `ActiveRecord::Persistence`. The goal here is to bring the API closer to the actual code that you would write. This commit only deals with Active Record. The other gems will be updated accordingly but in different commits. The pass through Active Record is not completely finished yet. A follow up commit will change the spots I haven't yet had the time to update. /cc @fxn
* Merge pull request #20574 from repinel/fix-db-fixtures-loadYves Senn2015-09-301-9/+30
|\ | | | | | | | | | | | | Allow fixtures YAML files to set the model class in the file itself Conflicts: activerecord/CHANGELOG.md
| * Allow fixtures YAML files to set the model class in the file itselfRoque Pinel2015-09-111-9/+33
|/ | | | | | | | Currently, `set_fixture_class` is only available using the `TestFixtures` concern and it is ignored for `rake db:fixtures:load`. Using the correct model class, it is possible for the fixture load to also load the associations from the YAML files (e.g., `:belongs_to` and `:has_many`).
* Add `:nodoc:` for internal testing methods [ci skip]Robin Dupret2015-07-281-2/+2
|
* Use default model enum in fixtures if not definedeileencodes2015-07-021-1/+3
| | | | | | | | | | | After 908cfef was introduced fixtures that did not set an enum would return nil instead of the default enum value. The fixtures should assume the default if a different enum is not defined. The change checks first if the enum is defined in the fixture before setting it based on the fixture.