hanyuu/database/2023_01_09_213225_create_users_tables.php

104 lines
5.4 KiB
PHP

<?php
use Index\Data\IDbConnection;
use Index\Data\Migration\IDbMigration;
final class CreateUsersTables_20230109_213225 implements IDbMigration {
public function migrate(IDbConnection $conn): void {
$conn->execute('
CREATE TABLE hau_users (
user_id INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
user_name VARCHAR(255) NOT NULL COLLATE \'ascii_general_ci\',
user_country CHAR(2) NOT NULL DEFAULT \'XX\' COLLATE \'ascii_general_ci\',
user_colour INT(10) UNSIGNED NULL DEFAULT NULL,
user_super TINYINT(1) UNSIGNED NOT NULL DEFAULT \'0\',
user_time_zone VARBINARY(255) NOT NULL DEFAULT \'UTC\',
user_created TIMESTAMP NOT NULL DEFAULT current_timestamp(),
user_updated TIMESTAMP NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
user_deleted TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (user_id) USING BTREE,
UNIQUE INDEX hau_users_name_unique (user_name) USING BTREE,
INDEX hau_users_created_index (user_created) USING BTREE,
INDEX hau_users_deleted_index (user_deleted) USING BTREE
) ENGINE=InnoDB COLLATE=utf8mb4_bin;
');
$conn->execute('
CREATE TABLE hau_users_auth (
user_id INT(10) UNSIGNED NOT NULL,
user_auth_type VARCHAR(32) NOT NULL COLLATE \'ascii_general_ci\',
user_auth_enabled TIMESTAMP NOT NULL DEFAULT current_timestamp(),
PRIMARY KEY (user_id, user_auth_type) USING BTREE,
INDEX hau_users_auth_user_foreign (user_id) USING BTREE,
CONSTRAINT hau_users_auth_user_foreign
FOREIGN KEY (user_id)
REFERENCES hau_users (user_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) ENGINE=InnoDB COLLATE=utf8mb4_bin;
');
$conn->execute('
CREATE TABLE hau_users_backup (
user_id INT(10) UNSIGNED NOT NULL,
user_backup_code BINARY(8) NOT NULL,
user_backup_created TIMESTAMP NOT NULL DEFAULT current_timestamp(),
user_backup_used TIMESTAMP NULL DEFAULT NULL,
PRIMARY KEY (user_id, user_backup_code) USING BTREE,
INDEX hau_users_backup_used_index (user_backup_used) USING BTREE,
CONSTRAINT hau_users_backup_user_foreign
FOREIGN KEY (user_id)
REFERENCES hau_users (user_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) ENGINE=InnoDB COLLATE=utf8mb4_bin;
');
$conn->execute('
CREATE TABLE hau_users_emails (
user_email_address VARCHAR(255) NOT NULL COLLATE \'ascii_general_ci\',
user_id INT(10) UNSIGNED NOT NULL,
user_email_created TIMESTAMP NOT NULL DEFAULT current_timestamp(),
user_email_verified TIMESTAMP NULL DEFAULT NULL,
user_email_recovery TINYINT(1) UNSIGNED NOT NULL DEFAULT \'0\',
PRIMARY KEY (user_email_address) USING BTREE,
UNIQUE INDEX hau_users_emails_user_foreign (user_id) USING BTREE,
INDEX hau_users_emails_created_index (user_email_created) USING BTREE,
INDEX hau_users_emails_recovery_index (user_email_recovery) USING BTREE,
INDEX hau_users_emails_verified_index (user_email_verified) USING BTREE,
CONSTRAINT hau_users_emails_user_foreign
FOREIGN KEY (user_id)
REFERENCES hau_users (user_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) ENGINE=InnoDB COLLATE=utf8mb4_bin;
');
$conn->execute('
CREATE TABLE hau_users_passwords (
user_id INT(10) UNSIGNED NOT NULL,
user_password_hash VARBINARY(255) NOT NULL,
user_password_changed TIMESTAMP NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
UNIQUE INDEX hau_users_passwords_user_foreign (user_id) USING BTREE,
CONSTRAINT hau_users_passwords_user_foreign
FOREIGN KEY (user_id)
REFERENCES hau_users (user_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) ENGINE=InnoDB COLLATE=utf8mb4_bin;
');
$conn->execute('
CREATE TABLE hau_users_totp (
user_id INT(10) UNSIGNED NOT NULL,
user_totp_key BINARY(26) NOT NULL,
user_totp_changed TIMESTAMP NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
UNIQUE INDEX hau_users_totp_user_foreign (user_id) USING BTREE,
CONSTRAINT hau_users_totp_user_foreign
FOREIGN KEY (user_id)
REFERENCES hau_users (user_id)
ON UPDATE CASCADE
ON DELETE CASCADE
) ENGINE=InnoDB COLLATE=utf8mb4_bin;
');
}
}