const db = require('../db'); async function createTables() { const client = await db.pool.connect(); try { console.log('开始创建通知相关表...'); await client.query('BEGIN'); // 创建 notifications 表 await client.query(` CREATE TABLE IF NOT EXISTS notifications ( id SERIAL PRIMARY KEY, title VARCHAR(255) NOT NULL, content TEXT NOT NULL, publisher_id INTEGER REFERENCES users(id), publisher_name VARCHAR(100), created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, is_top BOOLEAN DEFAULT FALSE, status VARCHAR(20) DEFAULT 'published' -- published, draft, archived ); `); console.log('notifications 表创建成功'); // 创建 notification_attachments 表 await client.query(` CREATE TABLE IF NOT EXISTS notification_attachments ( id SERIAL PRIMARY KEY, notification_id INTEGER REFERENCES notifications(id) ON DELETE CASCADE, file_name VARCHAR(255) NOT NULL, file_path VARCHAR(255) NOT NULL, created_at TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP ); `); console.log('notification_attachments 表创建成功'); await client.query('COMMIT'); console.log('所有表创建完成'); } catch (err) { await client.query('ROLLBACK'); console.error('创建表失败:', err); } finally { client.release(); process.exit(); } } createTables();