Files
wechatWeb/scripts/migrate_biz_plan_api_columns.js
张成 14f5d75d9d 1
2026-04-01 09:59:54 +08:00

74 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 为 biz_plans / biz_usage_monthly 补齐套餐 API 权限相关字段(可重复执行)
*/
const mysql = require("mysql2/promise");
const config = require("../config/config");
async function column_exists(conn, schema, table, column) {
const [rows] = await conn.query(
`SELECT COUNT(*) AS c FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND COLUMN_NAME = ?`,
[schema, table, column]
);
return Number(rows[0].c) > 0;
}
async function table_exists(conn, schema, table) {
const [rows] = await conn.query(
`SELECT COUNT(*) AS c FROM information_schema.TABLES
WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?`,
[schema, table]
);
return Number(rows[0].c) > 0;
}
async function main() {
const db = config.db;
if (!db || !db.database) {
throw new Error("config.db.database 未配置");
}
const conn = await mysql.createConnection({
host: db.host,
port: db.port || 3306,
user: db.username,
password: db.password,
database: db.database,
});
const schema = db.database;
try {
if (!(await table_exists(conn, schema, "biz_plans"))) {
console.warn("跳过 biz_plans当前库中不存在该表请确认连接的是已部署订阅模块的库");
} else if (!(await column_exists(conn, schema, "biz_plans", "allowed_apis"))) {
await conn.query(
"ALTER TABLE `biz_plans` ADD COLUMN `allowed_apis` JSON DEFAULT NULL COMMENT '可访问的接口路径列表null=不限制'"
);
console.log("已添加 biz_plans.allowed_apis");
}
if (
(await table_exists(conn, schema, "biz_plans")) &&
!(await column_exists(conn, schema, "biz_plans", "api_call_quota"))
) {
await conn.query(
"ALTER TABLE `biz_plans` ADD COLUMN `api_call_quota` INT NOT NULL DEFAULT 0 COMMENT '每月API总调用次数上限0=不限制'"
);
console.log("已添加 biz_plans.api_call_quota");
}
if (!(await table_exists(conn, schema, "biz_usage_monthly"))) {
console.warn("跳过 biz_usage_monthly当前库中不存在该表");
} else if (!(await column_exists(conn, schema, "biz_usage_monthly", "api_call_count"))) {
await conn.query(
"ALTER TABLE `biz_usage_monthly` ADD COLUMN `api_call_count` INT NOT NULL DEFAULT 0 COMMENT '当月API转发总调用次数'"
);
console.log("已添加 biz_usage_monthly.api_call_count");
}
console.log("字段检查完成");
} finally {
await conn.end();
}
}
main().catch((e) => {
console.error(e);
process.exit(1);
});