How to disable db:schema:dump for migrations

2020-02-08 15:22发布

I dont want Rails 3 to generate my schema every time I do migration. How to properly disable it?

Thanks

6条回答
看我几分像从前
2楼-- · 2020-02-08 15:46

For anyone who is still looking for a way to disable the db dump after migration a configuration is now available in rails 4 which can be set to false like this:

config.active_record.dump_schema_after_migration = false

will prevent it. The configuration has been added in this change - https://github.com/rails/rails/pull/13948

查看更多
家丑人穷心不美
3楼-- · 2020-02-08 15:51

The default behavior of Active Record is to dump the schema, but it only does it if the schema dump format is set to ruby (the default).

You can disable the schema dump after migrations by setting the schema format to sql. You can safely reverse it to ruby after the migration finishes without consequences.

You can do it in this way:

1) If you have a line like this one setting explicitly the schema to ruby:

config.active_record.schema_format = :ruby

(it could be inside config/application.rb, on config/environment or in the specific environment you are running the migration in).

Then comment it out:

#config.active_record.schema_format = :ruby

2) Then add a line to set the schema format to SQL, either in the environment you are using, or at config/application.rb, as follows:

config.active_record.schema_format = :sql
查看更多
三岁会撩人
4楼-- · 2020-02-08 15:56

I've tested the following - it works:

You could modify the file "databases.rake" inside of the Rails gem's lib/tasks folder on your server(s). Comment out lines with the following code:

Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby

By default on Ubuntu (and Ubuntu server) it is here: /var/lib/gems/1.8/gems/rails-x.x.x/lib/tasks/databases.rake.

Tested with Rails 2.3.11, but I'm pretty sure it'll work with Rails 3.x.x too.

Another possible solution (didn't test):

You wouldn't have to modify Rails files, just your application.

A rails plugin called "override_rake_task" could be used to override Rake task "db:schema:dump" which is defined inside if Rails gem.

Apparently if you are not using this plugin and if you define a task in your rails application with the same name, rake would execute both tasks: the default and yours.

查看更多
聊天终结者
5楼-- · 2020-02-08 15:57

Rather than overriding the db:migrate behavior (which is obviously necessary when you're building the migration in development, but unnecessary when deploying) I would suggest creating a separate task like:

# http://stackoverflow.com/questions/13646840/how-to-make-rake-dbmigrate-generate-schema-rb-when-using-sql-schema-format
namespace :db do
  desc "Migrate the database, without dumping the schema.rb"
  task :deploy => :environment do
    ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
    ActiveRecord::Migrator.migrate("db/migrate/", nil)
  end
end

Then during your deployments you can simply do

rake db:deploy
查看更多
▲ chillily
6楼-- · 2020-02-08 15:58

Create an application specific task (as Alex Kaushovik suggested) like so...

Create a file lib\tasks\db_schema_override (actual name doesn't matter, you need a .rake file in lib\tasks) with contents as below (credit to Matthew Bass for remove_task)

Rake::TaskManager.class_eval do
  def remove_task(task_name)
    @tasks.delete(task_name.to_s)
  end
end

Rake.application.remove_task('db:schema:dump')
namespace :db do
  namespace :schema do
    task :dump do
      # Overridden to do nothing
    end
  end
end
查看更多
手持菜刀,她持情操
7楼-- · 2020-02-08 16:00

A modification based on David Waller's answer that retains the ability to run db:schema:dump manually. Covers all tasks where schema dumping is automatically invoked. Based on rails 3.2.14 versions of the relevant tasks - when you change rails versions, you'll want to ensure that the task definitions are still valid.

Seriously, there should be a config option to disable automatic schema dumps.

Rake::TaskManager.class_eval do
  def remove_task(task_name)
    @tasks.delete(task_name.to_s)
  end

  Rake.application.remove_task("db:migrate")
  Rake.application.remove_task("db:rollback")
  Rake.application.remove_task("db:forward")
  Rake.application.remove_task("db:migrate:up")
  Rake.application.remove_task("db:migrate:down")

end

namespace :db do

  desc "Migrate the database (options: VERSION=x, VERBOSE=false)."
  task :migrate => [:environment, :load_config] do
    ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
    ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil) do |migration|
      ENV["SCOPE"].blank? || (ENV["SCOPE"] == migration.scope)
    end
    # db_namespace['_dump'].invoke
  end

  desc 'Rolls the schema back to the previous version (specify steps w/ STEP=n).'
  task :rollback => [:environment, :load_config] do
    step = ENV['STEP'] ? ENV['STEP'].to_i : 1
    ActiveRecord::Migrator.rollback(ActiveRecord::Migrator.migrations_paths, step)
    # db_namespace['_dump'].invoke
  end

  # desc 'Pushes the schema to the next version (specify steps w/ STEP=n).'
  task :forward => [:environment, :load_config] do
    step = ENV['STEP'] ? ENV['STEP'].to_i : 1
    ActiveRecord::Migrator.forward(ActiveRecord::Migrator.migrations_paths, step)
    # db_namespace['_dump'].invoke
  end

  namespace :migrate do

    # desc 'Runs the "up" for a given migration VERSION.'
    task :up => [:environment, :load_config] do
      version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
      raise 'VERSION is required' unless version
      ActiveRecord::Migrator.run(:up, ActiveRecord::Migrator.migrations_paths, version)
      # db_namespace['_dump'].invoke
    end

    # desc 'Runs the "down" for a given migration VERSION.'
    task :down => [:environment, :load_config] do
      version = ENV['VERSION'] ? ENV['VERSION'].to_i : nil
      raise 'VERSION is required' unless version
      ActiveRecord::Migrator.run(:down, ActiveRecord::Migrator.migrations_paths, version)
      # db_namespace['_dump'].invoke
    end

  end

end
查看更多
登录 后发表回答