diff --git a/bls-onoffline-backend/dist/index.js b/bls-onoffline-backend/dist/index.js index ab89c16..c43a110 100644 --- a/bls-onoffline-backend/dist/index.js +++ b/bls-onoffline-backend/dist/index.js @@ -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}; `); } } diff --git a/bls-onoffline-backend/src/db/partitionManager.js b/bls-onoffline-backend/src/db/partitionManager.js index a28d029..7a3651e 100644 --- a/bls-onoffline-backend/src/db/partitionManager.js +++ b/bls-onoffline-backend/src/db/partitionManager.js @@ -3,6 +3,16 @@ import { config } from '../config/config.js'; import dbManager from './databaseManager.js'; 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. * @param {Date} date - The date to calculate for. @@ -56,10 +66,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); } @@ -108,10 +119,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}; `); } }