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

Already on GitHub? Sign in to your account

table_name_prefix with db:schema:load causes double prefixes #4259

Closed
ankitenggcom opened this Issue Jan 2, 2012 · 20 comments

Comments

Projects
None yet
9 participants

I created a rails 3.1.3 application. And added table_name_prefix as 'pfx_' in config/application.rb. I also created a model user with name and address as table columns. Then run the db:create and db:migrate.
schema.rb file gets populated with the db dump having table prefix in table names. After then I tried to run my test cases which failed with the following error :

UsersControllerTest:
    ERROR should create user (0.02s) 
          ActiveRecord::StatementInvalid: Mysql2::Error: Table 'myapp_test.pfx_users' doesn't exist: SHOW FIELDS FROM `pfx_users`
          /home/ankit/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query'

    ERROR should destroy user (0.01s) 
          ActiveRecord::StatementInvalid: Mysql2::Error: Table 'myapp_test.pfx_users' doesn't exist: SHOW FIELDS FROM `pfx_users`
          /home/ankit/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query'

    ERROR should get edit (0.01s) 
          ActiveRecord::StatementInvalid: Mysql2::Error: Table 'myapp_test.pfx_users' doesn't exist: SHOW FIELDS FROM `pfx_users`
          /home/ankit/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query'

    ERROR should get index (0.01s) 
          ActiveRecord::StatementInvalid: Mysql2::Error: Table 'myapp_test.pfx_users' doesn't exist: SHOW FIELDS FROM `pfx_users`
          /home/ankit/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query'

    ERROR should get new (0.08s) 
          ActiveRecord::StatementInvalid: Mysql2::Error: Table 'myapp_test.pfx_users' doesn't exist: SHOW FIELDS FROM `pfx_users`
          /home/ankit/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query'

    ERROR should show user (0.01s) 
          ActiveRecord::StatementInvalid: Mysql2::Error: Table 'myapp_test.pfx_users' doesn't exist: SHOW FIELDS FROM `pfx_users`
          /home/ankit/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query'

    ERROR should update user (0.01s) 
          ActiveRecord::StatementInvalid: Mysql2::Error: Table 'myapp_test.pfx_users' doesn't exist: SHOW FIELDS FROM `pfx_users`
          /home/ankit/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/connection_adapters/mysql2_adapter.rb:283:in `query'

After debugging the issue I found that tables with double prefix, as 'pfx_pfx_users' gets created in the test database which is a blunder.

Please find the demo code on the following url : https://github.com/ankitenggcom/Myapp
Kindly look into this issue.

Can anybody let me know the status of this issue?

Contributor

kennyj commented Jan 9, 2012

I could reproduce this issue on your app.

I realized that the schema dumper is cause of this problem.
I fixed this issue. and commited on my repos.
Please confirm it. If you have no problem, I'll send pull request.

BTW, I noticed the below ticket on the way, @dlitz's fix and my fix are almost same ;-)
https://rails.lighthouseapp.com/projects/8994/tickets/1210-table_name_prefix-with-dbschemaload-causes-double-prefixes

kennyj added a commit to kennyj/rails that referenced this issue Jan 10, 2012

Contributor

kennyj commented Jan 10, 2012

I send pull request anyway :-)
#4396

Contributor

nashby commented Apr 7, 2012

looks like it can be closed. /cc @arunagw

Member

arunagw commented Apr 8, 2012

thanks @nashby :-)

@arunagw arunagw closed this Apr 8, 2012

Contributor

kennyj commented Apr 8, 2012

@nashby @arunagw
I think this issue still is existed ;-)
My PR #4396 fixed this issue partially (my plan was no good).

jakedev commented Apr 8, 2012

this issue still does exist....

@kennyj kennyj reopened this Apr 8, 2012

dlitz commented Apr 11, 2012

Please see my most recent comment attached to the pull request. I think it should be pulled as-is, since it doesn't seem possible to fix it (or desirable to try to fix it) with per-model table_name_prefixes.

Contributor

kennyj commented Apr 12, 2012

@dlitz
I think so. If we don't load model classes, we can't know per-model table_name_prefixes.
I'll reopen the PR :-)

This issue still exists in rails 3.2.3. So anybody can please let me know the fix of it.

Contributor

kstevens715 commented Jun 14, 2012

I'm having the same issue. rake spec has all failures because I'm setting a prefix of "nep_" and it's generating tables with prefix "nep_nep_" instead in the test db.

Contributor

kstevens715 commented Jun 20, 2012

I ended up creating a monkey patch to use temporarily based off the pull request that @kennyj made. It doesn't include all his changes, just the ones I needed but could easily be modified some more: https://gist.github.com/2960182

rafaelfranca added a commit that referenced this issue Jun 20, 2012

Merge pull request #4396 from kennyj/fix_4259
Fix GH #4259. When we execute schema dumper, we must remove table_name_prefix and table_name_suffix.
Owner

rafaelfranca commented Jun 20, 2012

I merged the @kennyj pull request at 1bdc098.

This issue can not be easily fixed when you are using per-model table_name_prefix. So I'm closing.

Feel free to ask to reopen if you think that we should.

Issue still exists in Rails 3.2.6. And database also complains relation "prefix_unique_schema_migrations" already exists. And migrations for tables without prefixes are broken.

So, using per-application table prefix is not easy way with current Rails 3. Should I consider get rid of table prefixes, or any chances it will be fixed soon?

Owner

rafaelfranca commented Jul 16, 2012

@denispeplin could you test it with the master branch? If fixed I can backport to 3-2-stable.

I tested it with fresh Rails 3.2.6 installation and with fresh Rails 4.0.0.beta installation. In 4.0.0.beta the issue with double prefixes is fixed, thanks!

Owner

rafaelfranca commented Jul 17, 2012

@denispeplin good. I'll backport it to 3-2-stable branch.

Thank you to confirm

rafaelfranca added a commit that referenced this issue Jul 17, 2012

Merge pull request #4396 from kennyj/fix_4259
Fix GH #4259. When we execute schema dumper, we must remove table_name_prefix and table_name_suffix.

Tested it, works fine in 3-2-stable branch. Thanks!

Contributor

kennyj commented Jul 17, 2012

thanks @rafaelfranca.

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