Resurrect the DB2 precompiled grammar to which we lost the source
[dbsrgits/SQL-Translator.git] / t / 47postgres-producer.t
index 10b2bc5..7b4cf27 100644 (file)
@@ -14,7 +14,7 @@ use FindBin qw/$Bin/;
 #=============================================================================
 
 BEGIN {
-    maybe_plan(39,
+    maybe_plan(51,
         'SQL::Translator::Producer::PostgreSQL',
         'Test::Differences',
     )
@@ -64,12 +64,113 @@ my $field2 = SQL::Translator::Schema::Field->new( name      => 'myfield',
                                                   is_foreign_key => 0,
                                                   is_unique => 0 );
 
+my $pk_constraint = SQL::Translator::Schema::Constraint->new(
+    table => $table,
+    name   => 'foo',
+    fields => [qw(myfield)],
+    type   => 'PRIMARY_KEY',
+);
+
+my  ($pk_constraint_def_ref, $pk_constraint_fk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($pk_constraint);
+ok(@{$pk_constraint_def_ref} == 1 && @{$pk_constraint_fk_ref} == 0,  'precheck of create_Primary Key constraint');
+is($pk_constraint_def_ref->[0], 'CONSTRAINT foo PRIMARY KEY (myfield)', 'Create 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 foo', 'Alter drop Primary Key constraint works');
+
+my $table2 = SQL::Translator::Schema::Table->new( name => 'mytable2');
+
+my $field1_2 = SQL::Translator::Schema::Field->new( name => 'myfield_2',
+                                                  table => $table,
+                                                  data_type => 'VARCHAR',
+                                                  size => 10,
+                                                  default_value => undef,
+                                                  is_auto_increment => 0,
+                                                  is_nullable => 1,
+                                                  is_foreign_key => 0,
+                                                  is_unique => 0 );
+
+# check named, and unnamed foreign keys
+for my $name ( 'foo', undef ) {
+    my $fk_constraint = SQL::Translator::Schema::Constraint->new(
+        table => $table,
+        name   => $name,
+        fields => [qw(myfield)],
+        type   => 'FOREIGN_KEY',
+        reference_table => $table2,
+        reference_fields => [qw(myfield_2)],
+    );
+    my $fk_constraint_2 = SQL::Translator::Schema::Constraint->new(
+        table => $table,
+        name   => $name,
+        fields => [qw(myfield)],
+        type   => 'FOREIGN_KEY',
+        reference_table => $table2,
+        reference_fields => [qw(myfield_2)],
+    );
+
+    my  ($fk_constraint_def_ref, $fk_constraint_fk_ref ) = SQL::Translator::Producer::PostgreSQL::create_constraint($fk_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');
+
+        # 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 named Foreign Key Constraint works');
+
+        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 named Foreign Key constraint works');
+    }
+}
+
+
 my $alter_field = SQL::Translator::Producer::PostgreSQL::alter_field($field1,
                                                                 $field2);
-is($alter_field, qq[ALTER TABLE mytable ALTER COLUMN myfield SET NOT NULL
+is($alter_field, qq[ALTER TABLE mytable ALTER COLUMN myfield SET NOT NULL;
 ALTER TABLE mytable ALTER COLUMN myfield TYPE character varying(25)],
  'Alter field works');
 
+my $field1_complex = SQL::Translator::Schema::Field->new(
+    name => 'my_complex_field',
+    table => $table,
+    data_type => 'VARCHAR',
+    size => 10,
+    default_value => undef,
+                                                  is_auto_increment => 0,
+    is_nullable => 1,
+    is_foreign_key => 0,
+    is_unique => 0
+);
+
+my $field2_complex = SQL::Translator::Schema::Field->new(
+    name      => 'my_altered_field',
+    table => $table,
+    data_type => 'VARCHAR',
+    size      => 60,
+    default_value => 'whatever',
+    is_auto_increment => 0,
+    is_nullable => 1,
+    is_foreign_key => 0,
+    is_unique => 0
+);
+
+my $alter_field_complex = SQL::Translator::Producer::PostgreSQL::alter_field($field1_complex, $field2_complex);
+is(
+    $alter_field_complex,
+    q{ALTER TABLE mytable RENAME COLUMN my_complex_field TO my_altered_field;
+ALTER TABLE mytable ALTER COLUMN my_altered_field TYPE character varying(60);
+ALTER TABLE mytable ALTER COLUMN my_altered_field SET DEFAULT 'whatever'},
+    'Complex Alter field works'
+);
+
 $field1->name('field3');
 my $add_field = SQL::Translator::Producer::PostgreSQL::add_field($field1);
 
@@ -98,14 +199,14 @@ my $field3_datetime_with_TZ = SQL::Translator::Schema::Field->new(
     size      => 7,
 );
 
-my $field3_datetime_with_TZ_sql = 
+my $field3_datetime_with_TZ_sql =
     SQL::Translator::Producer::PostgreSQL::create_field(
         $field3_datetime_with_TZ
     );
 
 is(
-    $field3_datetime_with_TZ_sql, 
-    'datetime_with_TZ timestamp(6) with time zone', 
+    $field3_datetime_with_TZ_sql,
+    'datetime_with_TZ timestamp(6) with time zone',
     'Create time field with time zone and size, works'
 );
 
@@ -116,14 +217,14 @@ my $field3_time_without_TZ = SQL::Translator::Schema::Field->new(
     size      => 2,
 );
 
-my $field3_time_without_TZ_sql 
+my $field3_time_without_TZ_sql
     = SQL::Translator::Producer::PostgreSQL::create_field(
         $field3_time_without_TZ
     );
 
 is(
-    $field3_time_without_TZ_sql, 
-    'time_without_TZ time(2) without time zone', 
+    $field3_time_without_TZ_sql,
+    'time_without_TZ time(2) without time zone',
     'Create time field without time zone but with size, works'
 );
 
@@ -449,3 +550,23 @@ is($view2_sql1, $view2_sql_replace, 'correct "CREATE OR REPLACE VIEW" SQL 2');
         is($def->[0], 'CONSTRAINT "constr" UNIQUE ("bar", lower(foo))', 'constraint created w/ quotes');
     }
 }
+
+my $drop_view_opts1 = { add_drop_view => 1, no_comments => 1, postgres_version => 8.001 };
+my $drop_view_8_1_produced = SQL::Translator::Producer::PostgreSQL::create_view($view1, $drop_view_opts1);
+
+my $drop_view_8_1_expected = "DROP VIEW view_foo;
+CREATE VIEW view_foo ( id, name ) AS
+    SELECT id, name FROM thing
+";
+
+is($drop_view_8_1_produced, $drop_view_8_1_expected, "My DROP VIEW statement for 8.1 is correct");
+
+my $drop_view_opts2 = { add_drop_view => 1, no_comments => 1, postgres_version => 9.001 };
+my $drop_view_9_1_produced = SQL::Translator::Producer::PostgreSQL::create_view($view1, $drop_view_opts2);
+
+my $drop_view_9_1_expected = "DROP VIEW IF EXISTS view_foo;
+CREATE VIEW view_foo ( id, name ) AS
+    SELECT id, name FROM thing
+";
+
+is($drop_view_9_1_produced, $drop_view_9_1_expected, "My DROP VIEW statement for 9.1 is correct");