diff --git a/lib/tasks/apartment.rake b/lib/tasks/apartment.rake index 2fd94f37..277122b4 100644 --- a/lib/tasks/apartment.rake +++ b/lib/tasks/apartment.rake @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/integration/apartment_rake_integration_spec.rb b/spec/integration/apartment_rake_integration_spec.rb index 4b9b098d..426eedff 100644 --- a/spec/integration/apartment_rake_integration_spec.rb +++ b/spec/integration/apartment_rake_integration_spec.rb @@ -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 } diff --git a/spec/tasks/apartment_rake_spec.rb b/spec/tasks/apartment_rake_spec.rb index 0f86aeee..9f838de0 100644 --- a/spec/tasks/apartment_rake_spec.rb +++ b/spec/tasks/apartment_rake_spec.rb @@ -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 @@ -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 @@ -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 @@ -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 @@ -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