cleanup_tables.sql 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. -- Cleanup 模块数据库表结构
  2. -- 创建时间: 2024-12-16
  3. -- 版本: v1.0.0
  4. -- 1. 清理配置表 (cleanup_configs)
  5. -- 存储每个数据表的基础清理配置信息
  6. CREATE TABLE `kku_cleanup_configs` (
  7. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  8. `table_name` varchar(100) NOT NULL COMMENT '表名',
  9. `module_name` varchar(50) NOT NULL COMMENT '模块名称',
  10. `data_category` tinyint(3) unsigned NOT NULL COMMENT '数据分类:1用户数据,2日志数据,3交易数据,4缓存数据,5配置数据',
  11. `default_cleanup_type` tinyint(3) unsigned NOT NULL COMMENT '默认清理类型:1清空表,2删除所有,3按时间删除,4按用户删除,5按条件删除',
  12. `default_conditions` json DEFAULT NULL COMMENT '默认清理条件JSON配置',
  13. `is_enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否启用清理',
  14. `priority` int(10) unsigned NOT NULL DEFAULT '100' COMMENT '清理优先级(数字越小优先级越高)',
  15. `batch_size` int(10) unsigned NOT NULL DEFAULT '1000' COMMENT '批处理大小',
  16. `description` text COMMENT '配置描述',
  17. `last_cleanup_at` timestamp NULL DEFAULT NULL COMMENT '最后清理时间',
  18. `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  19. `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  20. PRIMARY KEY (`id`),
  21. UNIQUE KEY `idx_table_name` (`table_name`),
  22. KEY `idx_module_category` (`module_name`, `data_category`),
  23. KEY `idx_enabled_priority` (`is_enabled`, `priority`),
  24. KEY `idx_last_cleanup` (`last_cleanup_at`)
  25. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='清理配置表';
  26. -- 2. 清理计划表 (cleanup_plans)
  27. -- 存储清理计划信息,如"农场模块清理"
  28. CREATE TABLE `kku_cleanup_plans` (
  29. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  30. `plan_name` varchar(100) NOT NULL COMMENT '计划名称',
  31. `plan_type` tinyint(3) unsigned NOT NULL COMMENT '计划类型:1全量清理,2模块清理,3分类清理,4自定义清理,5混合清理',
  32. `target_selection` json DEFAULT NULL COMMENT '目标选择配置',
  33. `global_conditions` json DEFAULT NULL COMMENT '全局清理条件',
  34. `backup_config` json DEFAULT NULL COMMENT '备份配置',
  35. `is_template` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否为模板',
  36. `is_enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否启用',
  37. `description` text COMMENT '计划描述',
  38. `created_by` bigint(20) unsigned DEFAULT NULL COMMENT '创建者用户ID',
  39. `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  40. `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  41. PRIMARY KEY (`id`),
  42. UNIQUE KEY `idx_plan_name` (`plan_name`),
  43. KEY `idx_plan_type` (`plan_type`),
  44. KEY `idx_is_template` (`is_template`),
  45. KEY `idx_is_enabled` (`is_enabled`),
  46. KEY `idx_created_by` (`created_by`)
  47. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='清理计划表';
  48. -- 3. 计划内容表 (cleanup_plan_contents)
  49. -- 存储计划的具体内容,即计划具体处理哪些表,怎么清理
  50. CREATE TABLE `kku_cleanup_plan_contents` (
  51. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  52. `plan_id` bigint(20) unsigned NOT NULL COMMENT '计划ID',
  53. `table_name` varchar(100) NOT NULL COMMENT '表名',
  54. `cleanup_type` tinyint(3) unsigned NOT NULL COMMENT '清理类型:1清空表,2删除所有,3按时间删除,4按用户删除,5按条件删除',
  55. `conditions` json DEFAULT NULL COMMENT '清理条件JSON配置',
  56. `priority` int(10) unsigned NOT NULL DEFAULT '100' COMMENT '清理优先级',
  57. `batch_size` int(10) unsigned NOT NULL DEFAULT '1000' COMMENT '批处理大小',
  58. `is_enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否启用',
  59. `backup_enabled` tinyint(1) NOT NULL DEFAULT '1' COMMENT '是否启用备份',
  60. `notes` text COMMENT '备注说明',
  61. `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  62. `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  63. PRIMARY KEY (`id`),
  64. UNIQUE KEY `idx_plan_table` (`plan_id`, `table_name`),
  65. KEY `idx_plan_id` (`plan_id`),
  66. KEY `idx_table_name` (`table_name`),
  67. KEY `idx_priority` (`priority`),
  68. FOREIGN KEY (`plan_id`) REFERENCES `kku_cleanup_plans` (`id`) ON DELETE CASCADE
  69. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='计划内容表';
  70. -- 4. 清理任务表 (cleanup_tasks)
  71. -- 存储清理任务的执行信息和状态,即执行某个计划的具体实例
  72. CREATE TABLE `kku_cleanup_tasks` (
  73. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  74. `task_name` varchar(100) NOT NULL COMMENT '任务名称',
  75. `plan_id` bigint(20) unsigned NOT NULL COMMENT '关联的清理计划ID',
  76. `backup_id` bigint(20) unsigned DEFAULT NULL COMMENT '关联的备份ID',
  77. `status` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '任务状态:1待执行,2备份中,3执行中,4已完成,5已失败,6已取消,7已暂停',
  78. `progress` decimal(5,2) NOT NULL DEFAULT '0.00' COMMENT '执行进度百分比',
  79. `current_step` varchar(50) DEFAULT NULL COMMENT '当前执行步骤',
  80. `total_tables` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '总表数',
  81. `processed_tables` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '已处理表数',
  82. `total_records` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '总记录数',
  83. `deleted_records` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '已删除记录数',
  84. `backup_size` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '备份文件大小(字节)',
  85. `execution_time` decimal(10,3) NOT NULL DEFAULT '0.000' COMMENT '执行时间(秒)',
  86. `backup_time` decimal(10,3) NOT NULL DEFAULT '0.000' COMMENT '备份时间(秒)',
  87. `started_at` timestamp NULL DEFAULT NULL COMMENT '开始时间',
  88. `backup_completed_at` timestamp NULL DEFAULT NULL COMMENT '备份完成时间',
  89. `completed_at` timestamp NULL DEFAULT NULL COMMENT '完成时间',
  90. `error_message` text COMMENT '错误信息',
  91. `created_by` bigint(20) unsigned DEFAULT NULL COMMENT '创建者用户ID',
  92. `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  93. `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  94. PRIMARY KEY (`id`),
  95. KEY `idx_plan_id` (`plan_id`),
  96. KEY `idx_backup_id` (`backup_id`),
  97. KEY `idx_status` (`status`),
  98. KEY `idx_created_by` (`created_by`),
  99. KEY `idx_created_at` (`created_at`),
  100. FOREIGN KEY (`plan_id`) REFERENCES `kku_cleanup_plans` (`id`) ON DELETE CASCADE
  101. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='清理任务表';
  102. -- 5. 备份记录表 (cleanup_backups)
  103. -- 存储计划的备份方案和备份内容
  104. CREATE TABLE `kku_cleanup_backups` (
  105. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  106. `plan_id` bigint(20) unsigned NOT NULL COMMENT '关联的清理计划ID',
  107. `task_id` bigint(20) unsigned DEFAULT NULL COMMENT '关联的清理任务ID(如果是任务触发的备份)',
  108. `backup_name` varchar(100) NOT NULL COMMENT '备份名称',
  109. `backup_type` tinyint(3) unsigned NOT NULL COMMENT '备份类型:1SQL,2JSON,3CSV',
  110. `compression_type` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '压缩类型:1none,2gzip,3zip',
  111. `backup_path` varchar(500) NOT NULL COMMENT '备份文件路径',
  112. `backup_size` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '备份文件大小(字节)',
  113. `original_size` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '原始数据大小(字节)',
  114. `tables_count` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '备份表数量',
  115. `records_count` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '备份记录数量',
  116. `backup_status` tinyint(3) unsigned NOT NULL DEFAULT '1' COMMENT '备份状态:1进行中,2已完成,3已失败',
  117. `backup_hash` varchar(64) DEFAULT NULL COMMENT '备份文件MD5哈希',
  118. `backup_config` json DEFAULT NULL COMMENT '备份配置信息',
  119. `started_at` timestamp NULL DEFAULT NULL COMMENT '备份开始时间',
  120. `completed_at` timestamp NULL DEFAULT NULL COMMENT '备份完成时间',
  121. `expires_at` timestamp NULL DEFAULT NULL COMMENT '备份过期时间',
  122. `error_message` text COMMENT '错误信息',
  123. `created_by` bigint(20) unsigned DEFAULT NULL COMMENT '创建者用户ID',
  124. `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  125. `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  126. PRIMARY KEY (`id`),
  127. KEY `idx_plan_id` (`plan_id`),
  128. KEY `idx_task_id` (`task_id`),
  129. KEY `idx_backup_status` (`backup_status`),
  130. KEY `idx_expires_at` (`expires_at`),
  131. KEY `idx_created_at` (`created_at`),
  132. FOREIGN KEY (`plan_id`) REFERENCES `kku_cleanup_plans` (`id`) ON DELETE CASCADE
  133. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='备份记录表';
  134. -- 6. 备份文件表 (cleanup_backup_files)
  135. -- 存储备份的具体文件信息
  136. CREATE TABLE `kku_cleanup_backup_files` (
  137. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  138. `backup_id` bigint(20) unsigned NOT NULL COMMENT '备份记录ID',
  139. `table_name` varchar(100) NOT NULL COMMENT '表名',
  140. `file_path` varchar(500) NOT NULL COMMENT '文件路径',
  141. `file_size` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '文件大小(字节)',
  142. `records_count` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '记录数量',
  143. `file_hash` varchar(64) DEFAULT NULL COMMENT '文件MD5哈希',
  144. `backup_conditions` json DEFAULT NULL COMMENT '备份条件',
  145. `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  146. PRIMARY KEY (`id`),
  147. KEY `idx_backup_id` (`backup_id`),
  148. KEY `idx_table_name` (`table_name`),
  149. FOREIGN KEY (`backup_id`) REFERENCES `kku_cleanup_backups` (`id`) ON DELETE CASCADE
  150. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='备份文件表';
  151. -- 7. 清理日志表 (cleanup_logs)
  152. -- 记录任务执行的详细日志
  153. CREATE TABLE `kku_cleanup_logs` (
  154. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  155. `task_id` bigint(20) unsigned NOT NULL COMMENT '任务ID',
  156. `table_name` varchar(100) NOT NULL COMMENT '表名',
  157. `operation_type` varchar(20) NOT NULL COMMENT '操作类型:BACKUP,TRUNCATE,DELETE,COUNT',
  158. `records_before` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '操作前记录数',
  159. `records_after` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '操作后记录数',
  160. `records_affected` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '影响记录数',
  161. `execution_time` decimal(8,3) NOT NULL DEFAULT '0.000' COMMENT '执行时间(秒)',
  162. `conditions_used` json DEFAULT NULL COMMENT '使用的清理条件',
  163. `sql_statement` text COMMENT '执行的SQL语句',
  164. `status` tinyint(3) unsigned NOT NULL COMMENT '执行状态:1成功,2失败,3跳过',
  165. `error_message` text COMMENT '错误信息',
  166. `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  167. PRIMARY KEY (`id`),
  168. KEY `idx_task_id` (`task_id`),
  169. KEY `idx_table_name` (`table_name`),
  170. KEY `idx_status` (`status`),
  171. KEY `idx_created_at` (`created_at`),
  172. KEY `idx_operation_type` (`operation_type`),
  173. FOREIGN KEY (`task_id`) REFERENCES `kku_cleanup_tasks` (`id`) ON DELETE CASCADE
  174. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='清理日志表';
  175. -- 8. 表统计信息表 (cleanup_table_stats)
  176. -- 存储数据表的统计信息,用于清理决策
  177. CREATE TABLE `kku_cleanup_table_stats` (
  178. `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
  179. `table_name` varchar(100) NOT NULL COMMENT '表名',
  180. `record_count` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '记录总数',
  181. `table_size_mb` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '表大小(MB)',
  182. `index_size_mb` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '索引大小(MB)',
  183. `data_free_mb` decimal(10,2) NOT NULL DEFAULT '0.00' COMMENT '碎片空间(MB)',
  184. `avg_row_length` int(10) unsigned NOT NULL DEFAULT '0' COMMENT '平均行长度',
  185. `auto_increment` bigint(20) unsigned DEFAULT NULL COMMENT '自增值',
  186. `oldest_record_time` timestamp NULL DEFAULT NULL COMMENT '最早记录时间',
  187. `newest_record_time` timestamp NULL DEFAULT NULL COMMENT '最新记录时间',
  188. `scan_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '扫描时间',
  189. `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  190. `updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  191. PRIMARY KEY (`id`),
  192. UNIQUE KEY `idx_table_scan` (`table_name`, `scan_time`),
  193. KEY `idx_table_name` (`table_name`),
  194. KEY `idx_record_count` (`record_count`),
  195. KEY `idx_table_size` (`table_size_mb`),
  196. KEY `idx_scan_time` (`scan_time`)
  197. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='表统计信息表';