• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

safe-global / safe-client-gateway / 10304686649

08 Aug 2024 03:08PM UTC coverage: 46.911% (-0.009%) from 46.92%
10304686649

push

github

web-flow
Log executed migrations at startup (#1816)

Modifies migration execution log to include migration names

476 of 2885 branches covered (16.5%)

Branch coverage included in aggregate %.

0 of 4 new or added lines in 2 files covered. (0.0%)

4551 of 7831 relevant lines covered (58.12%)

12.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

29.63
/src/datasources/db/postgres-database.migration.hook.ts
1
import { Inject, Injectable, OnModuleInit } from '@nestjs/common';
16✔
2
import { ILoggingService, LoggingService } from '@/logging/logging.interface';
16✔
3
import postgres from 'postgres';
16✔
4
import { PostgresDatabaseMigrator } from '@/datasources/db/postgres-database.migrator';
16✔
5
import { IConfigurationService } from '@/config/configuration.service.interface';
16✔
6
import { asError } from '@/logging/utils';
16✔
7

8
/**
9
 * The {@link PostgresDatabaseMigrationHook} is a Module Init hook meaning
10
 * that it will be executed once the dependencies are resolved.
11
 *
12
 * This happens before the Application Bootstraps, so route listeners are not
13
 * initiated and potentially generating queries.
14
 */
15
@Injectable({})
16
export class PostgresDatabaseMigrationHook implements OnModuleInit {
16✔
17
  private static LOCK_MAGIC_NUMBER = 132;
16✔
18
  private readonly runMigrations: boolean;
19

20
  constructor(
21
    @Inject('DB_INSTANCE') private readonly sql: postgres.Sql,
×
22
    @Inject(PostgresDatabaseMigrator)
23
    private readonly migrator: PostgresDatabaseMigrator,
×
24
    @Inject(LoggingService) private readonly loggingService: ILoggingService,
×
25
    @Inject(IConfigurationService)
26
    private readonly configurationService: IConfigurationService,
×
27
  ) {
28
    this.runMigrations = this.configurationService.getOrThrow<boolean>(
×
29
      'application.runMigrations',
30
    );
31
  }
32

33
  /**
34
   * Function executed when the module is initialized.
35
   *
36
   * This function will check if the migrations are enabled and if so, it will
37
   * acquire a lock to perform a migration. If the lock is not acquired, then
38
   * a migration is being executed by another instance.
39
   *
40
   * If the lock is acquired, the migration will be executed and the lock will
41
   * be released.
42
   */
43
  async onModuleInit(): Promise<void> {
44
    if (!this.runMigrations) {
×
45
      return this.loggingService.info('Database migrations are disabled');
×
46
    }
47

48
    try {
×
49
      this.loggingService.info('Checking migrations');
×
50
      await this.acquireLock();
×
NEW
51
      const executed = await this.migrator.migrate();
×
52
      await this.releaseLock();
×
NEW
53
      this.loggingService.info(
×
NEW
54
        `Pending migrations executed: [${executed.map((m) => m.name).join(', ')}]`,
×
55
      );
56
    } catch (e) {
57
      this.loggingService.error(`Error running migrations: ${asError(e)}`);
×
58
    }
59
  }
60

61
  private async acquireLock(): Promise<void> {
62
    await this
×
63
      .sql`SELECT pg_advisory_lock(${PostgresDatabaseMigrationHook.LOCK_MAGIC_NUMBER})`;
64
  }
65

66
  private async releaseLock(): Promise<void> {
67
    await this
×
68
      .sql`SELECT pg_advisory_unlock(${PostgresDatabaseMigrationHook.LOCK_MAGIC_NUMBER})`;
69
  }
70
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc