How to make rake db:migrate generate schema.rb when using :sql schema format
To generate/update db/schema.rb
even if using the :sql
option, you can put this in your Rakefile
:
Rake::Task["db:migrate"].enhance do
if ActiveRecord::Base.schema_format == :sql
Rake::Task["db:schema:dump"].invoke
end
end
That should be fine for IDea and RubyMine.
For others that just want the file for reference, you might want to rename it to something else like db/schema.rb.backup
so it won't be confusing. To do that:
Rake::Task["db:migrate"].enhance do
if ActiveRecord::Base.schema_format == :sql
Rake::Task["db:schema:dump"].invoke
File.rename(File.expand_path('../db/schema.rb', __FILE__), File.expand_path('../db/schema.rb.backup', __FILE__))
end
end
(Note: Using ../
in paths in Rakefile
because __FILE__
evaluates to a path that ends in /Rakefile
.)
How to dynamically set schema_format for custom db rake tasks in Rails?
Try this:
task :set_custom_db_config_paths do
ENV['SCHEMA'] = 'db_other/schema.rb'
Rails.application.config.paths['db'] = ['other']
Rails.application.config.paths['db/migrate'] = ['db_other/migrate']
Rails.application.config.paths['config/database'] = ['config/database_other.yml']
Rails.application.config.active_record.schema_format = :ruby
end
Note the last line setting Rails.application.config.active_record.schema_format = :ruby
Why does schema.rb update after I run db:migrate for Rails?
As far as I know schema can change after running rails db:migrate because of:
- A co-worker did not commit the schema.rb so when you fetched and run the migrations you get the diff
- A different DB version is running on your local machine. Based on db configuration schema may be changed accordingly.
Running git diff will help you to understand what is going.
`rake db:schema:dump` creates schema with empty system tables
There is hope:
ActiveRecord::SchemaDumper which does most of the heavy lifting supplies a singleton method for ignoring tables:
ActiveRecord::SchemaDumper.ignore_tables = ['MSreplication_objects', 'MSAnotherStupidSystemTable']
activerecord-sqlserver-adapter
does not seem to supply its own rake task for dumping the schema (which it should) where this should have been done.
Related Topics
Upsert Multiple Records with Mongodb
What's Wrong with the Square and Rectangle Inheritance
Rails 3 Caching: Expire Action for Named Route
Overriding Model in Gem, Adding Callback and Methods
How to Build Ruby 2.1.3 on Osx 10.10 Gm 3.0 with Rbenv
State MAChine, Model Validations and Rspec
Grit's Clone Method Is Undefined
Typeerror: Object Doesn't Support This Property or Method
Why Is the Splat Used Inside an Array Definition Here
Problem Running Thinking Sphinx with Rails 2.3.5
How to Access Current_User Object in Model
How to Execute 2 or More Commands in the Same Ssh Session
Include Erb Delimiters Inside of a String in an Erb Block
What Orm to Use in One Process Multiple Db Connections Sinatra Application
Updating from Rails 4.0 to 4.1 Gives SASS-Rails Railties Version Conflicts