How to Disable Db:Schema:Dump for Migrations

How to disable db:schema:dump for migrations

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

How to disable updating of structure.sql in rails?

You can disable that in your config/application.rb:

config.active_record.dump_schema_after_migration = false

See the configuration guide here.

How do I disable the migrations feature in a Rails app?

This came up again when testing finally came to the front. Thus, I took deeper look and came up with the following thanks, in part, to the comments left on the question. This removes all rake DB capabilities and tests still run fine. (In case anyone is wondering, we clone the test DB from elsewhere when we need to refresh it.)

Add this to the Rakefile:

# Disable DB migrations, DB test preparing, etc.
Rake::Task.tasks.each do |t|
if t.name[0,3] == "db:"
t.clear
t.add_description("!!! Disabled in favor of enterprise design at Acme.")
end
end

Comment out all the fixtures in test/test_helper.rb:

#fixtures :all

Keep a table out of schema.rb during migrations

Turns out there's an option for just this situation!

I found it in activerecord-2.3.4/lib/active_record/schema_dumper.rb:

##
# :singleton-method:
# A list of tables which should not be dumped to the schema.
# Acceptable values are strings as well as regexp.
# This setting is only used if ActiveRecord::Base.schema_format == :ruby
cattr_accessor :ignore_tables
@@ignore_tables = []

So all I had to do was stick this at the end of environment.rb:

ActiveRecord::SchemaDumper.ignore_tables = ["table_name"]

If ActiveRecord.schema_format == :ruby, the array can also contain RegExp. For example, to ignore all tables starting with "MS":

ActiveRecord::SchemaDumper.ignore_tables = [/^MS/]

Loading the DB dumb, and running all the migrations

I would first load the db dump, as it also contains all you data and the current schema structure.

running rake db:migrate will only run the new migration

This depends. If your db dump has a schema_migrations table, that lists all the previously applied migrations, it will not apply the migrations. Otherwise, it will try to and fail.

You basically need these steps:

  1. Import your dump
  2. Create a Rails schema (rake db:schema:dump)
  3. If you have newer migrations, run them

Rails -- working with an existing db, cannot run migrations without losing data

Migrates should be used to create and changes the tables and fields, not load data, as you can see here Ruby on Rails Guides

If you want to import data you could do it on the seeds, but in your specific case it seems to me that you should create a dump from your origin database and load it on the target database.

Here is a tutorial sqlite3: how to import/export data from/to a file



Related Topics



Leave a reply



Submit