diff options
| author | Robert | 2017-06-06 15:50:54 +0200 | 
|---|---|---|
| committer | Robert | 2017-06-06 15:50:54 +0200 | 
| commit | 15b7cf8213d730fa36740a74bc75baa5fc56dd62 (patch) | |
| tree | ad1901d30d814257e738a0348d190171c212d6e5 /lib | |
| parent | 12290bbdabb8f53c4dddb1a647296a426b88709e (diff) | |
| download | chouette-core-15b7cf8213d730fa36740a74bc75baa5fc56dd62.tar.bz2 | |
Refs: #3604; rewriting schema cloner in Ruby
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/af83/schema_cloner.rb | 38 | 
1 files changed, 38 insertions, 0 deletions
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  | 
