MarketingSystemDataTool/server/internal/migrate/migrate.go

92 lines
7.3 KiB
Go

package migrate
import (
"database/sql"
)
func Apply(db *sql.DB) error {
stmts := []string{
"CREATE TABLE IF NOT EXISTS export_templates (id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, datasource VARCHAR(32) NOT NULL, main_table VARCHAR(64) NOT NULL, joins_json JSON, fields_json JSON, filters_json JSON, file_format VARCHAR(16) NOT NULL, stats_enabled TINYINT(1) NOT NULL DEFAULT 0, sheet_split_by VARCHAR(64), visibility VARCHAR(16) NOT NULL DEFAULT 'private', owner_id BIGINT UNSIGNED NOT NULL, enabled TINYINT(1) NOT NULL DEFAULT 1, explain_json JSON, explain_score INT, last_validated_at DATETIME, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP)",
"CREATE TABLE IF NOT EXISTS export_jobs (id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, template_id BIGINT UNSIGNED NOT NULL, status VARCHAR(16) NOT NULL, requested_by BIGINT UNSIGNED NOT NULL, permission_scope_json JSON, options_json JSON, row_estimate BIGINT, total_rows BIGINT, file_format VARCHAR(16) NOT NULL, started_at DATETIME, finished_at DATETIME, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, INDEX idx_template_id (template_id), INDEX idx_status (status), INDEX idx_requested_by (requested_by))",
"CREATE TABLE IF NOT EXISTS export_job_files (id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, job_id BIGINT UNSIGNED NOT NULL, storage_uri VARCHAR(1024) NOT NULL, sheet_name VARCHAR(255), row_count BIGINT, size_bytes BIGINT, checksum VARCHAR(128), created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, INDEX idx_job_id (job_id))",
"CREATE TABLE IF NOT EXISTS export_audits (id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, actor_id BIGINT UNSIGNED NOT NULL, action VARCHAR(64) NOT NULL, entity_type VARCHAR(64) NOT NULL, entity_id BIGINT UNSIGNED NOT NULL, detail_json JSON, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, INDEX idx_entity (entity_type, entity_id), INDEX idx_actor (actor_id))",
}
for _, s := range stmts {
if _, err := db.Exec(s); err != nil {
return err
}
}
comments := []string{
"ALTER TABLE export_templates COMMENT='导出模板主表'",
"ALTER TABLE export_templates MODIFY id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID'",
"ALTER TABLE export_templates MODIFY name VARCHAR(255) NOT NULL COMMENT '模板名称'",
"ALTER TABLE export_templates MODIFY datasource VARCHAR(32) NOT NULL COMMENT '数据源标识'",
"ALTER TABLE export_templates MODIFY main_table VARCHAR(64) NOT NULL COMMENT '主表名'",
"ALTER TABLE export_templates MODIFY joins_json JSON COMMENT '关联表定义'",
"ALTER TABLE export_templates MODIFY fields_json JSON COMMENT '字段选择'",
"ALTER TABLE export_templates MODIFY filters_json JSON COMMENT '过滤条件'",
"ALTER TABLE export_templates MODIFY file_format VARCHAR(16) NOT NULL COMMENT '文件格式'",
"ALTER TABLE export_templates MODIFY stats_enabled TINYINT(1) NOT NULL COMMENT '统计开关'",
"ALTER TABLE export_templates MODIFY sheet_split_by VARCHAR(64) COMMENT '多sheet拆分条件'",
"ALTER TABLE export_templates MODIFY visibility VARCHAR(16) NOT NULL COMMENT '可见性'",
"ALTER TABLE export_templates MODIFY owner_id BIGINT UNSIGNED NOT NULL COMMENT '所有者ID'",
"ALTER TABLE export_templates MODIFY enabled TINYINT(1) NOT NULL COMMENT '启用状态'",
"ALTER TABLE export_templates MODIFY explain_json JSON COMMENT 'EXPLAIN结果'",
"ALTER TABLE export_templates MODIFY explain_score INT COMMENT 'EXPLAIN评分'",
"ALTER TABLE export_templates MODIFY last_validated_at DATETIME COMMENT '最近校验时间'",
"ALTER TABLE export_templates MODIFY created_at DATETIME NOT NULL COMMENT '创建时间'",
"ALTER TABLE export_templates MODIFY updated_at DATETIME NOT NULL COMMENT '更新时间'",
"ALTER TABLE export_jobs COMMENT='导出任务表'",
"ALTER TABLE export_jobs MODIFY id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID'",
"ALTER TABLE export_jobs MODIFY template_id BIGINT UNSIGNED NOT NULL COMMENT '模板ID'",
"ALTER TABLE export_jobs MODIFY status VARCHAR(16) NOT NULL COMMENT '任务状态'",
"ALTER TABLE export_jobs MODIFY requested_by BIGINT UNSIGNED NOT NULL COMMENT '请求人ID'",
"ALTER TABLE export_jobs MODIFY permission_scope_json JSON COMMENT '权限范围'",
"ALTER TABLE export_jobs MODIFY options_json JSON COMMENT '执行选项'",
"ALTER TABLE export_jobs MODIFY row_estimate BIGINT COMMENT '行数估算'",
"ALTER TABLE export_jobs MODIFY total_rows BIGINT COMMENT '实际行数'",
"ALTER TABLE export_jobs MODIFY file_format VARCHAR(16) NOT NULL COMMENT '文件格式'",
"ALTER TABLE export_jobs MODIFY started_at DATETIME COMMENT '开始时间'",
"ALTER TABLE export_jobs MODIFY finished_at DATETIME COMMENT '完成时间'",
"ALTER TABLE export_jobs MODIFY created_at DATETIME NOT NULL COMMENT '创建时间'",
"ALTER TABLE export_jobs MODIFY updated_at DATETIME NOT NULL COMMENT '更新时间'",
"ALTER TABLE export_job_files COMMENT='导出文件记录'",
"ALTER TABLE export_job_files MODIFY id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID'",
"ALTER TABLE export_job_files MODIFY job_id BIGINT UNSIGNED NOT NULL COMMENT '任务ID'",
"ALTER TABLE export_job_files MODIFY storage_uri VARCHAR(1024) NOT NULL COMMENT '存储地址'",
"ALTER TABLE export_job_files MODIFY sheet_name VARCHAR(255) COMMENT 'sheet名称'",
"ALTER TABLE export_job_files MODIFY row_count BIGINT COMMENT '行数'",
"ALTER TABLE export_job_files MODIFY size_bytes BIGINT COMMENT '文件大小'",
"ALTER TABLE export_job_files MODIFY checksum VARCHAR(128) COMMENT '校验值'",
"ALTER TABLE export_job_files MODIFY created_at DATETIME NOT NULL COMMENT '创建时间'",
"ALTER TABLE export_audits COMMENT='审计与变更记录'",
"ALTER TABLE export_audits MODIFY id BIGINT UNSIGNED AUTO_INCREMENT COMMENT '主键ID'",
"ALTER TABLE export_audits MODIFY actor_id BIGINT UNSIGNED NOT NULL COMMENT '操作者ID'",
"ALTER TABLE export_audits MODIFY action VARCHAR(64) NOT NULL COMMENT '动作'",
"ALTER TABLE export_audits MODIFY entity_type VARCHAR(64) NOT NULL COMMENT '对象类型'",
"ALTER TABLE export_audits MODIFY entity_id BIGINT UNSIGNED NOT NULL COMMENT '对象ID'",
"ALTER TABLE export_audits MODIFY detail_json JSON COMMENT '详情'",
"ALTER TABLE export_audits MODIFY created_at DATETIME NOT NULL COMMENT '创建时间'",
}
for _, s := range comments {
if _, err := db.Exec(s); err != nil {
return err
}
}
optional := []string{
"ALTER TABLE export_jobs ADD COLUMN explain_json JSON",
"ALTER TABLE export_jobs ADD COLUMN explain_score INT",
"ALTER TABLE export_jobs ADD COLUMN filters_json JSON",
}
for _, s := range optional {
if _, err := db.Exec(s); err != nil {
// ignore if column exists or syntax not supported
continue
}
}
return nil
}