Make sure options take precedence
Signed-off-by: Johan Haals <johan.haals@gmail.com>
This commit is contained in:
@@ -96,17 +96,47 @@ describe('DatabaseManagerImpl', () => {
|
||||
expect(connector2.getClient).toHaveBeenLastCalledWith('plugin2', undefined);
|
||||
});
|
||||
|
||||
it('retains the migration skip info', async () => {
|
||||
it('migration skip options take precedence over config', async () => {
|
||||
const connector = {
|
||||
getClient: jest.fn(),
|
||||
dropDatabase: jest.fn(),
|
||||
} satisfies Connector;
|
||||
|
||||
const impl = new DatabaseManagerImpl(
|
||||
new ConfigReader({
|
||||
client: 'pg',
|
||||
backend: {
|
||||
database: {
|
||||
skipMigrations: true,
|
||||
plugin: { plugin1: { skipMigrations: true } },
|
||||
},
|
||||
},
|
||||
}),
|
||||
{
|
||||
pg: connector,
|
||||
},
|
||||
{ migrations: { skip: false } },
|
||||
);
|
||||
expect((await impl.forPlugin('plugin1')).migrations).toEqual({
|
||||
skip: false,
|
||||
});
|
||||
|
||||
const impl1 = new DatabaseManagerImpl(new ConfigReader({ client: 'pg' }), {
|
||||
pg: connector,
|
||||
});
|
||||
|
||||
const impl2 = new DatabaseManagerImpl(
|
||||
expect((await impl1.forPlugin('plugin1')).migrations).toEqual({
|
||||
skip: false,
|
||||
});
|
||||
});
|
||||
|
||||
it('plugin can skip migrations using config', async () => {
|
||||
const connector = {
|
||||
getClient: jest.fn(),
|
||||
dropDatabase: jest.fn(),
|
||||
} satisfies Connector;
|
||||
|
||||
const impl = new DatabaseManagerImpl(
|
||||
new ConfigReader({
|
||||
client: 'pg',
|
||||
backend: {
|
||||
@@ -118,15 +148,14 @@ describe('DatabaseManagerImpl', () => {
|
||||
},
|
||||
);
|
||||
|
||||
expect((await impl1.forPlugin('plugin1')).migrations).toEqual({
|
||||
expect((await impl.forPlugin('plugin1')).migrations).toEqual({
|
||||
skip: true,
|
||||
});
|
||||
expect((await impl.forPlugin('plugin2')).migrations).toEqual({
|
||||
skip: false,
|
||||
});
|
||||
|
||||
expect((await impl2.forPlugin('plugin1')).migrations).toEqual({
|
||||
skip: true,
|
||||
});
|
||||
|
||||
const impl3 = new DatabaseManagerImpl(
|
||||
const impl2 = new DatabaseManagerImpl(
|
||||
new ConfigReader({
|
||||
client: 'pg',
|
||||
backend: {
|
||||
@@ -140,7 +169,10 @@ describe('DatabaseManagerImpl', () => {
|
||||
pg: connector,
|
||||
},
|
||||
);
|
||||
expect((await impl3.forPlugin('plugin1')).migrations).toEqual({
|
||||
expect((await impl2.forPlugin('plugin1')).migrations).toEqual({
|
||||
skip: false,
|
||||
});
|
||||
expect((await impl2.forPlugin('plugin2')).migrations).toEqual({
|
||||
skip: true,
|
||||
});
|
||||
});
|
||||
|
||||
@@ -41,6 +41,7 @@ function pluginPath(pluginId: string): string {
|
||||
* @public
|
||||
*/
|
||||
export type DatabaseManagerOptions = {
|
||||
migrations?: DatabaseService['migrations'];
|
||||
logger?: LoggerService;
|
||||
};
|
||||
|
||||
@@ -86,16 +87,20 @@ export class DatabaseManagerImpl implements LegacyRootDatabaseService {
|
||||
}
|
||||
const getClient = () => this.getDatabase(pluginId, connector, deps);
|
||||
|
||||
const migrationConfig =
|
||||
this.config.getOptionalBoolean('backend.database.skipMigrations') ||
|
||||
this.config.getOptionalBoolean(
|
||||
`backend.database.plugin.${pluginId}.skipMigrations`,
|
||||
);
|
||||
const migrations = {
|
||||
skip: migrationConfig || false,
|
||||
};
|
||||
let skip = false;
|
||||
// config options take precedence over config
|
||||
if (this.options?.migrations?.skip !== undefined) {
|
||||
skip = this.options.migrations.skip;
|
||||
} else {
|
||||
skip =
|
||||
this.config.getOptionalBoolean(
|
||||
`backend.database.plugin.${pluginId}.skipMigrations`,
|
||||
) ??
|
||||
this.config.getOptionalBoolean('backend.database.skipMigrations') ??
|
||||
false;
|
||||
}
|
||||
|
||||
return { getClient, migrations };
|
||||
return { getClient, migrations: { skip } };
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user