Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion utils/entity/__tests__/column-types.util.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jest.mock('typeorm', () => {
};
});

import { LongJsonColumn } from '../column-types.util';
import { LongJsonColumn, getAddMinutesExpression } from '../column-types.util';

describe('LongJsonColumn', () => {
beforeEach(() => {
Expand Down Expand Up @@ -49,3 +49,21 @@ describe('LongJsonColumn', () => {
expect(options.transformer).toBe(customTransformer);
});
});

describe('getAddMinutesExpression', () => {
it('uses strftime() modifiers on SQLite (test environment), keeping milliseconds', () => {
expect(getAddMinutesExpression('t.created_at', 15)).toBe("strftime('%Y-%m-%d %H:%M:%f', t.created_at, '+15 minutes')");
});

it('subtracts minutes on SQLite for negative values', () => {
expect(getAddMinutesExpression('t.created_at', -15)).toBe("strftime('%Y-%m-%d %H:%M:%f', t.created_at, '-15 minutes')");
});

it('uses INTERVAL arithmetic on MySQL', () => {
jest.isolateModules(() => {
jest.doMock('@shared/config/database.config', () => ({ databaseConfig: { type: 'mysql' } }));
const { getAddMinutesExpression: mysqlAddMinutes } = require('../column-types.util');
expect(mysqlAddMinutes('t.created_at', 15)).toBe('t.created_at + INTERVAL 15 MINUTE');
});
});
});
17 changes: 17 additions & 0 deletions utils/entity/column-types.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,23 @@ export function getGroupConcatExpression(column: string, separator: string = ',
}
}

/**
* Database-agnostic "datetime plus minutes" expression
* MySQL: column + INTERVAL 15 MINUTE
* SQLite: strftime('%Y-%m-%d %H:%M:%f', column, '+15 minutes') (keeps milliseconds, unlike datetime())
*/
export function getAddMinutesExpression(column: string, minutes: number): string {
const dbType = getDatabaseType();

if (dbType === DatabaseType.MYSQL) {
return `${column} + INTERVAL ${minutes} MINUTE`;
} else {
// SQLite
const sign = minutes >= 0 ? '+' : '';
return `strftime('%Y-%m-%d %H:%M:%f', ${column}, '${sign}${minutes} minutes')`;
}
}

/**
* Database-agnostic CONCAT function
* MySQL: CONCAT(col1, '_', col2)
Expand Down