表级清理配置设计.md 13 KB

表级清理配置设计

1. 设计理念

每个表都可以制定不同的清理方式,提供最大的灵活性和精确控制。不同的表根据其数据特征和业务需求,可以采用不同的清理策略。

2. 清理类型详解

2.1 清空表 (TRUNCATE - 1)

适用场景:临时数据、缓存数据、测试数据 执行方式TRUNCATE TABLE table_name 特点

  • 最快的清理方式
  • 重置自增ID
  • 不能回滚
  • 不触发触发器

配置示例

{
  "table_name": "kku_cache",
  "cleanup_type": 1,
  "conditions": null,
  "priority": 100,
  "backup_enabled": false,
  "notes": "缓存表直接清空,无需备份"
}

2.2 删除所有记录 (DELETE_ALL - 2)

适用场景:需要保留表结构和自增ID的场景 执行方式DELETE FROM table_name 特点

  • 保留自增ID当前值
  • 可以回滚
  • 触发触发器
  • 比TRUNCATE慢

配置示例

{
  "table_name": "kku_sessions",
  "cleanup_type": 2,
  "conditions": null,
  "priority": 200,
  "backup_enabled": false,
  "notes": "会话表删除所有记录,保留自增ID"
}

2.3 按时间删除 (DELETE_BY_TIME - 3)

适用场景:有时间字段的表,需要保留最近数据 执行方式DELETE FROM table_name WHERE time_field < 'date' 特点

  • 保留最近的数据
  • 可以设置保留天数
  • 支持多种时间格式

配置示例

{
  "table_name": "kku_farm_logs",
  "cleanup_type": 3,
  "conditions": {
    "time_field": "created_at",
    "before": "30_days_ago"
  },
  "priority": 300,
  "backup_enabled": true,
  "notes": "保留30天内的农场日志"
}

时间条件支持

// 绝对时间
"before": "2024-01-01 00:00:00"

// 相对时间
"before": "30_days_ago"
"before": "7_days_ago" 
"before": "1_month_ago"
"before": "1_year_ago"

// 时间范围
"between": ["2024-01-01", "2024-06-01"]

2.4 按用户删除 (DELETE_BY_USER - 4)

适用场景:用户相关数据,清理特定用户的数据 执行方式DELETE FROM table_name WHERE user_field IN (users) 特点

  • 精确控制用户范围
  • 支持用户ID列表
  • 支持用户条件筛选

配置示例

{
  "table_name": "kku_farm_users",
  "cleanup_type": 4,
  "conditions": {
    "user_field": "user_id",
    "user_condition": "in",
    "user_values": [1001, 1002, 1003]
  },
  "priority": 150,
  "backup_enabled": true,
  "notes": "清理指定测试用户的农场数据"
}

用户条件支持

// 指定用户列表
{
  "user_field": "user_id",
  "user_condition": "in",
  "user_values": [1001, 1002, 1003]
}

// 用户范围
{
  "user_field": "user_id", 
  "user_condition": "between",
  "user_range": [1000, 2000]
}

// 排除用户
{
  "user_field": "user_id",
  "user_condition": "not_in", 
  "user_values": [1, 2, 3]  // 排除管理员用户
}

2.5 按条件删除 (DELETE_BY_CONDITION - 5)

适用场景:复杂的删除条件,自定义SQL条件 执行方式DELETE FROM table_name WHERE custom_conditions 特点

  • 最大灵活性
  • 支持复杂SQL条件
  • 可以组合多个条件

配置示例

{
  "table_name": "kku_item_users",
  "cleanup_type": 5,
  "conditions": {
    "logic": "AND",
    "conditions": [
      {
        "field": "status",
        "operator": "=",
        "value": 0
      },
      {
        "field": "created_at",
        "operator": "<",
        "value": "2024-01-01"
      },
      {
        "field": "item_count",
        "operator": "<=",
        "value": 0
      }
    ]
  },
  "priority": 250,
  "backup_enabled": true,
  "notes": "清理无效的物品记录"
}

条件语法支持

// 简单条件
{
  "field": "status",
  "operator": "=",
  "value": 0
}

// 范围条件
{
  "field": "level",
  "operator": "between", 
  "value": [1, 10]
}

// 列表条件
{
  "field": "type",
  "operator": "in",
  "value": [1, 2, 3]
}

// 复合条件
{
  "logic": "AND",
  "conditions": [
    {"field": "status", "operator": "=", "value": 0},
    {"field": "created_at", "operator": "<", "value": "2024-01-01"}
  ]
}

3. 表级配置管理

3.1 配置继承机制

基础配置 (cleanup_configs) 
    ↓ 提供默认值
计划配置 (cleanup_plan_contents)
    ↓ 可以覆盖默认值
任务执行 (cleanup_tasks)

3.2 配置优先级

  1. 计划内容配置 - 最高优先级
  2. 全局条件配置 - 中等优先级
  3. 基础表配置 - 默认配置

3.3 配置验证

class CleanupConfigValidator
{
    /**
     * 验证表级清理配置
     */
    public function validateTableConfig(array $config): array
    {
        $errors = [];
        
        // 验证清理类型
        if (!in_array($config['cleanup_type'], [1, 2, 3, 4, 5])) {
            $errors[] = '无效的清理类型';
        }
        
        // 验证时间条件
        if ($config['cleanup_type'] == 3) {
            $errors = array_merge($errors, $this->validateTimeConditions($config['conditions']));
        }
        
        // 验证用户条件
        if ($config['cleanup_type'] == 4) {
            $errors = array_merge($errors, $this->validateUserConditions($config['conditions']));
        }
        
        // 验证自定义条件
        if ($config['cleanup_type'] == 5) {
            $errors = array_merge($errors, $this->validateCustomConditions($config['conditions']));
        }
        
        return $errors;
    }
}

4. 后台管理界面

4.1 表配置编辑器

<div class="table-config-editor">
    <h3>表:{{ table_name }}</h3>
    
    <!-- 清理类型选择 -->
    <div class="form-group">
        <label>清理类型:</label>
        <select name="cleanup_type" v-model="config.cleanup_type" @change="onCleanupTypeChange">
            <option value="1">清空表 (TRUNCATE)</option>
            <option value="2">删除所有记录 (DELETE ALL)</option>
            <option value="3">按时间删除</option>
            <option value="4">按用户删除</option>
            <option value="5">按条件删除</option>
        </select>
    </div>
    
    <!-- 时间条件配置 -->
    <div v-if="config.cleanup_type == 3" class="conditions-panel">
        <h4>时间条件配置</h4>
        <div class="form-group">
            <label>时间字段:</label>
            <select name="time_field" v-model="config.conditions.time_field">
                <option value="created_at">创建时间</option>
                <option value="updated_at">更新时间</option>
                <option value="last_login_at">最后登录时间</option>
            </select>
        </div>
        <div class="form-group">
            <label>删除条件:</label>
            <select name="time_condition" v-model="config.conditions.before">
                <option value="7_days_ago">7天前</option>
                <option value="30_days_ago">30天前</option>
                <option value="90_days_ago">90天前</option>
                <option value="1_year_ago">1年前</option>
                <option value="custom">自定义时间</option>
            </select>
        </div>
    </div>
    
    <!-- 用户条件配置 -->
    <div v-if="config.cleanup_type == 4" class="conditions-panel">
        <h4>用户条件配置</h4>
        <div class="form-group">
            <label>用户字段:</label>
            <select name="user_field" v-model="config.conditions.user_field">
                <option value="user_id">用户ID</option>
                <option value="urs_user_id">URS用户ID</option>
            </select>
        </div>
        <div class="form-group">
            <label>条件类型:</label>
            <select name="user_condition" v-model="config.conditions.user_condition">
                <option value="in">包含指定用户</option>
                <option value="not_in">排除指定用户</option>
                <option value="between">用户ID范围</option>
            </select>
        </div>
        <div class="form-group">
            <label>用户列表:</label>
            <textarea name="user_values" v-model="userValuesText" placeholder="用户ID列表,用逗号分隔"></textarea>
        </div>
    </div>
    
    <!-- 自定义条件配置 -->
    <div v-if="config.cleanup_type == 5" class="conditions-panel">
        <h4>自定义条件配置</h4>
        <div class="condition-builder">
            <!-- 条件构建器组件 -->
        </div>
    </div>
    
    <!-- 其他配置 -->
    <div class="form-group">
        <label>优先级:</label>
        <input type="number" name="priority" v-model="config.priority" min="1" max="999">
        <small>数字越小优先级越高</small>
    </div>
    
    <div class="form-group">
        <label>
            <input type="checkbox" name="backup_enabled" v-model="config.backup_enabled">
            启用备份
        </label>
    </div>
    
    <div class="form-group">
        <label>备注说明:</label>
        <textarea name="notes" v-model="config.notes" placeholder="清理说明和注意事项"></textarea>
    </div>
</div>

4.2 批量配置功能

class BatchConfigManager {
    /**
     * 批量应用配置
     */
    applyBatchConfig(tables, configTemplate) {
        tables.forEach(table => {
            // 应用模板配置
            const config = { ...configTemplate };
            
            // 根据表特征调整配置
            if (table.name.includes('_logs')) {
                config.cleanup_type = 3; // 日志表按时间删除
                config.conditions = {
                    time_field: 'created_at',
                    before: '30_days_ago'
                };
                config.backup_enabled = false;
            } else if (table.name.includes('_cache')) {
                config.cleanup_type = 1; // 缓存表直接清空
                config.backup_enabled = false;
            }
            
            this.updateTableConfig(table.name, config);
        });
    }
}

5. 使用示例

5.1 创建多样化清理计划

// 创建计划
$planData = [
    'plan_name' => '多样化清理计划',
    'plan_type' => 4,
    'target_selection' => [
        'selection_type' => 'custom',
        'tables' => [
            'kku_farm_users',
            'kku_item_users',
            'kku_pet_logs',
            'kku_cache',
            'kku_sessions'
        ]
    ]
];

// 配置每个表的清理方式
$planContents = [
    [
        'table_name' => 'kku_farm_users',
        'cleanup_type' => 3, // 按时间删除
        'conditions' => [
            'time_field' => 'created_at',
            'before' => '90_days_ago'
        ],
        'priority' => 100,
        'backup_enabled' => true,
        'notes' => '保留90天内的农场用户数据'
    ],
    [
        'table_name' => 'kku_item_users', 
        'cleanup_type' => 4, // 按用户删除
        'conditions' => [
            'user_field' => 'user_id',
            'user_condition' => 'in',
            'user_values' => [1001, 1002, 1003]
        ],
        'priority' => 150,
        'backup_enabled' => true,
        'notes' => '清理指定测试用户的物品数据'
    ],
    [
        'table_name' => 'kku_pet_logs',
        'cleanup_type' => 3, // 按时间删除
        'conditions' => [
            'time_field' => 'created_at',
            'before' => '7_days_ago'
        ],
        'priority' => 300,
        'backup_enabled' => false,
        'notes' => '只保留7天内的宠物日志'
    ],
    [
        'table_name' => 'kku_cache',
        'cleanup_type' => 1, // 清空表
        'conditions' => null,
        'priority' => 400,
        'backup_enabled' => false,
        'notes' => '缓存表直接清空'
    ],
    [
        'table_name' => 'kku_sessions',
        'cleanup_type' => 2, // 删除所有记录
        'conditions' => null,
        'priority' => 500,
        'backup_enabled' => false,
        'notes' => '清理所有会话,保留自增ID'
    ]
];

5.2 命令行配置

# 创建计划后,配置每个表的清理方式
php artisan cleanup:config-table kku_farm_users \
    --type=3 \
    --time-field=created_at \
    --before="90_days_ago" \
    --backup \
    --priority=100

php artisan cleanup:config-table kku_item_users \
    --type=4 \
    --user-field=user_id \
    --users=1001,1002,1003 \
    --backup \
    --priority=150

php artisan cleanup:config-table kku_cache \
    --type=1 \
    --no-backup \
    --priority=400

6. 总结

通过表级清理配置设计,每个表都可以:

  • 独立配置:每个表有独立的清理策略
  • 多种方式:支持5种不同的清理类型
  • 灵活条件:支持时间、用户、自定义条件
  • 优先级控制:可以控制清理执行顺序
  • 备份选择:每个表可以独立选择是否备份
  • 详细说明:每个配置都可以添加说明备注

这种设计提供了最大的灵活性,让用户可以根据每个表的特点和需求,制定最合适的清理策略。