From d44fa8ddb09d9bff2d0933b5737d5073d4d5fa72 Mon Sep 17 00:00:00 2001 From: OpenXE <> Date: Thu, 8 Dec 2022 14:01:32 +0000 Subject: [PATCH] upgrade added export option, added export, check and creation of VIEWS --- upgrade/data/db_schema.json | 339 +------------------- upgrade/upgrade.php | 58 ++-- vendor/mustal/mustal_mysql_upgrade_tool.php | 96 +++++- 3 files changed, 141 insertions(+), 352 deletions(-) diff --git a/upgrade/data/db_schema.json b/upgrade/data/db_schema.json index 413d068a..4d38d331 100644 --- a/upgrade/data/db_schema.json +++ b/upgrade/data/db_schema.json @@ -24308,328 +24308,6 @@ } ] }, - { - "name": "belege", - "type": "VIEW", - "columns": [ - { - "Field": "id", - "Type": "int(11)", - "Collation": null, - "Null": "NO", - "Key": "", - "Default": "0", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "adresse", - "Type": "int(11)", - "Collation": null, - "Null": "NO", - "Key": "", - "Default": "0", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "datum", - "Type": "date", - "Collation": null, - "Null": "NO", - "Key": "", - "Default": "0000-00-00", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "belegnr", - "Type": "varchar(255)", - "Collation": "utf8mb3_general_ci", - "Null": "NO", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "status", - "Type": "varchar(64)", - "Collation": "utf8mb3_general_ci", - "Null": "NO", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "land", - "Type": "varchar(255)", - "Collation": "utf8mb3_general_ci", - "Null": "NO", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "typ", - "Type": "varchar(10)", - "Collation": "utf8mb4_general_ci", - "Null": "NO", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "umsatz_netto", - "Type": "decimal(19,2)", - "Collation": null, - "Null": "NO", - "Key": "", - "Default": "0.00", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "erloes_netto", - "Type": "decimal(19,2)", - "Collation": null, - "Null": "NO", - "Key": "", - "Default": "0.00", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "deckungsbeitrag", - "Type": "decimal(11,2)", - "Collation": null, - "Null": "NO", - "Key": "", - "Default": "0.00", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "provision_summe", - "Type": "decimal(11,2)", - "Collation": null, - "Null": "YES", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "vertriebid", - "Type": "int(11)", - "Collation": null, - "Null": "YES", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "gruppe", - "Type": "int(11)", - "Collation": null, - "Null": "NO", - "Key": "", - "Default": "0", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - } - ], - "keys": [] - }, - { - "name": "belegegesamt", - "type": "VIEW", - "columns": [ - { - "Field": "id", - "Type": "int(11)", - "Collation": null, - "Null": "NO", - "Key": "", - "Default": "0", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "adresse", - "Type": "int(11)", - "Collation": null, - "Null": "NO", - "Key": "", - "Default": "0", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "datum", - "Type": "date", - "Collation": null, - "Null": "NO", - "Key": "", - "Default": "0000-00-00", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "belegnr", - "Type": "varchar(255)", - "Collation": "utf8mb3_general_ci", - "Null": "NO", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "status", - "Type": "varchar(64)", - "Collation": "utf8mb3_general_ci", - "Null": "NO", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "land", - "Type": "varchar(255)", - "Collation": "utf8mb3_general_ci", - "Null": "NO", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "typ", - "Type": "varchar(12)", - "Collation": "utf8mb4_general_ci", - "Null": "NO", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "umsatz_netto", - "Type": "varchar(21)", - "Collation": "utf8mb4_general_ci", - "Null": "NO", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "umsatz_brutto", - "Type": "varchar(21)", - "Collation": "utf8mb4_general_ci", - "Null": "NO", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "erloes_netto", - "Type": "varchar(21)", - "Collation": "utf8mb4_general_ci", - "Null": "NO", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "deckungsbeitrag", - "Type": "varchar(13)", - "Collation": "utf8mb4_general_ci", - "Null": "NO", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "provision_summe", - "Type": "varchar(13)", - "Collation": "utf8mb4_general_ci", - "Null": "YES", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "vertriebid", - "Type": "varchar(11)", - "Collation": "utf8mb4_general_ci", - "Null": "YES", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "gruppe", - "Type": "varchar(11)", - "Collation": "utf8mb4_general_ci", - "Null": "NO", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - }, - { - "Field": "projekt", - "Type": "varchar(222)", - "Collation": "utf8mb3_general_ci", - "Null": "NO", - "Key": "", - "Default": "", - "Extra": "", - "Privileges": "select,insert,update,references", - "Comment": "" - } - ], - "keys": [] - }, { "name": "belegeimport", "type": "BASE TABLE", @@ -114560,5 +114238,22 @@ } ] } + ], + "views": [ + { + "name": "belege", + "type": "VIEW", + "Create": "CREATE ALGORITHM=UNDEFINED DEFINER=`openxe`@`localhost` SQL SECURITY DEFINER VIEW `belege` AS select `rechnung`.`id` AS `id`,`rechnung`.`adresse` AS `adresse`,`rechnung`.`datum` AS `datum`,`rechnung`.`belegnr` AS `belegnr`,`rechnung`.`status` AS `status`,`rechnung`.`land` AS `land`,'rechnung' AS `typ`,`rechnung`.`umsatz_netto` AS `umsatz_netto`,`rechnung`.`erloes_netto` AS `erloes_netto`,`rechnung`.`deckungsbeitrag` AS `deckungsbeitrag`,`rechnung`.`provision_summe` AS `provision_summe`,`rechnung`.`vertriebid` AS `vertriebid`,`rechnung`.`gruppe` AS `gruppe` from `rechnung` where `rechnung`.`status` <> 'angelegt' union all select `gutschrift`.`id` AS `id`,`gutschrift`.`adresse` AS `adresse`,`gutschrift`.`datum` AS `datum`,`gutschrift`.`belegnr` AS `belegnr`,`gutschrift`.`status` AS `status`,`gutschrift`.`land` AS `land`,'gutschrift' AS `typ`,`gutschrift`.`umsatz_netto` * -1 AS `umsatz_netto*-1`,`gutschrift`.`erloes_netto` * -1 AS `erloes_netto*-1`,`gutschrift`.`deckungsbeitrag` * -1 AS `deckungsbeitrag*-1`,`gutschrift`.`provision_summe` * -1 AS `provision_summe*-1`,`gutschrift`.`vertriebid` AS `vertriebid`,`gutschrift`.`gruppe` AS `gruppe` from `gutschrift` where `gutschrift`.`status` <> 'angelegt'" + }, + { + "name": "belegegesamt", + "type": "VIEW", + "Create": "CREATE ALGORITHM=UNDEFINED DEFINER=`openxe`@`localhost` SQL SECURITY DEFINER VIEW `belegegesamt` AS select `rechnung`.`id` AS `id`,`rechnung`.`adresse` AS `adresse`,`rechnung`.`datum` AS `datum`,`rechnung`.`belegnr` AS `belegnr`,`rechnung`.`status` AS `status`,`rechnung`.`land` AS `land`,'rechnung' AS `typ`,`rechnung`.`umsatz_netto` AS `umsatz_netto`,`rechnung`.`soll` AS `umsatz_brutto`,`rechnung`.`erloes_netto` AS `erloes_netto`,`rechnung`.`deckungsbeitrag` AS `deckungsbeitrag`,`rechnung`.`provision_summe` AS `provision_summe`,`rechnung`.`vertriebid` AS `vertriebid`,`rechnung`.`gruppe` AS `gruppe`,`rechnung`.`projekt` AS `projekt` from `rechnung` union all select `gutschrift`.`id` AS `id`,`gutschrift`.`adresse` AS `adresse`,`gutschrift`.`datum` AS `datum`,`gutschrift`.`belegnr` AS `belegnr`,`gutschrift`.`status` AS `status`,`gutschrift`.`land` AS `land`,'gutschrift' AS `typ`,`gutschrift`.`umsatz_netto` * -1 AS `umsatz_netto*-1`,`gutschrift`.`soll` * -1 AS `umsatz_brutto*-1`,`gutschrift`.`erloes_netto` * -1 AS `erloes_netto*-1`,`gutschrift`.`deckungsbeitrag` * -1 AS `deckungsbeitrag*-1`,`gutschrift`.`provision_summe` * -1 AS `provision_summe*-1`,`gutschrift`.`vertriebid` AS `vertriebid`,`gutschrift`.`gruppe` AS `gruppe`,`gutschrift`.`projekt` AS `projekt` from `gutschrift` union all select `auftrag`.`id` AS `id`,`auftrag`.`adresse` AS `adresse`,`auftrag`.`datum` AS `datum`,`auftrag`.`belegnr` AS `belegnr`,`auftrag`.`status` AS `status`,`auftrag`.`land` AS `land`,'auftrag' AS `typ`,`auftrag`.`umsatz_netto` AS `umsatz_netto`,`auftrag`.`gesamtsumme` AS `umsatz_brutto`,`auftrag`.`erloes_netto` AS `erloes_netto`,`auftrag`.`deckungsbeitrag` AS `deckungsbeitrag`,`auftrag`.`provision_summe` AS `provision_summe`,`auftrag`.`vertriebid` AS `vertriebid`,`auftrag`.`gruppe` AS `gruppe`,`auftrag`.`projekt` AS `projekt` from `auftrag` union all select `bestellung`.`id` AS `id`,`bestellung`.`adresse` AS `adresse`,`bestellung`.`datum` AS `datum`,`bestellung`.`belegnr` AS `belegnr`,`bestellung`.`status` AS `status`,`bestellung`.`land` AS `land`,'bestellung' AS `typ`,`bestellung`.`gesamtsumme` AS `umsatz_netto`,`bestellung`.`gesamtsumme` AS `umsatz_brutto`,'0' AS `erloes_netto`,'0' AS `deckungsbeitrag`,'0' AS `provision_summe`,'0' AS `vertriebid`,'0' AS `gruppe`,`bestellung`.`projekt` AS `projekt` from `bestellung` union all select `lieferschein`.`id` AS `id`,`lieferschein`.`adresse` AS `adresse`,`lieferschein`.`datum` AS `datum`,`lieferschein`.`belegnr` AS `belegnr`,`lieferschein`.`status` AS `status`,`lieferschein`.`land` AS `land`,'lieferschein' AS `typ`,'0' AS `umsatz_netto`,'0' AS `umsatz_brutto`,'0' AS `erloes_netto`,'0' AS `deckungsbeitrag`,'0' AS `provision_summe`,'0' AS `vertriebid`,'0' AS `gruppe`,`lieferschein`.`projekt` AS `projekt` from `lieferschein` union all select `angebot`.`id` AS `id`,`angebot`.`adresse` AS `adresse`,`angebot`.`datum` AS `datum`,`angebot`.`belegnr` AS `belegnr`,`angebot`.`status` AS `status`,`angebot`.`land` AS `land`,'angebot' AS `typ`,`angebot`.`umsatz_netto` AS `umsatz_netto`,`angebot`.`gesamtsumme` AS `umsatz_brutto`,'0' AS `erloes_netto`,`angebot`.`deckungsbeitrag` AS `deckungsbeitrag`,'0' AS `provision_summe`,`angebot`.`vertriebid` AS `vertriebid`,'0' AS `gruppe`,`angebot`.`projekt` AS `projekt` from `angebot`" + }, + { + "name": "belegeregs", + "type": "VIEW", + "Create": "CREATE ALGORITHM=UNDEFINED DEFINER=`openxe`@`localhost` SQL SECURITY DEFINER VIEW `belegeregs` AS select `rechnung`.`id` AS `id`,`rechnung`.`adresse` AS `adresse`,`rechnung`.`datum` AS `datum`,`rechnung`.`belegnr` AS `belegnr`,`rechnung`.`status` AS `status`,`rechnung`.`land` AS `land`,'rechnung' AS `typ`,`rechnung`.`umsatz_netto` AS `umsatz_netto`,`rechnung`.`erloes_netto` AS `erloes_netto`,`rechnung`.`deckungsbeitrag` AS `deckungsbeitrag`,`rechnung`.`provision_summe` AS `provision_summe`,`rechnung`.`vertriebid` AS `vertriebid`,`rechnung`.`gruppe` AS `gruppe`,`rechnung`.`projekt` AS `projekt` from `rechnung` union all select `gutschrift`.`id` AS `id`,`gutschrift`.`adresse` AS `adresse`,`gutschrift`.`datum` AS `datum`,`gutschrift`.`belegnr` AS `belegnr`,`gutschrift`.`status` AS `status`,`gutschrift`.`land` AS `land`,'gutschrift' AS `typ`,`gutschrift`.`umsatz_netto` * -1 AS `umsatz_netto*-1`,`gutschrift`.`erloes_netto` * -1 AS `erloes_netto*-1`,`gutschrift`.`deckungsbeitrag` * -1 AS `deckungsbeitrag*-1`,`gutschrift`.`provision_summe` * -1 AS `provision_summe*-1`,`gutschrift`.`vertriebid` AS `vertriebid`,`gutschrift`.`gruppe` AS `gruppe`,`gutschrift`.`projekt` AS `projekt` from `gutschrift`" + } ] } diff --git a/upgrade/upgrade.php b/upgrade/upgrade.php index 2e9ae2a8..2e8a332e 100644 --- a/upgrade/upgrade.php +++ b/upgrade/upgrade.php @@ -68,7 +68,6 @@ if (php_sapi_name() == "cli") { if ($cli) { - $check_git = false; $do_git = false; $check_db = false; @@ -83,6 +82,12 @@ if ($cli) { $verbose = false; } + if (in_array('-e', $argv)) { + $export_db = true; + } else { + $export_db = false; + } + if (in_array('-f', $argv)) { $force = true; } else { @@ -112,14 +117,8 @@ if ($cli) { } } - if (in_array('-utf8fix', $argv)) { - $utf8fix = true; - } else { - $utf8fix = false; - } - if ($check_git || $check_db || $do_git || $do_db) { - upgrade_main($directory,$verbose,$check_git,$do_git,$check_db,$do_db,$force); + upgrade_main($directory,$verbose,$check_git,$do_git,$export_db,$check_db,$do_db,$force); } else { info(); } @@ -131,7 +130,7 @@ if ($cli) { } // -------------------------------- END -function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do_git, bool $check_db, bool $do_db, bool $force) { +function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do_git, bool $export_db, bool $check_db, bool $do_db, bool $force) { class DatabaseConnectionInfo { function __construct($dir) { @@ -279,14 +278,25 @@ function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do } // Dry run } // $check_git - if ($check_db || $do_db) { + if ($check_db || $do_db || $export_db) { echo_out("--------------- Loading from database '$schema@$host'... ---------------\n"); $db_def = mustal_load_tables_from_db($host, $schema, $user, $passwd, $mustal_replacers); if (empty($db_def)) { - echo ("Could not load from $schema@$host\n"); + echo_out("Could not load from $schema@$host\n"); exit; } + + if ($export_db) { + $export_file_name = "exported_db_schema.json"; + if (mustal_save_tables_to_json($db_def, $datafolder, $export_file_name, true) == 0) { + echo_out("Database exported to $datafolder/$export_file_name\n"); + } + else { + echo_out("Could not export database to $datafolder/$export_file_name\n"); + } + } + $compare_differences = array(); echo_out("--------------- Loading from JSON... ---------------\n"); @@ -296,11 +306,10 @@ function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do abort("Could not load from JSON $schema_file_name\n"); return(-1); } - echo_out("--------------- Comparing database '$schema@$host' vs. JSON '".$compare_def['database']."@".$compare_def['host']."' ---------------\n"); - - $compare_differences = mustal_compare_table_array($compare_def,"in JSON",$db_def,"in DB",true,true); - - if ($verbose) { + echo_out("Table count database ".count($db_def['tables'])." vs. JSON ".count($compare_def['tables'])."\n"); + echo_out("--------------- Comparing JSON '".$compare_def['database']."@".$compare_def['host']."' vs. database '$schema@$host' ---------------\n"); + $compare_differences = mustal_compare_table_array($db_def,"in DB",$compare_def,"in JSON",false,true); + if ($verbose) { foreach ($compare_differences as $compare_difference) { $comma = ""; foreach ($compare_difference as $key => $value) { @@ -310,7 +319,20 @@ function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do echo_out("\n"); } } + echo_out((empty($compare_differences)?0:count($compare_differences))." differences.\n"); + echo_out("--------------- Comparing database '$schema@$host' vs. JSON '".$compare_def['database']."@".$compare_def['host']."' ---------------\n"); + $compare_differences = mustal_compare_table_array($compare_def,"in JSON",$db_def,"in DB",true,true); + if ($verbose) { + foreach ($compare_differences as $compare_difference) { + $comma = ""; + foreach ($compare_difference as $key => $value) { + echo_out($comma."$key => [$value]"); + $comma = ", "; + } + echo_out("\n"); + } + } echo_out((empty($compare_differences)?0:count($compare_differences))." differences.\n"); echo_out("--------------- Calculating database upgrade for '$schema@$host'... ---------------\n"); @@ -322,7 +344,7 @@ function upgrade_main(string $directory,bool $verbose, bool $check_git, bool $do abort(count($result)." errors.\n"); if ($verbose) { foreach($result as $error) { - echo_out("Code: ".$error[0]." '".$error[1]."'."); + echo_out("Code: ".$error[0]." '".$error[1]."'\n"); } } return(-1); @@ -409,10 +431,10 @@ function info() { echo_out("Options:\n"); echo_out("\t-s: check/do system upgrades\n"); echo_out("\t-db: check/do database upgrades\n"); + echo_out("\t-e: export database schema\n"); echo_out("\t-do: execute all upgrades\n"); echo_out("\t-v: verbose output\n"); echo_out("\t-f: force override of existing files\n"); - echo_out("\t-utf8fix: apply fix for 'utf8' != 'utf8mb3'\n"); echo_out("\t-clean: (not yet implemented) create the needed SQL to remove items from the database not in the JSON\n"); echo_out("\n"); } diff --git a/vendor/mustal/mustal_mysql_upgrade_tool.php b/vendor/mustal/mustal_mysql_upgrade_tool.php index b426ea91..31edeea9 100644 --- a/vendor/mustal/mustal_mysql_upgrade_tool.php +++ b/vendor/mustal/mustal_mysql_upgrade_tool.php @@ -79,8 +79,7 @@ function mustal_load_tables_from_db(string $host, string $schema, string $user, } // Get db_def and views - - $sql = "SHOW FULL tables"; + $sql = "SHOW FULL tables WHERE Table_type = 'BASE TABLE'"; $query_result = mysqli_query($mysqli, $sql); if (!$query_result) { return(array()); @@ -149,11 +148,34 @@ function mustal_load_tables_from_db(string $host, string $schema, string $user, } unset($table); + $sql = "SHOW FULL tables WHERE Table_type = 'VIEW'"; + $query_result = mysqli_query($mysqli, $sql); + if (!$query_result) { + return(array()); + } + while ($row = mysqli_fetch_assoc($query_result)) { + $view = array(); + $view['name'] = $row['Tables_in_'.$schema]; + $view['type'] = $row['Table_type']; + $views[] = $view; // Add view to list of views + } + + foreach ($views as &$view) { + $sql = "SHOW CREATE VIEW ".$view['name']; + $query_result = mysqli_query($mysqli, $sql); + if (!$query_result) { + return(array()); + } + $viewdef = mysqli_fetch_assoc($query_result); + $view['Create'] = $viewdef['Create View']; + } + $result = array(); $result['host'] = $host; $result['database'] = $schema; $result['user'] = $user; $result['tables'] = $tables; + $result['views'] = $views; return($result); } @@ -217,15 +239,6 @@ function mustal_compare_table_array(array $nominal, string $nominal_name, array $column_collation_aliases = array(); } - - if (count($nominal['tables']) != count($actual['tables'])) { - $compare_difference = array(); - $compare_difference['type'] = "Table count"; - $compare_difference[$nominal_name] = count($nominal['tables']); - $compare_difference[$actual_name] = count($actual['tables']); - $compare_differences[] = $compare_difference; - } - foreach ($nominal['tables'] as $database_table) { $found_table = array(); @@ -353,6 +366,33 @@ function mustal_compare_table_array(array $nominal, string $nominal_name, array } unset($database_table); + foreach ($nominal['views'] as $database_view) { + $found_view = array(); + foreach ($actual['views'] as $compare_view) { + if ($database_view['name'] == $compare_view['name']) { + $found_view = $compare_view; + break; + } + } + unset($compare_view); + + if ($found_view) { + + if ($database_view['Create'] != $found_view['Create']) { + $compare_difference = array(); + $compare_difference['type'] = "View definition"; + $compare_difference[$nominal_name] = $database_view['name']; + $compare_differences[] = $compare_difference; + } + + } else { + $compare_difference = array(); + $compare_difference['type'] = "View existence"; + $compare_difference[$nominal_name] = $database_view['name']; + $compare_differences[] = $compare_difference; + } + } + return($compare_differences); } @@ -481,11 +521,14 @@ function mustal_implode_with_quote(string $quote, string $delimiter, array $arra function mustal_calculate_db_upgrade(array $compare_def, array $db_def, array &$upgrade_sql, array $replacers) : array { $result = array(); - $upgrade_sql = array(); + $upgrade_sql = array(); $compare_differences = mustal_compare_table_array($compare_def,"in JSON",$db_def,"in DB",true,true); foreach ($compare_differences as $compare_difference) { + + $drop_view = false; + switch ($compare_difference['type']) { case 'Table existence': @@ -657,6 +700,35 @@ function mustal_calculate_db_upgrade(array $compare_def, array $db_def, array &$ case 'Table type': $result[] = array(11,"Upgrade type '".$compare_difference['type']."' on table '".$compare_difference['table']."' not supported."); break; + case 'View definition': + $drop_view = true; + // intentionally omitted break; + case 'View existence': + $view_name = $compare_difference['in JSON']; + $view_key = array_search($view_name,array_column($compare_def['views'],'name')); + + if ($view_key !== false) { + $view = $compare_def['views'][$view_key]; + + switch ($view['type']) { + case 'VIEW': + + if ($drop_view === true) { + $sql = "DROP VIEW ".$view['name']; + $upgrade_sql[] = $sql; + } + + // Create view in DB + $upgrade_sql[] = $view['Create']; + break; + default: + $result[] = array(1,"Upgrade type '".$view['type']."' on view '".$view['name']."' not supported."); + break; + } + } else { + $result[] = array(2,"Error view_key while creating upgrade for view existence `$view_name`."); + } + break; default: $result[] = array(12,"Upgrade type '".$compare_difference['type']."' not supported."); break;