Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #5818 (reopened defect)

Opened 2 years ago

Last modified 11 months ago

second rake throws exception because test db has tables

Reported by: ryand-ruby@zenspider.com Assigned to: David
Priority: normal Milestone: 1.2.7
Component: ActiveRecord Version: 1.1.1
Severity: normal Keywords: postgresql migrations
Cc: bitsweat, tarmo

Description (Last modified by bitsweat)

If I try to run rake back to back against a virgin test DB, I get an exception on the second rake. Is it possible that :force => true doesn't work for postgresql?

############################################################
# This file is autogenerated. Instead of editing this file, please use the
# migrations feature of ActiveRecord to incrementally modify your database, and
# then regenerate this schema definition.

ActiveRecord::Schema.define(:version => 2) do

  create_table "bands", :force => true do |t|
    t.column "name", :string, :limit => 50
    t.column "image_url", :string
    t.column "homepage_url", :string
    t.column "description", :text
    t.column "band_blurb", :text
    t.column "sidebar", :text
  end

  create_table "podcasts", :force => true do |t|
    t.column "record_date", :date
    t.column "volume", :integer
    t.column "episode", :integer
    t.column "base_url", :string
    t.column "band_id", :integer, :null => false
    t.column "length", :integer
    t.column "transcript", :text
  end

  add_foreign_key_constraint "podcasts", "band_id", "bands", "id", :name => "podcasts_band_id_fkey", :on_update => :cascade, :on_delete => :cascade
end

############################################################
507 % dropdb brightside_test
DROP DATABASE
507 % createdb brightside_test
CREATE DATABASE
508 % rake
(in /Users/ryan/Work/svn/brightside/brightside)
/usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader.rb" "test/unit/application_helper_test.rb" "test/unit/band_test.rb" "test/unit/podcast_test.rb" 
Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader
Started
................
Finished in 1.009946 seconds.

16 tests, 27 assertions, 0 failures, 0 errors
/usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader.rb" "test/controllers/admin_controller_test.rb" "test/controllers/band_controller_test.rb" 
Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader
Started
......................
Finished in 1.415713 seconds.

22 tests, 56 assertions, 0 failures, 0 errors
/usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader.rb"  
/usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader.rb" "test/views/admin_view_test.rb" "test/views/band_view_test.rb" 
Loaded suite /usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader
Started
................
Finished in 0.902095 seconds.

16 tests, 60 assertions, 0 failures, 0 errors
/usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader.rb"  
/usr/local/bin/ruby -Ilib:test "/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake/rake_test_loader.rb"  
510 % rake --trace
rake --trace
(in /Users/ryan/Work/svn/brightside/brightside)
** Invoke default (first_time)
** Invoke test (first_time)
** Invoke test:units (first_time)
** Invoke db:test:prepare (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:test:prepare
** Invoke db:test:clone (first_time)
** Invoke db:schema:dump (first_time)
** Invoke environment 
** Execute db:schema:dump
** Execute db:test:clone
** Invoke db:schema:load (first_time)
** Invoke environment 
** Execute db:schema:load
rake aborted!
RuntimeError: ERROR	C42P07	Mrelation "bands" already exists	Fheap.c	L695	Rheap_create_with_catalog: CREATE TABLE bands ("id" serial primary key, "name" character varying(50), "image_url" character varying(255), "homepage_url" character varying(255), "description" text, "band_blurb" text, "sidebar" text) 
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract_adapter.rb:120:in `log'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/postgresql_adapter.rb:148:in `execute'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/connection_adapters/abstract/schema_statements.rb:104:in `create_table'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/migration.rb:273:in `method_missing'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/migration.rb:257:in `say_with_time'
/usr/local/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/migration.rb:257:in `say_with_time'
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/migration.rb:271:in `method_missing'
./db/schema.rb:7
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.14.4/lib/active_record/schema.rb:43:in `define'
./db/schema.rb:5
/usr/local/lib/ruby/gems/1.8/gems/activesupport-1.3.1/lib/active_support/dependencies.rb:140:in `load'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.5/lib/tasks/databases.rake:31
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:387:in `execute'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:387:in `execute'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:357:in `invoke'
/usr/local/lib/ruby/1.8/thread.rb:135:in `synchronize'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:350:in `invoke'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.5/lib/tasks/databases.rake:72
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:387:in `execute'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:387:in `execute'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:357:in `invoke'
/usr/local/lib/ruby/1.8/thread.rb:135:in `synchronize'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:350:in `invoke'
/usr/local/lib/ruby/gems/1.8/gems/rails-1.1.5/lib/tasks/databases.rake:139
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:387:in `execute'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:387:in `execute'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:357:in `invoke'
/usr/local/lib/ruby/1.8/thread.rb:135:in `synchronize'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:350:in `invoke'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:364:in `invoke_prerequisites'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:999:in `each'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:363:in `invoke_prerequisites'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:356:in `invoke'
/usr/local/lib/ruby/1.8/thread.rb:135:in `synchronize'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:350:in `invoke'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:364:in `invoke_prerequisites'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:999:in `each'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:363:in `invoke_prerequisites'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:356:in `invoke'
/usr/local/lib/ruby/1.8/thread.rb:135:in `synchronize'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:350:in `invoke'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:364:in `invoke_prerequisites'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:999:in `each'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:363:in `invoke_prerequisites'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:356:in `invoke'
/usr/local/lib/ruby/1.8/thread.rb:135:in `synchronize'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:350:in `invoke'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:1906:in `run'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/lib/rake.rb:1906:in `run'
/usr/local/lib/ruby/gems/1.8/gems/rake-0.7.1/bin/rake:7
/usr/local/bin/rake:18

Change History

08/19/06 05:39:14 changed by bitsweat

  • keywords set to postgresql migrations.
  • version set to 1.1.1.
  • description changed.
  • milestone set to 1.2.

08/19/06 05:40:31 changed by bitsweat

  • cc set to bitsweat.

Does it work without the fk constraint?

(in reply to: ↑ description ) 09/20/06 09:01:00 changed by alex

From schema_statements.rb:

        if options[:force]
          drop_table(name) rescue nil
        end

Ie., AR completely ignores any failure on the part of drop_table. If you have integrity constraints between tables, it's going to fail, because AR does not attempt to drop tables in the correct order.

08/22/07 04:38:25 changed by tarmo

  • status changed from new to closed.
  • resolution set to invalid.

As explained by alex, ":force => true" indeed may not work when there are foreign keys. So I'm closing this. Please reopen with more details for the unlikely event that it's a problem even when no using foreign keys.

08/22/07 21:31:08 changed by tarmo

  • cc changed from bitsweat to bitsweat, tarmo.
  • status changed from closed to reopened.
  • resolution deleted.

Well it seems something can be fixed here, namely the fact that drop_table exceptions are ignored. What really should be done is to test if the table exists and then assume that drop_table will be able to remove it. (if it can't with foreign keys then at least one would find out the reason).