feat: 添加当前或未来日期检查功能以优化分区创建逻辑
This commit is contained in:
13
bls-onoffline-backend/dist/index.js
vendored
13
bls-onoffline-backend/dist/index.js
vendored
@@ -174,6 +174,13 @@ class DatabaseManager {
|
|||||||
}
|
}
|
||||||
const dbManager = new DatabaseManager(config.db);
|
const dbManager = new DatabaseManager(config.db);
|
||||||
class PartitionManager {
|
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.
|
* Calculate the start and end timestamps (milliseconds) for a given date.
|
||||||
* @param {Date} date - The date to calculate for.
|
* @param {Date} date - The date to calculate for.
|
||||||
@@ -218,10 +225,11 @@ class PartitionManager {
|
|||||||
if (!checkRes.rows[0].exists) {
|
if (!checkRes.rows[0].exists) {
|
||||||
logger.info(`Creating partition ${partitionName} for range [${startMs}, ${endMs})`);
|
logger.info(`Creating partition ${partitionName} for range [${startMs}, ${endMs})`);
|
||||||
console.log(`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 = `
|
const createSql = `
|
||||||
CREATE TABLE IF NOT EXISTS ${partitionName}
|
CREATE TABLE IF NOT EXISTS ${partitionName}
|
||||||
PARTITION OF ${schema}.${table}
|
PARTITION OF ${schema}.${table}
|
||||||
FOR VALUES FROM (${startMs}) TO (${endMs});
|
FOR VALUES FROM (${startMs}) TO (${endMs})${tablespaceClause};
|
||||||
`;
|
`;
|
||||||
await client.query(createSql);
|
await client.query(createSql);
|
||||||
}
|
}
|
||||||
@@ -263,10 +271,11 @@ class PartitionManager {
|
|||||||
const checkRes = await client.query(`SELECT to_regclass($1) as exists;`, [partitionName]);
|
const checkRes = await client.query(`SELECT to_regclass($1) as exists;`, [partitionName]);
|
||||||
if (!checkRes.rows[0].exists) {
|
if (!checkRes.rows[0].exists) {
|
||||||
logger.info(`Creating partition ${partitionName} for range [${startMs}, ${endMs})`);
|
logger.info(`Creating partition ${partitionName} for range [${startMs}, ${endMs})`);
|
||||||
|
const tablespaceClause = this.isCurrentOrFutureDate(targetDate) ? " TABLESPACE ts_hot" : "";
|
||||||
await client.query(`
|
await client.query(`
|
||||||
CREATE TABLE IF NOT EXISTS ${partitionName}
|
CREATE TABLE IF NOT EXISTS ${partitionName}
|
||||||
PARTITION OF ${schema}.${table}
|
PARTITION OF ${schema}.${table}
|
||||||
FOR VALUES FROM (${startMs}) TO (${endMs});
|
FOR VALUES FROM (${startMs}) TO (${endMs})${tablespaceClause};
|
||||||
`);
|
`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,16 @@ import { config } from '../config/config.js';
|
|||||||
import dbManager from './databaseManager.js';
|
import dbManager from './databaseManager.js';
|
||||||
|
|
||||||
class PartitionManager {
|
class PartitionManager {
|
||||||
|
isCurrentOrFutureDate(date) {
|
||||||
|
const normalizedDate = new Date(date);
|
||||||
|
normalizedDate.setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
|
const today = new Date();
|
||||||
|
today.setHours(0, 0, 0, 0);
|
||||||
|
|
||||||
|
return normalizedDate.getTime() >= today.getTime();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Calculate the start and end timestamps (milliseconds) for a given date.
|
* Calculate the start and end timestamps (milliseconds) for a given date.
|
||||||
* @param {Date} date - The date to calculate for.
|
* @param {Date} date - The date to calculate for.
|
||||||
@@ -56,10 +66,11 @@ class PartitionManager {
|
|||||||
if (!checkRes.rows[0].exists) {
|
if (!checkRes.rows[0].exists) {
|
||||||
logger.info(`Creating partition ${partitionName} for range [${startMs}, ${endMs})`);
|
logger.info(`Creating partition ${partitionName} for range [${startMs}, ${endMs})`);
|
||||||
console.log(`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 = `
|
const createSql = `
|
||||||
CREATE TABLE IF NOT EXISTS ${partitionName}
|
CREATE TABLE IF NOT EXISTS ${partitionName}
|
||||||
PARTITION OF ${schema}.${table}
|
PARTITION OF ${schema}.${table}
|
||||||
FOR VALUES FROM (${startMs}) TO (${endMs});
|
FOR VALUES FROM (${startMs}) TO (${endMs})${tablespaceClause};
|
||||||
`;
|
`;
|
||||||
await client.query(createSql);
|
await client.query(createSql);
|
||||||
}
|
}
|
||||||
@@ -108,10 +119,11 @@ class PartitionManager {
|
|||||||
const checkRes = await client.query(`SELECT to_regclass($1) as exists;`, [partitionName]);
|
const checkRes = await client.query(`SELECT to_regclass($1) as exists;`, [partitionName]);
|
||||||
if (!checkRes.rows[0].exists) {
|
if (!checkRes.rows[0].exists) {
|
||||||
logger.info(`Creating partition ${partitionName} for range [${startMs}, ${endMs})`);
|
logger.info(`Creating partition ${partitionName} for range [${startMs}, ${endMs})`);
|
||||||
|
const tablespaceClause = this.isCurrentOrFutureDate(targetDate) ? ' TABLESPACE ts_hot' : '';
|
||||||
await client.query(`
|
await client.query(`
|
||||||
CREATE TABLE IF NOT EXISTS ${partitionName}
|
CREATE TABLE IF NOT EXISTS ${partitionName}
|
||||||
PARTITION OF ${schema}.${table}
|
PARTITION OF ${schema}.${table}
|
||||||
FOR VALUES FROM (${startMs}) TO (${endMs});
|
FOR VALUES FROM (${startMs}) TO (${endMs})${tablespaceClause};
|
||||||
`);
|
`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user