From e57d3758e4d9c94fd98ad9957df482b684e08561 Mon Sep 17 00:00:00 2001 From: Stuart Terrett Date: Tue, 1 May 2018 15:32:22 -0400 Subject: [PATCH 1/2] Dump schema after every tenant migration --- lib/tasks/apartment.rake | 16 ++++++++++++++++ spec/tasks/apartment_rake_spec.rb | 32 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) 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/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 From 5865e24ffe601a28ea8133fb3e38e6a1dd126e8b Mon Sep 17 00:00:00 2001 From: Stuart Terrett Date: Wed, 9 May 2018 13:37:19 -0400 Subject: [PATCH 2/2] Stub current_version call on migration context --- spec/integration/apartment_rake_integration_spec.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 }