sql修改提交
This commit is contained in:
@@ -154,13 +154,29 @@ export async function ensureCustomerLifecycleColumns() {
|
|||||||
if (customerLifecycleEnsured) {
|
if (customerLifecycleEnsured) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
await getPool().query("ALTER TABLE aps_customer ADD COLUMN IF NOT EXISTS active TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否有效 1=有效 0=释放'");
|
await ensureColumnExists('aps_customer', 'active', "ALTER TABLE aps_customer ADD COLUMN active TINYINT(1) NOT NULL DEFAULT 1 COMMENT '是否有效 1=有效 0=释放'");
|
||||||
await getPool().query("ALTER TABLE aps_customer ADD COLUMN IF NOT EXISTS status VARCHAR(32) DEFAULT 'active' COMMENT '客户状态'");
|
await ensureColumnExists('aps_customer', 'status', "ALTER TABLE aps_customer ADD COLUMN status VARCHAR(32) DEFAULT 'active' COMMENT '客户状态'");
|
||||||
await getPool().query("ALTER TABLE aps_customer ADD COLUMN IF NOT EXISTS released_at DATETIME NULL COMMENT '释放时间'");
|
await ensureColumnExists('aps_customer', 'released_at', "ALTER TABLE aps_customer ADD COLUMN released_at DATETIME NULL COMMENT '释放时间'");
|
||||||
await getPool().query("ALTER TABLE aps_customer ADD COLUMN IF NOT EXISTS release_reason VARCHAR(255) NULL COMMENT '释放原因'");
|
await ensureColumnExists('aps_customer', 'release_reason', "ALTER TABLE aps_customer ADD COLUMN release_reason VARCHAR(255) NULL COMMENT '释放原因'");
|
||||||
customerLifecycleEnsured = true;
|
customerLifecycleEnsured = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function ensureColumnExists(tableName, columnName, alterSql) {
|
||||||
|
const [rows] = await getPool().execute(
|
||||||
|
`SELECT COUNT(*) AS cnt
|
||||||
|
FROM INFORMATION_SCHEMA.COLUMNS
|
||||||
|
WHERE TABLE_SCHEMA = ?
|
||||||
|
AND TABLE_NAME = ?
|
||||||
|
AND COLUMN_NAME = ?`,
|
||||||
|
[config.db.database, tableName, columnName],
|
||||||
|
);
|
||||||
|
const exists = Array.isArray(rows) && Number(rows[0]?.cnt || 0) > 0;
|
||||||
|
if (exists) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
await getPool().query(alterSql);
|
||||||
|
}
|
||||||
|
|
||||||
export async function upsertCustomers(records) {
|
export async function upsertCustomers(records) {
|
||||||
if (!records?.length) {
|
if (!records?.length) {
|
||||||
return { inserted: 0 };
|
return { inserted: 0 };
|
||||||
|
|||||||
Reference in New Issue
Block a user