aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--app/models/referential_cloning.rb43
-rw-r--r--spec/models/referential_cloning_spec.rb7
2 files changed, 45 insertions, 5 deletions
diff --git a/app/models/referential_cloning.rb b/app/models/referential_cloning.rb
index 6e102a807..179091485 100644
--- a/app/models/referential_cloning.rb
+++ b/app/models/referential_cloning.rb
@@ -20,14 +20,51 @@ class ReferentialCloning < ApplicationModel
def clone!
report = Benchmark.measure do
- AF83::SchemaCloner
- .new(source_referential.slug, target_referential.slug)
- .clone_schema
+ command = "#{dump_command} | #{sed_command} | #{restore_command}"
+ unless system command
+ raise "Copy of #{source_schema} to #{target_schema} failed"
+ end
end
target_referential.check_migration_count(report)
clean
end
+ def source_schema
+ source_referential.slug
+ end
+
+ def target_schema
+ target_referential.slug
+ end
+
+ def host
+ ActiveRecord::Base.connection_config[:host]
+ end
+
+ def username
+ ActiveRecord::Base.connection_config[:username]
+ end
+
+ def password
+ ActiveRecord::Base.connection_config[:password]
+ end
+
+ def database
+ ActiveRecord::Base.connection_config[:database]
+ end
+
+ def dump_command
+ "PGPASSWORD=#{password} pg_dump --host #{host} --username #{username} --schema=#{source_schema} #{database}"
+ end
+
+ def sed_command
+ "sed -e 's@SCHEMA #{source_schema}@SCHEMA #{target_schema}@' -e 's@SET search_path = #{source_schema}@SET search_path = #{target_schema}@'"
+ end
+
+ def restore_command
+ "PGPASSWORD=#{password} psql -q --host #{host} --username #{username} #{database}"
+ end
+
def clean
CleanUp.new(referential: target_referential).clean
end
diff --git a/spec/models/referential_cloning_spec.rb b/spec/models/referential_cloning_spec.rb
index 917be3a77..b017a37e9 100644
--- a/spec/models/referential_cloning_spec.rb
+++ b/spec/models/referential_cloning_spec.rb
@@ -37,10 +37,13 @@ RSpec.describe ReferentialCloning, :type => :model do
let(:cloner) { double }
it 'creates a schema cloner with source and target schemas and clone schema' do
- expect(AF83::SchemaCloner).to receive(:new).with(source_referential.slug, target_referential.slug).and_return(cloner)
- expect(cloner).to receive(:clone_schema)
+ %w{dump_command sed_command restore_command}.each do |command|
+ allow(referential_cloning).to receive(command).and_return(command)
+ end
allow(referential_cloning).to receive(:clean)
+ expect(referential_cloning).to receive(:system).with("dump_command | sed_command | restore_command").and_return(true)
+
referential_cloning.clone!
end
end