From d23a0eb77ea660217aa4bb3fb409d3e0b7344d6b Mon Sep 17 00:00:00 2001 From: Jakub Vrana Date: Wed, 26 Feb 2025 23:55:17 +0100 Subject: [PATCH] PostgreSQL: Export ENUM types (bug #587) --- adminer/drivers/mysql.inc.php | 9 ++++++++- adminer/drivers/pgsql.inc.php | 5 +++++ adminer/dump.inc.php | 12 +++++++++++- adminer/include/functions.inc.php | 6 +++--- changes.txt | 1 + compile.php | 2 +- 6 files changed, 29 insertions(+), 6 deletions(-) diff --git a/adminer/drivers/mysql.inc.php b/adminer/drivers/mysql.inc.php index bdafbddc..3c3bd46b 100644 --- a/adminer/drivers/mysql.inc.php +++ b/adminer/drivers/mysql.inc.php @@ -1001,11 +1001,18 @@ if (!defined("DRIVER")) { /* Not used is MySQL but checked in compile.php: /** Get user defined types - * @return array + * @return array [$id => $name] function types() { return array(); } + /** Get values of user defined type + * @param int + * @return string + function type_values($id) { + return ""; + } + /** Get existing schemas * @return array function schemas() { diff --git a/adminer/drivers/pgsql.inc.php b/adminer/drivers/pgsql.inc.php index d983ad63..0a54ee21 100644 --- a/adminer/drivers/pgsql.inc.php +++ b/adminer/drivers/pgsql.inc.php @@ -730,6 +730,11 @@ AND typelem = 0" ); } + function type_values($id) { + $enums = get_vals("SELECT enumlabel FROM pg_enum WHERE enumtypid = $id ORDER BY enumsortorder"); + return ($enums ? "'" . implode("', '", array_map('addslashes', $enums)) . "'" : ""); + } + function schemas() { return get_vals("SELECT nspname FROM pg_namespace ORDER BY nspname"); } diff --git a/adminer/dump.inc.php b/adminer/dump.inc.php index 951e2f32..1a370e6b 100644 --- a/adminer/dump.inc.php +++ b/adminer/dump.inc.php @@ -3,7 +3,7 @@ $TABLE = $_GET["dump"]; if ($_POST && !$error) { $cookie = ""; - foreach (array("output", "format", "db_style", "routines", "events", "table_style", "auto_increment", "triggers", "data_style") as $key) { + foreach (array("output", "format", "db_style", "types", "routines", "events", "table_style", "auto_increment", "triggers", "data_style") as $key) { $cookie .= "&$key=" . urlencode($_POST[$key]); } cookie("adminer_export", substr($cookie, 1)); @@ -52,6 +52,15 @@ SET foreign_key_checks = 0; } $out = ""; + if ($_POST["types"]) { + foreach (types() as $id => $type) { + $enums = type_values($id); + if ($enums) { + $out .= ($style != 'DROP+CREATE' ? "DROP TYPE IF EXISTS " . idf_escape($type) . ";;\n" : "") . "CREATE TYPE " . idf_escape($type) . " AS ENUM ($enums);\n\n"; + } + } + } + if ($_POST["routines"]) { foreach (routines() as $row) { $name = $row["ROUTINE_NAME"]; @@ -157,6 +166,7 @@ echo "" . lang('Output') . "" . html_select("output", $adminer->dump echo "" . lang('Format') . "" . html_select("format", $adminer->dumpFormat(), $row["format"], 0) . "\n"; // 0 - radio echo ($jush == "sqlite" ? "" : "" . lang('Database') . "" . html_select('db_style', $db_style, $row["db_style"]) + . (support("type") ? checkbox("types", 1, $row["types"], lang('User types')) : "") . (support("routine") ? checkbox("routines", 1, $row["routines"], lang('Routines')) : "") . (support("event") ? checkbox("events", 1, $row["events"], lang('Events')) : "") ); diff --git a/adminer/include/functions.inc.php b/adminer/include/functions.inc.php index a20ad902..e39322df 100644 --- a/adminer/include/functions.inc.php +++ b/adminer/include/functions.inc.php @@ -909,11 +909,11 @@ function input($field, $value, $function) { $functions = (isset($_GET["select"]) || $reset ? array("orig" => lang('original')) : array()) + $adminer->editFunctions($field); $disabled = stripos($field["default"], "GENERATED ALWAYS AS ") === 0 ? " disabled=''" : ""; $attrs = " name='fields[$name]'$disabled"; - if ($jush == "pgsql" && in_array($field["type"], (array) $structured_types[lang('User types')])) { - $enums = get_vals("SELECT enumlabel FROM pg_enum WHERE enumtypid = " . $types[$field["type"]] . " ORDER BY enumsortorder"); + if (in_array($field["type"], (array) $structured_types[lang('User types')])) { + $enums = type_values($types[$field["type"]]); if ($enums) { $field["type"] = "enum"; - $field["length"] = "'" . implode("','", array_map('addslashes', $enums)) . "'"; + $field["length"] = $enums; } } if ($field["type"] == "enum") { diff --git a/changes.txt b/changes.txt index 33924e2f..7b742a5d 100644 --- a/changes.txt +++ b/changes.txt @@ -1,6 +1,7 @@ Adminer dev: PostgreSQL: Do not alter indexes with expressions PostgreSQL: Fix export of indexes with expressions (bug #768) +PostgreSQL: Export ENUM types (bug #587) SQLite: Support CHECK constraint SQLite: Add command Check tables SQLite: Display all rows of variable values diff --git a/compile.php b/compile.php index bd897ea0..eac03fc8 100755 --- a/compile.php +++ b/compile.php @@ -77,7 +77,7 @@ header("Cache-Control: immutable"); "status" => array("show_status"), "table" => array("is_view"), "trigger" => array("triggers", "trigger", "trigger_options", "trigger_sql"), - "type" => array("types"), + "type" => array("types", "type_values"), "variables" => array("show_variables"), ); foreach ($requires as $support => $fns) {