modify sql

This commit is contained in:
Jethro Lin 2024-12-04 17:27:55 +08:00
parent e0e55719d0
commit 8122ab2b26

View file

@ -1,4 +1,37 @@
-- Create admins table
-- 禁用外鍵檢查以避免在刪除表格時出現外鍵依賴問題
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,
@ -12,7 +45,7 @@ CREATE TABLE `admins` (
KEY `admins_email_index` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Create llm_providers table
-- 創建 `llm_providers` 表
CREATE TABLE `llm_providers` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
@ -28,7 +61,7 @@ CREATE TABLE `llm_providers` (
KEY `llm_providers_status_index` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Create clients table
-- 創建 `clients` 表
CREATE TABLE `clients` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
@ -45,7 +78,7 @@ CREATE TABLE `clients` (
REFERENCES `llm_providers` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Create auth_tokens table
-- 創建 `auth_tokens` 表
CREATE TABLE `auth_tokens` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`client_id` BIGINT UNSIGNED NOT NULL,
@ -60,7 +93,7 @@ CREATE TABLE `auth_tokens` (
REFERENCES `clients` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Create admin_client table (pivot table)
-- 創建 `admin_client` 表(聯接表)
CREATE TABLE `admin_client` (
`admin_id` BIGINT UNSIGNED NOT NULL,
`client_id` BIGINT UNSIGNED NOT NULL,
@ -74,7 +107,7 @@ CREATE TABLE `admin_client` (
REFERENCES `clients` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
-- Create operation_logs table
-- 創建 `operation_logs` 表
CREATE TABLE `operation_logs` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
`user_type` ENUM('admin', 'client') NOT NULL,
@ -85,3 +118,6 @@ CREATE TABLE `operation_logs` (
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;