From d76623f59d767a3e67f46c4a27d75b89f09da9fe Mon Sep 17 00:00:00 2001 From: Glore Zeng Date: Mon, 19 Oct 2026 22:47:31 +0800 Subject: [PATCH] feat: migrate course content start time and series price --- 1792400000_course_details.down.sql | 2 ++ 1792400000_course_details.up.sql | 2 ++ README.md | 2 ++ scripts/check-migrations.mjs | 8 +++++--- 4 files changed, 11 insertions(+), 3 deletions(-) create mode 100644 1792400000_course_details.down.sql create mode 100644 1792400000_course_details.up.sql diff --git a/1792400000_course_details.down.sql b/1792400000_course_details.down.sql new file mode 100644 index 0000000..c84b6c6 --- /dev/null +++ b/1792400000_course_details.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE course DROP COLUMN start_time, DROP COLUMN content_html; +ALTER TABLE course_series DROP COLUMN price, DROP COLUMN start_time, DROP COLUMN content_html; diff --git a/1792400000_course_details.up.sql b/1792400000_course_details.up.sql new file mode 100644 index 0000000..c9d376e --- /dev/null +++ b/1792400000_course_details.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE course ADD COLUMN start_time DATETIME NULL, ADD COLUMN content_html LONGTEXT NULL; +ALTER TABLE course_series ADD COLUMN price INT NULL COMMENT 'Independent series price in cents; NULL means not configured', ADD COLUMN start_time DATETIME NULL, ADD COLUMN content_html LONGTEXT NULL; diff --git a/README.md b/README.md index 9460dbf..2054e68 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,5 @@ 在上级工作区运行 `./scripts/workspace.ps1 migrate`,使用隔离的本地 MySQL 和 golang-migrate 记录版本。已有数据库须先核对 schema_migrations 与实际结构;禁止盲目 force。已执行的 SQL 不修改,新变更新增时间戳文件。 `node scripts/check-migrations.mjs` 仅检查命名、版本唯一性和非空。SQL 是否可执行须在脱敏基线数据库验证。现有迁移无 down 脚本,生产回退依赖经验证的备份恢复或补偿迁移,不自动回滚。 + +新增 `1792400000_course_details.up.sql`:为 course 和 course_series 增加 start_time、content_html,系列增加可空独立售价 price(分)。先迁移,再部署对应 API。该版本包含 down 脚本,执行会丢失这些新字段中的数据,必须先备份。历史版本仍无 down 脚本。已在 MySQL 8 临时表验证此版本 up/down;生产完整基线仍需部署前验证。 diff --git a/scripts/check-migrations.mjs b/scripts/check-migrations.mjs index 666a36b..97c9268 100644 --- a/scripts/check-migrations.mjs +++ b/scripts/check-migrations.mjs @@ -4,10 +4,12 @@ const files = readdirSync('.').filter(f => f.endsWith('.sql')).sort(); assert(files.length > 0, 'No migrations'); const versions = new Set(); for (const file of files) { - const match = /^(\d+)_[a-z0-9_]+\.up\.sql$/.exec(file); + const match = /^(\d+)_([a-z0-9_]+)\.(up|down)\.sql$/.exec(file); assert(match, `Invalid migration filename: ${file}`); - assert(!versions.has(match[1]), `Duplicate version: ${file}`); - versions.add(match[1]); + const key = `${match[1]}:${match[3]}`; + assert(!versions.has(key), `Duplicate version and direction: ${file}`); + versions.add(key); + if (match[3] === 'down') assert(files.includes(`${match[1]}_${match[2]}.up.sql`), `Missing matching up migration: ${file}`); assert(readFileSync(file, 'utf8').trim(), `Empty migration: ${file}`); } console.log(`${files.length} migration files checked (structure only; MySQL execution still required).`);