-- 禁用外鍵檢查以避免在刪除表格時出現外鍵依賴問題 SET FOREIGN_KEY_CHECKS = 0; -- =========================== -- 1. 刪除外鍵約束(如果存在) -- =========================== -- 刪除 `clients` 表的外鍵約束 ALTER TABLE `clients` DROP FOREIGN KEY IF EXISTS `clients_llm_provider_id_foreign`; -- 刪除 `auth_tokens` 表的外鍵約束 ALTER TABLE `auth_tokens` DROP FOREIGN KEY IF EXISTS `auth_tokens_client_id_foreign`; -- 刪除 `admin_client` 表的外鍵約束 ALTER TABLE `admin_client` DROP FOREIGN KEY IF EXISTS `admin_client_admin_id_foreign`; ALTER TABLE `admin_client` DROP FOREIGN KEY IF EXISTS `admin_client_client_id_foreign`; -- =========================== -- 2. 刪除表格(如果存在) -- =========================== -- 按照依賴順序刪除表格 DROP TABLE IF EXISTS `admin_client`; DROP TABLE IF EXISTS `auth_tokens`; DROP TABLE IF EXISTS `clients`; DROP TABLE IF EXISTS `llm_providers`; DROP TABLE IF EXISTS `admins`; DROP TABLE IF EXISTS `operation_logs`; -- =========================== -- 3. 創建表格 -- =========================== -- 創建 `admins` 表 CREATE TABLE `admins` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `username` VARCHAR(50) NOT NULL, `password` VARCHAR(255) NOT NULL, `email` VARCHAR(100) NULL DEFAULT NULL, `role` ENUM('super', 'admin') NOT NULL DEFAULT 'admin', `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `admins_username_unique` (`username`), KEY `admins_email_index` (`email`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- 創建 `llm_providers` 表 CREATE TABLE `llm_providers` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(100) NOT NULL, `service_name` VARCHAR(100) NOT NULL, `api_url` VARCHAR(255) NOT NULL, `api_token` VARCHAR(255) NOT NULL, `status` ENUM('active', 'inactive') NOT NULL DEFAULT 'active', `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `llm_providers_name_unique` (`name`), KEY `llm_providers_service_name_index` (`service_name`), KEY `llm_providers_status_index` (`status`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- 創建 `clients` 表 CREATE TABLE `clients` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(100) NOT NULL, `llm_provider_id` BIGINT UNSIGNED NOT NULL, `rate_limit` INT NOT NULL DEFAULT 60, `timeout` INT NOT NULL DEFAULT 30, `status` ENUM('active', 'inactive') NOT NULL DEFAULT 'active', `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `clients_llm_provider_id_foreign` (`llm_provider_id`), KEY `clients_status_index` (`status`), CONSTRAINT `clients_llm_provider_id_foreign` FOREIGN KEY (`llm_provider_id`) REFERENCES `llm_providers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- 創建 `auth_tokens` 表 CREATE TABLE `auth_tokens` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `client_id` BIGINT UNSIGNED NOT NULL, `token` CHAR(64) NOT NULL, `expires_at` TIMESTAMP NULL DEFAULT NULL, `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`), UNIQUE KEY `auth_tokens_token_unique` (`token`), KEY `auth_tokens_client_id_index` (`client_id`), CONSTRAINT `auth_tokens_client_id_foreign` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- 創建 `admin_client` 表(聯接表) CREATE TABLE `admin_client` ( `admin_id` BIGINT UNSIGNED NOT NULL, `client_id` BIGINT UNSIGNED NOT NULL, `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`admin_id`, `client_id`), KEY `admin_client_client_id_foreign` (`client_id`), CONSTRAINT `admin_client_admin_id_foreign` FOREIGN KEY (`admin_id`) REFERENCES `admins` (`id`) ON DELETE CASCADE, CONSTRAINT `admin_client_client_id_foreign` FOREIGN KEY (`client_id`) REFERENCES `clients` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- 創建 `operation_logs` 表 CREATE TABLE `operation_logs` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `user_type` ENUM('admin', 'client') NOT NULL, `user_id` BIGINT UNSIGNED NOT NULL, `operation` VARCHAR(255) NOT NULL, `ip_address` VARCHAR(45) NULL DEFAULT NULL, `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (`id`), KEY `operation_logs_user_type_user_id_index` (`user_type`, `user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- 啟用外鍵檢查 SET FOREIGN_KEY_CHECKS = 1;