Files
cwd/cwd-api/scripts/auto_migrate.js
anghunk b2fd3e72e6 feat: 添加评论文章链接字段并优化自动迁移脚本
- 在 Comment 表中添加 post_url 字段,用于存储文章完整 URL
- 更新前后端 API 以返回 post_url 字段
- 管理后台评论列表显示文章链接(优先显示 post_url)
- 移除前端调试日志并优化自动迁移脚本输出信息
- 通过脚本入口文件统一管理迁移任务
2026-02-09 13:18:58 +08:00

84 lines
2.6 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
function findDatabaseName() {
const files = ['wrangler.jsonc', 'wrangler.toml'];
const baseDirs = [process.cwd(), path.resolve(__dirname, '..')];
for (const base of baseDirs) {
for (const file of files) {
const filePath = path.join(base, file);
if (!fs.existsSync(filePath)) {
continue;
}
const content = fs.readFileSync(filePath, 'utf-8');
if (file.endsWith('.jsonc')) {
const jsonMatch = content.match(/"database_name"\s*:\s*["'](.*?)["']/);
if (jsonMatch) {
return jsonMatch[1];
}
} else {
const chunks = content.split('[[d1_databases]]');
for (const chunk of chunks) {
if (chunk.includes('CWD_DB') || chunk.includes('"CWD_DB"')) {
const nameMatch = chunk.match(/database_name\s*=\s*["'](.*?)["']/);
if (nameMatch) {
return nameMatch[1];
}
}
}
const match = content.match(/database_name\s*=\s*["'](.*?)["']/);
if (match) {
return match[1];
}
}
}
}
return null;
}
function run() {
try {
const dbName = findDatabaseName();
if (!dbName) {
return;
}
try {
const checkCmd = `npx wrangler d1 execute ${dbName} --command "SELECT count(*) as count FROM pragma_table_info('Comment') WHERE name='post_url'" --remote --json`;
const output = execSync(checkCmd, { encoding: 'utf-8', stdio: ['ignore', 'pipe', 'ignore'] });
const result = JSON.parse(output);
const count = result[0]?.results?.[0]?.count;
if (count > 0) {
return;
}
} catch (e) {
return;
}
const sql = `
ALTER TABLE Comment ADD COLUMN post_url TEXT;
UPDATE Comment SET post_url = post_slug;
UPDATE Comment
SET post_slug = SUBSTR(
REPLACE(REPLACE(post_slug, 'https://', ''), 'http://', ''),
INSTR(REPLACE(REPLACE(post_slug, 'https://', ''), 'http://', ''), '/')
)
WHERE post_slug LIKE 'http%' AND INSTR(REPLACE(REPLACE(post_slug, 'https://', ''), 'http://', ''), '/') > 0;
`;
const flatSql = sql.replace(/\s+/g, ' ').trim();
const migrateCmd = `npx wrangler d1 execute ${dbName} --command "${flatSql}" --remote --yes`;
execSync(migrateCmd, { stdio: 'inherit' });
console.log('[Auto-Migrate] Migration applied for Comment.post_url.');
} catch (error) {
console.error('[Auto-Migrate] Failed:', error.message);
process.exit(1);
}
}
run();