feat: multi-module iteration
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* 发版前一次性迁移:在 prisma db push 删除 task_dispatch_* 列之前执行。
|
||||
* 创建 wecom_message_push 表(若不存在)并写入默认推送。
|
||||
*/
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import {
|
||||
WECOM_PUSH_DEFAULT_ALERT_CONDITIONS,
|
||||
WECOM_PUSH_DEFAULT_DEV_DISPATCH_CONDITIONS,
|
||||
} from '@dukang/shared-types';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function ensureTable() {
|
||||
await prisma.$executeRawUnsafe(`
|
||||
CREATE TABLE IF NOT EXISTS wecom_message_push (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(64) NOT NULL,
|
||||
avatar_url VARCHAR(512) NULL,
|
||||
webhook_url VARCHAR(512) NOT NULL,
|
||||
enabled TINYINT(1) NOT NULL DEFAULT 1,
|
||||
mention_wecom_user_id VARCHAR(64) NULL,
|
||||
push_conditions TEXT NOT NULL,
|
||||
sort_order INT NOT NULL DEFAULT 0,
|
||||
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
|
||||
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
|
||||
INDEX wecom_message_push_enabled_sort_order_idx (enabled, sort_order)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
|
||||
`);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await ensureTable();
|
||||
const count = await prisma.wecomMessagePush.count();
|
||||
if (count > 0) {
|
||||
console.log(`wecom_message_push already has ${count} row(s), skip`);
|
||||
return;
|
||||
}
|
||||
|
||||
const alertUrl = (process.env.WECOM_ALERT_WEBHOOK_URL || '').trim();
|
||||
if (alertUrl) {
|
||||
await prisma.wecomMessagePush.create({
|
||||
data: {
|
||||
name: '运营告警',
|
||||
webhookUrl: alertUrl,
|
||||
enabled: process.env.WECOM_ALERT_ENABLED !== 'false',
|
||||
pushConditions: JSON.stringify(WECOM_PUSH_DEFAULT_ALERT_CONDITIONS),
|
||||
sortOrder: 0,
|
||||
},
|
||||
});
|
||||
console.log('migrated: 运营告警');
|
||||
}
|
||||
|
||||
try {
|
||||
const rows = await prisma.$queryRawUnsafe<
|
||||
Array<{
|
||||
task_dispatch_webhook_url: string | null;
|
||||
task_dispatch_wecom_user_id: string | null;
|
||||
task_dispatch_enabled: number | boolean | null;
|
||||
}>
|
||||
>(
|
||||
'SELECT task_dispatch_webhook_url, task_dispatch_wecom_user_id, task_dispatch_enabled FROM dev_plan_settings LIMIT 1',
|
||||
);
|
||||
const row = rows[0];
|
||||
if (row?.task_dispatch_webhook_url?.trim()) {
|
||||
await prisma.wecomMessagePush.create({
|
||||
data: {
|
||||
name: '开发任务派发',
|
||||
webhookUrl: row.task_dispatch_webhook_url.trim(),
|
||||
enabled: !!row.task_dispatch_enabled,
|
||||
mentionWecomUserId: row.task_dispatch_wecom_user_id?.trim() || null,
|
||||
pushConditions: JSON.stringify(WECOM_PUSH_DEFAULT_DEV_DISPATCH_CONDITIONS),
|
||||
sortOrder: 10,
|
||||
},
|
||||
});
|
||||
console.log('migrated: 开发任务派发');
|
||||
}
|
||||
} catch (e) {
|
||||
console.log('dev_plan_settings task_dispatch columns unavailable:', e instanceof Error ? e.message : e);
|
||||
}
|
||||
|
||||
console.log('migrate-wecom-message-push done');
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user