From 68d4b8dc021d8512cd76742ee882d9bb20b90eea Mon Sep 17 00:00:00 2001 From: Elmar Ludwig Date: Fri, 8 Jul 2022 14:25:37 +0000 Subject: avoid running migration twice, fixes #1265 Closes #1265 Merge request studip/studip!774 --- db/migrations/5.1.21_missing_indices_v50.php | 7 ++++++ db/migrations/5.1.24_biest_348.php | 9 +++---- .../5.1.27_add_index_to_cw_user_progresses_v50.php | 7 ++++++ ...5.1.29_add_index_resource_booking_intervals.php | 28 ++++++++-------------- .../5.1.30_remove_column_termine_topic_id.php | 8 ++----- .../5.1.33_remove_column_ex_termine_topic_id.php | 8 ++----- lib/migrations/DatabaseMigrationTrait.php | 22 +++++++++++++++++ 7 files changed, 53 insertions(+), 36 deletions(-) create mode 100644 lib/migrations/DatabaseMigrationTrait.php diff --git a/db/migrations/5.1.21_missing_indices_v50.php b/db/migrations/5.1.21_missing_indices_v50.php index 39056fa..66af9c3 100644 --- a/db/migrations/5.1.21_missing_indices_v50.php +++ b/db/migrations/5.1.21_missing_indices_v50.php @@ -1,6 +1,8 @@ keyExists('mvv_files_ranges', 'range_id')) { + return; + } + $query = "CREATE INDEX `range_id` ON `mvv_files_ranges` (`range_id`)"; DBManager::get()->exec($query); diff --git a/db/migrations/5.1.24_biest_348.php b/db/migrations/5.1.24_biest_348.php index 67f854f..dd15560 100644 --- a/db/migrations/5.1.24_biest_348.php +++ b/db/migrations/5.1.24_biest_348.php @@ -1,6 +1,8 @@ columnExists()) { + if ($this->columnExists('resources', 'lockable')) { return; } @@ -22,9 +24,4 @@ class Biest348 extends Migration $query = 'ALTER TABLE `resources` DROP `lockable`'; DBManager::get()->exec($query); } - - private function columnExists() - { - return DBManager::get()->fetchFirst("SHOW COLUMNS FROM `resources` LIKE 'lockable'"); - } } diff --git a/db/migrations/5.1.27_add_index_to_cw_user_progresses_v50.php b/db/migrations/5.1.27_add_index_to_cw_user_progresses_v50.php index aaea146..bceb745 100644 --- a/db/migrations/5.1.27_add_index_to_cw_user_progresses_v50.php +++ b/db/migrations/5.1.27_add_index_to_cw_user_progresses_v50.php @@ -1,6 +1,8 @@ keyExists('cw_user_progresses', 'block_id')) { + return; + } + $query = "ALTER TABLE `cw_user_progresses` ADD INDEX `block_id` (`block_id`)"; DBManager::get()->exec($query); diff --git a/db/migrations/5.1.29_add_index_resource_booking_intervals.php b/db/migrations/5.1.29_add_index_resource_booking_intervals.php index d819125..e3c6557 100644 --- a/db/migrations/5.1.29_add_index_resource_booking_intervals.php +++ b/db/migrations/5.1.29_add_index_resource_booking_intervals.php @@ -2,6 +2,8 @@ class AddIndexResourceBookingIntervals extends Migration { + use DatabaseMigrationTrait; + public function description() { return 'add index for booking_id to resource_booking_intervals'; @@ -9,34 +11,24 @@ class AddIndexResourceBookingIntervals extends Migration public function up() { - $db = DBManager::get(); - // avoid running this migration twice - $sql = "SHOW INDEX FROM resource_booking_intervals WHERE Key_name = 'booking_id'"; - $result = $db->query($sql); - - if ($result && $result->rowCount() > 0) { + if ($this->keyExists('resource_booking_intervals', 'booking_id')) { return; } // index "assign_object_id" may not exist (depending on upgrade path) - $sql = "SHOW INDEX FROM resource_booking_intervals WHERE Key_name = 'assign_object_id'"; - $result = $db->query($sql); - - if ($result && $result->rowCount() > 0) { - $sql = 'ALTER TABLE resource_booking_intervals DROP INDEX assign_object_id'; - $db->exec($sql); + if ($this->keyExists('resource_booking_intervals', 'assign_object_id')) { + $sql = "ALTER TABLE resource_booking_intervals DROP INDEX assign_object_id"; + DBManager::get()->exec($sql); } - $sql = 'ALTER TABLE resource_booking_intervals ADD INDEX booking_id (booking_id)'; - $db->exec($sql); + $sql = "ALTER TABLE resource_booking_intervals ADD INDEX booking_id (booking_id)"; + DBManager::get()->exec($sql); } public function down() { - $db = DBManager::get(); - - $query = 'ALTER TABLE resource_booking_intervals DROP INDEX booking_id'; - $db->exec($query); + $query = "ALTER TABLE resource_booking_intervals DROP INDEX booking_id"; + DBManager::get()->exec($query); } } diff --git a/db/migrations/5.1.30_remove_column_termine_topic_id.php b/db/migrations/5.1.30_remove_column_termine_topic_id.php index 992e765..23972c3 100644 --- a/db/migrations/5.1.30_remove_column_termine_topic_id.php +++ b/db/migrations/5.1.30_remove_column_termine_topic_id.php @@ -4,6 +4,8 @@ */ final class RemoveColumnTermineTopicId extends Migration { + use DatabaseMigrationTrait; + public function description() { return 'Removes unused column topic_id from table termine.'; @@ -32,10 +34,4 @@ final class RemoveColumnTermineTopicId extends Migration ADD COLUMN `topic_id` VARCHAR(32) COLLATE latin1_bin DEFAULT NULL"; DBManager::get()->exec($query); } - - protected function columnExists(string $table, string $column): bool - { - $query = "SHOW COLUMNS FROM `{$table}` LIKE ?"; - return (bool) DBManager::get()->fetchOne($query, [$column]); - } } diff --git a/db/migrations/5.1.33_remove_column_ex_termine_topic_id.php b/db/migrations/5.1.33_remove_column_ex_termine_topic_id.php index 57e6c3d..363c2d5 100644 --- a/db/migrations/5.1.33_remove_column_ex_termine_topic_id.php +++ b/db/migrations/5.1.33_remove_column_ex_termine_topic_id.php @@ -5,6 +5,8 @@ */ final class RemoveColumnExTermineTopicId extends Migration { + use DatabaseMigrationTrait; + public function description() { return 'Removes unused column topic_id from table ex_termine.'; @@ -33,10 +35,4 @@ final class RemoveColumnExTermineTopicId extends Migration ADD COLUMN `topic_id` VARCHAR(32) COLLATE latin1_bin DEFAULT NULL"; DBManager::get()->exec($query); } - - protected function columnExists(string $table, string $column): bool - { - $query = "SHOW COLUMNS FROM `{$table}` LIKE ?"; - return (bool) DBManager::get()->fetchOne($query, [$column]); - } } diff --git a/lib/migrations/DatabaseMigrationTrait.php b/lib/migrations/DatabaseMigrationTrait.php new file mode 100644 index 0000000..f8fad72 --- /dev/null +++ b/lib/migrations/DatabaseMigrationTrait.php @@ -0,0 +1,22 @@ +fetchOne($query, [$key]); + } + + /** + * Returns whether a column with the given name exists on the given table. + */ + protected function columnExists(string $table, string $column): bool + { + $query = "SHOW COLUMNS FROM `{$table}` LIKE ?"; + return (bool) DBManager::get()->fetchOne($query, [$column]); + } +} -- cgit v1.0