Why isn't db:migrate get called as a prerequisite? - ruby-on-rails

I have a bootstrap task, which I intend to have db:reset and db:migrate as prerequisites. I defined it like this:
task :bootstrap => [:environment,:"db:reset",:"db:migrate"] do ...
When I run it, I get the following output:
** Invoke bs:bootstrap (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:reset (first_time)
** Invoke db:drop (first_time)
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
** Execute db:drop
** Invoke db:setup (first_time)
** Invoke db:create (first_time)
** Invoke db:load_config
** Execute db:create
** Invoke db:schema:load (first_time)
** Invoke environment
** Execute db:schema:load
-- create_table("projects", {:force=>true})
-> 0.0770s
-- create_table("users", {:force=>true})
-> 0.1110s
...
** Invoke db:seed (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment
** Execute db:abort_if_pending_migrations
You have 1 pending migrations:
20120109172252 CreateObjectives
Run "rake db:migrate" to update your database then try again.
Why isn't db:migrate being called, since it's listed as a prerequisite?

It looks like db:reset is aborting since you have pending migrations. Since db:reset will use db:schema:load to use your db/schema.rb file to reset the DB, you shouldn't really need to run migrations.
What you could do instead is put db:migrate before db:reset -- this would run migrations, update your schema.rb file so db:reset would use that updated version when resetting the DB (thus avoiding that pending migrations error).

Related

Error when trying to set up db from command line "rake aborted"

I've been trying to set up a test db, but when trying to create, I keep coming across an error that I can't seem to get past when trying to populate the db.
When I execute:
bundle exec rake db:create db:migrate db:test:prepare --trace
create and migrate seem to run without errors when run separately, but test:prepare seems to throw the error.
The error starts with:
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:abort_if_pending_migrations
** Invoke environment
** Execute db:test:prepare
** Invoke db:test:load (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:test:purge
** Execute db:test:load
** Invoke db:test:load_schema (first_time)
** Invoke db:test:purge
** Execute db:test:load_schema
** Invoke db:schema:load (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:load
rake aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "capabilities" does not exist
LINE 5:
I've looked up, and tried a lot of the results shown on google, but none seem to help. New to this, so I'm completely stumped at this stage.
UPDATE:
I should mention that when doing:
rake db:create RAILS_ENV=test
->payments_test already exists
rake db:migrate RAILS_ENV=test
->rake aborted!
NoMethodError: undefined method `mq' for #<Rails::Application::Configuration:0x0000000508acf0> blah blah blah

How to tell if rake db:migrate and rake db:seed were successful

Ruby rake db:seed aborting due to ** Execute db:abort_if_pending_migrations, but I think all the migrations were successful.
Here's the last portion of the output when I run rake db:migrate --trace
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:dump
I assume that mean it was successful (I didn't see any aborts)?
Then when I run rake db:seed --trace I get (in summary):
** Invoke db:seed (first_time)
** Execute db:seed
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
loading plugins
(the plugins load with no errors) then:
** Execute db:abort_if_pending_migrations
does this mean the migration and the seed worked properly or not?
Thank you for your time & input!
If it didn't abort, it succeeded. Take a look at the code:
# desc "Raises an error if there are pending migrations"
task :abort_if_pending_migrations => :environment do
pending_migrations = ActiveRecord::Migrator.open(ActiveRecord::Migrator.migrations_paths).pending_migrations
if pending_migrations.any?
puts "You have #{pending_migrations.size} pending #{pending_migrations.size > 1 ? 'migrations:' : 'migration:'}"
pending_migrations.each do |pending_migration|
puts ' %4d %s' % [pending_migration.version, pending_migration.name]
end
abort %{Run `rake db:migrate` to update your database then try again.}
end
end
It literally does nothing if there aren't any pending migrations.

Test database structure is not created when using Factory girl with Rspec (Rails)

I am using FactoryGirl in my rails application instead of Fixtures.
When i try to use factory girl in my test and create some test data, it shows like
PG:Error relation "users" doesn't exists (i have a model named User)
But when i run rake db:test:clone, and then run the test, the test is passed. The rake db:test:clone command clones all the table structure from development db to test db, and this fixes the issue.
But is there any way for me to not to run rake db:test:clone when using FactoryGirl?
or what am i missing?
Update :
I found out one other thing, i have another application which uses Rspec and FactoryGirl. In that application below are executed when running rake spec --trace command
** Invoke spec (first_time)
** Invoke db:test:clone_structure (first_time)
** Invoke db:structure:dump (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:structure:dump
** Invoke db:test:load_structure (first_time)
** Invoke db:test:purge (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:test:purge
** Execute db:test:load_structure
** Invoke db:structure:load (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:structure:load
But in any of the new application i see the below executed when running rake spec --trace
** Invoke spec (first_time)
** Invoke noop (first_time)
** Execute noop
** Execute spec
Please suggest what am i missing?
regards
Balan
Every time you run a db:migrate, run a db:test:prepare as well, so the database changes are mirrored on your test database as well.
I found the solution, we need to create a rake task customrake.rake under /lib/tasks
and add the line task :spec => 'db:test:prepare', this will make sure rake db:test:prepare is executed before running Specs.

`rake spec` with rails 2 running extemely slow

I am running the following comand on my mac os x shell:
RAILS_ENV=test rake spec --trace
** Invoke spec (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
** Execute db:test:prepare
** Invoke db:test:clone_structure (first_time)
** Invoke db:structure:dump (first_time)
** Invoke environment
** Execute db:structure:dump
** Invoke db:test:purge (first_time)
** Invoke environment
** Execute db:test:purge
** Execute db:test:clone_structure
** Execute spec
This command stops here for more than 5 minutes before continuing, so I don't know what is happening to cause this big big delay.
Any ideas on how can I find what is slowing down "Execute spec" ?
Thanks

Migrate returns (in /app)

I've push my app to Heroku however my database(Sqlite3) fails to migrate. I understand that heroku will automatically migrate as a PG. [ I don't have PG install].
I figured if something was wrong, I would of received an error prompt.
This is my result:
pc-name$ heroku rake db:migrate --trace
(in /app)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
** Invoke db:schema:dump (first_time)
** Invoke environment
** Execute db:schema:dump
Also when I try to create-
pc-name$ heroku rake db:create --trace
cwogwtvrpc already exists
(in /app)
** Invoke db:create (first_time)
** Invoke db:load_config (first_time)
** Invoke rails_env (first_time)
** Execute rails_env
** Execute db:load_config
** Execute db:create
Try destroying the app and creating it again :)

Resources