|
|
@@ -20,6 +20,7 @@ const createUsersTable: MigrationLiveDefinition = {
|
|
|
table.string('email').unique();
|
|
|
table.string('nickname');
|
|
|
table.string('name');
|
|
|
+ table.integer('organization_id').unsigned().nullable().references('id').inTable('organizations').comment('所属机构ID');
|
|
|
table.integer('is_disabled').defaultTo(0);
|
|
|
table.integer('is_deleted').defaultTo(0);
|
|
|
table.timestamps(true, true);
|
|
|
@@ -52,7 +53,6 @@ const createLoginHistoryTable: MigrationLiveDefinition = {
|
|
|
// 添加索引
|
|
|
table.index('user_id');
|
|
|
table.index('login_time');
|
|
|
- // table.index(['longitude', 'latitude']);
|
|
|
})
|
|
|
},
|
|
|
down: async (api) => {
|
|
|
@@ -190,6 +190,77 @@ const createSystemSettingsTable: MigrationLiveDefinition = {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+// 创建消息表迁移
|
|
|
+const createMessagesTable: MigrationLiveDefinition = {
|
|
|
+ name: "create_messages_table",
|
|
|
+ up: async (api) => {
|
|
|
+ await api.schema.createTable('messages', (table) => {
|
|
|
+ table.increments('id').primary().comment('消息ID');
|
|
|
+ table.string('title').notNullable().comment('消息标题');
|
|
|
+ table.text('content').notNullable().comment('消息内容');
|
|
|
+ table.enum('type', ['system', 'private', 'announce']).notNullable().comment('消息类型');
|
|
|
+ table.integer('sender_id').unsigned().references('id').inTable('users').onDelete('SET NULL').comment('发送者ID');
|
|
|
+ table.string('sender_name').comment('发送者名称');
|
|
|
+ table.timestamps(true, true);
|
|
|
+
|
|
|
+ // 添加索引
|
|
|
+ table.index('type');
|
|
|
+ table.index('sender_id');
|
|
|
+ });
|
|
|
+ },
|
|
|
+ down: async (api) => {
|
|
|
+ await api.schema.dropTable('messages');
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// 创建用户消息关联表迁移
|
|
|
+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(0).comment('阅读状态(0=未读,1=已读)');
|
|
|
+ table.integer('is_deleted').defaultTo(0).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');
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+// 创建机构表迁移
|
|
|
+const createOrganizationsTable: MigrationLiveDefinition = {
|
|
|
+ name: "create_organizations_table",
|
|
|
+ up: async (api) => {
|
|
|
+ await api.schema.createTable('organizations', (table) => {
|
|
|
+ table.increments('id').primary();
|
|
|
+ table.string('name').notNullable().comment('机构名称');
|
|
|
+ table.string('code').notNullable().comment('机构代码');
|
|
|
+ table.integer('parent_id').unsigned().nullable().comment('父机构ID');
|
|
|
+ table.integer('level').defaultTo(1).comment('机构层级');
|
|
|
+ table.integer('is_deleted').defaultTo(DeleteStatus.NOT_DELETED).comment('是否被删除 (0否 1是)');
|
|
|
+ table.timestamps(true, true);
|
|
|
+
|
|
|
+ // 添加索引
|
|
|
+ table.index('name');
|
|
|
+ table.index('code');
|
|
|
+ table.index('parent_id');
|
|
|
+ });
|
|
|
+ },
|
|
|
+ down: async (api) => {
|
|
|
+ await api.schema.dropTable('organizations');
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
// 初始测试数据迁移
|
|
|
const seedInitialData: MigrationLiveDefinition = {
|
|
|
name: "seed_initial_data",
|
|
|
@@ -474,55 +545,6 @@ const seedInitialData: MigrationLiveDefinition = {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-// 创建消息表迁移
|
|
|
-const createMessagesTable: MigrationLiveDefinition = {
|
|
|
- name: "create_messages_table",
|
|
|
- up: async (api) => {
|
|
|
- await api.schema.createTable('messages', (table) => {
|
|
|
- table.increments('id').primary().comment('消息ID');
|
|
|
- table.string('title').notNullable().comment('消息标题');
|
|
|
- table.text('content').notNullable().comment('消息内容');
|
|
|
- table.enum('type', ['system', 'private', 'announce']).notNullable().comment('消息类型');
|
|
|
- table.integer('sender_id').unsigned().references('id').inTable('users').onDelete('SET NULL').comment('发送者ID');
|
|
|
- table.string('sender_name').comment('发送者名称');
|
|
|
- table.timestamps(true, true);
|
|
|
-
|
|
|
- // 添加索引
|
|
|
- table.index('type');
|
|
|
- table.index('sender_id');
|
|
|
- });
|
|
|
- },
|
|
|
- down: async (api) => {
|
|
|
- await api.schema.dropTable('messages');
|
|
|
- }
|
|
|
-};
|
|
|
-
|
|
|
-// 创建用户消息关联表迁移
|
|
|
-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(0).comment('阅读状态(0=未读,1=已读)');
|
|
|
- table.integer('is_deleted').defaultTo(0).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 const migrations = [
|
|
|
createUsersTable,
|
|
|
@@ -534,5 +556,6 @@ export const migrations = [
|
|
|
createSystemSettingsTable,
|
|
|
createMessagesTable,
|
|
|
createUserMessagesTable,
|
|
|
+ createOrganizationsTable,
|
|
|
seedInitialData,
|
|
|
];
|