flash.moe/database/2023_10_13_215920_existing_tables.php

110 lines
6.2 KiB
PHP
Raw Normal View History

2023-10-13 22:25:40 +00:00
<?php
use Index\Data\IDbConnection;
use Index\Data\Migration\IDbMigration;
final class ExistingTables_20231013_215920 implements IDbMigration {
public function migrate(IDbConnection $conn): void {
$existingTables = [];
$result = $conn->query('SHOW TABLES');
while($result->next())
$existingTables[] = $result->getString(0);
if(!in_array('fm_contacts', $existingTables))
$conn->execute('
CREATE TABLE fm_contacts (
cont_name VARBINARY(16) NOT NULL,
cont_homepage TINYINT(4) NOT NULL DEFAULT "0",
cont_order INT(11) NOT NULL DEFAULT "0",
cont_title VARCHAR(255) NOT NULL COLLATE "utf8mb4_bin",
cont_icon VARCHAR(255) NOT NULL COLLATE "utf8mb4_bin",
cont_colour INT(10) UNSIGNED NOT NULL,
cont_display VARCHAR(255) NOT NULL COLLATE "utf8mb4_bin",
cont_link VARCHAR(255) NULL DEFAULT NULL COLLATE "utf8mb4_bin",
PRIMARY KEY (cont_name),
KEY cont_homepage (cont_homepage),
KEY cont_order (cont_order)
) ENGINE=InnoDB COLLATE="utf8mb4_bin";
');
if(!in_array('fm_public_keys', $existingTables))
$conn->execute('
CREATE TABLE fm_public_keys (
key_id INT(11) NOT NULL AUTO_INCREMENT,
key_level INT(11) NOT NULL DEFAULT "0",
key_algo VARCHAR(20) NOT NULL COLLATE "utf8mb4_bin",
key_body TEXT NOT NULL COLLATE "utf8mb4_bin",
key_comment VARCHAR(255) NOT NULL COLLATE "utf8mb4_bin",
key_created TIMESTAMP NOT NULL DEFAULT current_timestamp(),
key_deprecated TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (key_id),
UNIQUE KEY key_body (key_body),
KEY key_deprecated (key_deprecated),
KEY key_level (key_level)
) ENGINE=InnoDB COLLATE="utf8mb4_bin";
');
if(!in_array('fm_proglangs', $existingTables))
$conn->execute('
CREATE TABLE fm_proglangs (
language_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
language_name VARCHAR(50) NOT NULL COLLATE "utf8mb4_unicode_ci",
language_colour MEDIUMINT(8) UNSIGNED NULL DEFAULT NULL,
PRIMARY KEY (language_id),
UNIQUE KEY language_name (language_name)
) ENGINE=InnoDB COLLATE="utf8mb4_bin";
');
if(!in_array('fm_projects', $existingTables))
$conn->execute('
CREATE TABLE fm_projects (
project_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
project_name VARCHAR(255) NOT NULL COLLATE "utf8mb4_unicode_ci",
project_name_clean VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_general_ci",
project_summary VARCHAR(255) NULL DEFAULT NULL COLLATE "utf8mb4_unicode_ci",
project_description MEDIUMTEXT NULL DEFAULT NULL COLLATE "utf8mb4_unicode_ci",
project_order INT(11) NOT NULL DEFAULT "0",
project_type ENUM("Project", "Tool") NOT NULL DEFAULT "Project" COLLATE "ascii_general_ci",
project_featured TINYINT(3) UNSIGNED NOT NULL DEFAULT "0",
project_colour MEDIUMINT(8) UNSIGNED NULL DEFAULT NULL,
project_homepage VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_general_ci",
project_repository VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_general_ci",
project_forum VARCHAR(255) NULL DEFAULT NULL COLLATE "ascii_general_ci",
project_created TIMESTAMP NOT NULL DEFAULT current_timestamp(),
project_deleted TIMESTAMP NULL DEFAULT NULL,
project_archived TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (project_id),
UNIQUE KEY project_name (project_name),
UNIQUE KEY project_name_clean (project_name_clean),
KEY project_order (project_order),
KEY project_type (project_type),
KEY project_archived (project_archived),
KEY project_deleted (project_deleted),
KEY project_created (project_created),
KEY project_featured (project_featured)
) ENGINE=InnoDB COLLATE="utf8mb4_bin";
');
if(!in_array('fm_projects_proglangs', $existingTables))
$conn->execute('
CREATE TABLE fm_projects_proglangs (
project_id INT(10) UNSIGNED NOT NULL,
language_id INT(10) UNSIGNED NOT NULL,
priority INT(10) NOT NULL DEFAULT "0",
UNIQUE KEY projects_proglangs_unique (project_id, language_id),
KEY projects_proglangs_language_foreign (language_id),
KEY projects_proglangs_priority_index (priority),
CONSTRAINT projects_proglangs_language_foreign
FOREIGN KEY (language_id)
REFERENCES fm_proglangs (language_id)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT projects_proglangs_project_foreign
FOREIGN KEY (project_id)
REFERENCES fm_projects (project_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) ENGINE=InnoDB COLLATE="utf8mb4_bin";
');
}
}