handle triggers, and give callback support for handling columns
authorJason Etheridge <jason@esilibrary.com>
Thu, 25 Jul 2013 19:30:01 +0000 (15:30 -0400)
committerJason Etheridge <jason@esilibrary.com>
Thu, 25 Jul 2013 19:30:01 +0000 (15:30 -0400)
Signed-off-by: Jason Etheridge <jason@esilibrary.com>
Open-ILS/src/sql/Pg/make-pgtap-tests.pl

index cd929fb..18ee763 100755 (executable)
@@ -48,6 +48,11 @@ handle_schemas(
                 $table_or_view,
                 undef
             );
+            handle_triggers(
+                $schema,
+                $table_or_view,
+                undef
+            );
         }
 
         handle_tables(
@@ -169,6 +174,26 @@ sub fetch_columns {
     return $columns;
 }
 
+sub fetch_triggers {
+    my ($schema,$table) = (shift,shift);
+    my $sth = $dbh->prepare("
+        SELECT DISTINCT
+            trigger_schema,
+            trigger_name,
+            event_object_schema,
+            event_object_table
+        FROM information_schema.triggers
+            WHERE event_object_catalog = ?
+            AND event_object_schema = ?
+            AND event_object_table = ?
+            AND trigger_schema = event_object_schema -- I don't think pgTAP can handle it otherwise
+    ");
+    $sth->execute(($db_name,$schema,$table));
+    my $triggers = $sth->fetchall_hashref('trigger_name');
+    $sth->finish;
+    return $triggers;
+}
+
 sub handle_schemas {
     my $callback = shift;
 
@@ -233,7 +258,7 @@ sub handle_views {
 }
 
 sub handle_columns {
-    my ($schema,$table) = (shift,shift);
+    my ($schema,$table,$callback) = (shift,shift,shift);
     my $columns = fetch_columns($schema,$table);
     if (!%{ $columns }) {
         return;
@@ -252,6 +277,8 @@ sub handle_columns {
 
     foreach my $column ( sort keys %{ $columns } ) {
 
+        $callback->($schema,$table,$column,undef) if $callback;
+
         my $col_type_original = $columns->{$column}->{data_type};
         my $col_type = $col_type_original;
         my $col_nullable = $columns->{$column}->{is_nullable};
@@ -354,4 +381,29 @@ sub handle_columns {
     }
 }
 
+sub handle_triggers {
+    my ($schema,$table,$callback) = (shift,shift,shift);
+    my $triggers = fetch_triggers($schema,$table);
+    if (!%{ $triggers }) {
+        return;
+    }
+
+    print "\n-- -- -- triggers on " . $dbh->quote("$schema.$table") . "\n";
+    print "SELECT triggers_are(\n";
+    print "\t" . $dbh->quote($schema) . ",\n";
+    print "\t" . $dbh->quote($table) . ",\n";
+    print "\tARRAY[\n\t\t";
+    print join(
+        ",\n\t\t",
+        map { $dbh->quote($_) } sort keys %{ $triggers }
+    );
+    print "\n\t],\t" . $dbh->quote("Found expected triggers for $schema.$table");
+    print "\n);\n";
+
+    foreach my $trigger ( sort keys %{ $triggers } ) {
+        $callback->($schema,$table,$trigger,undef) if $callback;
+    }
+
+}
+