From 2bcd11d80beb632f5204672bdbd9efc8b706ca12 Mon Sep 17 00:00:00 2001 From: RobertDober Date: Tue, 18 Apr 2017 09:44:21 +0200 Subject: basic clone_schema spex added; Refs #2864 --- .../af83/stored_procedures/clone_schema_spec.rb | 122 +++++++++++++++++++-- 1 file changed, 112 insertions(+), 10 deletions(-) mode change 100644 => 100755 spec/lib/af83/stored_procedures/clone_schema_spec.rb (limited to 'spec/lib') diff --git a/spec/lib/af83/stored_procedures/clone_schema_spec.rb b/spec/lib/af83/stored_procedures/clone_schema_spec.rb old mode 100644 new mode 100755 index 646e97d9f..6ec0de5e7 --- 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 + -- cgit v1.2.3