|
36 | 36 | use Glpi\Dashboard\Dashboard; |
37 | 37 | use Glpi\Dashboard\Item as Dashboard_Item; |
38 | 38 | use Glpi\Dashboard\Right as Dashboard_Right; |
39 | | - |
| 39 | +use Glpi\System\Diagnostic\DatabaseSchemaIntegrityChecker; |
40 | 40 | use Ramsey\Uuid\Uuid; |
| 41 | + |
41 | 42 | class PluginFormcreatorInstall { |
42 | 43 | protected $migration; |
43 | 44 |
|
@@ -125,15 +126,24 @@ public function upgrade(Migration $migration, $args = []): bool { |
125 | 126 | } |
126 | 127 | if ($hasMyisamTables) { |
127 | 128 | // Need to convert myisam tables into innodb first |
| 129 | + $message = __('Upgrade tables to innoDB; run php bin/console glpi:migration:myisam_to_innodb', 'formcreator'); |
128 | 130 | if (isCommandLine()) { |
129 | | - echo "Upgrade tables to innoDB; run php bin/console glpi:migration:myisam_to_innodb" . PHP_EOL; |
| 131 | + echo $message . PHP_EOL; |
130 | 132 | } else { |
131 | | - Session::addMessageAfterRedirect(__('Upgrade tables to innoDB; run php bin/console glpi:migration:myisam_to_innodb', 'formcreator'), false, ERROR); |
| 133 | + Session::addMessageAfterRedirect($message, false, ERROR); |
132 | 134 | } |
133 | 135 | return false; |
134 | 136 | } |
135 | 137 | } |
136 | 138 |
|
| 139 | + // Check schema of tables |
| 140 | + if (!isset($args['skip-db-check'])) { |
| 141 | + $oldVersion = Config::getConfigurationValue('formcreator', 'previous_version'); |
| 142 | + if ($oldVersion !== null && !$this->checkSchema($oldVersion)) { |
| 143 | + return false; |
| 144 | + } |
| 145 | + } |
| 146 | + |
137 | 147 | $this->migration = $migration; |
138 | 148 | if (isset($args['force-upgrade']) && $args['force-upgrade'] === true) { |
139 | 149 | // Might return false |
@@ -262,7 +272,7 @@ public function isPluginInstalled() { |
262 | 272 | protected function installSchema() { |
263 | 273 | global $DB; |
264 | 274 |
|
265 | | - $dbFile = __DIR__ . '/mysql/plugin_formcreator_empty.sql'; |
| 275 | + $dbFile = plugin_formcreator_getSchemaPath(); |
266 | 276 | if (!$DB->runFile($dbFile)) { |
267 | 277 | $this->migration->displayWarning("Error creating tables : " . $DB->error(), true); |
268 | 278 | die('Giving up'); |
@@ -733,4 +743,68 @@ public function deleteMiniDashboard(): bool { |
733 | 743 |
|
734 | 744 | return true; |
735 | 745 | } |
| 746 | + |
| 747 | + /** |
| 748 | + * Check the schema of all tables of the plugin against the expected schema of the given version |
| 749 | + * |
| 750 | + * @return boolean |
| 751 | + */ |
| 752 | + public function checkSchema(string $version): bool { |
| 753 | + global $DB; |
| 754 | + |
| 755 | + $schemaFile = plugin_formcreator_getSchemaPath($version); |
| 756 | + |
| 757 | + $checker = new DatabaseSchemaIntegrityChecker( |
| 758 | + $DB, |
| 759 | + true, |
| 760 | + false, |
| 761 | + false, |
| 762 | + false, |
| 763 | + false, |
| 764 | + false |
| 765 | + ); |
| 766 | + |
| 767 | + try { |
| 768 | + $differences = $checker->checkCompleteSchema($schemaFile, true, 'plugin:formcreator'); |
| 769 | + } catch (\Throwable $e) { |
| 770 | + $message = __('Failed to check the sanity of the tables!', 'formcreator'); |
| 771 | + if (isCommandLine()) { |
| 772 | + echo $message . PHP_EOL; |
| 773 | + } else { |
| 774 | + Session::addMessageAfterRedirect($message, false, ERROR); |
| 775 | + } |
| 776 | + return false; |
| 777 | + } |
| 778 | + |
| 779 | + if (count($differences) > 0) { |
| 780 | + if (!isCommandLine()) { |
| 781 | + Session::addMessageAfterRedirect(sprintf( |
| 782 | + __('Inconsistencies detected in the database. To see the logs run the command %s', 'formcreator'), |
| 783 | + 'bin/console glpi:plugin:install formcreator' |
| 784 | + ), false, ERROR); |
| 785 | + } |
| 786 | + foreach ($differences as $table_name => $difference) { |
| 787 | + $message = null; |
| 788 | + switch ($difference['type']) { |
| 789 | + case DatabaseSchemaIntegrityChecker::RESULT_TYPE_ALTERED_TABLE: |
| 790 | + $message = sprintf(__('Table schema differs for table "%s".'), $table_name); |
| 791 | + break; |
| 792 | + case DatabaseSchemaIntegrityChecker::RESULT_TYPE_MISSING_TABLE: |
| 793 | + $message = sprintf(__('Table "%s" is missing.'), $table_name); |
| 794 | + break; |
| 795 | + case DatabaseSchemaIntegrityChecker::RESULT_TYPE_UNKNOWN_TABLE: |
| 796 | + $message = sprintf(__('Unknown table "%s" has been found in database.'), $table_name); |
| 797 | + break; |
| 798 | + } |
| 799 | + if (isCommandLine()) { |
| 800 | + echo $message . PHP_EOL; |
| 801 | + echo $difference['diff'] . PHP_EOL; |
| 802 | + } |
| 803 | + } |
| 804 | + |
| 805 | + return false; |
| 806 | + } |
| 807 | + |
| 808 | + return true; |
| 809 | + } |
736 | 810 | } |
0 commit comments