fix: set end date on migrations

This commit is contained in:
Carl-Gerhard Lindesvärd
2025-12-10 21:50:08 +01:00
parent 396f509a2e
commit 6403c77e06
2 changed files with 14 additions and 0 deletions

View File

@@ -142,12 +142,18 @@ export async function up() {
const firstEventDate = new Date(firstEventDateJson[0]?.created_at ?? ''); const firstEventDate = new Date(firstEventDateJson[0]?.created_at ?? '');
if (firstEventDate) { if (firstEventDate) {
// Step 2: Copy data from old tables to new tables (partitioned by month for efficiency) // Step 2: Copy data from old tables to new tables (partitioned by month for efficiency)
// Set endDate to first of next month to ensure we capture all data in the current month
const endDate = new Date();
endDate.setMonth(endDate.getMonth() + 1);
endDate.setDate(1);
sqls.push( sqls.push(
...moveDataBetweenTables({ ...moveDataBetweenTables({
from: 'events', from: 'events',
to: 'events_new_20251123', to: 'events_new_20251123',
batch: { batch: {
startDate: firstEventDate, startDate: firstEventDate,
endDate: endDate,
column: 'toDate(created_at)', column: 'toDate(created_at)',
interval: 'month', interval: 'month',
transform: (date: Date) => { transform: (date: Date) => {
@@ -170,6 +176,11 @@ export async function up() {
const firstSessionDate = new Date(firstSessionDateJson[0]?.created_at ?? ''); const firstSessionDate = new Date(firstSessionDateJson[0]?.created_at ?? '');
if (firstSessionDate) { if (firstSessionDate) {
// Set endDate to first of next month to ensure we capture all data in the current month
const endDate = new Date();
endDate.setMonth(endDate.getMonth() + 1);
endDate.setDate(1);
sqls.push( sqls.push(
...moveDataBetweenTables({ ...moveDataBetweenTables({
from: 'sessions', from: 'sessions',
@@ -215,6 +226,7 @@ export async function up() {
], ],
batch: { batch: {
startDate: firstSessionDate, startDate: firstSessionDate,
endDate: endDate,
column: 'toDate(created_at)', column: 'toDate(created_at)',
interval: 'month', interval: 'month',
transform: (date: Date) => { transform: (date: Date) => {

View File

@@ -59,6 +59,8 @@ async function migrate() {
if (!getIsDry()) { if (!getIsDry()) {
printBoxMessage('🕒 Migrations starts in 10 seconds', []); printBoxMessage('🕒 Migrations starts in 10 seconds', []);
await new Promise((resolve) => setTimeout(resolve, 10000)); await new Promise((resolve) => setTimeout(resolve, 10000));
} else {
printBoxMessage('🕒 Migrations starts now (dry run)', []);
} }
} }