Skip to content

Commit

Permalink
Add spec for rails 3.2-style migrations.
Browse files Browse the repository at this point in the history
I changed DbCharmer::ConnectionFactory.connect to connection_with_name, because rails 3.2 call .with_connection, so .execute is no longer being called on DbCharmer::ConnectionProxy, but on the real adapter
  • Loading branch information
libc committed Feb 3, 2012
1 parent 57832cd commit 641d9d5
Showing 1 changed file with 66 additions and 12 deletions.
78 changes: 66 additions & 12 deletions spec/unit/active_record/migration/multi_db_migrations_spec.rb
Expand Up @@ -58,6 +58,28 @@ def self.down
end
end

class SpecMultiDbMigration5 < ActiveRecord::Migration
db_magic :connections => [:logs, :default]

def up
execute "UPDATE log_records SET level = 'hoho'"
end

def down
execute "UPDATE log_records SET level = 'blah'"
end
end

class SpecMultiDbMigration6 < ActiveRecord::Migration
def change
on_db(:logs) do
create_table :logs_rails32_test do |t|
t.text :t
end
end
end
end

describe "Multi-db migractions" do
before(:all) do
DbCharmer.connections_should_exist = true
Expand All @@ -67,6 +89,10 @@ def self.down
DbCharmer.connections_should_exist = false
end

def connection_with_name(name)
DbCharmer::ConnectionFactory.connect(name).abstract_connection_class.retrieve_connection
end

describe "w/o any magic calls" do
it "should send all up requests to the default connection" do
ActiveRecord::Base.connection.should_receive(:execute).with("UPDATE log_records SET level = 'debug'")
Expand All @@ -82,7 +108,7 @@ def self.down
it "should use default migration config" do
ActiveRecord::Migration.db_magic :connection => :logs
ActiveRecord::Base.connection.should_not_receive(:execute)
DbCharmer::ConnectionFactory.connect(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'debug'")
connection_with_name(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'debug'")
SpecMigration.migrate(:up)
ActiveRecord::Migration.db_magic :connection => :default
end
Expand All @@ -92,22 +118,22 @@ def self.down
describe "with db_magic calls" do
it "should send all up requests to specified connection" do
ActiveRecord::Base.connection.should_not_receive(:execute)
DbCharmer::ConnectionFactory.connect(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'debug'")
connection_with_name(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'debug'")
SpecMultiDbMigration.migrate(:up)
end

it "should send all down requests to specified connection" do
ActiveRecord::Base.connection.should_not_receive(:execute)
DbCharmer::ConnectionFactory.connect(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'blah'")
connection_with_name(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'blah'")
SpecMultiDbMigration.migrate(:down)
end

describe "after AR::Migration db_magic call" do
it "should use spcified connection and ignore global migration config" do
it "should use specified connection and ignore global migration config" do
ActiveRecord::Migration.db_magic :connection => :slave01
ActiveRecord::Base.connection.should_not_receive(:execute)
DbCharmer::ConnectionFactory.connect(:slave01).should_not_receive(:execute)
DbCharmer::ConnectionFactory.connect(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'debug'")
connection_with_name(:slave01).should_not_receive(:execute)
connection_with_name(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'debug'")
SpecMultiDbMigration.migrate(:up)
ActiveRecord::Migration.db_magic :connection => :default
end
Expand All @@ -117,42 +143,70 @@ def self.down
describe "with on_db blocks" do
it "should send specified up requests to specified connection" do
ActiveRecord::Base.connection.should_receive(:execute).with("UPDATE log_records SET level = 'yo'")
DbCharmer::ConnectionFactory.connect(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'debug'")
connection_with_name(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'debug'")
SpecMultiDbMigration2.migrate(:up)
end

it "should send secified down requests to specified connection" do
ActiveRecord::Base.connection.should_receive(:execute).with("UPDATE log_records SET level = 'bar'")
DbCharmer::ConnectionFactory.connect(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'blah'")
connection_with_name(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'blah'")
SpecMultiDbMigration2.migrate(:down)
end
end

describe "with db_magic calls" do
it "should send all up requests to specified connection" do
ActiveRecord::Base.connection.should_receive(:execute).with("UPDATE log_records SET level = 'hoho'")
DbCharmer::ConnectionFactory.connect(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'hoho'")
connection_with_name(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'hoho'")
SpecMultiDbMigration3.migrate(:up)
end

it "should send all down requests to specified connection" do
ActiveRecord::Base.connection.should_receive(:execute).with("UPDATE log_records SET level = 'blah'")
DbCharmer::ConnectionFactory.connect(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'blah'")
connection_with_name(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'blah'")
SpecMultiDbMigration3.migrate(:down)
end
end

describe "with db_magic calls" do
it "should send all up requests to specified connection" do
ActiveRecord::Base.connection.should_receive(:execute).with("UPDATE log_records SET level = 'hoho'")
DbCharmer::ConnectionFactory.connect(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'hoho'")
connection_with_name(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'hoho'")
SpecMultiDbMigration4.migrate(:up)
end

it "should send all down requests to specified connection" do
ActiveRecord::Base.connection.should_receive(:execute).with("UPDATE log_records SET level = 'blah'")
DbCharmer::ConnectionFactory.connect(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'blah'")
connection_with_name(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'blah'")
SpecMultiDbMigration4.migrate(:down)
end
end

describe 'with db_magic calls in instance methods' do
it "should send all up requests to specified connection" do
ActiveRecord::Base.connection.should_receive(:execute).with("UPDATE log_records SET level = 'hoho'")
connection_with_name(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'hoho'")
SpecMultiDbMigration5.migrate(:up)
end

it "should send all down requests to specified connection" do
ActiveRecord::Base.connection.should_receive(:execute).with("UPDATE log_records SET level = 'blah'")
connection_with_name(:logs).should_receive(:execute).with("UPDATE log_records SET level = 'blah'")
SpecMultiDbMigration5.migrate(:down)
end
end

describe 'with db_magic calls in recorder' do
it "should send all up requests to specified connection" do
ActiveRecord::Base.connection.should_not_receive(:execute)
connection_with_name(:logs).should_receive(:execute).with(/CREATE TABLE/)
SpecMultiDbMigration6.migrate(:up)
end

it "should send all down requests to specified connection" do
ActiveRecord::Base.connection.should_not_receive(:execute)
connection_with_name(:logs).should_receive(:execute).with(/DROP TABLE/)
SpecMultiDbMigration6.migrate(:down)
end
end
end

0 comments on commit 641d9d5

Please sign in to comment.