fix(database): enforce UTC for pooled connections
This commit is contained in:
parent
0d3e219fd1
commit
d9917b9ee1
|
|
@ -9,15 +9,27 @@ import { PrismaClient } from '../generated/prisma/client';
|
|||
import { ENVIRONMENT } from '../config/environment.module';
|
||||
import type { Environment } from '../config/environment';
|
||||
|
||||
export function utcDatabaseUrl(value: string, fallbackOptions = '') {
|
||||
const url = new URL(value);
|
||||
const options = url.searchParams.get('options') || fallbackOptions;
|
||||
url.searchParams.set('options', `${options} -c timezone=UTC`.trim());
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
@Injectable()
|
||||
export class DatabaseService
|
||||
extends PrismaClient
|
||||
implements OnModuleInit, OnModuleDestroy
|
||||
{
|
||||
constructor(@Inject(ENVIRONMENT) environment: Environment) {
|
||||
// The installed adapter serializes UTC wall-clock values without offsets.
|
||||
// Enforce UTC on every pooled connection, preserving other startup options.
|
||||
super({
|
||||
adapter: new PrismaPg({
|
||||
connectionString: environment.DATABASE_URL,
|
||||
connectionString: utcDatabaseUrl(
|
||||
environment.DATABASE_URL,
|
||||
process.env.PGOPTIONS ?? '',
|
||||
),
|
||||
connectionTimeoutMillis: 3000,
|
||||
query_timeout: 3000,
|
||||
max: environment.DATABASE_POOL_SIZE,
|
||||
|
|
|
|||
|
|
@ -1,8 +1,40 @@
|
|||
import 'reflect-metadata';
|
||||
import { DatabaseService } from '../src/database/database.service';
|
||||
import {
|
||||
DatabaseService,
|
||||
utcDatabaseUrl,
|
||||
} from '../src/database/database.service';
|
||||
import { parseEnvironment } from '../src/config/environment';
|
||||
|
||||
describe('database lifecycle', () => {
|
||||
it('preserves credentials and other startup options while enforcing UTC last', () => {
|
||||
const source = new URL(
|
||||
'postgresql://user:p%40ss@localhost/mani?sslmode=require',
|
||||
);
|
||||
source.searchParams.set(
|
||||
'options',
|
||||
'-c application_name=mani -c timezone=Asia/Kolkata',
|
||||
);
|
||||
const result = new URL(utcDatabaseUrl(source.toString()));
|
||||
expect(result.username).toBe(source.username);
|
||||
expect(result.password).toBe(source.password);
|
||||
expect(result.searchParams.get('sslmode')).toBe('require');
|
||||
expect(result.searchParams.get('options')).toBe(
|
||||
'-c application_name=mani -c timezone=Asia/Kolkata -c timezone=UTC',
|
||||
);
|
||||
expect(
|
||||
new URL(utcDatabaseUrl('postgresql://localhost/mani')).searchParams.get(
|
||||
'options',
|
||||
),
|
||||
).toBe('-c timezone=UTC');
|
||||
expect(
|
||||
new URL(
|
||||
utcDatabaseUrl(
|
||||
'postgresql://localhost/mani',
|
||||
'-c application_name=env',
|
||||
),
|
||||
).searchParams.get('options'),
|
||||
).toBe('-c application_name=env -c timezone=UTC');
|
||||
});
|
||||
let database: DatabaseService;
|
||||
beforeEach(() => {
|
||||
database = new DatabaseService(
|
||||
|
|
|
|||
|
|
@ -31,6 +31,8 @@ export async function testDatabase() {
|
|||
};
|
||||
} else {
|
||||
const pg = await PGlite.create();
|
||||
// The socket emulator ignores startup options; match application UTC sessions.
|
||||
await pg.exec("SET TIME ZONE 'UTC'");
|
||||
const server = new PGLiteSocketServer({
|
||||
db: pg,
|
||||
port: 0,
|
||||
|
|
|
|||
Loading…
Reference in New Issue