Enhance SQL script for timewish table; implement conditional column addition for start_date and end_date to ensure compatibility with existing schema and prevent errors if columns already exist.
This commit is contained in:
@@ -2,9 +2,38 @@
|
|||||||
-- Führe dieses Script in deiner stechuhr2 Datenbank aus
|
-- Führe dieses Script in deiner stechuhr2 Datenbank aus
|
||||||
|
|
||||||
-- Schritt 1: Tabelle anpassen (Spalten hinzufügen)
|
-- Schritt 1: Tabelle anpassen (Spalten hinzufügen)
|
||||||
ALTER TABLE `timewish`
|
-- Spalten einzeln hinzufügen (kompatibel mit MySQL 8.0)
|
||||||
ADD COLUMN IF NOT EXISTS `start_date` DATE DEFAULT '2023-01-01' COMMENT 'Ab welchem Datum gilt dieser Timewish' AFTER `end_time`,
|
-- Ignoriert Fehler falls Spalten bereits existieren
|
||||||
ADD COLUMN IF NOT EXISTS `end_date` DATE DEFAULT NULL COMMENT 'Bis welchem Datum gilt dieser Timewish (NULL = bis heute)' AFTER `start_date`;
|
|
||||||
|
-- start_date Spalte (ignoriert Fehler falls vorhanden)
|
||||||
|
SET @sql = (
|
||||||
|
SELECT IF(
|
||||||
|
COUNT(*) = 0,
|
||||||
|
'ALTER TABLE `timewish` ADD COLUMN `start_date` DATE DEFAULT ''2023-01-01'' COMMENT ''Ab welchem Datum gilt dieser Timewish'' AFTER `end_time`;',
|
||||||
|
'SELECT ''Spalte start_date existiert bereits'' AS Info;'
|
||||||
|
) FROM information_schema.COLUMNS
|
||||||
|
WHERE TABLE_SCHEMA = DATABASE()
|
||||||
|
AND TABLE_NAME = 'timewish'
|
||||||
|
AND COLUMN_NAME = 'start_date'
|
||||||
|
);
|
||||||
|
PREPARE stmt FROM @sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
|
|
||||||
|
-- end_date Spalte (ignoriert Fehler falls vorhanden)
|
||||||
|
SET @sql = (
|
||||||
|
SELECT IF(
|
||||||
|
COUNT(*) = 0,
|
||||||
|
'ALTER TABLE `timewish` ADD COLUMN `end_date` DATE DEFAULT NULL COMMENT ''Bis welchem Datum gilt dieser Timewish (NULL = bis heute)'' AFTER `start_date`;',
|
||||||
|
'SELECT ''Spalte end_date existiert bereits'' AS Info;'
|
||||||
|
) FROM information_schema.COLUMNS
|
||||||
|
WHERE TABLE_SCHEMA = DATABASE()
|
||||||
|
AND TABLE_NAME = 'timewish'
|
||||||
|
AND COLUMN_NAME = 'end_date'
|
||||||
|
);
|
||||||
|
PREPARE stmt FROM @sql;
|
||||||
|
EXECUTE stmt;
|
||||||
|
DEALLOCATE PREPARE stmt;
|
||||||
|
|
||||||
-- Schritt 2: Bestehende Einträge löschen (falls vorhanden)
|
-- Schritt 2: Bestehende Einträge löschen (falls vorhanden)
|
||||||
DELETE FROM `timewish` WHERE user_id = 1;
|
DELETE FROM `timewish` WHERE user_id = 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user