-- 安全的表名规范化脚本
-- 只处理存在的表

USE tg_manage;

-- 禁用外键检查
SET FOREIGN_KEY_CHECKS = 0;

-- 重命名存在的tg_前缀表
RENAME TABLE tg_account_health TO account_health;
RENAME TABLE tg_account_pool TO account_pools;
RENAME TABLE tg_account_usage TO account_usages;
RENAME TABLE tg_account_usage_log TO account_usage_logs;

RENAME TABLE tg_group TO chat_groups;
RENAME TABLE tg_group_listener TO group_listeners;
RENAME TABLE tg_group_marketing_log TO group_marketing_logs;
RENAME TABLE tg_group_muster TO group_musters;
RENAME TABLE tg_group_send_log TO group_send_logs;
RENAME TABLE tg_group_task TO group_tasks;
RENAME TABLE tg_group_user TO group_users;

RENAME TABLE tg_message TO messages;
RENAME TABLE tg_message_muster TO message_musters;

RENAME TABLE tg_script TO scripts;
RENAME TABLE tg_script_article TO script_articles;
RENAME TABLE tg_script_project TO script_projects;
RENAME TABLE tg_script_task TO script_tasks;

RENAME TABLE tg_pull_member_task TO pull_member_tasks;
RENAME TABLE tg_smart_task_execution TO smart_task_executions;
RENAME TABLE tg_smart_group_task TO smart_group_tasks;

RENAME TABLE tg_login_code_log TO login_code_logs;
RENAME TABLE tg_register_log TO register_logs;
RENAME TABLE tg_pull_member_log TO pull_member_logs;
RENAME TABLE tg_join_group_log TO join_group_logs;
RENAME TABLE tg_project_invite_log TO project_invite_logs;

RENAME TABLE tg_pull_member_statistic TO pull_member_statistics;
RENAME TABLE tg_pull_member_project_statistic TO pull_member_project_statistics;

RENAME TABLE tg_config TO configs;
RENAME TABLE tg_dc TO data_centers;
RENAME TABLE tg_exchange TO exchanges;
RENAME TABLE tg_api_data TO api_data;

RENAME TABLE tg_telegram_users TO telegram_users;
RENAME TABLE tg_user TO users;
RENAME TABLE tg_lines TO lines;
RENAME TABLE tg_performer TO performers;

-- 重新启用外键检查
SET FOREIGN_KEY_CHECKS = 1;

SELECT 'Table normalization completed successfully!' as status;