From a2c4a001e711fb9572063a2b195d9c39369f8909 Mon Sep 17 00:00:00 2001 From: Alastair McGowan-Douglas Date: Tue, 3 Nov 2015 12:04:59 +0000 Subject: [PATCH] Test deferrable on/off - not very thorough yet --- t/30sqlt-new-diff-pgsql.t | 4 +- t/47postgres-producer.t | 75 ++++++++++++++++++++++------------- t/data/diff/pgsql/create1.yml | 2 +- t/data/diff/pgsql/create2.yml | 2 +- 4 files changed, 52 insertions(+), 31 deletions(-) diff --git a/t/30sqlt-new-diff-pgsql.t b/t/30sqlt-new-diff-pgsql.t index 5bc782038..07e51ed8d 100644 --- a/t/30sqlt-new-diff-pgsql.t +++ b/t/30sqlt-new-diff-pgsql.t @@ -90,7 +90,7 @@ ALTER TABLE "person" ADD CONSTRAINT "unique_name" UNIQUE ("name"); ALTER TABLE "person" ADD CONSTRAINT "UC_person_id" UNIQUE ("person_id"); -ALTER TABLE "person" ADD CONSTRAINT "UC_age_name" UNIQUE ("age", "name"); +ALTER TABLE "person" ADD CONSTRAINT "UC_age_name" UNIQUE ("age", "name") DEFERRABLE; DROP TABLE "deleted" CASCADE; @@ -143,7 +143,7 @@ ALTER TABLE person RENAME COLUMN description TO physical_description; ALTER TABLE person ADD CONSTRAINT UC_person_id UNIQUE (person_id); -ALTER TABLE person ADD CONSTRAINT UC_age_name UNIQUE (age, name); +ALTER TABLE person ADD CONSTRAINT UC_age_name UNIQUE (age, name) DEFERRABLE; DROP TABLE deleted CASCADE; diff --git a/t/47postgres-producer.t b/t/47postgres-producer.t index 9c50db736..08a043bc6 100644 --- a/t/47postgres-producer.t +++ b/t/47postgres-producer.t @@ -22,6 +22,16 @@ BEGIN { use Test::Differences; use SQL::Translator; +# normalised-space-is, i.e. are these the same after normalising whitespace? +sub ns_is { + my ($first, $second) = (shift, shift); + $first =~ s/\s+/ /g; + $second =~ s/\s+/ /g; + + # avoid prototype + &is( $first, $second, @_ ); +} + my $PRODUCER = \&SQL::Translator::Producer::PostgreSQL::create_field; { @@ -146,32 +156,39 @@ for my $name ( 'foo', undef ) { type => 'FOREIGN_KEY', reference_table => $table2, reference_fields => [qw(myfield_2)], + deferrable => 0, ); - my ($fk_constraint_def_ref, $fk_constraint_fk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($fk_constraint); + for my $constraint ($fk_constraint, $fk_constraint_2) { + my ($fk_constraint_def_ref, $fk_constraint_fk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($constraint); - ok(@{$fk_constraint_def_ref} == 0 && @{$fk_constraint_fk_ref} == 1, 'precheck of create_Foreign Key constraint'); + ok(@{$fk_constraint_def_ref} == 0 && @{$fk_constraint_fk_ref} == 1, 'precheck of create_Foreign Key constraint'); - if ( $name ) { - is($fk_constraint_fk_ref->[0], "ALTER TABLE mytable ADD CONSTRAINT $name FOREIGN KEY (myfield) - REFERENCES mytable2 (myfield_2) DEFERRABLE", 'Create Foreign Key Constraint works'); + my $deferrable = $constraint->deferrable ? ' DEFERRABLE' : ''; - # ToDo: may we should check if the constraint name was valid, or if next - # unused_name created has choosen a different one - my $alter_fk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($fk_constraint); - is($alter_fk_constraint, "ALTER TABLE mytable DROP CONSTRAINT $name", 'Alter drop Foreign Key constraint works'); - } - else { - is($fk_constraint_fk_ref->[0], 'ALTER TABLE mytable ADD FOREIGN KEY (myfield) - REFERENCES mytable2 (myfield_2) DEFERRABLE', 'Create un-named Foreign Key Constraint works'); + if ( $name ) { - my $alter_fk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($fk_constraint); - is($alter_fk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT mytable_myfield_fkey', 'Alter drop un-named Foreign Key constraint works'); + ns_is($fk_constraint_fk_ref->[0], "ALTER TABLE mytable ADD CONSTRAINT $name FOREIGN KEY (myfield) + REFERENCES mytable2 (myfield_2)$deferrable", 'Create Foreign Key Constraint works'); + + # ToDo: may we should check if the constraint name was valid, or if next + # unused_name created has choosen a different one + my $alter_fk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($constraint); + is($alter_fk_constraint, "ALTER TABLE mytable DROP CONSTRAINT $name", 'Alter drop Foreign Key constraint works'); + } + else { + ns_is($fk_constraint_fk_ref->[0], "ALTER TABLE mytable ADD FOREIGN KEY (myfield) + REFERENCES mytable2 (myfield_2)$deferrable", 'Create un-named Foreign Key Constraint works'); + + my $alter_fk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($constraint); + is($alter_fk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT mytable_myfield_fkey', 'Alter drop un-named Foreign Key constraint works'); + } } } # check named, and unnamed primary keys for my $name ( 'foo', undef ) { + # PK defaults to deferrable => 0 my $pk_constraint = SQL::Translator::Schema::Constraint->new( table => $table, name => $name, @@ -183,23 +200,27 @@ for my $name ( 'foo', undef ) { name => $name, fields => [qw(myfield)], type => 'PRIMARY_KEY', + deferrable => 1, ); - my ($pk_constraint_def_ref, $pk_constraint_pk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($pk_constraint); + for my $pk ($pk_constraint, $pk_constraint_2) { + my ($pk_constraint_def_ref, $pk_constraint_pk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($pk); - if ( $name ) { - is($pk_constraint_def_ref->[0], "CONSTRAINT $name PRIMARY KEY (myfield)", 'Create Primary Key Constraint works'); + my $deferrable = $pk->deferrable ? ' DEFERRABLE' : ''; + if ( $name ) { + is($pk_constraint_def_ref->[0], "CONSTRAINT $name PRIMARY KEY (myfield)$deferrable", 'Create Primary Key Constraint works'); - # ToDo: may we should check if the constraint name was valid, or if next - # unused_name created has choosen a different one - my $alter_pk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($pk_constraint); - is($alter_pk_constraint, "ALTER TABLE mytable DROP CONSTRAINT $name", 'Alter drop Primary Key constraint works'); - } - else { - is($pk_constraint_def_ref->[0], 'PRIMARY KEY (myfield)', 'Create un-named Primary Key Constraint works'); + # ToDo: may we should check if the constraint name was valid, or if next + # unused_name created has choosen a different one + my $alter_pk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($pk); + is($alter_pk_constraint, "ALTER TABLE mytable DROP CONSTRAINT $name", 'Alter drop Primary Key constraint works'); + } + else { + is($pk_constraint_def_ref->[0], "PRIMARY KEY (myfield)$deferrable", 'Create un-named Primary Key Constraint works'); - my $alter_pk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($pk_constraint); - is($alter_pk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT mytable_pkey', 'Alter drop un-named Foreign Key constraint works'); + my $alter_pk_constraint = SQL::Translator::Producer::PostgreSQL::alter_drop_constraint($pk); + is($alter_pk_constraint, 'ALTER TABLE mytable DROP CONSTRAINT mytable_pkey', 'Alter drop un-named Foreign Key constraint works'); + } } } diff --git a/t/data/diff/pgsql/create1.yml b/t/data/diff/pgsql/create1.yml index 3bea5f9eb..c77e3743c 100644 --- a/t/data/diff/pgsql/create1.yml +++ b/t/data/diff/pgsql/create1.yml @@ -123,7 +123,7 @@ schema: reference_fields: [] reference_table: '' type: PRIMARY KEY - - deferrable: 0 + - deferrable: 1 expression: '' fields: - age diff --git a/t/data/diff/pgsql/create2.yml b/t/data/diff/pgsql/create2.yml index ab510d431..2a27df649 100644 --- a/t/data/diff/pgsql/create2.yml +++ b/t/data/diff/pgsql/create2.yml @@ -120,7 +120,7 @@ schema: reference_fields: [] reference_table: '' type: UNIQUE - - deferrable: 0 + - deferrable: 1 expression: '' fields: - age