New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema migrations not loaded by test:prepare using table_name_prefix #10411

Closed
kstevens715 opened this Issue May 2, 2013 · 2 comments

Comments

Projects
None yet
1 participant
@kstevens715
Contributor

kstevens715 commented May 2, 2013

A bug surfaced in RC1 that makes rake test:prepare fail when used in conjunction with ActiveRecord's table_name_prefix or table_name_suffix options.

  1. Create a new app using RC1.
  2. Set config.active_record.table_name_prefix = 'nep_' in application.rb
  3. Create a model: rails g model Product style:string color:string size:string
  4. Migrate: rake db:migrate
  5. Run rake test:prepare
  6. Run RAILS_ENV=test rake db:migrate:status

Expected: Status of migration, "Create products" should be "up".
Actual: Status of migration is "down", and a non-existent migration "000" is up.

Thanks

@kstevens715

This comment has been minimized.

Show comment
Hide comment
@kstevens715

kstevens715 May 7, 2013

Contributor

Here is a minimal Rails 4.0.0.rc1 app to demonstrate the problem. Running rake test:prepare results in a schema_migrations table with a single '0' entry.

https://github.com/kstevens715/testapp

Contributor

kstevens715 commented May 7, 2013

Here is a minimal Rails 4.0.0.rc1 app to demonstrate the problem. Running rake test:prepare results in a schema_migrations table with a single '0' entry.

https://github.com/kstevens715/testapp

@kstevens715

This comment has been minimized.

Show comment
Hide comment
@kstevens715

kstevens715 May 7, 2013

Contributor

A more detailed explanation:

When the rake task runs, it fails saying there are pending migrations. If I look in the test database's nep_schema_migrations table, there is only a single record with value 0 (prefix=nep_). Although the schema has been successfully loaded.

After digging into the problem, I discovered the reason for the bug was that in ActiveRecord::Migration, the call to assume_migrated_upto_version was picked up by method_missing which than forwarded the request to connection. But before it forwarded the request, it "fixed" the method arguments by calling Migrator.proper_table_name on it. This turned a regular version such as 20130322190237 into nep_20130322190237. Then, assume_migrated_upto_version was calling to_i on the argument which resulted in the 0 I saw in the schema_migrations table. Additionally, later in the method it only inserts versions that are less than the current version. Since none of them will be less than 0, the 0 is the only version that was inserted.

The solution is to simply send assume_migrated_upto_version directly to the proper receiver.

This problem has been around for a while, but only with Rails 4.0.0.rc1 did it start to matter as test:prepare now does a check to make sure the test database does not have any pending migrations.

Contributor

kstevens715 commented May 7, 2013

A more detailed explanation:

When the rake task runs, it fails saying there are pending migrations. If I look in the test database's nep_schema_migrations table, there is only a single record with value 0 (prefix=nep_). Although the schema has been successfully loaded.

After digging into the problem, I discovered the reason for the bug was that in ActiveRecord::Migration, the call to assume_migrated_upto_version was picked up by method_missing which than forwarded the request to connection. But before it forwarded the request, it "fixed" the method arguments by calling Migrator.proper_table_name on it. This turned a regular version such as 20130322190237 into nep_20130322190237. Then, assume_migrated_upto_version was calling to_i on the argument which resulted in the 0 I saw in the schema_migrations table. Additionally, later in the method it only inserts versions that are less than the current version. Since none of them will be less than 0, the 0 is the only version that was inserted.

The solution is to simply send assume_migrated_upto_version directly to the proper receiver.

This problem has been around for a while, but only with Rails 4.0.0.rc1 did it start to matter as test:prepare now does a check to make sure the test database does not have any pending migrations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment