Skip to content

Commit

Permalink
PostgreSQL: export all FKs after all CREATE TABLE
Browse files Browse the repository at this point in the history
  • Loading branch information
helix84 committed Jul 19, 2019
1 parent 6e4fd88 commit 72b1dcd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
7 changes: 1 addition & 6 deletions adminer/drivers/pgsql.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ function foreign_keys_sql($table) {
ksort($fkeys);

foreach ($fkeys as $fkey_name => $fkey) {
$return .= "ALTER TABLE ONLY " . idf_escape($status['nspname']) . "." . idf_escape($status['Name']) . " ADD CONSTRAINT " . idf_escape($fkey_name) . " $fkey[definition] " . ($fkey['deferrable'] ? 'DEFERRABLE' : 'NOT DEFERRABLE');
$return .= "ALTER TABLE ONLY " . idf_escape($status['nspname']) . "." . idf_escape($status['Name']) . " ADD CONSTRAINT " . idf_escape($fkey_name) . " $fkey[definition] " . ($fkey['deferrable'] ? 'DEFERRABLE' : 'NOT DEFERRABLE') . ";\n";
}

return $return;
Expand Down Expand Up @@ -793,8 +793,6 @@ function create_sql($table, $auto_increment, $style) {
$return_parts[] = "CONSTRAINT " . idf_escape($conname) . " CHECK " . $consrc;
}

//$return .= "\n\n" . var_export($constraints, true) . "\n\n";

$return .= implode(",\n ", $return_parts) . "\n) WITH (oids = " . ($status['Oid'] ? 'true' : 'false') . ");";

// "basic" indexes after table definition
Expand All @@ -819,9 +817,6 @@ function create_sql($table, $auto_increment, $style) {
}
}

$return .= "\n\n-- FKs\n";
$return .= foreign_keys_sql($table);

return rtrim($return, ';');
}

Expand Down
11 changes: 11 additions & 0 deletions adminer/dump.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@
}
}

// add FKs after creating tables (except in mysql which uses SET FOREIGN_KEY_CHECKS=0;)
if (function_exists('foreign_keys_sql') {
foreach (table_status('', true) as $name => $table_status) {
$table = (DB == "" || in_array($name, (array) $_POST["tables"]));
$data = (DB == "" || in_array($name, (array) $_POST["data"]));
if ($table && !is_view($table_status)) {
echo foreign_keys_sql($name)."\n";
}
}
}

foreach ($views as $view) {
$adminer->dumpTable($view, $_POST["table_style"], 1);
}
Expand Down

0 comments on commit 72b1dcd

Please sign in to comment.