Rails does not recreate mysql views in the test database, even if config.active_record.schema_format =: sql

We have some mysql Views in our test and test databases that were created using the execute (sql) statement during the migration process. By default, schema.rb Rails creates these table views. If config.active_record.schema_format is set to: sql, these views are not created at all.

Is there a parameter to re-create these views in test db?

If not, can anyone suggest a workaround?

NB, show create table for this view is something like:

 CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `sales_reports` AS select ... 

and view is included in show tables

After the investigation, it seems that activerecord is doing this on purpose.

active_record / connection_adapters / mysql_adapter.rb

Is there a good reason for this?

+6
ruby mysql ruby-on-rails activerecord migration
source share
1 answer

Be sure to read this section of the manual .

Make sure in the application block in config/application.rb :

 config.active_record.schema_format = :sql 

You can then use this Rake task to unload the schema, but it needs to be reset / updated whenever you migrate / etc. because of this above:

 rake db:structure:dump 

Your flush structure should be in db/structure.sql . It should look like a dump schema file from your database, i.e. Do not include data, except for migration data that will be placed at the end (at least for postgres).

When using config.active_record.schema_format = :sql your db/schema.rb not updated by default whenever you migrate, because db/schema.rb not designed to completely decommission the SQL schema. However, some tools like IntelliJ Rubymine and IDea with a Ruby plugin similar to this file, so add them to your Rakefile (as mentioned here ):

 Rake::Task["db:migrate"].enhance do if ActiveRecord::Base.schema_format == :sql Rake::Task["db:schema:dump"].invoke end end Rake::Task["db:rollback"].enhance do if ActiveRecord::Base.schema_format == :sql Rake::Task["db:schema:dump"].invoke end end 

When the test database is recreated by Rails, it uses db/structure.sql as the base if config.active_record.schema_format = :sql . If you roll back or make changes to the database from the outside and redump using the above command or do the migration, etc., It will also update db/structure.sql (and db/schema.rb with these tasks above, although db/schema.rb does not fully cover information from the dump of the circuit).

+5
source share

All Articles