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

safe-global / safe-client-gateway / 9546514333

17 Jun 2024 11:01AM UTC coverage: 49.473% (-42.8%) from 92.296%
9546514333

push

github

web-flow
Add `PostgresDatabaseMigrator` for (testing) migration (#1655)

Adds a new `PostgresDatabaseMigrator` that has the core logic of `postgres-shift`, as well as a testing method. The `migrate` method mirrors `postgres-shift` and the `test` method reuses part of it, executing each migration in a separate transaction. It allows us to interact with the database _before_ and _after_ a migration has executed, stopping after the desired migration:

- Create `PostgresDatabaseMigrator` and inject it
- Remove `postgres-shift` and associated definition/patch, replacing usage with the above
- Add appropriate test coverage

394 of 2367 branches covered (16.65%)

Branch coverage included in aggregate %.

10 of 62 new or added lines in 3 files covered. (16.13%)

2191 existing lines in 221 files now uncovered.

3969 of 6452 relevant lines covered (61.52%)

12.49 hits per line

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

42.11
/src/datasources/db/postgres-database.module.ts
1
import postgres from 'postgres';
16✔
2
import { Module } from '@nestjs/common';
16✔
3
import { PostgresDatabaseShutdownHook } from '@/datasources/db/postgres-database.shutdown.hook';
16✔
4
import { IConfigurationService } from '@/config/configuration.service.interface';
16✔
5
import { PostgresDatabaseMigrationHook } from '@/datasources/db/postgres-database.migration.hook';
16✔
6
import fs from 'fs';
16✔
7
import { PostgresDatabaseMigrator } from '@/datasources/db/postgres-database.migrator';
16✔
8

9
function dbFactory(configurationService: IConfigurationService): postgres.Sql {
UNCOV
10
  const caPath = configurationService.get<string>('db.postgres.ssl.caPath');
×
11
  const ca: string | undefined =
UNCOV
12
    caPath && caPath.length > 0 ? fs.readFileSync(caPath, 'utf8') : undefined;
×
13

UNCOV
14
  const sslConfig = configurationService.getOrThrow('db.postgres.ssl.enabled')
×
15
    ? {
16
        requestCert: configurationService.getOrThrow(
17
          'db.postgres.ssl.requestCert',
18
        ),
19
        rejectUnauthorized: configurationService.getOrThrow(
20
          'db.postgres.ssl.rejectUnauthorized',
21
        ),
22
        ca,
23
      }
24
    : false;
UNCOV
25
  return postgres({
×
26
    host: configurationService.getOrThrow('db.postgres.host'),
27
    port: configurationService.getOrThrow('db.postgres.port'),
28
    db: configurationService.getOrThrow('db.postgres.database'),
29
    user: configurationService.getOrThrow('db.postgres.username'),
30
    password: configurationService.getOrThrow('db.postgres.password'),
31
    ssl: sslConfig,
32
  });
33
}
34

35
function migratorFactory(sql: postgres.Sql): PostgresDatabaseMigrator {
NEW
36
  return new PostgresDatabaseMigrator(sql);
×
37
}
38

39
@Module({
40
  providers: [
41
    {
42
      provide: 'DB_INSTANCE',
43
      useFactory: dbFactory,
44
      inject: [IConfigurationService],
45
    },
46
    {
47
      provide: PostgresDatabaseMigrator,
48
      useFactory: migratorFactory,
49
      inject: ['DB_INSTANCE'],
50
    },
51
    PostgresDatabaseShutdownHook,
52
    PostgresDatabaseMigrationHook,
53
  ],
54
  exports: ['DB_INSTANCE'],
55
})
56
export class PostgresDatabaseModule {}
16✔
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

© 2025 Coveralls, Inc