user419017
user419017

Reputation:

Rake sequential tasks

I have run into a very strange problem. I have a task which resets my database as so:

task :reset => [:drop, :create, :migrate, :seed]

The problem is, I am receiving errors when seeding because of missing columns which are added in late migration files. One example:

undefined method new_attr= for User

Yet this attribute is already added in a migration. The strange part is, I receive no errors if I run the above tasks separately. Can anybody shed some light? Surely these tasks cannot be run asynchronously.

Another way to avoid errors is to amend my earlier migrations create_ with the new attributes. Then running :reset doesn't trigger errors for those attributes.

The migrations are clearly fine as I can run the above tasks separately, just not bundled under a single task.

Upvotes: 0

Views: 722

Answers (3)

Nisa Balakrishnan
Nisa Balakrishnan

Reputation: 11

If these rake tasks are executed in the production mode, model attributes are cached. Even though migrations work perfect, it wont apply to the cached. This will break your succeeding seed as newly added columns will be missing in the cache. A possible solution is to reload your rails environment before seeding.

Upvotes: 1

house9
house9

Reputation: 20594

maybe you want to make your reset task more explicit?

namespace :db_tasks do
  desc "Rebuild development db"
  task :rebuild_database, [] => :environment do
    raise "Only run in development or staging" if Rails.env.production?

    Rake::Task['db:drop'].execute
    Rake::Task['db:create'].execute
    Rake::Task['db:migrate'].execute
    Rake::Task['db:seed'].execute
    Rake::Task['db:test:prepare'].execute
  end
end

Upvotes: 1

Paulo Fidalgo
Paulo Fidalgo

Reputation: 22296

Probably your problem is already solved using this:

rake db:reset

The rake db:reset task will drop the database, recreate it and load the current schema into it.

Have you tried with namespace?

task :reset => [db:drop, db:create, db:migrate, db:seed]

Upvotes: 1

Related Questions