Add options with migrations category
Signed-off-by: Marcus Eide <eide@spotify.com>
This commit is contained in:
@@ -58,20 +58,22 @@ describe('DatabaseManager', () => {
|
||||
expect(getConfigSpy).toHaveBeenCalledWith('backend.database');
|
||||
});
|
||||
|
||||
it('runMigrations defaults to true', () => {
|
||||
it('handles default options', () => {
|
||||
const config = new ConfigReader(backendConfig);
|
||||
const database = DatabaseManager.fromConfig(config);
|
||||
const client = database.forPlugin('test');
|
||||
|
||||
expect(client.runMigrations).toBe(true);
|
||||
expect(client.migrations?.apply).toBe(true);
|
||||
});
|
||||
|
||||
it('runMigrations can be set', () => {
|
||||
it('handles migrations options', () => {
|
||||
const config = new ConfigReader(backendConfig);
|
||||
const database = DatabaseManager.fromConfig(config, false);
|
||||
const database = DatabaseManager.fromConfig(config, {
|
||||
migrations: { apply: false },
|
||||
});
|
||||
const client = database.forPlugin('test');
|
||||
|
||||
expect(client.runMigrations).toBe(false);
|
||||
expect(client.migrations?.apply).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -36,6 +36,10 @@ function pluginPath(pluginId: string): string {
|
||||
return `plugin.${pluginId}`;
|
||||
}
|
||||
|
||||
type Options = {
|
||||
migrations?: PluginDatabaseManager['migrations'];
|
||||
};
|
||||
|
||||
/** @public */
|
||||
export class DatabaseManager {
|
||||
/**
|
||||
@@ -47,22 +51,22 @@ export class DatabaseManager {
|
||||
* names if config is not provided.
|
||||
*
|
||||
* @param config - The loaded application configuration.
|
||||
* @param runMigrations - Controls whether or not to perform database migrations.
|
||||
* @param options - An optional configuration object.
|
||||
*/
|
||||
static fromConfig(config: Config, runMigrations?: boolean): DatabaseManager {
|
||||
static fromConfig(config: Config, options?: Options): DatabaseManager {
|
||||
const databaseConfig = config.getConfig('backend.database');
|
||||
|
||||
return new DatabaseManager(
|
||||
databaseConfig,
|
||||
databaseConfig.getOptionalString('prefix'),
|
||||
runMigrations,
|
||||
options,
|
||||
);
|
||||
}
|
||||
|
||||
private constructor(
|
||||
private readonly config: Config,
|
||||
private readonly prefix: string = 'backstage_plugin_',
|
||||
private readonly runMigrations: boolean = true,
|
||||
private readonly options?: Options,
|
||||
) {}
|
||||
|
||||
/**
|
||||
@@ -74,12 +78,15 @@ export class DatabaseManager {
|
||||
*/
|
||||
forPlugin(pluginId: string): PluginDatabaseManager {
|
||||
const _this = this;
|
||||
const defaultMigrationOptions = {
|
||||
apply: true,
|
||||
};
|
||||
|
||||
return {
|
||||
getClient(): Promise<Knex> {
|
||||
return _this.getDatabase(pluginId);
|
||||
},
|
||||
runMigrations: _this.runMigrations,
|
||||
migrations: _this.options?.migrations ?? defaultMigrationOptions,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -32,12 +32,19 @@ export interface PluginDatabaseManager {
|
||||
getClient(): Promise<Knex>;
|
||||
|
||||
/**
|
||||
* runMigrations can be used to determine if database migrations
|
||||
* should be performed.
|
||||
*
|
||||
* Useful if connecting to a read-only database.
|
||||
* This optional property is used to control the behavior of database migrations.
|
||||
*/
|
||||
runMigrations: boolean;
|
||||
migrations?: {
|
||||
/**
|
||||
* apply can be used to determine if database migrations
|
||||
* should be performed.
|
||||
*
|
||||
* Useful if connecting to a read-only database.
|
||||
*
|
||||
* @default true
|
||||
*/
|
||||
apply: boolean;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -336,7 +336,7 @@ export class NextCatalogBuilder {
|
||||
const parser = this.parser || defaultEntityDataParser;
|
||||
|
||||
const dbClient = await database.getClient();
|
||||
if (database.runMigrations) {
|
||||
if (database.migrations?.apply) {
|
||||
logger.info('Performing database migration');
|
||||
await applyDatabaseMigrations(dbClient);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user