Waterlink
Waterlink

Reputation: 2369

ActiveRecord how to ignore pending migrations

The problem is the following:

Result is:

I wanted to somehow tell rake to ignore pending migrations, but unable to do it so far.

UPDATE (due to additional experience)

Sometimes migrations and model code goes out of sync, so migrations are not being run. To avoid this problem recently used redefining of model in migrations:

# reset all callbacks, hooks, etc for this model
class MyAwesomeModel < ActiveRecord::Base
end

class DoSomethingCool < ActiveRecord::Migration
  def change
    ...
  end
end

Upvotes: 74

Views: 37723

Answers (6)

whizcreed
whizcreed

Reputation: 2752

I am not very sure if this will help you. But I was looking for something and found this question. So it looks like this might help:

In RAILS_ROOT/config/environments/development.rb Set the following setting to false:

 # NOTE: original version is `:page_load`
 config.active_record.migration_error = false

In my situation it now does not show the pending migration error anymore. Should work for rake tasks and console for the same environment as well.

Source in rails/rails

Upvotes: 169

stwr667
stwr667

Reputation: 1738

If you came across the "pending migrations" issue when trying to seed your data from within a running Rails application, you can simply call this directly which avoids the abort_if_pending_migrations check:

ActiveRecord::Tasks::DatabaseTasks.load_seed

See where seeds are actually called from within ActiveRecord:

https://github.com/rails/rails/blob/v6.0.3.2/activerecord/lib/active_record/railties/databases.rake#L331

and see the DatabaseTasks docs:

https://apidock.com/rails/v6.0.0/ActiveRecord/Tasks/DatabaseTasks https://apidock.com/rails/v6.0.0/ActiveRecord/Tasks/DatabaseTasks/load_seed

Upvotes: 1

siva
siva

Reputation: 51

I had a similar issue. I commented out the add_column lines and ran the rake db:migrate commands and then removed the comment when I will need it for the testing or production environment.

Upvotes: 4

mirelon
mirelon

Reputation: 4996

Rename the migration dependent on the data from:

20140730091353_migration_name.rb

to

.20140730091353_migration_name.rb

(add a dot at the start of the filename)

Then run rake db:seed (it will no longer complain on the pending migrations) and then rename back the migration.

If you have more migrations following after, you have to rename all of them or just move it temporary away.

Upvotes: 45

Dan Grahn
Dan Grahn

Reputation: 9414

Rails stores migration information in a table called schema_migrations.

You can add the version from your migration into that table to skip a specific migration.

The version is the number string which comes before the description in the file name.

[version]_Create_Awesome.rb

Upvotes: 15

Simone Carletti
Simone Carletti

Reputation: 176402

There is no way unless you monkey patch the Rails code. I strongly advise you to fix your migrations instead.

A migration should not depend on the existence of some data in the database. It can depend on a previous migration, but of course absolutely not on the data on the db.

Upvotes: 2

Related Questions