From 274049be8481a8178248fc11921f3b659e900a53 Mon Sep 17 00:00:00 2001 From: Thetsmr Date: Mon, 5 Oct 2026 12:11:33 +0200 Subject: [PATCH 1/5] Fix install https://github.com/pluginsGLPI/order/issues/616 --- inc/bill.class.php | 4 +- inc/config.class.php | 29 +++++++- inc/deliverystate.class.php | 4 +- inc/order.class.php | 31 ++++----- inc/order_item.class.php | 114 ++++++++++++++----------------- inc/orderpayment.class.php | 4 +- inc/ordertax.class.php | 4 +- inc/reference.class.php | 9 +-- inc/reference_supplier.class.php | 63 +++++++++-------- inc/surveysupplier.class.php | 55 +++++++-------- 10 files changed, 167 insertions(+), 150 deletions(-) diff --git a/inc/bill.class.php b/inc/bill.class.php index 773f982e62..27e59864cd 100644 --- a/inc/bill.class.php +++ b/inc/bill.class.php @@ -609,7 +609,9 @@ public static function install(Migration $migration) } } - $migration->changeField($table, "value", "value", "decimal(20,6) NOT NULL DEFAULT '0.000000'"); + if (!PluginOrderConfig::fieldHasType($table, "value", 'decimal(20,6)')) { + $migration->changeField($table, "value", "value", "decimal(20,6) NOT NULL DEFAULT '0.000000'"); + } $migration->migrationOneTable($table); } diff --git a/inc/config.class.php b/inc/config.class.php index 8e170183e9..668613d299 100644 --- a/inc/config.class.php +++ b/inc/config.class.php @@ -28,7 +28,7 @@ * ------------------------------------------------------------------------- */ - +use function Safe\preg_replace; class PluginOrderConfig extends CommonDBTM { @@ -669,6 +669,33 @@ public function canRenameDocuments() //----------------- Install & uninstall -------------------// + + /** + * Tell whether a column already has the expected SQL type, so that an upgrade does + * not replay a type change: every CHANGE rebuilds the whole table. + * + * @param string $table Table name + * @param string $field Column name + * @param string $type Expected type, as reported by SHOW COLUMNS (ie. "decimal(20,6)", "int unsigned") + * + * @return bool true when the column is missing or already has this type + */ + public static function fieldHasType(string $table, string $field, string $type): bool + { + /** @var DBmysql $DB */ + global $DB; + + $column = $DB->getField($table, $field, false); + if ($column === null) { + return true; + } + + // MySQL 5.7 and MariaDB report an integer display width ("int(10) unsigned"), MySQL 8 does not + $current = preg_replace('/^(tinyint|smallint|mediumint|int|bigint)\(\d+\)/', '$1', strtolower((string) $column['Type'])); + + return $current === strtolower($type); + } + public static function install(Migration $migration) { /** @var DBmysql $DB */ diff --git a/inc/deliverystate.class.php b/inc/deliverystate.class.php index 86203effa6..6fbcf57faf 100644 --- a/inc/deliverystate.class.php +++ b/inc/deliverystate.class.php @@ -69,7 +69,9 @@ public static function install(Migration $migration) //Upgrade 1.2.0 $migration->renameTable("glpi_dropdown_plugin_order_deliverystate", $table); $migration->changeField($table, "ID", "id", sprintf('int %s NOT NULL auto_increment', $default_key_sign)); - $migration->changeField($table, "name", "name", "varchar(255) default NULL"); + if (!PluginOrderConfig::fieldHasType($table, "name", 'varchar(255)')) { + $migration->changeField($table, "name", "name", "varchar(255) default NULL"); + } $migration->changeField($table, "comments", "comment", "text"); $migration->migrationOneTable($table); } diff --git a/inc/order.class.php b/inc/order.class.php index b655b1d0be..b056811d49 100644 --- a/inc/order.class.php +++ b/inc/order.class.php @@ -2735,12 +2735,9 @@ public static function install(Migration $migration) "is_recursive", "tinyint NOT NULL default 0", ); - $migration->changeField( - $table, - "name", - "name", - "varchar(255) default NULL", - ); + if (!PluginOrderConfig::fieldHasType($table, "name", 'varchar(255)')) { + $migration->changeField($table, "name", "name", "varchar(255) default NULL"); + } $migration->changeField( $table, "budget", @@ -2795,12 +2792,9 @@ public static function install(Migration $migration) "states_id", sprintf("int %s NOT NULL default '0'", $default_key_sign), ); - $migration->changeField( - $table, - "comment", - "comment", - "text", - ); + if (!PluginOrderConfig::fieldHasType($table, "comment", 'text')) { + $migration->changeField($table, "comment", "comment", "text"); + } $migration->changeField( $table, "notes", @@ -2863,12 +2857,9 @@ public static function install(Migration $migration) "end_date", "date default NULL", ); - $migration->changeField( - "glpi_plugin_order_budgets", - "value", - "value", - "float NOT NULL DEFAULT '0'", - ); + if (!PluginOrderConfig::fieldHasType("glpi_plugin_order_budgets", "value", 'float')) { + $migration->changeField("glpi_plugin_order_budgets", "value", "value", "float NOT NULL DEFAULT '0'"); + } $migration->addKey("glpi_plugin_order_budgets", "entities_id"); $migration->addKey("glpi_plugin_order_budgets", "is_deleted"); $migration->migrationOneTable("glpi_plugin_order_budgets"); @@ -3023,7 +3014,9 @@ public static function install(Migration $migration) $migration->addField($table, "global_discount", "FLOAT NOT NULL default '0'"); //2.7.3 - $migration->changeField($table, "plugin_order_billstates_id", "plugin_order_billstates_id", sprintf('int %s NOT NULL DEFAULT 0', $default_key_sign)); + if (!PluginOrderConfig::fieldHasType($table, "plugin_order_billstates_id", trim('int ' . $default_key_sign))) { + $migration->changeField($table, "plugin_order_billstates_id", "plugin_order_billstates_id", sprintf('int %s NOT NULL DEFAULT 0', $default_key_sign)); + } // Add ecotax fields if they don't exist if (!$DB->fieldExists($table, 'ecotax_price')) { diff --git a/inc/order_item.class.php b/inc/order_item.class.php index 7d5bd9bc3e..a1b82a7be4 100644 --- a/inc/order_item.class.php +++ b/inc/order_item.class.php @@ -2107,26 +2107,29 @@ public static function install(Migration $migration) $migration->migrationOneTable($table); //1.4.0 - $migration->addField( + $vat_added = $migration->addField( $table, "plugin_order_ordertaxes_id", sprintf("INT %s NOT NULL default '0' COMMENT 'RELATION to glpi_plugin_order_ordertaxes (id)'", $default_key_sign), ); $migration->migrationOneTable($table); - /* Migrate VAT */ - foreach ($DB->request(['FROM' => 'glpi_plugin_order_orders']) as $data) { - $migration->addPostQuery( - $DB->buildUpdate( - 'glpi_plugin_order_orders_items', - ['plugin_order_ordertaxes_id' => $data['plugin_order_ordertaxes_id']], - ['plugin_order_orders_id' => $data['id']], - ), - ); + /* Migrate VAT, only when the column has just been created: one query per order + on every upgrade makes the migration time out on large databases */ + if ($vat_added) { + foreach ($DB->request(['FROM' => 'glpi_plugin_order_orders']) as $data) { + $migration->addPostQuery( + $DB->buildUpdate( + 'glpi_plugin_order_orders_items', + ['plugin_order_ordertaxes_id' => $data['plugin_order_ordertaxes_id']], + ['plugin_order_orders_id' => $data['id']], + ), + ); + } } //1.5.0 - $migration->addField($table, "entities_id", sprintf("INT %s NOT NULL DEFAULT '0'", $default_key_sign)); + $entities_added = $migration->addField($table, "entities_id", sprintf("INT %s NOT NULL DEFAULT '0'", $default_key_sign)); $migration->addField($table, "is_recursive", "TINYINT NOT NULL DEFAULT '0'"); $migration->addField($table, "plugin_order_bills_id", sprintf("INT %s NOT NULL DEFAULT '0'", $default_key_sign)); $migration->addField($table, "plugin_order_billstates_id", sprintf("INT %s NOT NULL DEFAULT '0'", $default_key_sign)); @@ -2137,62 +2140,51 @@ public static function install(Migration $migration) $migration->migrationOneTable($table); //Change format for prices : from float to decimal - $migration->changeField( - $table, - "price_taxfree", - "price_taxfree", - "decimal(20,6) NOT NULL DEFAULT '0.000000'", - ); - $migration->changeField( - $table, - "price_discounted", - "price_discounted", - "decimal(20,6) NOT NULL DEFAULT '0.000000'", - ); - $migration->changeField( - $table, - "price_ati", - "price_ati", - "decimal(20,6) NOT NULL DEFAULT '0.000000'", - ); - $migration->changeField( - $table, - "discount", - "discount", - "decimal(20,6) NOT NULL DEFAULT '0.000000'", - ); + foreach (['price_taxfree', 'price_discounted', 'price_ati', 'discount'] as $field) { + if (!PluginOrderConfig::fieldHasType($table, $field, 'decimal(20,6)')) { + $migration->changeField( + $table, + $field, + $field, + "decimal(20,6) NOT NULL DEFAULT '0.000000'", + ); + } + } //Drop unused fields from previous migration $migration->dropField($table, "price_taxfree2"); $migration->dropField($table, "price_discounted2"); $migration->migrationOneTable($table); - //Forward entities_id and is_recursive into table glpi_plugin_order_orders_items - $query = [ - 'SELECT' => [ - 'go.entities_id as entities_id', - 'go.is_recursive as is_recursive', - 'goi.id as items_id', - ], - 'FROM' => [ - 'glpi_plugin_order_orders as go', - $table . ' as goi', - ], - 'WHERE' => [ - 'goi.plugin_order_orders_id' => new QueryExpression(DBmysql::quoteName('go.id')), - ], - ]; - foreach ($DB->request($query) as $data) { - $migration->addPostQuery( - $DB->buildUpdate( - $table, - [ - 'entities_id' => $data['entities_id'], - 'is_recursive' => $data['is_recursive'], - ], - ['id' => $data['items_id']], - ), - ); + //Forward entities_id and is_recursive into table glpi_plugin_order_orders_items, + //only when the column has just been created (one query per order line otherwise) + if ($entities_added) { + $query = [ + 'SELECT' => [ + 'go.entities_id as entities_id', + 'go.is_recursive as is_recursive', + 'goi.id as items_id', + ], + 'FROM' => [ + 'glpi_plugin_order_orders as go', + $table . ' as goi', + ], + 'WHERE' => [ + 'goi.plugin_order_orders_id' => new QueryExpression(DBmysql::quoteName('go.id')), + ], + ]; + foreach ($DB->request($query) as $data) { + $migration->addPostQuery( + $DB->buildUpdate( + $table, + [ + 'entities_id' => $data['entities_id'], + 'is_recursive' => $data['is_recursive'], + ], + ['id' => $data['items_id']], + ), + ); + } } if (!$DB->fieldExists($table, 'plugin_order_analyticnatures_id')) { diff --git a/inc/orderpayment.class.php b/inc/orderpayment.class.php index de87bbdaa9..5d0c0e370d 100644 --- a/inc/orderpayment.class.php +++ b/inc/orderpayment.class.php @@ -68,7 +68,9 @@ public static function install(Migration $migration) //1.2.0 $migration->renameTable("glpi_dropdown_plugin_order_payment", $table); $migration->changeField($table, "ID", "id", sprintf('int %s NOT NULL auto_increment', $default_key_sign)); - $migration->changeField($table, "name", "name", "varchar(255) default NULL"); + if (!PluginOrderConfig::fieldHasType($table, "name", 'varchar(255)')) { + $migration->changeField($table, "name", "name", "varchar(255) default NULL"); + } $migration->changeField($table, "comments", "comment", "text"); $migration->migrationOneTable($table); } diff --git a/inc/ordertax.class.php b/inc/ordertax.class.php index ca8a167e91..df89cec2a4 100644 --- a/inc/ordertax.class.php +++ b/inc/ordertax.class.php @@ -78,7 +78,9 @@ public static function install(Migration $migration) //1.2.0 $migration->renameTable("glpi_dropdown_plugin_order_taxes", $table); $migration->changeField($table, "ID", "id", sprintf('int %s NOT NULL auto_increment', $default_key_sign)); - $migration->changeField($table, "name", "name", "varchar(255) default NULL"); + if (!PluginOrderConfig::fieldHasType($table, "name", 'varchar(255)')) { + $migration->changeField($table, "name", "name", "varchar(255) default NULL"); + } $migration->changeField($table, "comments", "comment", "text"); $migration->migrationOneTable($table); diff --git a/inc/reference.class.php b/inc/reference.class.php index 029953e544..362b7a5fd8 100644 --- a/inc/reference.class.php +++ b/inc/reference.class.php @@ -1146,12 +1146,9 @@ public static function install(Migration $migration) "is_recursive", "tinyint NOT NULL default '0'", ); - $migration->changeField( - $table, - "name", - "name", - "varchar(255) default NULL", - ); + if (!PluginOrderConfig::fieldHasType($table, "name", 'varchar(255)')) { + $migration->changeField($table, "name", "name", "varchar(255) default NULL"); + } $migration->changeField( $table, "FK_glpi_enterprise", diff --git a/inc/reference_supplier.class.php b/inc/reference_supplier.class.php index 7cecf6dced..001eadf762 100644 --- a/inc/reference_supplier.class.php +++ b/inc/reference_supplier.class.php @@ -436,6 +436,9 @@ public static function install(Migration $migration) $migration->migrationOneTable("glpi_plugin_order_references_manufacturers"); } + //Legacy data forwarding below only applies to a pre-1.2.0 table + $is_legacy = $DB->tableExists("glpi_plugin_order_references_manufacturers"); + //1.2.0 $migration->renameTable("glpi_plugin_order_references_manufacturers", $table); $migration->addField($table, "is_recursive", sprintf("int %s NOT NULL default '0'", $default_key_sign)); @@ -465,40 +468,36 @@ public static function install(Migration $migration) "suppliers_id", sprintf("int %s NOT NULL default '0' COMMENT 'RELATION to glpi_suppliers (id)'", $default_key_sign), ); - $migration->changeField( - $table, - "reference_code", - "reference_code", - "varchar(255) default NULL", - ); - $migration->changeField( - $table, - "price_taxfree", - "price_taxfree", - "decimal(20,6) NOT NULL DEFAULT '0.000000'", - ); + if (!PluginOrderConfig::fieldHasType($table, "reference_code", 'varchar(255)')) { + $migration->changeField($table, "reference_code", "reference_code", "varchar(255) default NULL"); + } + if (!PluginOrderConfig::fieldHasType($table, "price_taxfree", 'decimal(20,6)')) { + $migration->changeField($table, "price_taxfree", "price_taxfree", "decimal(20,6) NOT NULL DEFAULT '0.000000'"); + } $migration->migrationOneTable($table); - //1.5.0 - $query = [ - 'SELECT' => [ - 'entities_id', - 'is_recursive', - 'id', - ], - 'FROM' => 'glpi_plugin_order_references', - ]; - foreach ($DB->request($query) as $data) { - $migration->addPostQuery( - $DB->buildUpdate( - 'glpi_plugin_order_references_suppliers', - [ - 'entities_id' => $data['entities_id'], - 'is_recursive' => $data['is_recursive'], - ], - ['plugin_order_references_id' => $data['id']], - ), - ); + //1.5.0, replayed on every upgrade otherwise (one query per reference) + if ($is_legacy) { + $query = [ + 'SELECT' => [ + 'entities_id', + 'is_recursive', + 'id', + ], + 'FROM' => 'glpi_plugin_order_references', + ]; + foreach ($DB->request($query) as $data) { + $migration->addPostQuery( + $DB->buildUpdate( + 'glpi_plugin_order_references_suppliers', + [ + 'entities_id' => $data['entities_id'], + 'is_recursive' => $data['is_recursive'], + ], + ['plugin_order_references_id' => $data['id']], + ), + ); + } } } } diff --git a/inc/surveysupplier.class.php b/inc/surveysupplier.class.php index c92d9d366a..3dc2a79a7e 100644 --- a/inc/surveysupplier.class.php +++ b/inc/surveysupplier.class.php @@ -556,38 +556,39 @@ public static function install(Migration $migration) "suppliers_id", sprintf("int %s NOT NULL default '0' COMMENT 'RELATION to glpi_suppliers (id)'", $default_key_sign), ); - $migration->changeField( - $table, - "comment", - "comment", - "text", - ); - $migration->addField($table, "entities_id", sprintf("int %s NOT NULL default '0'", $default_key_sign)); + if (!PluginOrderConfig::fieldHasType($table, "comment", 'text')) { + $migration->changeField($table, "comment", "comment", "text"); + } + $entities_added = $migration->addField($table, "entities_id", sprintf("int %s NOT NULL default '0'", $default_key_sign)); $migration->addField($table, "is_recursive", "tinyint NOT NULL default '0'"); $migration->addKey($table, "plugin_order_orders_id"); $migration->addKey($table, "suppliers_id"); $migration->migrationOneTable($table); - $query = [ - 'SELECT' => [ - 'suppliers_id', - 'entities_id', - 'is_recursive', - 'id', - ], - 'FROM' => 'glpi_plugin_order_orders', - ]; - foreach ($DB->request($query) as $data) { - $migration->addPostQuery( - $DB->buildUpdate( - 'glpi_plugin_order_surveysuppliers', - [ - 'entities_id' => $data['entities_id'], - 'is_recursive' => $data['is_recursive'], - ], - ['plugin_order_orders_id' => $data['id']], - ), - ); + //Forward the order entity only when the column has just been created + //(one query per order on every upgrade otherwise) + if ($entities_added) { + $query = [ + 'SELECT' => [ + 'suppliers_id', + 'entities_id', + 'is_recursive', + 'id', + ], + 'FROM' => 'glpi_plugin_order_orders', + ]; + foreach ($DB->request($query) as $data) { + $migration->addPostQuery( + $DB->buildUpdate( + 'glpi_plugin_order_surveysuppliers', + [ + 'entities_id' => $data['entities_id'], + 'is_recursive' => $data['is_recursive'], + ], + ['plugin_order_orders_id' => $data['id']], + ), + ); + } } } } From 7538e65c19440df4cad030fdd4bf3946101e2e0d Mon Sep 17 00:00:00 2001 From: Thetsmr Date: Mon, 5 Oct 2026 12:19:44 +0200 Subject: [PATCH 2/5] Fix CI --- inc/bill.class.php | 1 + inc/deliverystate.class.php | 1 + inc/order.class.php | 3 +++ inc/orderpayment.class.php | 1 + inc/ordertax.class.php | 1 + inc/reference.class.php | 1 + inc/reference_supplier.class.php | 2 ++ inc/surveysupplier.class.php | 1 + 8 files changed, 11 insertions(+) diff --git a/inc/bill.class.php b/inc/bill.class.php index 27e59864cd..ad9fbc5d84 100644 --- a/inc/bill.class.php +++ b/inc/bill.class.php @@ -612,6 +612,7 @@ public static function install(Migration $migration) if (!PluginOrderConfig::fieldHasType($table, "value", 'decimal(20,6)')) { $migration->changeField($table, "value", "value", "decimal(20,6) NOT NULL DEFAULT '0.000000'"); } + $migration->migrationOneTable($table); } diff --git a/inc/deliverystate.class.php b/inc/deliverystate.class.php index 6fbcf57faf..ed62703047 100644 --- a/inc/deliverystate.class.php +++ b/inc/deliverystate.class.php @@ -72,6 +72,7 @@ public static function install(Migration $migration) if (!PluginOrderConfig::fieldHasType($table, "name", 'varchar(255)')) { $migration->changeField($table, "name", "name", "varchar(255) default NULL"); } + $migration->changeField($table, "comments", "comment", "text"); $migration->migrationOneTable($table); } diff --git a/inc/order.class.php b/inc/order.class.php index b056811d49..e2d35b54fa 100644 --- a/inc/order.class.php +++ b/inc/order.class.php @@ -2738,6 +2738,7 @@ public static function install(Migration $migration) if (!PluginOrderConfig::fieldHasType($table, "name", 'varchar(255)')) { $migration->changeField($table, "name", "name", "varchar(255) default NULL"); } + $migration->changeField( $table, "budget", @@ -2795,6 +2796,7 @@ public static function install(Migration $migration) if (!PluginOrderConfig::fieldHasType($table, "comment", 'text')) { $migration->changeField($table, "comment", "comment", "text"); } + $migration->changeField( $table, "notes", @@ -2860,6 +2862,7 @@ public static function install(Migration $migration) if (!PluginOrderConfig::fieldHasType("glpi_plugin_order_budgets", "value", 'float')) { $migration->changeField("glpi_plugin_order_budgets", "value", "value", "float NOT NULL DEFAULT '0'"); } + $migration->addKey("glpi_plugin_order_budgets", "entities_id"); $migration->addKey("glpi_plugin_order_budgets", "is_deleted"); $migration->migrationOneTable("glpi_plugin_order_budgets"); diff --git a/inc/orderpayment.class.php b/inc/orderpayment.class.php index 5d0c0e370d..dfc23f542f 100644 --- a/inc/orderpayment.class.php +++ b/inc/orderpayment.class.php @@ -71,6 +71,7 @@ public static function install(Migration $migration) if (!PluginOrderConfig::fieldHasType($table, "name", 'varchar(255)')) { $migration->changeField($table, "name", "name", "varchar(255) default NULL"); } + $migration->changeField($table, "comments", "comment", "text"); $migration->migrationOneTable($table); } diff --git a/inc/ordertax.class.php b/inc/ordertax.class.php index df89cec2a4..90f24dc3c4 100644 --- a/inc/ordertax.class.php +++ b/inc/ordertax.class.php @@ -81,6 +81,7 @@ public static function install(Migration $migration) if (!PluginOrderConfig::fieldHasType($table, "name", 'varchar(255)')) { $migration->changeField($table, "name", "name", "varchar(255) default NULL"); } + $migration->changeField($table, "comments", "comment", "text"); $migration->migrationOneTable($table); diff --git a/inc/reference.class.php b/inc/reference.class.php index 362b7a5fd8..c81b1fa68a 100644 --- a/inc/reference.class.php +++ b/inc/reference.class.php @@ -1149,6 +1149,7 @@ public static function install(Migration $migration) if (!PluginOrderConfig::fieldHasType($table, "name", 'varchar(255)')) { $migration->changeField($table, "name", "name", "varchar(255) default NULL"); } + $migration->changeField( $table, "FK_glpi_enterprise", diff --git a/inc/reference_supplier.class.php b/inc/reference_supplier.class.php index 001eadf762..29d0b0da9f 100644 --- a/inc/reference_supplier.class.php +++ b/inc/reference_supplier.class.php @@ -471,9 +471,11 @@ public static function install(Migration $migration) if (!PluginOrderConfig::fieldHasType($table, "reference_code", 'varchar(255)')) { $migration->changeField($table, "reference_code", "reference_code", "varchar(255) default NULL"); } + if (!PluginOrderConfig::fieldHasType($table, "price_taxfree", 'decimal(20,6)')) { $migration->changeField($table, "price_taxfree", "price_taxfree", "decimal(20,6) NOT NULL DEFAULT '0.000000'"); } + $migration->migrationOneTable($table); //1.5.0, replayed on every upgrade otherwise (one query per reference) diff --git a/inc/surveysupplier.class.php b/inc/surveysupplier.class.php index 3dc2a79a7e..ea9d4ac42a 100644 --- a/inc/surveysupplier.class.php +++ b/inc/surveysupplier.class.php @@ -559,6 +559,7 @@ public static function install(Migration $migration) if (!PluginOrderConfig::fieldHasType($table, "comment", 'text')) { $migration->changeField($table, "comment", "comment", "text"); } + $entities_added = $migration->addField($table, "entities_id", sprintf("int %s NOT NULL default '0'", $default_key_sign)); $migration->addField($table, "is_recursive", "tinyint NOT NULL default '0'"); $migration->addKey($table, "plugin_order_orders_id"); From 65116237f4ed830678d468e4d7f54168241189a9 Mon Sep 17 00:00:00 2001 From: Thetsmr Date: Mon, 5 Oct 2026 12:28:59 +0200 Subject: [PATCH 3/5] Updat changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ab6b25b19..6cc293725e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Automatic generation on delivery no longer creates a consumable model per received consumable - CI: fix Psalm cache directory, drop the stray `glpi-project/tools` composer dependency, declare a unique composer autoloader suffix +- Plugin upgrade no longer rebuilds tables whose columns already have the expected type, nor replays legacy data migrations, which made it time out on large databases (#616) ## [2.12.11] - 2026-09-18 From 2e126992190d34b12c8de95979c23e8042146103 Mon Sep 17 00:00:00 2001 From: Thetsmr Date: Tue, 10 Nov 2026 11:37:58 +0100 Subject: [PATCH 4/5] Add install tests --- hook.php | 32 ++++++--- inc/order_item.class.php | 9 +++ inc/orderstate.class.php | 6 +- inc/reference.class.php | 27 ++++--- inc/reference_supplier.class.php | 8 ++- tests/Units/InstallTest.php | 116 +++++++++++++++++++++++++++++++ 6 files changed, 167 insertions(+), 31 deletions(-) create mode 100644 tests/Units/InstallTest.php diff --git a/hook.php b/hook.php index c8198e00df..c1d42f6524 100644 --- a/hook.php +++ b/hook.php @@ -38,17 +38,15 @@ * * @return boolean */ -function plugin_order_install() +/** + * Classes whose install() method plugin_order_install() runs, in this order: some upgrade steps + * rely on the ones run before them (see PluginOrderOrder_Item::$upgraded_from_before_1_5_0). + * + * @return list + */ +function plugin_order_get_install_classes(): array { - foreach (glob(PLUGIN_ORDER_DIR . '/inc/*.php') as $file) { - //Do not load datainjection files (not needed and avoid missing class error message) - if (preg_match('/injection.class.php/', $file) === 0) { - include_once($file); - } - } - - $migration = new Migration(PLUGIN_ORDER_VERSION); - $classes = ['PluginOrderConfig', 'PluginOrderBillState', 'PluginOrderBillType', + return ['PluginOrderConfig', 'PluginOrderBillState', 'PluginOrderBillType', 'PluginOrderOrderState', 'PluginOrderOrder','PluginOrderOrder_Item', 'PluginOrderReference', 'PluginOrderDeliverystate', 'PluginOrderNotificationTargetOrder', @@ -59,7 +57,19 @@ function plugin_order_install() 'PluginOrderReferenceFree', 'PluginOrderAccountsection', 'PluginOrderAnalyticnature', ]; - foreach ($classes as $class) { +} + +function plugin_order_install() +{ + foreach (glob(PLUGIN_ORDER_DIR . '/inc/*.php') as $file) { + //Do not load datainjection files (not needed and avoid missing class error message) + if (preg_match('/injection.class.php/', $file) === 0) { + include_once($file); + } + } + + $migration = new Migration(PLUGIN_ORDER_VERSION); + foreach (plugin_order_get_install_classes() as $class) { if ($plug = isPluginItemType($class)) { $plugname = strtolower((string) $plug['plugin']); $dir = Plugin::getPhpDir($plugname) . "/inc/"; diff --git a/inc/order_item.class.php b/inc/order_item.class.php index cdd1668ce9..14cecc895d 100644 --- a/inc/order_item.class.php +++ b/inc/order_item.class.php @@ -55,6 +55,13 @@ class PluginOrderOrder_Item extends CommonDBRelation // phpcs:ignore public static bool $check_entity_coherency = false; + /** + * Set by install() when the upgraded database predates 1.5.0 (its order lines had no + * entities_id yet). The 1.5.0 entity forwarding of the tables installed afterwards + * (PluginOrderReference_Supplier) relies on it: their own schema no longer tells. + */ + public static bool $upgraded_from_before_1_5_0 = false; + //TODO better right and entity menber (ex Computer_Item) @@ -1999,6 +2006,7 @@ public static function install(Migration $migration) $default_key_sign = DBConnection::getDefaultPrimaryKeySignOption(); $table = self::getTable(); + self::$upgraded_from_before_1_5_0 = false; if (!$DB->tableExists($table) && !$DB->tableExists("glpi_plugin_order_detail")) { $migration->displayMessage('Installing ' . $table); @@ -2130,6 +2138,7 @@ public static function install(Migration $migration) //1.5.0 $entities_added = $migration->addField($table, "entities_id", sprintf("INT %s NOT NULL DEFAULT '0'", $default_key_sign)); + self::$upgraded_from_before_1_5_0 = $entities_added; $migration->addField($table, "is_recursive", "TINYINT NOT NULL DEFAULT '0'"); $migration->addField($table, "plugin_order_bills_id", sprintf("INT %s NOT NULL DEFAULT '0'", $default_key_sign)); $migration->addField($table, "plugin_order_billstates_id", sprintf("INT %s NOT NULL DEFAULT '0'", $default_key_sign)); diff --git a/inc/orderstate.class.php b/inc/orderstate.class.php index 4b4b3d1a20..534a979a3d 100644 --- a/inc/orderstate.class.php +++ b/inc/orderstate.class.php @@ -83,8 +83,10 @@ public static function install(Migration $migration) $table = self::getTable(); - //1.2.0 - $DB->doQuery("DROP TABLE IF EXISTS `glpi_dropdown_plugin_order_status`;"); + //1.2.0, only when the legacy table is still there: no DDL on an up-to-date schema + if ($DB->tableExists("glpi_dropdown_plugin_order_status")) { + $DB->dropTable("glpi_dropdown_plugin_order_status"); + } if (!$DB->tableExists($table)) { $migration->displayMessage('Installing ' . $table); diff --git a/inc/reference.class.php b/inc/reference.class.php index 2365991d49..803110641c 100644 --- a/inc/reference.class.php +++ b/inc/reference.class.php @@ -1205,21 +1205,18 @@ public static function install(Migration $migration) $migration->addKey($table, "date_mod"); $migration->migrationOneTable($table); - //1.3.0 - $migration->addPostQuery( - $DB->buildUpdate( - 'glpi_plugin_order_references', - ['itemtype' => 'ConsumableItem'], - ['itemtype' => 'Consumable'], - ), - ); - $migration->addPostQuery( - $DB->buildUpdate( - 'glpi_plugin_order_references', - ['itemtype' => 'CartridgeItem'], - ['itemtype' => 'Cartridge'], - ), - ); + //1.3.0, only while references still use the old itemtypes + foreach (['Consumable' => 'ConsumableItem', 'Cartridge' => 'CartridgeItem'] as $old => $new) { + if (countElementsInTable('glpi_plugin_order_references', ['itemtype' => $old]) > 0) { + $migration->addPostQuery( + $DB->buildUpdate( + 'glpi_plugin_order_references', + ['itemtype' => $new], + ['itemtype' => $old], + ), + ); + } + } //1.7.0 $migration->addField($table, "date_mod", "timestamp NULL DEFAULT NULL"); diff --git a/inc/reference_supplier.class.php b/inc/reference_supplier.class.php index 65d2b92cd5..70cdc7c0a9 100644 --- a/inc/reference_supplier.class.php +++ b/inc/reference_supplier.class.php @@ -434,8 +434,10 @@ public static function install(Migration $migration) $migration->migrationOneTable("glpi_plugin_order_references_manufacturers"); } - //Legacy data forwarding below only applies to a pre-1.2.0 table - $is_legacy = $DB->tableExists("glpi_plugin_order_references_manufacturers"); + //The 1.5.0 entity forwarding below only applies to a database older than 1.5.0: a + //pre-1.2.0 table, or a 1.2.0 to 1.4.x one (told by the order lines, installed before) + $before_1_5_0 = $DB->tableExists("glpi_plugin_order_references_manufacturers") + || PluginOrderOrder_Item::$upgraded_from_before_1_5_0; //1.2.0 $migration->renameTable("glpi_plugin_order_references_manufacturers", $table); @@ -477,7 +479,7 @@ public static function install(Migration $migration) $migration->migrationOneTable($table); //1.5.0, replayed on every upgrade otherwise (one query per reference) - if ($is_legacy) { + if ($before_1_5_0) { $query = [ 'SELECT' => [ 'entities_id', diff --git a/tests/Units/InstallTest.php b/tests/Units/InstallTest.php new file mode 100644 index 0000000000..015a63c4af --- /dev/null +++ b/tests/Units/InstallTest.php @@ -0,0 +1,116 @@ +. + * ------------------------------------------------------------------------- + * @copyright Copyright (C) 2009-2023 by Order plugin team. + * @license GPLv3 https://www.gnu.org/licenses/gpl-3.0.html + * @link https://github.com/pluginsGLPI/order + * ------------------------------------------------------------------------- + */ + +namespace GlpiPlugin\Order\Tests\Units; + +use GlpiPlugin\Order\Tests\OrderTestCase; +use Migration; +use PHPUnit\Framework\Attributes\DataProvider; +use Plugin; +use PluginOrderConfig; +use PluginOrderOrder_Item; + +/** + * Upgrade of an up-to-date schema (#616): every CHANGE rebuilds a whole table and every post + * query of the data migrations runs once per row, so replaying them on each upgrade made the + * upgrade of large databases time out. + */ +final class InstallTest extends OrderTestCase +{ + public function testUpgradeOfAnUpToDateSchemaQueuesNothing(): void + { + // As from the plugins page: PluginOrderProfile::install() reads the active profile + $this->login(); + require_once Plugin::getPhpDir('order') . '/hook.php'; + + // Records what install() would queue, without touching the schema of the test database + $migration = new class (PLUGIN_ORDER_VERSION) extends Migration { + /** @var list columns whose type (or name) would be changed */ + public array $changed_fields = []; + + /** @var list */ + public array $post_queries = []; + + public function changeField($table, $oldfield, $newfield, $type, $options = []) + { + /** @var \DBmysql $DB */ + global $DB; + + // A legacy column renamed long ago is missing: Migration skips it. An existing + // column would be altered. + if ($DB->fieldExists($table, $oldfield, false)) { + $this->changed_fields[] = sprintf('%s.%s => %s %s', $table, $oldfield, $newfield, $type); + } + + return false; + } + + public function addPostQuery($query, $message = null) + { + $this->post_queries[] = $query; + } + + public function migrationOneTable($table) + { + // Queued changes are only recorded: nothing reaches the schema of the test + // database, whose transaction a DDL statement would commit + } + + public function displayMessage($msg) {} + }; + + foreach (plugin_order_get_install_classes() as $class) { + $class::install($migration); + } + + $this->assertSame([], $migration->changed_fields); + $this->assertSame([], $migration->post_queries); + $this->assertFalse(PluginOrderOrder_Item::$upgraded_from_before_1_5_0); + } + + public static function fieldHasTypeProvider(): iterable + { + yield 'decimal, same precision' => ['glpi_plugin_order_orders_items', 'price_taxfree', 'decimal(20,6)', true]; + yield 'decimal, other precision' => ['glpi_plugin_order_orders_items', 'price_taxfree', 'decimal(10,2)', false]; + // MySQL 5.7 and MariaDB report "int(10) unsigned", MySQL 8 "int unsigned": both match + yield 'int unsigned, display width ignored' => ['glpi_plugin_order_orders_items', 'id', 'int unsigned', true]; + yield 'int unsigned, other type' => ['glpi_plugin_order_orders_items', 'id', 'bigint unsigned', false]; + yield 'varchar' => ['glpi_plugin_order_references_suppliers', 'reference_code', 'varchar(255)', true]; + yield 'type compared case insensitively' => ['glpi_plugin_order_references_suppliers', 'reference_code', 'VARCHAR(255)', true]; + // Nothing to change on a column that does not exist (renamed or dropped long ago) + yield 'missing column' => ['glpi_plugin_order_orders_items', 'price_taxfree2', 'decimal(20,6)', true]; + } + + #[DataProvider('fieldHasTypeProvider')] + public function testFieldHasType(string $table, string $field, string $type, bool $expected): void + { + $this->assertSame($expected, PluginOrderConfig::fieldHasType($table, $field, $type)); + } +} From 1777b5703eee243e15c19838c7920ea7da316260 Mon Sep 17 00:00:00 2001 From: Thetsmr Date: Sun, 22 Nov 2026 16:27:30 +0100 Subject: [PATCH 5/5] Fix PR --- hook.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/hook.php b/hook.php index c1d42f6524..8dbe8fd6c8 100644 --- a/hook.php +++ b/hook.php @@ -33,11 +33,6 @@ use function Safe\mkdir; use function Safe\preg_match; -/** - * Plugin install process - * - * @return boolean - */ /** * Classes whose install() method plugin_order_install() runs, in this order: some upgrade steps * rely on the ones run before them (see PluginOrderOrder_Item::$upgraded_from_before_1_5_0). @@ -59,6 +54,11 @@ function plugin_order_get_install_classes(): array ]; } +/** + * Plugin install process + * + * @return boolean + */ function plugin_order_install() { foreach (glob(PLUGIN_ORDER_DIR . '/inc/*.php') as $file) {