Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions lib/tasks/apartment.rake
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ apartment_namespace = namespace :apartment do
begin
puts("Migrating #{tenant} tenant")
Apartment::Migrator.migrate tenant
dump_schema
rescue Apartment::TenantNotFound => e
puts e.message
end
Expand Down Expand Up @@ -66,6 +67,7 @@ apartment_namespace = namespace :apartment do
begin
puts("Rolling back #{tenant} tenant")
Apartment::Migrator.rollback tenant, step
dump_schema
rescue Apartment::TenantNotFound => e
puts e.message
end
Expand All @@ -84,6 +86,7 @@ apartment_namespace = namespace :apartment do
begin
puts("Migrating #{tenant} tenant up")
Apartment::Migrator.run :up, tenant, version
dump_schema
rescue Apartment::TenantNotFound => e
puts e.message
end
Expand All @@ -101,6 +104,7 @@ apartment_namespace = namespace :apartment do
begin
puts("Migrating #{tenant} tenant down")
Apartment::Migrator.run :down, tenant, version
dump_schema
rescue Apartment::TenantNotFound => e
puts e.message
end
Expand Down Expand Up @@ -142,4 +146,16 @@ apartment_namespace = namespace :apartment do
WARNING
end
end

def dump_schema
if ActiveRecord::Base.dump_schema_after_migration
Rake::TaskManager.record_task_metadata=true
case ActiveRecord::Base.schema_format
when :ruby then Rake.application.invoke_task("db:schema:dump")
when :sql then Rake.application.invoke_task("db:structure:dump")
else
raise "unknown schema format #{ActiveRecord::Base.schema_format}"
end
end
end
end
6 changes: 5 additions & 1 deletion spec/integration/apartment_rake_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,11 @@
end

context "with ActiveRecord above or equal to 5.2.0" do
let(:migration_context_double) { double(:migration_context) }
let(:migration_context_double) do
context = double(:migration_context)
allow(context).to receive(:current_version).and_return("20111202022214")
context
end

before do
allow(Apartment::Migrator).to receive(:activerecord_below_5_2?) { false }
Expand Down
32 changes: 32 additions & 0 deletions spec/tasks/apartment_rake_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,19 @@
Rake::Task.define_task('db:migrate:up')
Rake::Task.define_task('db:migrate:down')
Rake::Task.define_task('db:migrate:redo')
Rake::Task.define_task('db:schema:dump')
Rake::Task.define_task('db:structure:dump')
end

after do
Rake.application = nil
ENV['VERSION'] = nil # linux users reported env variable carrying on between tests

structure_file = "spec/dummy/db/structure.sql"

if File.exists?(structure_file)
File.delete(structure_file)
end
end

after(:all) do
Expand All @@ -47,6 +55,12 @@
expect(Apartment::Migrator).to receive(:migrate).exactly(tenant_count).times
@rake['apartment:migrate'].invoke
end

it "should dump the schema after each tenant" do
allow(Apartment::Migrator).to receive(:migrate)
expect(@rake).to receive(:invoke_task).exactly(tenant_count).times
@rake["apartment:migrate"].invoke
end
end

describe "apartment:migrate:up" do
Expand All @@ -73,6 +87,12 @@
expect(Apartment::Migrator).to receive(:run).with(:up, anything, version.to_i).exactly(tenant_count).times
@rake['apartment:migrate:up'].invoke
end

it "should dump the schema after each tenant" do
allow(Apartment::Migrator).to receive(:run)
expect(Rake.application).to receive(:invoke_task).exactly(tenant_count).times
@rake["apartment:migrate:up"].invoke
end
end
end

Expand Down Expand Up @@ -100,6 +120,12 @@
expect(Apartment::Migrator).to receive(:run).with(:down, anything, version.to_i).exactly(tenant_count).times
@rake['apartment:migrate:down'].invoke
end

it "should dump the schema after each tenant" do
allow(Apartment::Migrator).to receive(:run)
expect(Rake.application).to receive(:invoke_task).exactly(tenant_count).times
@rake["apartment:migrate:down"].invoke
end
end
end

Expand All @@ -116,6 +142,12 @@
ENV['STEP'] = step
@rake['apartment:rollback'].invoke
end

it "should dump the schema after each tenant" do
allow(Apartment::Migrator).to receive(:rollback)
expect(Rake.application).to receive(:invoke_task).exactly(tenant_count).times
@rake["apartment:rollback"].invoke
end
end

describe "apartment:drop" do
Expand Down