feat: 添加当前或未来日期检查功能以优化分区创建逻辑

This commit is contained in:
2026-03-03 20:21:57 +08:00
parent c6d7cea8cf
commit ba61a540da
2 changed files with 25 additions and 4 deletions

View File

@@ -174,6 +174,13 @@ class DatabaseManager {
}
const dbManager = new DatabaseManager(config.db);
class PartitionManager {
isCurrentOrFutureDate(date) {
const normalizedDate = new Date(date);
normalizedDate.setHours(0, 0, 0, 0);
const today = /* @__PURE__ */ new Date();
today.setHours(0, 0, 0, 0);
return normalizedDate.getTime() >= today.getTime();
}
/**
* Calculate the start and end timestamps (milliseconds) for a given date.
* @param {Date} date - The date to calculate for.
@@ -218,10 +225,11 @@ class PartitionManager {
if (!checkRes.rows[0].exists) {
logger.info(`Creating partition ${partitionName} for range [${startMs}, ${endMs})`);
console.log(`Creating partition ${partitionName} for range [${startMs}, ${endMs})`);
const tablespaceClause = this.isCurrentOrFutureDate(targetDate) ? " TABLESPACE ts_hot" : "";
const createSql = `
CREATE TABLE IF NOT EXISTS ${partitionName}
PARTITION OF ${schema}.${table}
FOR VALUES FROM (${startMs}) TO (${endMs});
FOR VALUES FROM (${startMs}) TO (${endMs})${tablespaceClause};
`;
await client.query(createSql);
}
@@ -263,10 +271,11 @@ class PartitionManager {
const checkRes = await client.query(`SELECT to_regclass($1) as exists;`, [partitionName]);
if (!checkRes.rows[0].exists) {
logger.info(`Creating partition ${partitionName} for range [${startMs}, ${endMs})`);
const tablespaceClause = this.isCurrentOrFutureDate(targetDate) ? " TABLESPACE ts_hot" : "";
await client.query(`
CREATE TABLE IF NOT EXISTS ${partitionName}
PARTITION OF ${schema}.${table}
FOR VALUES FROM (${startMs}) TO (${endMs});
FOR VALUES FROM (${startMs}) TO (${endMs})${tablespaceClause};
`);
}
}