From 10052f53d0ef7e36e1f4c3c1cb421026499c638d Mon Sep 17 00:00:00 2001
From: Jan-Hendrik Willms <tleilax+studip@gmail.com>
Date: Wed, 29 Jun 2022 08:11:10 +0000
Subject: [PATCH] add migration that removes column ex_termine.topic_id, fixes
 #1224

Closes #1224

Merge request studip/studip!735
---
 ...1.33_remove_column_ex_termine_topic_id.php | 42 +++++++++++++++++++
 1 file changed, 42 insertions(+)
 create mode 100644 db/migrations/5.1.33_remove_column_ex_termine_topic_id.php

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
new file mode 100644
index 00000000000..57e6c3dcfda
--- /dev/null
+++ b/db/migrations/5.1.33_remove_column_ex_termine_topic_id.php
@@ -0,0 +1,42 @@
+<?php
+/**
+ * @see https://gitlab.studip.de/studip/studip/-/issues/1224
+ * @see https://gitlab.studip.de/studip/studip/-/issues/881
+ */
+final class RemoveColumnExTermineTopicId extends Migration
+{
+    public function description()
+    {
+        return 'Removes unused column topic_id from table ex_termine.';
+    }
+
+    protected function up()
+    {
+        if (!$this->columnExists('ex_termine', 'topic_id')) {
+            $this->write("Column ex_termine.topic_id does not exist");
+            return;
+        }
+
+        $query = "ALTER TABLE `ex_termine`
+                  DROP COLUMN `topic_id`";
+        DBManager::get()->exec($query);
+    }
+
+    protected function down()
+    {
+        if ($this->columnExists('ex_termine', 'topic_id')) {
+            $this->write("Column ex_termine.topic_id already exists");
+            return;
+        }
+
+        $query = "ALTER TABLE `ex_termine`
+                  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]);
+    }
+}
-- 
GitLab