Merge pull request #26175 from backstage/jhaals/skipMigrations

backend-defaults: Add config options to skip database migrations
This commit is contained in:
Johan Haals
2024-08-27 11:13:15 +02:00
committed by GitHub
4 changed files with 82 additions and 9 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-defaults': patch
---
Added the option to skip database migrations by setting `skipMigrations: true` in config. This can be done globally in the database config or by plugin id.
+4
View File
@@ -453,6 +453,8 @@ export interface Config {
* and asyncStackTraces booleans
*/
knexConfig?: object;
/** Skip running database migrations. */
skipMigrations?: boolean;
/** Plugin specific database configuration and client override */
plugin?: {
[pluginId: string]: {
@@ -485,6 +487,8 @@ export interface Config {
knexConfig?: object;
/** Configures the ownership of newly created schemas in pg databases. */
role?: string;
/** Skip running database migrations. */
skipMigrations?: boolean;
};
};
};
@@ -96,27 +96,83 @@ 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(
new ConfigReader({ client: 'pg' }),
{ pg: connector },
{ migrations: { skip: true } },
);
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: {
database: { plugin: { plugin1: { skipMigrations: true } } },
},
}),
{
pg: connector,
},
);
expect((await impl.forPlugin('plugin1')).migrations).toEqual({
skip: true,
});
expect((await impl.forPlugin('plugin2')).migrations).toEqual({
skip: false,
});
const impl2 = new DatabaseManagerImpl(
new ConfigReader({
client: 'pg',
backend: {
database: {
skipMigrations: true,
plugin: { plugin1: { skipMigrations: false } },
},
},
}),
{
pg: connector,
},
);
expect((await impl2.forPlugin('plugin1')).migrations).toEqual({
skip: false,
});
expect((await impl2.forPlugin('plugin2')).migrations).toEqual({
skip: true,
});
});
@@ -86,8 +86,16 @@ export class DatabaseManagerImpl implements LegacyRootDatabaseService {
);
}
const getClient = () => this.getDatabase(pluginId, connector, deps);
const migrations = { skip: false, ...this.options?.migrations };
return { getClient, migrations };
const skip =
this.options?.migrations?.skip ??
this.config.getOptionalBoolean(
`backend.database.plugin.${pluginId}.skipMigrations`,
) ??
this.config.getOptionalBoolean('backend.database.skipMigrations') ??
false;
return { getClient, migrations: { skip } };
}
/**