diff options
| -rwxr-xr-x[-rw-r--r--] | spec/lib/af83/stored_procedures/clone_schema_spec.rb | 122 | ||||
| -rw-r--r-- | spec/support/pg_catalog.rb | 26 |
2 files changed, 127 insertions, 21 deletions
diff --git a/spec/lib/af83/stored_procedures/clone_schema_spec.rb b/spec/lib/af83/stored_procedures/clone_schema_spec.rb index 646e97d9f..6ec0de5e7 100644..100755 --- a/spec/lib/af83/stored_procedures/clone_schema_spec.rb +++ b/spec/lib/af83/stored_procedures/clone_schema_spec.rb @@ -1,19 +1,121 @@ -require 'rails_helper' +require 'spec_helper' + +include Support::PGCatalog RSpec.describe StoredProcedures do + let( :source_schema ){ "source_schema" } + let( :target_schema ){ "target_schema" } + let( :child_table ){ "children" } + let( :parent_table ){ "parents" } + + before do + create_schema_with_tables + StoredProcedures.create_stored_procedure :clone_schema + end + + context "meta specs describe source schema's introspection" do + it "shows, sequences are correctly read", :meta do + expect(get_sequences(source_schema, child_table)) + .to eq([{"sequence_name"=>"#{child_table}_id_seq", + "last_value"=>"1", + "start_value"=>"1", + "increment_by"=>"1", + "max_value"=>"9223372036854775807", + "min_value"=>"1", + "cache_value"=>"1", + "log_cnt"=>"0", + "is_cycled"=>"f", + "is_called"=>"f"}]) + + expect(get_sequences(source_schema, parent_table)) + .to eq([{"sequence_name"=>"#{parent_table}_id_seq", + "last_value"=>"1", + "start_value"=>"1", + "increment_by"=>"1", + "max_value"=>"9223372036854775807", + "min_value"=>"1", + "cache_value"=>"1", + "log_cnt"=>"0", + "is_cycled"=>"f", + "is_called"=>"f"}]) + end + + it "shows foreign key constraints are correctly read" do + expect( get_foreign_keys(source_schema, child_table) ) + .to eq([{ + "constraint_name" => "children_parents", + "constraint_def" => "FOREIGN KEY (parents_id) REFERENCES source_schema.parents(id)"}]) + end + end + + context "before cloning" do + it "target schema does not exist" do + expect( get_schema_oid(target_schema) ).to be_nil + end + end - include Support::PGCatalog - context "clone_schema creates correct table in dest schema" do + context "after cloning" do before do - drop_schema!(dest_schema_name) + described_class.invoke_stored_procedure(:clone_schema, source_schema, target_schema, false) + end + + it "target schema does exist" do + expect( get_schema_oid(target_schema) ).not_to be_nil + end + + it "has the correct sequences" do + expect(get_sequences(target_schema, child_table)) + .to eq([{"sequence_name"=>"#{child_table}_id_seq", + "last_value"=>"1", + "start_value"=>"1", + "increment_by"=>"1", + "max_value"=>"9223372036854775807", + "min_value"=>"1", + "cache_value"=>"1", + "log_cnt"=>"0", + "is_cycled"=>"f", + "is_called"=>"f"}]) + + expect(get_sequences(target_schema, parent_table)) + .to eq([{"sequence_name"=>"#{parent_table}_id_seq", + "last_value"=>"1", + "start_value"=>"1", + "increment_by"=>"1", + "max_value"=>"9223372036854775807", + "min_value"=>"1", + "cache_value"=>"1", + "log_cnt"=>"0", + "is_cycled"=>"f", + "is_called"=>"f"}]) end - it "creates the schema" do - expect( get_fks!(dest_schema_name, "access_links") ).to be_empty - described_class.invoke_stored_procedure(:clone_schema, source_schema_name, dest_schema_name, true) - expect( get_fks!(dest_schema_name, "access_links") ) - .to eq( [{"constraint_name"=>"aclk_acpt_fkey", - "constraint_def"=>"FOREIGN KEY (access_point_id) REFERENCES parissudest201604_v1.access_points(id)"}]) + it "has the correct foreign keys" do + expect( get_foreign_keys(target_schema, child_table) ) + .to eq([{ + "constraint_name" => "children_parents", + "constraint_def" => "FOREIGN KEY (parents_id) REFERENCES target_schema.parents(id)"}]) end + end + end + +def create_schema_with_tables + execute("CREATE SCHEMA IF NOT EXISTS #{source_schema}") + execute <<-EOSQL + DROP SCHEMA IF EXISTS #{source_schema} CASCADE; + CREATE SCHEMA #{source_schema}; + + CREATE TABLE #{source_schema}.#{parent_table} ( + id bigserial PRIMARY KEY + ); + CREATE TABLE #{source_schema}.#{child_table} ( + id bigserial PRIMARY KEY, + #{parent_table}_id bigint + ); + ALTER TABLE #{source_schema}.#{child_table} + ADD CONSTRAINT #{child_table}_#{parent_table} + FOREIGN KEY( #{parent_table}_id ) REFERENCES #{source_schema}.#{parent_table}(id); + EOSQL +end + diff --git a/spec/support/pg_catalog.rb b/spec/support/pg_catalog.rb index 629bee9ae..3cd3966c7 100644 --- a/spec/support/pg_catalog.rb +++ b/spec/support/pg_catalog.rb @@ -1,25 +1,29 @@ module Support module PGCatalog - def drop_schema!(schema_name) - execute("DROP SCHEMA #{schema_name} CASCADE") - rescue - nil - end - - def get_foreign_keys!(schema_oid, table_name) - schema_oid = get_schema_oid!(schema_oid) unless Integer === schema_oid + def get_foreign_keys(schema_oid, table_name) + schema_oid = get_schema_oid(schema_oid) unless Integer === schema_oid return [] unless schema_oid execute(foreign_key_query(schema_oid, table_name)) .to_a end - def get_schema_oid!(schema_name) + + def get_schema_oid(schema_name) execute("SELECT oid FROM pg_namespace WHERE nspname = '#{schema_name}'") .values .flatten .first end + def get_sequences(schema_name, table_name) + sequences = execute <<-EOSQL + SELECT sequence_name FROM information_schema.sequences + WHERE sequence_schema = '#{schema_name}' AND sequence_name LIKE '#{table_name}%' + EOSQL + sequences.values.flatten.map do | sequence | + execute "SELECT * from #{schema_name}.#{sequence}" + end.flat_map(&:to_a) + end def table_from_schema(schema_name, table_name) execute @@ -54,8 +58,8 @@ module Support end - def get_or_create_query(query_key, query_value) - queries.fetch(query_key){ queries[query_key] = query_value } + def get_or_create_query(query_key, &query_value) + queries.fetch(query_key){ queries[query_key] = query_value.() } end def queries |
