Skip to content

Commit 356efc4

Browse files
committed
Temporarily add timings to see how long DB resets take.
1 parent 5bf98da commit 356efc4

2 files changed

Lines changed: 46 additions & 3 deletions

File tree

apps/backend/src/datasources/postgres/AdminDataSource.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ import {
4343
const dbPatchesFolderPath = path.join(cwd(), 'db_patches');
4444
const seedsPath = path.join(dbPatchesFolderPath, 'db_seeds');
4545

46+
const getDurationMs = (startedAt: number) => Date.now() - startedAt;
47+
4648
@injectable()
4749
export default class PostgresAdminDataSource implements AdminDataSource {
4850
private autoUpgradedDBReady = false;
@@ -280,20 +282,38 @@ export default class PostgresAdminDataSource implements AdminDataSource {
280282
* NB! This will actually wipe the database
281283
*/
282284
async resetDB(includeSeeds: boolean) {
285+
const resetStartedAt = Date.now();
286+
const shouldApplySeeds = env.INCLUDE_SEEDS === '1' || includeSeeds;
287+
283288
try {
289+
const dropSchemaStartedAt = Date.now();
290+
284291
await database.raw(`
285292
DROP SCHEMA public CASCADE;
286293
CREATE SCHEMA public;
287294
GRANT ALL ON SCHEMA public TO duouser;
288295
GRANT ALL ON SCHEMA public TO public;
289296
`);
290297

298+
logger.logInfo('resetDB timing: schema recreated', {
299+
durationMs: getDurationMs(dropSchemaStartedAt),
300+
includeSeeds,
301+
shouldApplySeeds,
302+
});
303+
291304
const applyPatchesOutput = await this.applyPatches();
292305

293-
if (env.INCLUDE_SEEDS === '1' || includeSeeds) {
306+
if (shouldApplySeeds) {
294307
await this.applySeeds();
295308
}
296309

310+
logger.logInfo('resetDB timing: reset finished', {
311+
durationMs: getDurationMs(resetStartedAt),
312+
includeSeeds,
313+
shouldApplySeeds,
314+
patchesApplied: applyPatchesOutput.length,
315+
});
316+
297317
return applyPatchesOutput;
298318
} catch (e) {
299319
logger.logException('resetDB failed', e);
@@ -304,6 +324,7 @@ export default class PostgresAdminDataSource implements AdminDataSource {
304324

305325
async applyPatches(): Promise<string[]> {
306326
this.autoUpgradedDBReady = false;
327+
const applyPatchesStartedAt = Date.now();
307328

308329
logger.logInfo('Applying patches started', { timestamp: new Date() });
309330

@@ -337,14 +358,19 @@ export default class PostgresAdminDataSource implements AdminDataSource {
337358
});
338359
}
339360

340-
logger.logInfo('Applying patches finished', { timestamp: new Date() });
361+
logger.logInfo('Applying patches finished', {
362+
timestamp: new Date(),
363+
durationMs: getDurationMs(applyPatchesStartedAt),
364+
patchesApplied: patches.length,
365+
});
341366

342367
this.autoUpgradedDBReady = true;
343368

344369
return patches.map(([file]) => file);
345370
}
346371

347372
private async applySeeds() {
373+
const applySeedsStartedAt = Date.now();
348374
logger.logInfo('Applying seeds started', { timestamp: new Date() });
349375

350376
const log: string[] = [];
@@ -372,7 +398,10 @@ export default class PostgresAdminDataSource implements AdminDataSource {
372398
});
373399
}
374400

375-
logger.logInfo('Applying seeds finished', {});
401+
logger.logInfo('Applying seeds finished', {
402+
durationMs: getDurationMs(applySeedsStartedAt),
403+
seedsApplied: log.length,
404+
});
376405
}
377406

378407
protected async upgrade() {

apps/backend/src/mutations/AdminMutations.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import { signToken } from '../utils/jwt';
2828
import { ApolloServerErrorCodeExtended } from '../utils/utilTypes';
2929

3030
const IS_BACKEND_VALIDATION = true;
31+
const getDurationMs = (startedAt: number) => Date.now() - startedAt;
32+
3133
@injectable()
3234
export default class AdminMutations {
3335
constructor(
@@ -44,11 +46,23 @@ export default class AdminMutations {
4446
} else {
4547
logger.logWarn('Resetting database', {});
4648

49+
const resetStartedAt = Date.now();
4750
const log = await this.dataSource.resetDB(includeSeeds);
51+
const configureEnvironmentStartedAt = Date.now();
52+
4853
await container.resolve<() => Promise<void>>(
4954
Tokens.ConfigureEnvironment
5055
)();
5156

57+
logger.logInfo('resetDB timing: environment configured', {
58+
durationMs: getDurationMs(configureEnvironmentStartedAt),
59+
includeSeeds,
60+
});
61+
logger.logInfo('resetDB timing: mutation finished', {
62+
durationMs: getDurationMs(resetStartedAt),
63+
includeSeeds,
64+
});
65+
5266
return log;
5367
}
5468
}

0 commit comments

Comments
 (0)