You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
24 lines
792 B
24 lines
792 B
/**
|
|
* Auto-migrate: ensure chat_messages table and columns exist.
|
|
* Runs on every server start — idempotent DDL.
|
|
*/
|
|
import { dbGlobal } from "drizzle-pkg/lib/db"
|
|
|
|
export default defineNitroPlugin(async () => {
|
|
await dbGlobal.run(`
|
|
CREATE TABLE IF NOT EXISTS chat_messages (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
|
nickname TEXT(20) NOT NULL,
|
|
content TEXT NOT NULL,
|
|
created_at INTEGER NOT NULL,
|
|
client_id TEXT,
|
|
user_id INTEGER
|
|
)
|
|
`)
|
|
try { await dbGlobal.run(`ALTER TABLE chat_messages ADD COLUMN client_id TEXT`) } catch {}
|
|
try { await dbGlobal.run(`ALTER TABLE chat_messages ADD COLUMN user_id INTEGER`) } catch {}
|
|
|
|
await dbGlobal.run(`
|
|
CREATE INDEX IF NOT EXISTS idx_chat_msg_created ON chat_messages (created_at)
|
|
`)
|
|
})
|
|
|