Merge pull request #25113 from backstage/freben/no-connectors

databases: remove the connectors
This commit is contained in:
Fredrik Adelöw
2024-06-10 17:11:38 +02:00
committed by GitHub
5 changed files with 32 additions and 152 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-defaults': patch
---
Internal minor refactors of the database connectors
@@ -14,6 +14,6 @@
* limitations under the License.
*/
export * from './mysql';
export * from './postgres';
export * from './sqlite3';
export { MysqlConnector } from './mysql';
export { PgConnector } from './postgres';
export { Sqlite3Connector } from './sqlite3';
@@ -25,7 +25,7 @@ import knexFactory, { Knex } from 'knex';
import { merge, omit } from 'lodash';
import limiterFactory from 'p-limit';
import yn from 'yn';
import { Connector, DatabaseConnector } from '../types';
import { Connector } from '../types';
import defaultNameOverride from './defaultNameOverride';
import { mergeDatabaseConfig } from './mergeDatabaseConfig';
@@ -226,19 +226,6 @@ export async function dropMysqlDatabase(
}
}
/**
* MySQL database connector.
*
* Exposes database connector functionality via an immutable object.
*/
export const mysqlConnector: DatabaseConnector = Object.freeze({
createClient: createMysqlDatabaseClient,
ensureDatabaseExists: ensureMysqlDatabaseExists,
createNameOverride: defaultNameOverride,
parseConnectionString: parseMysqlConnectionString,
dropDatabase: dropMysqlDatabase,
});
/**
* Provides a config lookup path for a plugin's config block.
*/
@@ -248,31 +235,16 @@ function pluginPath(pluginId: string): string {
function normalizeConnection(
connection: Knex.StaticConnectionConfig | JsonObject | string | undefined,
client: string,
): Partial<Knex.StaticConnectionConfig> {
if (typeof connection === 'undefined' || connection === null) {
return {};
}
return typeof connection === 'string' || connection instanceof String
? mysqlConnector.parseConnectionString(connection as string, client)
? parseMysqlConnectionString(connection as string)
: connection;
}
function createNameOverride(
client: string,
name: string,
): Partial<Knex.Config> {
try {
return mysqlConnector.createNameOverride(name);
} catch (e) {
throw new InputError(
`Unable to create database name override for '${client}' connector`,
e,
);
}
}
export class MysqlConnector implements Connector {
constructor(
private readonly config: Config,
@@ -281,7 +253,7 @@ export class MysqlConnector implements Connector {
async getClient(
pluginId: string,
deps?: {
_deps?: {
lifecycle: LifecycleService;
pluginMetadata: PluginMetadataService;
},
@@ -293,7 +265,7 @@ export class MysqlConnector implements Connector {
const databaseName = this.getDatabaseName(pluginId);
if (databaseName && this.getEnsureExistsConfig(pluginId)) {
try {
await mysqlConnector.ensureDatabaseExists!(pluginConfig, databaseName);
await ensureMysqlDatabaseExists(pluginConfig, databaseName);
} catch (error) {
throw new Error(
`Failed to connect to the database to make sure that '${databaseName}' exists, ${error}`,
@@ -313,10 +285,9 @@ export class MysqlConnector implements Connector {
this.getDatabaseOverrides(pluginId),
);
const client = mysqlConnector.createClient(
const client = createMysqlDatabaseClient(
pluginConfig,
databaseClientOverrides,
deps,
);
return client;
@@ -418,12 +389,9 @@ export class MysqlConnector implements Connector {
* unless `pluginDivisionMode` is set to `schema`.
*/
private getConnectionConfig(pluginId: string): Knex.StaticConnectionConfig {
const { client, overridden } = this.getClientType(pluginId);
const { overridden } = this.getClientType(pluginId);
let baseConnection = normalizeConnection(
this.config.get('connection'),
this.config.getString('client'),
);
let baseConnection = normalizeConnection(this.config.get('connection'));
// Databases cannot be shared unless the `pluginDivisionMode` is set to `schema`. The
// `database` property from the base connection is omitted unless `pluginDivisionMode`
@@ -435,7 +403,6 @@ export class MysqlConnector implements Connector {
// get and normalize optional plugin specific database connection
const connection = normalizeConnection(
this.config.getOptional(`${pluginPath(pluginId)}.connection`),
client,
);
return {
@@ -473,8 +440,6 @@ export class MysqlConnector implements Connector {
*/
private getDatabaseOverrides(pluginId: string): Knex.Config {
const databaseName = this.getDatabaseName(pluginId);
return databaseName
? createNameOverride(this.getClientType(pluginId).client, databaseName)
: {};
return databaseName ? defaultNameOverride(databaseName) : {};
}
}
@@ -19,13 +19,13 @@ import {
PluginMetadataService,
} from '@backstage/backend-plugin-api';
import { Config, ConfigReader } from '@backstage/config';
import { ForwardedError, InputError } from '@backstage/errors';
import { ForwardedError } from '@backstage/errors';
import { JsonObject } from '@backstage/types';
import knexFactory, { Knex } from 'knex';
import { merge, omit } from 'lodash';
import limiterFactory from 'p-limit';
import { Client } from 'pg';
import { Connector, DatabaseConnector } from '../types';
import { Connector } from '../types';
import defaultNameOverride from './defaultNameOverride';
import defaultSchemaOverride from './defaultSchemaOverride';
import { mergeDatabaseConfig } from './mergeDatabaseConfig';
@@ -232,16 +232,6 @@ export async function dropPgDatabase(
}
}
export const pgConnector: DatabaseConnector = Object.freeze({
createClient: createPgDatabaseClient,
ensureDatabaseExists: ensurePgDatabaseExists,
ensureSchemaExists: ensurePgSchemaExists,
createNameOverride: defaultNameOverride,
createSchemaOverride: defaultSchemaOverride,
parseConnectionString: parsePgConnectionString,
dropDatabase: dropPgDatabase,
});
/**
* Provides a config lookup path for a plugin's config block.
*/
@@ -251,45 +241,16 @@ function pluginPath(pluginId: string): string {
function normalizeConnection(
connection: Knex.StaticConnectionConfig | JsonObject | string | undefined,
client: string,
): Partial<Knex.StaticConnectionConfig> {
if (typeof connection === 'undefined' || connection === null) {
return {};
}
return typeof connection === 'string' || connection instanceof String
? pgConnector.parseConnectionString(connection as string, client)
? parsePgConnectionString(connection as string)
: connection;
}
function createSchemaOverride(
client: string,
name: string,
): Partial<Knex.Config | undefined> {
try {
return pgConnector.createSchemaOverride?.(name);
} catch (e) {
throw new InputError(
`Unable to create database schema override for '${client}' connector`,
e,
);
}
}
function createNameOverride(
client: string,
name: string,
): Partial<Knex.Config> {
try {
return pgConnector.createNameOverride(name);
} catch (e) {
throw new InputError(
`Unable to create database name override for '${client}' connector`,
e,
);
}
}
export class PgConnector implements Connector {
constructor(
private readonly config: Config,
@@ -298,7 +259,7 @@ export class PgConnector implements Connector {
async getClient(
pluginId: string,
deps?: {
_deps?: {
lifecycle: LifecycleService;
pluginMetadata: PluginMetadataService;
},
@@ -310,7 +271,7 @@ export class PgConnector implements Connector {
const databaseName = this.getDatabaseName(pluginId);
if (databaseName && this.getEnsureExistsConfig(pluginId)) {
try {
await pgConnector.ensureDatabaseExists!(pluginConfig, databaseName);
await ensurePgDatabaseExists(pluginConfig, databaseName);
} catch (error) {
throw new Error(
`Failed to connect to the database to make sure that '${databaseName}' exists, ${error}`,
@@ -320,13 +281,13 @@ export class PgConnector implements Connector {
let schemaOverrides;
if (this.getPluginDivisionModeConfig() === 'schema') {
schemaOverrides = this.getSchemaOverrides(pluginId);
schemaOverrides = defaultSchemaOverride(pluginId);
if (
this.getEnsureSchemaExistsConfig(pluginId) ||
this.getEnsureExistsConfig(pluginId)
) {
try {
await pgConnector.ensureSchemaExists!(pluginConfig, pluginId);
await ensurePgSchemaExists(pluginConfig, pluginId);
} catch (error) {
throw new Error(
`Failed to connect to the database to make sure that schema for plugin '${pluginId}' exists, ${error}`,
@@ -341,10 +302,9 @@ export class PgConnector implements Connector {
schemaOverrides,
);
const client = pgConnector.createClient(
const client = createPgDatabaseClient(
pluginConfig,
databaseClientOverrides,
deps,
);
return client;
@@ -465,12 +425,9 @@ export class PgConnector implements Connector {
* unless `pluginDivisionMode` is set to `schema`.
*/
private getConnectionConfig(pluginId: string): Knex.StaticConnectionConfig {
const { client, overridden } = this.getClientType(pluginId);
const { overridden } = this.getClientType(pluginId);
let baseConnection = normalizeConnection(
this.config.get('connection'),
this.config.getString('client'),
);
let baseConnection = normalizeConnection(this.config.get('connection'));
// Databases cannot be shared unless the `pluginDivisionMode` is set to `schema`. The
// `database` property from the base connection is omitted unless `pluginDivisionMode`
@@ -482,7 +439,6 @@ export class PgConnector implements Connector {
// get and normalize optional plugin specific database connection
const connection = normalizeConnection(
this.config.getOptional(`${pluginPath(pluginId)}.connection`),
client,
);
(
@@ -516,17 +472,6 @@ export class PgConnector implements Connector {
};
}
/**
* Provides a partial `Knex.Config` database schema override for a given
* plugin.
*
* @param pluginId - Target plugin to get database schema override
* @returns Partial `Knex.Config` with database schema override
*/
private getSchemaOverrides(pluginId: string): Knex.Config | undefined {
return createSchemaOverride(this.getClientType(pluginId).client, pluginId);
}
/**
* Provides a partial `Knex.Config`• database name override for a given plugin.
*
@@ -535,8 +480,6 @@ export class PgConnector implements Connector {
*/
private getDatabaseOverrides(pluginId: string): Knex.Config {
const databaseName = this.getDatabaseName(pluginId);
return databaseName
? createNameOverride(this.getClientType(pluginId).client, databaseName)
: {};
return databaseName ? defaultNameOverride(databaseName) : {};
}
}
@@ -20,13 +20,12 @@ import {
PluginMetadataService,
} from '@backstage/backend-plugin-api';
import { Config, ConfigReader } from '@backstage/config';
import { InputError } from '@backstage/errors';
import { JsonObject } from '@backstage/types';
import { ensureDirSync } from 'fs-extra';
import knexFactory, { Knex } from 'knex';
import { merge, omit } from 'lodash';
import path from 'path';
import { Connector, DatabaseConnector } from '../types';
import { Connector } from '../types';
import { mergeDatabaseConfig } from './mergeDatabaseConfig';
/**
@@ -157,17 +156,6 @@ export function parseSqliteConnectionString(
};
}
/**
* SQLite3 database connector.
*
* Exposes database connector functionality via an immutable object.
*/
export const sqliteConnector: DatabaseConnector = Object.freeze({
createClient: createSqliteDatabaseClient,
createNameOverride: createSqliteNameOverride,
parseConnectionString: parseSqliteConnectionString,
});
/**
* Provides a config lookup path for a plugin's config block.
*/
@@ -177,31 +165,16 @@ function pluginPath(pluginId: string): string {
function normalizeConnection(
connection: Knex.StaticConnectionConfig | JsonObject | string | undefined,
client: string,
): Partial<Knex.StaticConnectionConfig> {
if (typeof connection === 'undefined' || connection === null) {
return {};
}
return typeof connection === 'string' || connection instanceof String
? sqliteConnector.parseConnectionString(connection as string, client)
? parseSqliteConnectionString(connection as string)
: connection;
}
function createNameOverride(
client: string,
name: string,
): Partial<Knex.Config> {
try {
return sqliteConnector.createNameOverride(name);
} catch (e) {
throw new InputError(
`Unable to create database name override for '${client}' connector`,
e,
);
}
}
export class Sqlite3Connector implements Connector {
constructor(private readonly config: Config) {}
@@ -228,7 +201,7 @@ export class Sqlite3Connector implements Connector {
this.getDatabaseOverrides(pluginId),
);
const client = sqliteConnector.createClient(
const client = createSqliteDatabaseClient(
pluginConfig,
databaseClientOverrides,
deps,
@@ -340,10 +313,7 @@ export class Sqlite3Connector implements Connector {
private getConnectionConfig(pluginId: string): Knex.StaticConnectionConfig {
const { client, overridden } = this.getClientType(pluginId);
let baseConnection = normalizeConnection(
this.config.get('connection'),
this.config.getString('client'),
);
let baseConnection = normalizeConnection(this.config.get('connection'));
if (
client.includes('sqlite3') &&
@@ -366,7 +336,6 @@ export class Sqlite3Connector implements Connector {
// get and normalize optional plugin specific database connection
const connection = normalizeConnection(
this.config.getOptional(`${pluginPath(pluginId)}.connection`),
client,
);
return {
@@ -404,8 +373,6 @@ export class Sqlite3Connector implements Connector {
*/
private getDatabaseOverrides(pluginId: string): Knex.Config {
const databaseName = this.getDatabaseName(pluginId);
return databaseName
? createNameOverride(this.getClientType(pluginId).client, databaseName)
: {};
return databaseName ? createSqliteNameOverride(databaseName) : {};
}
}