27 lines
1.2 KiB
TypeScript
27 lines
1.2 KiB
TypeScript
import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
|
|
import { DeleteStatus, MessageStatus } from "../../client/share/types.ts";
|
|
|
|
const createUserMessagesTable: MigrationLiveDefinition = {
|
|
name: "create_user_messages_table",
|
|
up: async (api) => {
|
|
await api.schema.createTable('user_messages', (table) => {
|
|
table.increments('id').primary().comment('关联ID');
|
|
table.integer('user_id').unsigned().references('id').inTable('users').onDelete('CASCADE').comment('用户ID');
|
|
table.integer('message_id').unsigned().references('id').inTable('messages').onDelete('CASCADE').comment('消息ID');
|
|
table.integer('status').defaultTo(MessageStatus.UNREAD).comment('阅读状态(0=未读,1=已读)');
|
|
table.integer('is_deleted').defaultTo(DeleteStatus.NOT_DELETED).comment('删除状态(0=未删除,1=已删除)');
|
|
table.timestamp('read_at').nullable().comment('阅读时间');
|
|
table.timestamps(true, true);
|
|
|
|
// 添加复合索引
|
|
table.index(['user_id', 'status']);
|
|
table.index(['user_id', 'is_deleted']);
|
|
table.unique(['user_id', 'message_id']);
|
|
});
|
|
},
|
|
down: async (api) => {
|
|
await api.schema.dropTable('user_messages');
|
|
}
|
|
}
|
|
|
|
export default createUserMessagesTable; |