migrations.ts 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. import type { MigrationLiveDefinition } from '@d8d-appcontainer/types'
  2. import {
  3. EnableStatus, DeleteStatus,
  4. AuditStatus, ThemeMode, FontSize, CompactMode,
  5. SystemSettingKey,
  6. SystemSettingGroup,
  7. ALLOWED_FILE_TYPES,
  8. } from '../client/share/types.ts';
  9. // 定义用户表迁移
  10. const createUsersTable: MigrationLiveDefinition = {
  11. name: "create_users_table",
  12. up: async (api) => {
  13. await api.schema.createTable('users', (table) => {
  14. table.increments('id').primary();
  15. table.string('username').unique().notNullable();
  16. table.string('password').notNullable();
  17. table.string('phone').unique();
  18. table.string('email').unique();
  19. table.string('nickname');
  20. table.string('name');
  21. table.integer('is_disabled').defaultTo(0);
  22. table.integer('is_deleted').defaultTo(0);
  23. table.timestamps(true, true);
  24. // 添加索引
  25. table.index('username');
  26. table.index('is_disabled');
  27. table.index('is_deleted');
  28. });
  29. },
  30. down: async (api) => {
  31. await api.schema.dropTable('users');
  32. }
  33. }
  34. // 定义登录历史表迁移
  35. const createLoginHistoryTable: MigrationLiveDefinition = {
  36. name: "create_login_history_table",
  37. up: async (api) => {
  38. await api.schema.createTable('login_history', (table) => {
  39. table.increments('id').primary()
  40. table.integer('user_id').unsigned().references('id').inTable('users').onDelete('CASCADE')
  41. table.timestamp('login_time').defaultTo(api.fn.now())
  42. table.string('ip_address')
  43. table.text('user_agent')
  44. table.decimal('longitude', 10, 6).nullable() // 经度
  45. table.decimal('latitude', 10, 6).nullable() // 纬度
  46. table.string('location_name').nullable() // 地点名称
  47. // 添加索引
  48. table.index('user_id');
  49. table.index('login_time');
  50. // table.index(['longitude', 'latitude']);
  51. })
  52. },
  53. down: async (api) => {
  54. await api.schema.dropTable('login_history')
  55. }
  56. }
  57. // 定义知识库文章表迁移
  58. const createKnowInfoTable: MigrationLiveDefinition = {
  59. name: "create_know_info_table",
  60. up: async (api) => {
  61. await api.schema.createTable('know_info', (table) => {
  62. table.increments('id').primary();
  63. table.string('title').comment('文章标题');
  64. table.string('tags').comment('文章标签');
  65. table.text('content').comment('文章内容');
  66. table.string('author').comment('作者');
  67. table.string('category').comment('分类');
  68. table.string('cover_url').comment('封面图片URL');
  69. table.integer('audit_status').defaultTo(AuditStatus.PENDING).comment('审核状态');
  70. table.integer('sort_order').defaultTo(0).comment('排序权重');
  71. table.integer('is_deleted').defaultTo(0).comment('是否被删除 (0否 1是)');
  72. table.timestamps(true, true);
  73. // 添加索引
  74. table.index('title');
  75. table.index('tags');
  76. table.index('author');
  77. table.index('category');
  78. table.index('audit_status');
  79. table.index('sort_order');
  80. table.index('is_deleted');
  81. });
  82. },
  83. down: async (api) => {
  84. await api.schema.dropTable('know_info');
  85. }
  86. };
  87. // 定义文件分类表迁移
  88. const createFileCategoryTable: MigrationLiveDefinition = {
  89. name: "create_file_category_table",
  90. up: async (api) => {
  91. await api.schema.createTable('file_categories', (table) => {
  92. table.increments('id').primary();
  93. table.string('name').notNullable().comment('分类名称');
  94. table.string('code').notNullable().unique().comment('分类编码');
  95. table.text('description').comment('分类描述');
  96. table.integer('is_deleted').defaultTo(DeleteStatus.NOT_DELETED).comment('是否被删除 (0否 1是)');
  97. table.timestamps(true, true);
  98. // 添加索引
  99. table.index('name');
  100. table.index('code');
  101. table.index('is_deleted');
  102. });
  103. },
  104. down: async (api) => {
  105. await api.schema.dropTable('file_categories');
  106. }
  107. };
  108. // 定义文件库表迁移
  109. const createFileLibraryTable: MigrationLiveDefinition = {
  110. name: "create_file_library_table",
  111. up: async (api) => {
  112. await api.schema.createTable('file_library', (table) => {
  113. table.increments('id').primary();
  114. table.string('file_name').notNullable().comment('文件名称');
  115. table.string('original_filename').comment('原始文件名');
  116. table.string('file_path').notNullable().comment('文件路径');
  117. table.string('file_type').comment('文件类型');
  118. table.integer('file_size').unsigned().comment('文件大小(字节)');
  119. table.integer('uploader_id').unsigned().references('id').inTable('users').onDelete('SET NULL').comment('上传用户ID');
  120. table.string('uploader_name').comment('上传者名称');
  121. table.integer('category_id').unsigned().references('id').inTable('file_categories').onDelete('SET NULL').comment('文件分类');
  122. table.string('tags').comment('文件标签');
  123. table.text('description').comment('文件描述');
  124. table.integer('download_count').defaultTo(0).comment('下载次数');
  125. table.integer('is_disabled').defaultTo(EnableStatus.DISABLED).comment('是否禁用 (0否 1是)');
  126. table.integer('is_deleted').defaultTo(DeleteStatus.NOT_DELETED).comment('是否被删除 (0否 1是)');
  127. table.timestamps(true, true);
  128. // 添加索引
  129. table.index('file_name');
  130. table.index('file_type');
  131. table.index('category_id');
  132. table.index('uploader_id');
  133. table.index('is_deleted');
  134. });
  135. },
  136. down: async (api) => {
  137. await api.schema.dropTable('file_library');
  138. }
  139. };
  140. // 定义主题设置表迁移
  141. const createThemeSettingsTable: MigrationLiveDefinition = {
  142. name: "create_theme_settings_table",
  143. up: async (api) => {
  144. await api.schema.createTable('theme_settings', (table) => {
  145. table.increments('id').primary();
  146. table.integer('user_id').unsigned().references('id').inTable('users').onDelete('CASCADE');
  147. table.jsonb('settings').comment('主题设置');
  148. table.timestamps(true, true);
  149. // 添加索引
  150. table.index('user_id');
  151. });
  152. },
  153. down: async (api) => {
  154. await api.schema.dropTable('theme_settings');
  155. }
  156. };
  157. // 定义系统设置表迁移
  158. const createSystemSettingsTable: MigrationLiveDefinition = {
  159. name: "create_system_settings_table",
  160. up: async (api) => {
  161. await api.schema.createTable('system_settings', (table) => {
  162. table.increments('id').primary();
  163. table.string('key').notNullable().unique().comment('设置键');
  164. table.text('value').notNullable().comment('设置值');
  165. table.string('description').nullable().comment('设置描述');
  166. table.string('group').notNullable().comment('设置分组');
  167. table.timestamps(true, true);
  168. // 添加索引
  169. table.index('key');
  170. table.index('group');
  171. });
  172. },
  173. down: async (api) => {
  174. await api.schema.dropTable('system_settings');
  175. }
  176. };
  177. // 初始测试数据迁移
  178. const seedInitialData: MigrationLiveDefinition = {
  179. name: "seed_initial_data",
  180. up: async (api) => {
  181. // 1. 添加默认用户
  182. const defaultUser = {
  183. username: 'admin',
  184. password: 'admin123', // 实际应用中应使用加密后的密码
  185. email: 'admin@example.com',
  186. nickname: '系统管理员',
  187. name: '管理员',
  188. is_disabled: EnableStatus.ENABLED,
  189. is_deleted: DeleteStatus.NOT_DELETED
  190. };
  191. const [userId] = await api.table('users').insert(defaultUser);
  192. // 2. 添加默认主题设置
  193. await api.table('theme_settings').insert({
  194. user_id: userId,
  195. settings: {
  196. theme_mode: ThemeMode.LIGHT,
  197. primary_color: '#1890ff',
  198. font_size: FontSize.MEDIUM,
  199. is_compact: CompactMode.NORMAL
  200. },
  201. created_at: api.fn.now(),
  202. updated_at: api.fn.now()
  203. });
  204. // 3. 添加知识库文章示例
  205. await api.table('know_info').insert([
  206. {
  207. title: '欢迎使用应用Starter',
  208. tags: 'starter,指南',
  209. content: '# 欢迎使用应用Starter\n\n这是一个基础的应用Starter,提供了用户认证、文件管理、知识库、主题管理等功能。\n\n## 主要功能\n\n- 用户认证与管理\n- 文件上传与管理\n- 知识库文章管理\n- 主题设置(暗黑模式/明亮模式)\n- 图表数据统计\n- 地图集成\n\n更多功能请参考文档...',
  210. author: '系统管理员',
  211. category: '使用指南',
  212. audit_status: AuditStatus.APPROVED,
  213. is_deleted: DeleteStatus.NOT_DELETED
  214. },
  215. {
  216. title: '如何使用文件管理',
  217. tags: '文件,上传,管理',
  218. content: '# 文件管理使用指南\n\n文件管理模块可以帮助您上传、分类和管理各种文件。\n\n## 上传文件\n\n1. 点击"上传文件"按钮\n2. 选择要上传的文件\n3. 填写文件信息(分类、标签等)\n4. 点击"确定"完成上传\n\n## 文件分类\n\n您可以创建自定义的文件分类,方便管理不同类型的文件...',
  219. author: '系统管理员',
  220. category: '使用指南',
  221. audit_status: AuditStatus.APPROVED,
  222. is_deleted: DeleteStatus.NOT_DELETED
  223. },
  224. {
  225. title: '主题设置指南',
  226. tags: '主题,设置,外观',
  227. content: '# 主题设置指南\n\n主题设置允许您自定义应用的外观和感觉,包括颜色模式、字体大小等。\n\n## 颜色模式\n\n您可以选择明亮模式或暗黑模式,适应不同的工作环境和个人偏好。\n\n## 主题颜色\n\n可以选择主题的主色调,系统会根据选择自动生成配色方案。\n\n## 字体大小\n\n提供小、中、大三种字体大小选项,满足不同用户的阅读需求。',
  228. author: '系统管理员',
  229. category: '使用指南',
  230. audit_status: AuditStatus.APPROVED,
  231. is_deleted: DeleteStatus.NOT_DELETED
  232. },
  233. {
  234. title: '数据分析功能介绍',
  235. tags: '分析,图表,数据',
  236. content: '# 数据分析功能介绍\n\n数据分析模块提供了多种图表和可视化工具,帮助您理解和分析数据。\n\n## 图表类型\n\n支持柱状图、折线图、饼图等多种图表类型,适用于不同的数据展示需求。\n\n## 数据筛选\n\n可以根据时间范围、数据类型等条件筛选数据,获得更精确的分析结果。',
  237. author: '系统管理员',
  238. category: '使用指南',
  239. audit_status: AuditStatus.APPROVED,
  240. is_deleted: DeleteStatus.NOT_DELETED
  241. }
  242. ]);
  243. // 4. 添加文件分类示例
  244. await api.table('file_categories').insert([
  245. {
  246. name: '文档',
  247. code: 'doc',
  248. description: '各类文档文件',
  249. is_deleted: DeleteStatus.NOT_DELETED
  250. },
  251. {
  252. name: '图片',
  253. code: 'image',
  254. description: '各类图片文件',
  255. is_deleted: DeleteStatus.NOT_DELETED
  256. },
  257. {
  258. name: '视频',
  259. code: 'video',
  260. description: '各类视频文件',
  261. is_deleted: DeleteStatus.NOT_DELETED
  262. },
  263. {
  264. name: '音频',
  265. code: 'audio',
  266. description: '各类音频文件',
  267. is_deleted: DeleteStatus.NOT_DELETED
  268. },
  269. {
  270. name: '其他',
  271. code: 'other',
  272. description: '其他类型文件',
  273. is_deleted: DeleteStatus.NOT_DELETED
  274. }
  275. ]);
  276. // 5. 添加系统设置示例
  277. await api.table('system_settings').insert([
  278. {
  279. key: SystemSettingKey.SITE_NAME,
  280. value: '应用管理系统',
  281. description: '站点名称',
  282. group: SystemSettingGroup.BASIC
  283. },
  284. {
  285. key: SystemSettingKey.SITE_DESCRIPTION,
  286. value: '一个功能完善的应用管理系统',
  287. description: '站点描述',
  288. group: SystemSettingGroup.BASIC
  289. },
  290. {
  291. key: SystemSettingKey.SITE_KEYWORDS,
  292. value: '应用,管理,系统',
  293. description: '站点关键词',
  294. group: SystemSettingGroup.BASIC
  295. },
  296. {
  297. key: SystemSettingKey.ENABLE_REGISTER,
  298. value: true,
  299. description: '是否开启注册功能',
  300. group: SystemSettingGroup.FEATURE
  301. },
  302. {
  303. key: SystemSettingKey.ENABLE_CAPTCHA,
  304. value: true,
  305. description: '是否开启验证码',
  306. group: SystemSettingGroup.FEATURE
  307. },
  308. {
  309. key: SystemSettingKey.LOGIN_ATTEMPTS,
  310. value: 5,
  311. description: '允许的登录尝试次数',
  312. group: SystemSettingGroup.FEATURE
  313. },
  314. {
  315. key: SystemSettingKey.SESSION_TIMEOUT,
  316. value: 120,
  317. description: '会话超时时间(分钟)',
  318. group: SystemSettingGroup.FEATURE
  319. },
  320. {
  321. key: SystemSettingKey.UPLOAD_MAX_SIZE,
  322. value: 10,
  323. description: '最大上传大小(MB)',
  324. group: SystemSettingGroup.UPLOAD
  325. },
  326. {
  327. key: SystemSettingKey.ALLOWED_FILE_TYPES,
  328. value: ALLOWED_FILE_TYPES,
  329. description: '允许上传的文件类型',
  330. group: SystemSettingGroup.UPLOAD
  331. },
  332. {
  333. key: SystemSettingKey.IMAGE_COMPRESS,
  334. value: true,
  335. description: '是否压缩图片',
  336. group: SystemSettingGroup.UPLOAD
  337. },
  338. {
  339. key: SystemSettingKey.IMAGE_MAX_WIDTH,
  340. value: 1920,
  341. description: '图片最大宽度',
  342. group: SystemSettingGroup.UPLOAD
  343. },
  344. {
  345. key: SystemSettingKey.NOTIFY_ON_LOGIN,
  346. value: true,
  347. description: '是否开启登录通知',
  348. group: SystemSettingGroup.NOTIFICATION
  349. },
  350. {
  351. key: SystemSettingKey.NOTIFY_ON_UPLOAD,
  352. value: true,
  353. description: '是否开启上传通知',
  354. group: SystemSettingGroup.NOTIFICATION
  355. },
  356. {
  357. key: SystemSettingKey.NOTIFY_ON_ERROR,
  358. value: true,
  359. description: '是否开启错误通知',
  360. group: SystemSettingGroup.NOTIFICATION
  361. }
  362. ]);
  363. },
  364. down: async (api) => {
  365. // 删除初始数据
  366. await api.table('login_history').where('user_id', 1).delete();
  367. await api.table('theme_settings').where('user_id', 1).delete();
  368. await api.table('know_info').delete();
  369. await api.table('file_categories').delete();
  370. await api.table('users').where('username', 'admin').delete();
  371. }
  372. };
  373. // 创建消息表迁移
  374. const createMessagesTable: MigrationLiveDefinition = {
  375. name: "create_messages_table",
  376. up: async (api) => {
  377. await api.schema.createTable('messages', (table) => {
  378. table.increments('id').primary().comment('消息ID');
  379. table.string('title').notNullable().comment('消息标题');
  380. table.text('content').notNullable().comment('消息内容');
  381. table.enum('type', ['system', 'private', 'announce']).notNullable().comment('消息类型');
  382. table.integer('sender_id').unsigned().references('id').inTable('users').onDelete('SET NULL').comment('发送者ID');
  383. table.string('sender_name').comment('发送者名称');
  384. table.timestamps(true, true);
  385. // 添加索引
  386. table.index('type');
  387. table.index('sender_id');
  388. });
  389. },
  390. down: async (api) => {
  391. await api.schema.dropTable('messages');
  392. }
  393. };
  394. // 创建用户消息关联表迁移
  395. const createUserMessagesTable: MigrationLiveDefinition = {
  396. name: "create_user_messages_table",
  397. up: async (api) => {
  398. await api.schema.createTable('user_messages', (table) => {
  399. table.increments('id').primary().comment('关联ID');
  400. table.integer('user_id').unsigned().references('id').inTable('users').onDelete('CASCADE').comment('用户ID');
  401. table.integer('message_id').unsigned().references('id').inTable('messages').onDelete('CASCADE').comment('消息ID');
  402. table.integer('status').defaultTo(0).comment('阅读状态(0=未读,1=已读)');
  403. table.integer('is_deleted').defaultTo(0).comment('删除状态(0=未删除,1=已删除)');
  404. table.timestamp('read_at').nullable().comment('阅读时间');
  405. table.timestamps(true, true);
  406. // 添加复合索引
  407. table.index(['user_id', 'status']);
  408. table.index(['user_id', 'is_deleted']);
  409. table.unique(['user_id', 'message_id']);
  410. });
  411. },
  412. down: async (api) => {
  413. await api.schema.dropTable('user_messages');
  414. }
  415. };
  416. // 导出所有迁移
  417. export const migrations = [
  418. createUsersTable,
  419. createLoginHistoryTable,
  420. createKnowInfoTable,
  421. createFileCategoryTable,
  422. createFileLibraryTable,
  423. createThemeSettingsTable,
  424. createSystemSettingsTable,
  425. createMessagesTable,
  426. createUserMessagesTable,
  427. seedInitialData,
  428. ];