feat(db-config): support a ensureExists config option

Signed-off-by: Phil Kuang <pkuang@factset.com>
This commit is contained in:
Phil Kuang
2021-07-08 17:53:19 -04:00
parent 9eccc589b7
commit 5f6f2fd96f
3 changed files with 43 additions and 7 deletions
+16
View File
@@ -0,0 +1,16 @@
---
'@backstage/backend-common': patch
---
Support a `ensureExists` config option to skip ensuring a configured database exists. This allows deployment scenarios where
limited permissions are given for provisioned databases without privileges to create new databases. If set to `false`, the
database connection will not be validated prior to use which means the backend will not attempt to create the database if it
doesn't exist. You can configure this in your app-config.yaml:
```yaml
backend:
database:
ensureExists: false
```
This defaults to `true` if unspecified. You can also configure this per plugin connection and will override the base option.
+10
View File
@@ -64,6 +64,11 @@ export interface Config {
connection: string | object;
/** Database name prefix override */
prefix?: string;
/**
* Whether to ensure the given database exists by creating it if it does not.
* Defaults to true if unspecified.
*/
ensureExists?: boolean;
/** Plugin specific database configuration and client override */
plugin?: {
[pluginId: string]: {
@@ -74,6 +79,11 @@ export interface Config {
* @secret
*/
connection?: string | object;
/**
* Whether to ensure the given database exists by creating it if it does not.
* Defaults to base config if unspecified.
*/
ensureExists?: boolean;
};
};
};
@@ -128,6 +128,14 @@ export class DatabaseManager {
};
}
private getEnsureExistsConfig(pluginId: string): boolean {
const baseConfig = this.config.getOptionalBoolean('ensureExists') ?? true;
return (
this.config.getOptionalBoolean(`${pluginPath(pluginId)}.ensureExists`) ??
baseConfig
);
}
/**
* Provides a Knex connection plugin config by combining base and plugin config.
*
@@ -203,13 +211,15 @@ export class DatabaseManager {
this.getConfigForPlugin(pluginId) as JsonObject,
);
const databaseName = this.getDatabaseName(pluginId);
try {
await ensureDatabaseExists(pluginConfig, databaseName);
} catch (error) {
throw new Error(
`Failed to connect to the database to make sure that '${databaseName}' exists, ${error}`,
);
if (this.getEnsureExistsConfig(pluginId)) {
const databaseName = this.getDatabaseName(pluginId);
try {
await ensureDatabaseExists(pluginConfig, databaseName);
} catch (error) {
throw new Error(
`Failed to connect to the database to make sure that '${databaseName}' exists, ${error}`,
);
}
}
return createDatabaseClient(