scripts-seed-demo-data-template.mjs
13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/env node
// scripts/seed-demo-data.mjs —— 演示假数据(demo seed)的注入脚本。
//
// 目标库 = 一次性**副本库**(ERP_TEST_DB_SCHEMA / marker / `<source>__test`)——副本由 scripts/setup-test-db.mjs
// 从源库复制而来,已带源库全部数据 + 其 _demo_seed_history 账本,故源库里已灌过的种子在此**自动跳过**,本脚本
// 只把源库还没有的新种子文件灌进副本。绝不写源库。
//
// 用途(四个调用方,时序均为:复制源库→副本 → 起后端让 Flyway apply 本轮新迁移 → 本脚本注入新种子):
// 1) 前端 e2e(Playwright)globalSetup —— e2e 基线 = 源库副本 + 本轮新迁移 + 补充演示种子;
// 2) coding.mjs 行为门 step2.3 —— 行为验收前注入演示数据;
// 3) 里程碑后人工验收 / 演示 —— 手动跑一次即可复现演示态;
// 4) coding.mjs Seed stage —— 模块种子生成后冷起栈真跑验证。
//
// 前提:schema 必须已存在(副本自带源库 schema + Flyway 历史)——本脚本绝不建 schema,只灌数据。
// 幂等机制:已应用的种子文件记入账本表 _demo_seed_history(file 为主键),再次运行自动跳过。
// 主键区间约定:1–999=初始数据(admin_init 等)/ 1000–9999=演示种子 / ≥100000=行为门 sentinel。
// 注意(新模型):副本继承源库真实数据——若源库在 1000–9999 区间已有非种子行,新种子应避开已占主键。
//
// DB 凭据从仓库根 config-vars.yaml 的 database: 段读取;host / user / password 信任该文件,port 仅校验范围。
// 纯 mysql CLI(spawnSync),零 npm 依赖。退出码:0 成功(含全跳过 / 无文件),1 失败。
import { spawnSync } from 'node:child_process'
import { existsSync, readFileSync, readdirSync } from 'node:fs'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
const SCRIPT_DIR = dirname(fileURLToPath(import.meta.url))
const PROJECT_ROOT = join(SCRIPT_DIR, '..')
const CONFIG_FILE = join(PROJECT_ROOT, 'config-vars.yaml')
const SEED_DIR = join(PROJECT_ROOT, 'sql', 'seed')
const LOG = '[seed-demo-data]'
// 极简 YAML 读取(2 层 map + 标量;与 scripts/setup-test-db.mjs 同规则,内联以免运行时依赖)。
function parseScalar(raw) {
let s = String(raw).trim()
if (s === '' || s[0] === '#') return ''
const q = s[0]
if (q === '"' || q === "'") {
const end = s.indexOf(q, 1)
if (end !== -1) return s.slice(1, end)
}
const hash = s.indexOf(' #')
if (hash !== -1) s = s.slice(0, hash).trim()
return s
}
function parseYamlConfig(text) {
const root = {}
let section = null
for (const rawLine of text.split('\n')) {
const line = rawLine.replace(/\r$/, '')
const trimmed = line.trim()
if (trimmed === '' || trimmed[0] === '#') continue
const colon = line.indexOf(':')
if (colon === -1) continue
const key = line.slice(0, colon).trim()
if (key === '') continue
const indent = line.length - line.replace(/^\s+/, '').length
const value = parseScalar(line.slice(colon + 1))
if (indent === 0) {
if (value === '') {
section = {}
root[key] = section
} else {
root[key] = value
section = null
}
} else if (section) {
section[key] = value
} else {
root[key] = value
}
}
return root
}
// 单引号字符串字面量转义(用于喂给 mysql 的 SQL 值,如 table_schema / file 名)。
function quoteSqlString(value) {
return "'" + String(value).replaceAll('\\', '\\\\').replaceAll("'", "''") + "'"
}
// ──────────────────────────────────────────────────────────────────────────
// ① config-vars 校验(占位拒绝 / port 范围 / schema 非空,照抄 setup-test-db 模式)
// 所有本地校验前置于任何 mysql 调用,保证离线可测。
// ──────────────────────────────────────────────────────────────────────────
if (!existsSync(CONFIG_FILE)) {
console.error(`${LOG} config-vars.yaml 不存在(${CONFIG_FILE})`)
process.exit(1)
}
const db = parseYamlConfig(readFileSync(CONFIG_FILE, 'utf8')).database || {}
// lane 兜底 marker:并行波次执行器建好 lane worktree 后在仓库根写 .tmp/erp-lane-db(一行 lane 副本库名)。
// 与 scripts/setup-test-db.mjs 同一约定:目标 = 副本库,取值顺序 env > marker > `<source>__test`——提示词链
// 丢前缀时种子仍确定性灌进 lane 副本,绝不污染源库(COPY≠SOURCE 硬守卫兜底);串行缺省灌 `<source>__test`。
function readLaneDbMarker() {
const markerFile = join(PROJECT_ROOT, '.tmp', 'erp-lane-db')
if (!existsSync(markerFile)) return ''
return readFileSync(markerFile, 'utf8').split('\n')[0].trim()
}
const DB_HOST = db.host ?? ''
const DB_PORT = db.port ?? '3306'
const DB_USER = db.user ?? ''
const DB_PASSWORD = db.password ?? ''
// SOURCE = 源库(只用于派生副本缺省名,绝不被本脚本写入)。
const SOURCE_SCHEMA = db.schema ?? ''
// DB_SCHEMA = 注入目标副本库。取值顺序 env > marker > `<source>__test`。
const LANE_MARKER_SCHEMA = readLaneDbMarker()
const DB_SCHEMA = process.env.ERP_TEST_DB_SCHEMA || LANE_MARKER_SCHEMA || `${SOURCE_SCHEMA}__test`
// marker 是不可见的兜底路径(env 前缀在命令行上自明),生效时必须留痕便于排查指错库。
if (!process.env.ERP_TEST_DB_SCHEMA && LANE_MARKER_SCHEMA) {
console.log(`${LOG} lane DB marker 生效: ${LANE_MARKER_SCHEMA}`)
}
function rejectPlaceholder(key, value) {
if (typeof value === 'string' && value.includes('【人工填写')) {
console.error(`${LOG} database.${key} 仍是占位,请先在 config-vars.yaml 填真实值(database.password 可填 '' 表示空密码)`)
process.exit(1)
}
}
for (const [key, value] of [['host', DB_HOST], ['port', DB_PORT], ['user', DB_USER], ['password', DB_PASSWORD], ['schema', SOURCE_SCHEMA]]) {
rejectPlaceholder(key, value)
}
if (!/^\d+$/.test(DB_PORT) || Number(DB_PORT) <= 0 || Number(DB_PORT) > 65535) {
console.error(`${LOG} database.port 非法: ${DB_PORT}(必须是 1..65535 的整数)`)
process.exit(1)
}
if (String(SOURCE_SCHEMA).trim() === '') {
console.error(`${LOG} database.schema(源库名)未填`)
process.exit(1)
}
// **安全红线**:注入目标绝不能是源库——演示种子只灌一次性副本。
if (DB_SCHEMA === SOURCE_SCHEMA) {
console.error(`${LOG} 拒绝:注入目标副本名与源库名相同(${SOURCE_SCHEMA})——演示种子绝不写源库。`)
process.exit(1)
}
// ──────────────────────────────────────────────────────────────────────────
// ② 列 sql/seed/*.sql 升序(确定性显式 .sort());校验文件名。
// ──────────────────────────────────────────────────────────────────────────
if (!existsSync(SEED_DIR)) {
console.log(`${LOG} 无种子文件(目录不存在: ${SEED_DIR}),无需注入`)
process.exit(0)
}
// 文件名契约:<NN>__<module_id>.sql —— NN 为两位序号、module_id 为 [A-Za-z0-9_]+,中间双下划线分隔。
const SEED_NAME_RE = /^[0-9]{2}__[A-Za-z0-9_]+\.sql$/
const seedFiles = readdirSync(SEED_DIR).filter((name) => name.toLowerCase().endsWith('.sql')).sort()
if (seedFiles.length === 0) {
console.log(`${LOG} 无种子文件(${SEED_DIR} 为空),无需注入`)
process.exit(0)
}
const illegal = seedFiles.filter((name) => !SEED_NAME_RE.test(name))
if (illegal.length > 0) {
console.error(`${LOG} 发现非法种子文件名(要求匹配 <NN>__<module_id>.sql,即 /^[0-9]{2}__[A-Za-z0-9_]+\\.sql$/):`)
for (const name of illegal) console.error(`${LOG} - ${name}`)
process.exit(1)
}
// ──────────────────────────────────────────────────────────────────────────
// 以下进入 DB 阶段。目标库名作为 mysql 位置参数原样传值。
// ──────────────────────────────────────────────────────────────────────────
// 查询型调用:mysql -N -B -e,捕获 stdout(utf8);目标库作为位置参数。
function mysqlQuery(sql) {
return spawnSync(
'mysql',
[`--host=${DB_HOST}`, `--port=${DB_PORT}`, `--user=${DB_USER}`, `--password=${DB_PASSWORD}`, '-N', '-B', '-e', sql, DB_SCHEMA],
{ encoding: 'utf8' },
)
}
// 应用型调用:把文件内容喂 stdin(--comments 防剥注释);目标库作为位置参数。
function mysqlApply(sqlText) {
return spawnSync(
'mysql',
[`--host=${DB_HOST}`, `--port=${DB_PORT}`, `--user=${DB_USER}`, `--password=${DB_PASSWORD}`, '--comments', DB_SCHEMA],
{ input: sqlText, encoding: 'utf8' },
)
}
function fatalMysql(res, label) {
if (res.error) {
console.error(`${LOG} FATAL: 无法执行 mysql(请确认其在 PATH 中): ${res.error.message}`)
process.exit(1)
}
if (res.status !== 0) {
if (res.stderr) process.stderr.write(res.stderr)
console.error(`${LOG} FAIL (${label}): mysql exit=${res.status}`)
process.exit(res.status === null ? 1 : res.status)
}
}
console.log(`${LOG} 目标库 ${DB_SCHEMA} on ${DB_HOST}:${DB_PORT},待处理种子 ${seedFiles.length} 个`)
// ──────────────────────────────────────────────────────────────────────────
// ③ 查 flyway_schema_history(information_schema)是否存在 —— 不存在说明 schema 未建。
// ──────────────────────────────────────────────────────────────────────────
const flywayCheck = mysqlQuery(
`SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = ${quoteSqlString(DB_SCHEMA)} AND table_name = 'flyway_schema_history'`,
)
fatalMysql(flywayCheck, 'check-flyway')
if (flywayCheck.stdout.trim() !== '1') {
console.error(`${LOG} 副本 schema 未初始化(${DB_SCHEMA} 中找不到 flyway_schema_history)——请先跑 setup-test-db 复制源库副本并起后端(Flyway apply 迁移),再注入种子`)
process.exit(1)
}
// ──────────────────────────────────────────────────────────────────────────
// ④ 账本表 _demo_seed_history(已应用文件账本,幂等核心)。
// ──────────────────────────────────────────────────────────────────────────
const createLedger = mysqlApply(
'CREATE TABLE IF NOT EXISTS _demo_seed_history (' +
' file VARCHAR(255) NOT NULL PRIMARY KEY,' +
' applied_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP' +
') ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;',
)
fatalMysql(createLedger, 'create-ledger')
// 读出已应用文件集合。
const appliedRes = mysqlQuery('SELECT file FROM _demo_seed_history')
fatalMysql(appliedRes, 'read-ledger')
const applied = new Set(
appliedRes.stdout.split('\n').map((s) => s.trim()).filter((s) => s !== ''),
)
// ──────────────────────────────────────────────────────────────────────────
// ⑤ 逐文件按文件名升序应用(已应用跳过;失败 exit 1 透传 stderr;成功后写账本)。
// ──────────────────────────────────────────────────────────────────────────
let appliedCount = 0
let skippedCount = 0
for (const name of seedFiles) {
if (applied.has(name)) {
console.log(`${LOG} 跳过(已应用): ${name}`)
skippedCount += 1
continue
}
console.log(`${LOG} 应用: ${name}`)
const sqlText = readFileSync(join(SEED_DIR, name), 'utf8')
// 账本 INSERT 拼到同一批 SQL 末尾、同一次 mysql 调用执行:mysql 批处理遇错即停,
// 账本行只在前面全部种子语句成功后才落——杜绝「已应用未记账 → 重跑重复插入」的半截状态。
const body = sqlText.trimEnd()
const sep = /;$/.test(body) ? '\n' : ';\n'
const applyRes = mysqlApply(`${body}${sep}INSERT INTO _demo_seed_history (file) VALUES (${quoteSqlString(name)});`)
fatalMysql(applyRes, `apply ${name}`)
appliedCount += 1
}
console.log(`${LOG} done — applied=${appliedCount} skipped=${skippedCount}(共 ${seedFiles.length} 个)`)