Browse Source
- Added new migration scripts for the `users` table, replacing the deprecated `users_table`. - Updated `package.json` with new database commands for generating, pushing, and migrating schemas. - Enhanced `README.md` with instructions for database migration and deployment. - Refactored database connection handling to use a connection pool for improved performance.db
9 changed files with 107 additions and 42 deletions
@ -0,0 +1,10 @@ |
|||||
|
CREATE TABLE `users` ( |
||||
|
`id` serial AUTO_INCREMENT NOT NULL, |
||||
|
`username` varchar(64) NOT NULL, |
||||
|
`email` varchar(255) NOT NULL, |
||||
|
`password_hash` varchar(255) NOT NULL, |
||||
|
`is_active` boolean NOT NULL DEFAULT true, |
||||
|
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, |
||||
|
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, |
||||
|
CONSTRAINT `users_id` PRIMARY KEY(`id`) |
||||
|
); |
||||
@ -1,8 +0,0 @@ |
|||||
CREATE TABLE `users_table` ( |
|
||||
`id` serial AUTO_INCREMENT NOT NULL, |
|
||||
`name` varchar(255) NOT NULL, |
|
||||
`age` int NOT NULL, |
|
||||
`email` varchar(255) NOT NULL, |
|
||||
CONSTRAINT `users_table_id` PRIMARY KEY(`id`), |
|
||||
CONSTRAINT `users_table_email_unique` UNIQUE(`email`) |
|
||||
); |
|
||||
@ -1,4 +1,11 @@ |
|||||
import { drizzle } from "drizzle-orm/mysql2"; |
import { drizzle } from "drizzle-orm/mysql2"; |
||||
|
import mysql from "mysql2/promise"; |
||||
|
|
||||
// You can specify any property from the mysql2 connection options
|
// 使用连接池提升并发能力;支持通过 DATABASE_URL 直连(例:mysql://user:pass@host:3306/db)
|
||||
const db = drizzle({ connection: { uri: process.env.DATABASE_URL } }); |
// 生产/本地统一从环境变量读取,避免硬编码
|
||||
|
const pool = mysql.createPool(process.env.DATABASE_URL as string); |
||||
|
|
||||
|
// Drizzle 实例,供全局复用;建议在应用层只导入 db,不直接操作 pool
|
||||
|
const db = drizzle(pool); |
||||
|
|
||||
|
export { db, pool }; |
||||
|
|||||
@ -0,0 +1,19 @@ |
|||||
|
import { migrate } from 'drizzle-orm/mysql2/migrator' |
||||
|
import { db, pool } from './index' |
||||
|
|
||||
|
// 运行数据库迁移脚本(部署阶段调用)
|
||||
|
// 要求:环境变量 DATABASE_URL 已配置
|
||||
|
// 迁移文件目录:packages/server/drizzle
|
||||
|
(async () => { |
||||
|
try { |
||||
|
await migrate(db, { migrationsFolder: new URL('../../drizzle', import.meta.url).pathname }) |
||||
|
console.log('[drizzle] migration completed') |
||||
|
} catch (err) { |
||||
|
console.error('[drizzle] migration failed:', err) |
||||
|
process.exitCode = 1 |
||||
|
} finally { |
||||
|
await pool.end() |
||||
|
} |
||||
|
})() |
||||
|
|
||||
|
|
||||
@ -1,8 +1,13 @@ |
|||||
import { int, mysqlTable, serial, varchar } from 'drizzle-orm/mysql-core'; |
import { mysqlTable, serial, varchar, timestamp, boolean } from 'drizzle-orm/mysql-core' |
||||
|
import { sql } from 'drizzle-orm' |
||||
|
|
||||
export const usersTable = mysqlTable('users_table', { |
// 用户表:满足基础登录/鉴权与审计需求
|
||||
id: serial().primaryKey(), |
export const usersTable = mysqlTable('users', { |
||||
name: varchar({ length: 255 }).notNull(), |
id: serial('id').primaryKey(), // 自增主键
|
||||
age: int().notNull(), |
username: varchar('username', { length: 64 }).notNull(), // 用户名,登录凭证之一
|
||||
email: varchar({ length: 255 }).notNull().unique(), |
email: varchar('email', { length: 255 }).notNull(), // 邮箱,唯一
|
||||
}); |
passwordHash: varchar('password_hash', { length: 255 }).notNull(), // 密码哈希
|
||||
|
isActive: boolean('is_active').notNull().default(true), // 启用状态
|
||||
|
createdAt: timestamp('created_at').notNull().default(sql`CURRENT_TIMESTAMP`), // 创建时间
|
||||
|
updatedAt: timestamp('updated_at').notNull().default(sql`CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP`), // 更新时间
|
||||
|
}) |
||||
|
|||||
Loading…
Reference in new issue