mince/database/2023_08_16_161818_existing_structure.php

47 lines
2 KiB
PHP
Raw Normal View History

2023-08-16 21:18:12 +00:00
<?php
2024-10-05 14:48:18 +00:00
use Index\Db\DbConnection;
use Index\Db\Migration\DbMigration;
2023-08-16 21:18:12 +00:00
2024-10-05 14:48:18 +00:00
final class ExistingStructure_20230816_161818 implements DbMigration {
public function migrate(DbConnection $conn): void {
2023-08-16 21:18:12 +00:00
$hasWhiteList2020 = false;
$hasWhiteList2022 = false;
// check if the old tables exist
$tables = $conn->query('SHOW TABLES');
while($tables->next()) {
$tableName = $tables->getString(0);
if($tableName === 'whitelist')
$hasWhiteList2020 = true;
elseif($tableName === 'whitelist_2022')
$hasWhiteList2022 = true;
if($hasWhiteList2020 && $hasWhiteList2022)
break;
}
if(!$hasWhiteList2020)
$conn->execute('
CREATE TABLE whitelist (
flashii_id INT(10) UNSIGNED NOT NULL,
minecraft_username VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_ci",
whitelist_added TIMESTAMP NOT NULL DEFAULT current_timestamp(),
UNIQUE KEY whitelist_unique (flashii_id, minecraft_username),
KEY whitelist_flashii_key (flashii_id),
KEY whitelist_minecraft_key (minecraft_username)
) ENGINE=InnoDB COLLATE="latin1_swedish_ci";
');
if(!$hasWhiteList2022)
$conn->execute('
CREATE TABLE whitelist_2022 (
flashii_id INT(10) UNSIGNED NOT NULL,
minecraft_username VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_ci",
whitelist_added TIMESTAMP NOT NULL DEFAULT current_timestamp(),
UNIQUE KEY whitelist_2022_unique (flashii_id, minecraft_username),
KEY whitelist_2022_flashii_key (flashii_id),
KEY whitelist_2022_minecraft_key (minecraft_username)
) ENGINE=InnoDB COLLATE="latin1_swedish_ci";
');
}
}