Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
288 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -72,3 +72,4 @@ typings/ | |
.stryker-tmp | ||
/reports | ||
!@types/** | ||
!/tools |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,3 +99,4 @@ tools/ | |
/stryker.conf.js | ||
/.stryker-tmp/ | ||
/reports/ | ||
tools/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { injectable, multiInject } from 'inversify'; | ||
import * as path from 'path'; | ||
import { memoize } from '../../utils/memoize/memoize'; | ||
import { IConnectionSettings } from '../interfaces/IConnectionSettings'; | ||
import { IEntity, IEntityConstructor } from '../interfaces/IEntity'; | ||
import { ConnectionOptions } from 'typeorm/connection/ConnectionOptions'; | ||
import { IConnectionOptionsProvider } from '../interfaces/IConnectionOptionsProvider'; | ||
|
||
const DB_FILE = `cache.db`; | ||
const MIGRATIONS_DIR = path.join(__dirname, `..`, `migrations`); | ||
const MIGRATIONS_DIR_RELATIVE = MIGRATIONS_DIR.slice(process.cwd().length + 1); | ||
const MIGRATIONS_FILES = path.join(MIGRATIONS_DIR, `**`, `!(*.d).[jt]s`); | ||
|
||
@injectable() | ||
export class ConnectionOptionsProvider extends IConnectionOptionsProvider { | ||
constructor( | ||
private readonly settings: IConnectionSettings, | ||
@multiInject(IEntity) private readonly entities: IEntityConstructor[] | ||
) { | ||
super(); | ||
} | ||
|
||
@memoize() | ||
public getConnectionOptions(): ConnectionOptions { | ||
const connectionOptions: ConnectionOptions = { | ||
name: this.settings.migrationGenerationConfig ? `default` : this.settings.databaseFilePath, | ||
type: `sqlite`, | ||
database: path.join(this.settings.databaseFilePath, DB_FILE), | ||
synchronize: false, | ||
migrations: [MIGRATIONS_FILES], | ||
logging: false, | ||
dropSchema: false, | ||
entities: this.entities, | ||
cli: { | ||
migrationsDir: MIGRATIONS_DIR_RELATIVE, | ||
}, | ||
}; | ||
Object.defineProperty(connectionOptions, `migrationsRun`, { | ||
get: (): boolean => { | ||
return true; | ||
}, | ||
set: (): void => { | ||
// Do nothing as we want migration generation to build incremental queries | ||
}, | ||
enumerable: true, | ||
}); | ||
return connectionOptions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { ConnectionOptions } from 'typeorm'; | ||
|
||
export abstract class IConnectionOptionsProvider { | ||
public abstract getConnectionOptions(): ConnectionOptions; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
export abstract class IConnectionSettings { | ||
public abstract readonly databaseFilePath: string; | ||
public abstract readonly dropSchema: boolean; | ||
public abstract readonly migrationGenerationConfig?: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/class-name-casing | ||
export class autoGeneratedMigration1587733162051 implements MigrationInterface { | ||
name = `autoGeneratedMigration1587733162051`; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE TABLE "dependency" ("name" text NOT NULL, "version" text NOT NULL, "targetNode" text NOT NULL, "match" boolean, "reason" text, PRIMARY KEY ("name", "version", "targetNode"))`, | ||
undefined | ||
); | ||
await queryRunner.query( | ||
`CREATE TABLE "dependency_version" ("name" text NOT NULL, "version" text NOT NULL, "repoUrl" text NOT NULL, "releaseDate" text NOT NULL, "commitSha" text, PRIMARY KEY ("name", "version"))`, | ||
undefined | ||
); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query(`DROP TABLE "dependency_version"`, undefined); | ||
await queryRunner.query(`DROP TABLE "dependency"`, undefined); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as path from 'path'; | ||
import { Dependency, IConnectionSettings } from '../../../../src/db'; | ||
import * as tmp from 'tmp'; | ||
import { normalize, join } from 'path'; | ||
import { ConnectionOptionsProvider } from '../../../../src/db/impl/connectionOptionsProvider'; | ||
|
||
describe(`connection options provider`, () => { | ||
let tmpDir: string; | ||
|
||
beforeEach(() => { | ||
tmpDir = tmp.dirSync().name; | ||
}); | ||
|
||
it(`should not be able to change the migrationsRun property`, async () => { | ||
const settings: IConnectionSettings = { | ||
databaseFilePath: tmpDir, | ||
}; | ||
const connectionOptionsProvider = new ConnectionOptionsProvider(settings, [Dependency]); | ||
const options = connectionOptionsProvider.getConnectionOptions(); | ||
expect(options.migrationsRun).toBe(true); | ||
Object.assign(options, { | ||
migrationsRun: false, | ||
}); | ||
expect(options.migrationsRun).toBe(true); | ||
}); | ||
|
||
it(`should generate options properly`, async () => { | ||
const settings: IConnectionSettings = { | ||
databaseFilePath: tmpDir, | ||
}; | ||
const connectionOptionsProvider = new ConnectionOptionsProvider(settings, [Dependency]); | ||
const options = connectionOptionsProvider.getConnectionOptions(); | ||
expect(options).toEqual({ | ||
name: tmpDir, | ||
type: `sqlite`, | ||
database: path.join(tmpDir, `cache.db`), | ||
synchronize: false, | ||
migrationsRun: true, | ||
migrations: [normalize(join(__dirname, `../../../../src/db/migrations/**/!(*.d).[jt]s`))], | ||
cli: { | ||
migrationsDir: normalize(`src/db/migrations`), | ||
}, | ||
logging: false, | ||
dropSchema: false, | ||
entities: [Dependency], | ||
}); | ||
}); | ||
|
||
it(`should generate options properly for migration`, async () => { | ||
const settings: IConnectionSettings = { | ||
databaseFilePath: tmpDir, | ||
migrationGenerationConfig: true, | ||
}; | ||
const connectionOptionsProvider = new ConnectionOptionsProvider(settings, [Dependency]); | ||
const options = connectionOptionsProvider.getConnectionOptions(); | ||
expect(options).toEqual({ | ||
name: `default`, | ||
type: `sqlite`, | ||
database: path.join(tmpDir, `cache.db`), | ||
synchronize: false, | ||
migrationsRun: true, | ||
migrations: [normalize(join(__dirname, `../../../../src/db/migrations/**/!(*.d).[jt]s`))], | ||
cli: { | ||
migrationsDir: normalize(`src/db/migrations`), | ||
}, | ||
logging: false, | ||
dropSchema: false, | ||
entities: [Dependency], | ||
}); | ||
}); | ||
|
||
it(`should cache connection options`, async () => { | ||
const settings: IConnectionSettings = { | ||
databaseFilePath: tmpDir, | ||
}; | ||
const connectionOptionsProvider = new ConnectionOptionsProvider(settings, []); | ||
const options = connectionOptionsProvider.getConnectionOptions(); | ||
const options2 = connectionOptionsProvider.getConnectionOptions(); | ||
expect(options).toBe(options2); | ||
}); | ||
}); |
Oops, something went wrong.