Rake craziness with Migrate as a dependency

Hi everyone,

For one of our projects we have a need to deploy and maintain a demo.
As a result I’ve attempted to setup a set of rake tasks for the job.
My goal is have one rake task called “deploy_demo” which purges the
database, migrates to the current schema, and loads the db from a set
of fixture files in a specified location.

I’ve written two new tasks the perform portions of this task:

db:purge - purges the db set in RAILS_ENV.
load_my_fixtures - loads the db using fixtures from a directory
specified by the SRC env var

Then I setup deploy_demo as a task that basically calls db:purge,
db:migrate, and then load_my_fixtures. The source is below:

– Start –
desc “This is a task to deploy a demo. It is setup to work in the
context of a local machine. It wipes the DB, migrates up to the
current version of the schema, and loads fixtures from
RAILS_ROOT/db/fixtures/demo”

task :deploy_demo => [“db:purge”,“db:migrate”] do

we need to invoke the one in the body so we can set SRC in the

environment
ENV[‘SRC’] = “db/fixtures/demo”
Rake::Task[:load_my_fixtures].invoke
end
– End –

So, I can run each of the component tasks (db:purge, db:migrate:
load_my_fixtures) from the command line and they work fine. When I run
deploy_demo the db:fixtures task completes without an error but doesn’t
actually modify the database. Does anyone know why db:migrate would
not modify the db when called as a dependency but would from the
command line? I’ve included output from both methods of calling these
tasks with --trace on.

– Output from calling each sub-task from command line –
c:\dve\ideeli_trunk\lib\tasks>rake db:purge --trace
rake db:purge --trace
(in c:/dve/ideeli_trunk)
** Invoke db:purge (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:purge
Loaded suite c:/ruby/bin/rake.bat
Started

Finished in 0.0 seconds.

0 tests, 0 assertions, 0 failures, 0 errors

c:\dve\ideeli_trunk\lib\tasks>rake db:migrate --trace
rake db:migrate --trace
(in c:/dve/ideeli_trunk)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
– create_table(“rails_crons”)
-> 0.1010s
– add_column(:rails_crons, :concurrent, :boolean)
-> 0.2100s
** Execute db:migrate
== Init: migrating

– create_table(“colors”, {:force=>true})
-> 0.0800s
– add_index(“colors”, [“product_id”, “name”], {:name=>“product_name”,
:unique=>true})
-> 0.2300s
– create_table(“groups”, {:force=>true})
-> 0.0700s
– add_index(“groups”, [“parent_id”],
{:name=>“groups_parent_id_index”})
-> 0.2010s
– create_table(“groups_roles”, {:force=>true, :id=>false})
-> 0.1000s
– add_index(“groups_roles”, [“group_id”, “role_id”],
{:name=>“groups_roles_all_index”, :unique=>true})
-> 0.2200s
– add_index(“groups_roles”, [“role_id”], {:name=>“role_id”})
-> 0.2200s
– create_table(“groups_users”, {:force=>true, :id=>false})
-> 0.1010s
– add_index(“groups_users”, [“group_id”, “user_id”],
{:name=>“groups_users_all_index”, :unique=>true})
-> 0.2100s
– add_index(“groups_users”, [“user_id”], {:name=>“user_id”})
-> 0.2100s
– create_table(“images”, {:force=>true})
-> 0.0900s
– add_index(“images”, [“product_id”], {:name=>“fk_images_product”})
-> 0.2210s
– add_index(“images”, [“color_id”], {:name=>“fk_images_color”})
-> 0.2400s
– create_table(“products”, {:force=>true})
-> 0.1000s
– create_table(“roles”, {:force=>true})
-> 0.0900s
– add_index(“roles”, [“parent_id”], {:name=>“roles_parent_id_index”})
-> 0.2210s
– create_table(“roles_static_permissions”, {:force=>true, :id=>false})
-> 0.1000s
– add_index(“roles_static_permissions”, [“static_permission_id”,
“role_id”], {:name=>“roles_static_permissions_all_index”,
:unique=>true})
-> 0.2200s
– add_index(“roles_static_permissions”, [“role_id”],
{:name=>“role_id”})
-> 0.2400s
– create_table(“roles_users”, {:force=>true, :id=>false})
-> 0.0800s
– add_index(“roles_users”, [“user_id”, “role_id”],
{:name=>“roles_users_all_index”, :unique=>true})
-> 0.1810s
– add_index(“roles_users”, [“role_id”], {:name=>“role_id”})
-> 0.1800s
– create_table(“skus”, {:force=>true})
-> 0.0700s
– add_index(“skus”, [“product_id”, “size”], {:name=>“product_color”,
:unique=>true})
-> 0.1800s
– create_table(“static_permissions”, {:force=>true})
-> 0.0800s
– add_index(“static_permissions”, [“title”],
{:name=>“static_permissions_title_index”, :unique=>true})
-> 0.2810s
– create_table(“user_registrations”, {:force=>true})
-> 0.2000s
– add_index(“user_registrations”, [“user_id”],
{:name=>“user_registrations_user_id_index”, :unique=>true})
-> 0.2400s
– add_index(“user_registrations”, [“expires_at”],
{:name=>“user_registrations_expires_at_index”})
-> 0.1810s
– create_table(“users”, {:force=>true})
-> 0.0900s
– add_index(“users”, [“login”], {:name=>“users_login_index”,
:unique=>true})
-> 0.2100s
– add_index(“users”, [“password”], {:name=>“users_password_index”})
-> 0.1900s
== Init: migrated (5.3370s)

== UpdateDateTimeToDate: migrating

– add_column(:products, :shipment_received_on, :date, {:null=>true})
-> 0.2000s
– remove_column(:products, :shipment_received_at)
-> 0.1900s
== UpdateDateTimeToDate: migrated (0.3900s)

== FixSkusIndex: migrating

– remove_index(:skus, {:name=>“product_color”})
-> 0.2200s
– add_index(:skus, [:product_id, :color_id, :size],
{:name=>“product_color_size”, :unique=>true})
-> 0.2110s
== FixSkusIndex: migrated (0.4310s)

== CreateImages: migrating

– remove_column(:images, :product_id)
-> 0.2100s
– remove_column(:images, :filename)
-> 0.2100s
– add_column(:images, “image”, :string, {:default=>"", :limit=>255,
:null=>false})
-> 0.2210s
– add_column(:images, “position”, :integer)
-> 0.2200s
== CreateImages: migrated (0.8610s)

== CreateOffers: migrating

– create_table(:offers)
-> 0.1000s
== CreateOffers: migrated (0.1000s)

== CreateLineItems: migrating

– create_table(“line_items”, {:force=>true})
-> 0.0800s
== CreateLineItems: migrated (0.0800s)

== AddProfileToUsers: migrating

– add_column(:users, “billing_title”, :string, {:default=>"",
:limit=>6, :null=>false})
-> 0.2110s
– add_column(:users, “billing_first_name”, :string, {:default=>"",
:limit=>100, :null=>false})
-> 0.2100s
– add_column(:users, “billing_last_name”, :string, {:default=>"",
:limit=>100, :null=>false})
-> 0.2000s
– add_column(:users, “billing_address1”, :string, {:default=>"",
:limit=>150, :null=>false})
-> 0.3410s
– add_column(:users, “billing_address2”, :string, {:default=>"",
:limit=>150, :null=>false})
-> 0.1700s
– add_column(:users, “billing_city”, :string, {:default=>"",
:limit=>150, :null=>false})
-> 0.2100s
– add_column(:users, “billing_state”, :string, {:default=>"",
:limit=>150, :null=>false})
-> 0.2110s
– add_column(:users, “billing_zip”, :string, {:default=>"",
:limit=>150, :null=>false})
-> 0.2100s
– add_column(:users, “shipping_title”, :string, {:default=>"",
:limit=>6, :null=>false})
-> 0.4310s
– add_column(:users, “shipping_first_name”, :string, {:default=>"",
:limit=>100, :null=>false})
-> 0.3400s
– add_column(:users, “shipping_last_name”, :string, {:default=>"",
:limit=>100, :null=>false})
-> 0.2400s
– add_column(:users, “shipping_address1”, :string, {:default=>"",
:limit=>150, :null=>false})
-> 0.2310s
– add_column(:users, “shipping_address2”, :string, {:default=>"",
:limit=>150, :null=>false})
-> 0.2200s
– add_column(:users, “shipping_city”, :string, {:default=>"",
:limit=>150, :null=>false})
-> 0.2100s
– add_column(:users, “shipping_state”, :string, {:default=>"",
:limit=>150, :null=>false})
-> 0.2110s
– add_column(:users, “shipping_zip”, :string, {:default=>"",
:limit=>150, :null=>false})
-> 0.2700s
– add_column(:users, “shipping_telephone”, :string, {:default=>"",
:limit=>150, :null=>false})
-> 0.2000s
– add_column(:users, “shipping_different”, :string, {:default=>"",
:limit=>1, :null=>false})
-> 0.2110s
– add_column(:users, “mobile”, :string, {:default=>"", :limit=>100,
:null=>false})
-> 0.2300s
– add_column(:users, “email_notifications”, :string, {:default=>"",
:limit=>1, :null=>false})
-> 0.2400s
– add_column(:users, “sms_notifications”, :string, {:default=>"",
:limit=>1, :null=>false})
-> 0.2310s
– add_column(:users, “income”, :string, {:default=>"", :limit=>50,
:null=>false})
-> 0.1800s
– add_column(:users, “education”, :string, {:default=>"", :limit=>50,
:null=>false})
-> 0.2100s
– add_column(:users, “birthday_on”, :date, {:null=>true})
-> 0.2110s
== AddProfileToUsers: migrated (5.6290s)

== CreateOrders: migrating

– create_table(“orders”, {:force=>true})
-> 0.1400s
== CreateOrders: migrated (0.1400s)

== AddShippingPriceToLineItems: migrating

– add_column(:line_items, “unit_shipping_fee”, :integer, {:default=>0,
:limit=>10, :null=>false})
-> 0.1900s
== AddShippingPriceToLineItems: migrated (0.1900s)

== CreateComments: migrating

– create_table(:comments)
-> 0.0900s
== CreateComments: migrated (0.0900s)

== NoProductSkuRelationship: migrating

– remove_column(:skus, “product_id”)
-> 0.2110s
== NoProductSkuRelationship: migrated (0.2110s)

== CreatePosts: migrating

– create_table(:posts)
-> 0.0900s
== CreatePosts: migrated (0.0900s)

== CreateEcards: migrating

– create_table(:ecards)
-> 0.0700s
== CreateEcards: migrated (0.0700s)

== AddColumnsToProduct: migrating

– add_column(:products, “spec”, :text, {:default=>"", :null=>false})
-> 0.2200s
– add_column(:products, “strapline”, :string, {:default=>"",
:limit=>100, :null=>false})
-> 0.2110s
== AddColumnsToProduct: migrated (0.4310s)

== AddToEcards: migrating

– drop_table(:ecards)
-> 0.0400s
– create_table(:ecards)
-> 0.0700s
== AddToEcards: migrated (0.1100s)

== AddReferrerToUsers: migrating

– add_column(:users, “referrer_type”, :string)
-> 0.2200s
– add_column(:users, “user_id”, :integer)
-> 0.2710s
== AddReferrerToUsers: migrated (0.4910s)

== MobileToThreeCell: migrating

– remove_column(:users, “mobile”)
-> 0.2100s
– add_column(:users, “cell1”, :string, {:default=>"", :limit=>3,
:null=>false})
-> 0.2100s
– add_column(:users, “cell2”, :string, {:default=>"", :limit=>3,
:null=>false})
-> 0.2210s
– add_column(:users, “cell3”, :string, {:default=>"", :limit=>4,
:null=>false})
-> 0.2200s
== MobileToThreeCell: migrated (0.8610s)

== AddShowComingSoonToUsers: migrating

– add_column(:offers, “show_in_coming_soon”, :string, {:default=>"",
:limit=>1, :null=>false})
-> 0.2000s
== AddShowComingSoonToUsers: migrated (0.2000s)

== AddDreamToUser: migrating

– add_column(:users, “dream_item”, :string, {:default=>"",
:limit=>150, :null=>false})
-> 0.1910s
== AddDreamToUser: migrated (0.1910s)

== CreateFaqs: migrating

– create_table(:faqs)
-> 0.0800s
== CreateFaqs: migrated (0.0800s)

== CreateCategories: migrating

– create_table(:categories)
-> 0.0700s
== CreateCategories: migrated (0.0700s)

== CreateBlogComments: migrating

– create_table(:blog_comments)
-> 0.0900s
== CreateBlogComments: migrated (0.0900s)

== CreateSweepstakes: migrating

– add_column(:offers, “type”, :text, {:default=>""})
-> 0.2810s
– add_column(:offers, “max_entries”, :integer, {:default=>“0”})
-> 0.1800s
– add_column(:offers, “entries_made”, :integer, {:default=>“0”})
-> 0.1900s
– add_column(:offers, “drawing_at”, :datetime)
-> 0.1900s
== CreateSweepstakes: migrated (0.8410s)

== CreateEntrants: migrating

– create_table(:entrants)
-> 0.1210s
== CreateEntrants: migrated (0.1210s)

== SetDefaultAmtAvailable: migrating

– change_column(:skus, “amt_available”, :integer, {:default=>0,
:null=>false})
-> 0.2500s
== SetDefaultAmtAvailable: migrated (0.2500s)

== AddOffertoEntrant: migrating

– add_column(:entrants, “offer_id”, :integer, {:default=>“0”})
-> 0.1900s
– add_column(:entrants, “draw_position”, :integer, {:default=>“0”})
-> 0.2210s
== AddOffertoEntrant: migrated (0.4110s)

== AddDrawnFlagToSweepstake: migrating

– add_column(:offers, “is_drawn”, :string, {:default=>“0”})
-> 0.2300s
== AddDrawnFlagToSweepstake: migrated (0.2300s)

== ChangeEntrantOfferIdFieldToSweepstakeId: migrating

– rename_column(:entrants, “offer_id”, “sweepstake_id”)
-> 0.4810s
== ChangeEntrantOfferIdFieldToSweepstakeId: migrated (0.4810s)

== AddPictureToUser: migrating

– add_column(:users, “image”, :string, {:default=>"", :limit=>255,
:null=>false})
-> 0.2100s
== AddPictureToUser: migrated (0.2100s)

== AddShowPictureFlagToUsers: migrating

– add_column(:users, “show_image”, :string, {:default=>“0”, :limit=>1,
:null=>false})
-> 0.4610s
== AddShowPictureFlagToUsers: migrated (0.4610s)

== AddAmtSoldToSkus: migrating

– add_column(:skus, “amt_sold”, :integer, {:default=>“0”,
:null=>false})
-> 0.2710s
– rename_column(:skus, “amt_available”, “amt_forsale”)
-> 0.2200s
== AddAmtSoldToSkus: migrated (0.4910s)

== AddOfferIdToLineItem: migrating

– add_column(:line_items, “offer_id”, :integer, {:default=>“0”,
:null=>false})
-> 0.2300s
== AddOfferIdToLineItem: migrated (0.2300s)

== AddNewEmailToUser: migrating

– add_column(:users, “new_email”, :string, {:default=>"", :limit=>255,
:null=>false})
-> 0.2110s
– add_column(:users, “new_email_token”, :string, {:default=>"",
:limit=>255, :null=>false})
-> 0.1800s
== AddNewEmailToUser: migrated (0.3910s)

** Invoke db:schema:dump (first_time)
** Invoke environment
** Execute db:schema:dump
Loaded suite c:/ruby/bin/rake.bat
Started

Finished in 0.0 seconds.

0 tests, 0 assertions, 0 failures, 0 errors

c:\dve\ideeli_trunk\lib\tasks>rake SRC=db/fixtures/demo
load_my_fixtures
rake SRC=db/fixtures/demo load_my_fixtures
(in c:/dve/ideeli_trunk)
Loading Fixtures from db/fixtures/demo
Loaded suite c:/ruby/bin/rake.bat
Started

Finished in 0.0 seconds.

0 tests, 0 assertions, 0 failures, 0 errors
– end output –

– Output from running rake deploy_demo --trace –
c:\dve\ideeli_trunk\lib\tasks>rake deploy_demo --trace
rake deploy_demo --trace
(in c:/dve/ideeli_trunk)
** Invoke deploy_demo (first_time)
** Invoke db:purge (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:purge
** Invoke db:migrate (first_time)
** Invoke environment
** Execute db:migrate
** Invoke db:schema:dump (first_time)
** Invoke environment
** Execute db:schema:dump
** Execute deploy_demo
** Invoke load_my_fixtures (first_time)
** Invoke environment
** Execute load_my_fixtures
Loading Fixtures from db/fixtures/demo
rake aborted!
Mysql::Error: #42S02Table ‘ideeli_development.blog_comments’ doesn’t
exist: DELETE FROM blog_comments
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract_adapter.rb:120:in
log' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/mysql_adapter.rb:184:inexecute’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/mysql_adapter.rb:199:in
delete' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:283:indelete_existing_fixtures’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:256:in
create_fixtures' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:256:increate_fixtures’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract/database_statements.rb:51:in
transaction' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:255:increate_fixtures’
c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/base.rb:794:in
silence' c:/ruby/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/fixtures.rb:248:increate_fixtures’
./lib/tasks/load_my_fixtures.rake:14
./lib/tasks/load_my_fixtures.rake:12
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:387:in execute' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:387:inexecute’
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:357:in invoke' c:/ruby/lib/ruby/1.8/thread.rb:135:insynchronize’
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:350:in invoke' ./lib/tasks/deploy_demo.rake:9 c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:387:inexecute’
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:387:in execute' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:357:ininvoke’
c:/ruby/lib/ruby/1.8/thread.rb:135:in synchronize' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:350:ininvoke’
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:1906:in run' c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:1906:inrun’
c:/ruby/lib/ruby/gems/1.8/gems/rake-0.7.1/bin/rake:7
c:/ruby/bin/rake.bat:25

– end –

When I check the db after running rake deploy_demo it is entirely empty
(ie. no tables).

Any ideas?

Thanks,

Mark