Fix TEST env not using the correct DB for datasources (#10780)

## Context
Config was programmatically loaded in our datasources however the
default behavior of dotenv is to ignore vars if they are already
defined. This means we need to be careful about the order of env
injection and sometimes it's done at a higher level (for example
db:reset will depend on build). To make things easier I'm using the
override flag to properly override the PG_DATABASE_URL if different (and
to properly work with the 'test' DB instead of 'default' during
testing).
This commit is contained in:
Weiko
2025-03-11 16:20:01 +01:00
committed by GitHub
parent 0516e95330
commit 293e3f58c4
4 changed files with 16 additions and 4 deletions

View File

@ -2,7 +2,10 @@ import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { config } from 'dotenv';
import { DataSource, DataSourceOptions } from 'typeorm';
config({ path: process.env.NODE_ENV === 'test' ? '.env.test' : '.env' });
config({
path: process.env.NODE_ENV === 'test' ? '.env.test' : '.env',
override: true,
});
const isJest = process.argv.some((arg) => arg.includes('jest'));

View File

@ -2,7 +2,10 @@ import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { config } from 'dotenv';
import { DataSource, DataSourceOptions } from 'typeorm';
config({ path: process.env.NODE_ENV === 'test' ? '.env.test' : '.env' });
config({
path: process.env.NODE_ENV === 'test' ? '.env.test' : '.env',
override: true,
});
const isJest = process.argv.some((arg) => arg.includes('jest'));

View File

@ -1,6 +1,9 @@
import { config } from 'dotenv';
import { DataSource, DataSourceOptions } from 'typeorm';
config({ path: process.env.NODE_ENV === 'test' ? '.env.test' : '.env' });
config({
path: process.env.NODE_ENV === 'test' ? '.env.test' : '.env',
override: true,
});
const typeORMRawModuleOptions: DataSourceOptions = {
url: process.env.PG_DATABASE_URL,

View File

@ -2,7 +2,10 @@ import * as fs from 'fs';
import * as path from 'path';
import { config } from 'dotenv';
config({ path: process.env.NODE_ENV === 'test' ? '.env.test' : '.env' });
config({
path: process.env.NODE_ENV === 'test' ? '.env.test' : '.env',
override: true,
});
export function generateFrontConfig(): void {
const configObject = {