Skip to content
Snippets Groups Projects
Commit 0d9044d1 authored by Elmar Ludwig's avatar Elmar Ludwig
Browse files

avoid running migration twice, fixes #1265

Closes #1265

Merge request studip/studip!774
parent 97a2beca
No related branches found
No related tags found
No related merge requests found
<?php
final class MissingIndicesV50 extends Migration
{
use DatabaseMigrationTrait;
public function description()
{
return 'Add missing indices on some tables';
......@@ -8,6 +10,11 @@ final class MissingIndicesV50 extends Migration
public function up()
{
// avoid running this migration twice
if ($this->keyExists('mvv_files_ranges', 'range_id')) {
return;
}
$query = "CREATE INDEX `range_id` ON `mvv_files_ranges` (`range_id`)";
DBManager::get()->exec($query);
......
<?php
class Biest348 extends Migration
{
use DatabaseMigrationTrait;
public function description ()
{
return 'Adds a column to the resources table to mark resources as lockable, default is 1.';
......@@ -8,7 +10,7 @@ class Biest348 extends Migration
public function up()
{
if ($this->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'");
}
}
<?php
final class AddIndexToCwUserProgressesV50 extends Migration
{
use DatabaseMigrationTrait;
public function description()
{
return 'Alter cw_user_progresses table, add index for block_id';
......@@ -8,6 +10,11 @@ final class AddIndexToCwUserProgressesV50 extends Migration
public function up()
{
// avoid running this migration twice
if ($this->keyExists('cw_user_progresses', 'block_id')) {
return;
}
$query = "ALTER TABLE `cw_user_progresses`
ADD INDEX `block_id` (`block_id`)";
DBManager::get()->exec($query);
......
......@@ -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);
}
}
......@@ -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]);
}
}
......@@ -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]);
}
}
<?php
trait DatabaseMigrationTrait
{
/**
* Returns whether a key/index with the given name exists on the given
* table.
*/
protected function keyExists(string $table, string $key): bool
{
$query = "SHOW INDEX FROM `$table` WHERE Key_name = ?";
return (bool) DBManager::get()->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]);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment