1
This commit is contained in:
73
scripts/migrate_biz_plan_api_columns.js
Normal file
73
scripts/migrate_biz_plan_api_columns.js
Normal file
@@ -0,0 +1,73 @@
|
||||
/**
|
||||
* 为 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);
|
||||
});
|
||||
Reference in New Issue
Block a user