From a25ce604f08d724e5d238bd14caba932bb1e0c9c Mon Sep 17 00:00:00 2001 From: wmp <17516219072@163.com> Date: Tue, 16 Sep 2025 09:46:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- prisma/init.sql | 61 +++++++++++++++++++++++++++++++++++++++++++++++++ prisma/seed.ts | 48 -------------------------------------- 2 files changed, 61 insertions(+), 48 deletions(-) create mode 100644 prisma/init.sql delete mode 100644 prisma/seed.ts diff --git a/prisma/init.sql b/prisma/init.sql new file mode 100644 index 0000000..039da3c --- /dev/null +++ b/prisma/init.sql @@ -0,0 +1,61 @@ +-- jdbox.accounts definition + +CREATE TABLE `accounts` ( + `id` varchar(191) NOT NULL, + `user_id` int(11) NOT NULL, + `type` varchar(191) NOT NULL, + `provider` varchar(191) NOT NULL, + `provider_account_id` varchar(191) NOT NULL, + `refresh_token` text DEFAULT NULL, + `access_token` text DEFAULT NULL, + `expires_at` int(11) DEFAULT NULL, + `token_type` varchar(191) DEFAULT NULL, + `scope` varchar(191) DEFAULT NULL, + `id_token` text DEFAULT NULL, + `session_state` varchar(191) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `accounts_provider_provider_account_id_key` (`provider`,`provider_account_id`), + KEY `accounts_user_id_fkey` (`user_id`) USING BTREE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- jdbox.sessions definition + +CREATE TABLE `sessions` ( + `id` varchar(191) NOT NULL, + `expires` datetime(3) NOT NULL, + `createdAt` datetime(3) NOT NULL DEFAULT current_timestamp(3), + `userId` int(11) NOT NULL, + PRIMARY KEY (`id`), + KEY `sessions_userId_idx` (`userId`) USING BTREE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + + +-- jdbox.users definition + +CREATE TABLE `users` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `name` varchar(191) DEFAULT NULL, + `createdAt` datetime(3) NOT NULL DEFAULT current_timestamp(3), + `password` varchar(191) NOT NULL, + `phone` varchar(191) NOT NULL, + `updatedAt` datetime(3) NOT NULL, + `verifiedPhone` tinyint(1) NOT NULL DEFAULT 0, + PRIMARY KEY (`id`), + UNIQUE KEY `users_phone_key` (`phone`) +) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- jdbox.verification_codes definition + +CREATE TABLE `verification_codes` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `phone` varchar(191) NOT NULL, + `code` varchar(191) NOT NULL, + `type` varchar(191) NOT NULL, + `expiresAt` datetime(3) NOT NULL, + `createdAt` datetime(3) NOT NULL DEFAULT current_timestamp(3), + PRIMARY KEY (`id`), + KEY `verification_codes_phone_type_idx` (`phone`,`type`) USING BTREE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; + +-- 插入初始用户 +INSERT INTO users(phone, password, name) VALUES('admin', '$2a$10$k.p3.s28OdLmGCMtuvBoqOxABp03h0Zhmop4eqqlR8sIjkThCcsnS', '管理员'); \ No newline at end of file diff --git a/prisma/seed.ts b/prisma/seed.ts deleted file mode 100644 index b065557..0000000 --- a/prisma/seed.ts +++ /dev/null @@ -1,48 +0,0 @@ -import { PrismaClient } from '@prisma/client' -import { hash } from 'bcryptjs' - -const prisma = new PrismaClient() - -async function main() { - console.log('🚀 开始执行种子脚本...') - - try { - // 首先检查用户是否已存在 - const existingUser = await prisma.user.findUnique({ - where: { phone: '17516219072' } - }) - - if (existingUser) { - console.log('✅ 用户已存在:', existingUser) - return - } - - console.log('🔐 加密密码...') - const password = await hash('123456', 10) - console.log('✅ 加密完成') - - console.log('👤 创建用户...') - const user = await prisma.user.create({ - data: { - phone: '17516219072', - password: password, - name: '测试用户', - }, - }) - console.log('✅ 用户创建成功:', user) - - } catch (error) { - console.error('❌ 种子脚本错误:', error) - throw error - } -} - -main() - .catch((e) => { - console.error('❌ 种子脚本执行失败:', e) - process.exit(1) - }) - .finally(async () => { - await prisma.$disconnect() - console.log('🔚 数据库连接已关闭') - }) \ No newline at end of file