From 15b7cf8213d730fa36740a74bc75baa5fc56dd62 Mon Sep 17 00:00:00 2001 From: Robert Date: Tue, 6 Jun 2017 15:50:54 +0200 Subject: Refs: #3604; rewriting schema cloner in Ruby --- lib/af83/schema_cloner.rb | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 lib/af83/schema_cloner.rb (limited to 'lib') diff --git a/lib/af83/schema_cloner.rb b/lib/af83/schema_cloner.rb new file mode 100644 index 000000000..933fffc3d --- /dev/null +++ b/lib/af83/schema_cloner.rb @@ -0,0 +1,38 @@ +module AF83 + class SchemaCloner + + attr_reader :source_schema, :target_schema, :include_records + def clone_schema(source_schema, target_schema, include_records: true) + @source_schema = source_schema + @target_schema = target_schema + @include_records = include_records + + clone_schema_ + end + + private + def assure_schema_preconditons + raise RuntimeError, "Target Schema #{target_schema} does already exist" unless + execute("SELECT oid FROM pg_namespace WHERE nspname = '#{target_schema}' LIMIT 1").empty? + + raise RuntimeError, "Source Schema #{source_schema} does not exist" unless source + end + + def clone_schema_ + assure_schema_preconditons + end + def connection + @__connection__ ||= ActiveRecord::Base.connection + end + def execute(str) + connection.execute(str).to_a + end + + def source + @__source__ ||= execute("SELECT oid FROM pg_namespace WHERE nspname = '#{source_schema}' LIMIT 1").first; + end + def source_oid + @__source_oid__ ||= source["oid"].to_i; + end + end +end -- cgit v1.2.3