Let the Backend manage creating databases
This commit is contained in:
@@ -27,9 +27,9 @@ describe('database connection', () => {
|
||||
]);
|
||||
|
||||
describe(createDatabaseClient, () => {
|
||||
it('returns a postgres connection', async () => {
|
||||
it('returns a postgres connection', () => {
|
||||
expect(
|
||||
await createDatabaseClient(
|
||||
createDatabaseClient(
|
||||
createConfig({
|
||||
client: 'pg',
|
||||
connection: {
|
||||
@@ -43,9 +43,9 @@ describe('database connection', () => {
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
it('returns an sqlite connection', async () => {
|
||||
it('returns an sqlite connection', () => {
|
||||
expect(
|
||||
await createDatabaseClient(
|
||||
createDatabaseClient(
|
||||
createConfig({
|
||||
client: 'sqlite3',
|
||||
connection: ':memory:',
|
||||
@@ -55,25 +55,24 @@ describe('database connection', () => {
|
||||
});
|
||||
|
||||
it('tries to create a mysql connection as a passthrough', () => {
|
||||
expect(
|
||||
async () =>
|
||||
await createDatabaseClient(
|
||||
createConfig({
|
||||
client: 'mysql',
|
||||
connection: {
|
||||
host: '127.0.0.1',
|
||||
user: 'foo',
|
||||
password: 'bar',
|
||||
database: 'dbname',
|
||||
},
|
||||
}),
|
||||
),
|
||||
expect(() =>
|
||||
createDatabaseClient(
|
||||
createConfig({
|
||||
client: 'mysql',
|
||||
connection: {
|
||||
host: '127.0.0.1',
|
||||
user: 'foo',
|
||||
password: 'bar',
|
||||
database: 'dbname',
|
||||
},
|
||||
}),
|
||||
),
|
||||
).toThrowError(/Cannot find module 'mysql'/);
|
||||
});
|
||||
|
||||
it('accepts overrides', async () => {
|
||||
it('accepts overrides', () => {
|
||||
expect(
|
||||
await createDatabaseClient(
|
||||
createDatabaseClient(
|
||||
createConfig({
|
||||
client: 'pg',
|
||||
connection: {
|
||||
@@ -93,24 +92,22 @@ describe('database connection', () => {
|
||||
});
|
||||
|
||||
it('throws an error without a client', () => {
|
||||
expect(
|
||||
async () =>
|
||||
await createDatabaseClient(
|
||||
createConfig({
|
||||
connection: '',
|
||||
}),
|
||||
),
|
||||
expect(() =>
|
||||
createDatabaseClient(
|
||||
createConfig({
|
||||
connection: '',
|
||||
}),
|
||||
),
|
||||
).toThrowError();
|
||||
});
|
||||
|
||||
it('throws an error without a connection', () => {
|
||||
expect(
|
||||
async () =>
|
||||
await createDatabaseClient(
|
||||
createConfig({
|
||||
client: 'pg',
|
||||
}),
|
||||
),
|
||||
expect(() =>
|
||||
createDatabaseClient(
|
||||
createConfig({
|
||||
client: 'pg',
|
||||
}),
|
||||
),
|
||||
).toThrowError();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -17,7 +17,7 @@
|
||||
import knex from 'knex';
|
||||
import { Config } from '@backstage/config';
|
||||
import { mergeDatabaseConfig } from './config';
|
||||
import { createPgDatabaseClient } from './postgres';
|
||||
import { createPgDatabaseClient, ensurePgDatabaseExists } from './postgres';
|
||||
import { createSqliteDatabaseClient } from './sqlite3';
|
||||
|
||||
type DatabaseClient = 'pg' | 'sqlite3' | string;
|
||||
@@ -28,7 +28,7 @@ type DatabaseClient = 'pg' | 'sqlite3' | string;
|
||||
* @param dbConfig The database config
|
||||
* @param overrides Additional options to merge with the config
|
||||
*/
|
||||
export async function createDatabaseClient(
|
||||
export function createDatabaseClient(
|
||||
dbConfig: Config,
|
||||
overrides?: Partial<knex.Config>,
|
||||
) {
|
||||
@@ -48,3 +48,19 @@ export async function createDatabaseClient(
|
||||
* @deprecated Use createDatabaseClient instead
|
||||
*/
|
||||
export const createDatabase = createDatabaseClient;
|
||||
|
||||
/**
|
||||
* Ensures that the given databases all exist, creating them if they do not.
|
||||
*/
|
||||
export async function ensureDatabaseExists(
|
||||
dbConfig: Config,
|
||||
...databases: Array<string>
|
||||
) {
|
||||
const client: DatabaseClient = dbConfig.getString('client');
|
||||
|
||||
if (client === 'pg') {
|
||||
return ensurePgDatabaseExists(dbConfig, ...databases);
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -164,9 +164,9 @@ describe('postgres', () => {
|
||||
});
|
||||
|
||||
describe(createPgDatabaseClient, () => {
|
||||
it('creates a postgres knex instance', async () => {
|
||||
it('creates a postgres knex instance', () => {
|
||||
expect(
|
||||
await createPgDatabaseClient(
|
||||
createPgDatabaseClient(
|
||||
createConfig({
|
||||
host: 'acme',
|
||||
user: 'foo',
|
||||
@@ -177,14 +177,13 @@ describe('postgres', () => {
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
it('attempts to read an ssl cert', async () => {
|
||||
expect(
|
||||
async () =>
|
||||
await createPgDatabaseClient(
|
||||
createConfig(
|
||||
'postgresql://postgres:pass@localhost:5432/dbname?sslrootcert=/path/to/file',
|
||||
),
|
||||
it('attempts to read an ssl cert', () => {
|
||||
expect(() =>
|
||||
createPgDatabaseClient(
|
||||
createConfig(
|
||||
'postgresql://postgres:pass@localhost:5432/dbname?sslrootcert=/path/to/file',
|
||||
),
|
||||
),
|
||||
).toThrowError(/no such file or directory/);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,37 +15,26 @@
|
||||
*/
|
||||
|
||||
import knex, { PgConnectionConfig } from 'knex';
|
||||
import { cloneDeep } from 'lodash';
|
||||
import { Config, JsonValue } from '@backstage/config';
|
||||
import { Config } from '@backstage/config';
|
||||
import { mergeDatabaseConfig } from './config';
|
||||
|
||||
/**
|
||||
* Creates a knex Postgres database connection
|
||||
* Creates a knex postgres database connection
|
||||
*
|
||||
* @param dbConfig The database config
|
||||
* @param overrides Additional options to merge with the config
|
||||
*/
|
||||
export async function createPgDatabaseClient(
|
||||
export function createPgDatabaseClient(
|
||||
dbConfig: Config,
|
||||
overrides?: knex.Config,
|
||||
) {
|
||||
const baseConfig = buildPgDatabaseConfig(dbConfig, overrides);
|
||||
const knexConfig = baseConfig;
|
||||
|
||||
// Bootstrap the missing database.
|
||||
if (!!baseConfig?.connection.database) {
|
||||
const knexAdminConfig = buildPgDatabaseAdminConfig(cloneDeep(baseConfig));
|
||||
const admin = knex(knexAdminConfig);
|
||||
|
||||
await ensurePgDatabase(admin, baseConfig.connection.database);
|
||||
}
|
||||
|
||||
const knexConfig = buildPgDatabaseConfig(dbConfig, overrides);
|
||||
const database = knex(knexConfig);
|
||||
return database;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a knex Postgres database connection
|
||||
* Builds a knex postgres database connection
|
||||
*
|
||||
* @param dbConfig The database config
|
||||
* @param overrides Additional options to merge with the config
|
||||
@@ -55,7 +44,7 @@ export function buildPgDatabaseConfig(
|
||||
overrides?: knex.Config,
|
||||
) {
|
||||
return mergeDatabaseConfig(
|
||||
cloneDeep(dbConfig.get()),
|
||||
dbConfig.get(),
|
||||
{
|
||||
connection: getPgConnectionConfig(dbConfig, !!overrides),
|
||||
useNullAsDefault: true,
|
||||
@@ -65,23 +54,10 @@ export function buildPgDatabaseConfig(
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds a knex Postgres database connection for database creation
|
||||
* Gets the postgres connection config
|
||||
*
|
||||
* @param dbConfig The database config
|
||||
*/
|
||||
function buildPgDatabaseAdminConfig(dbConfig: JsonValue) {
|
||||
return mergeDatabaseConfig(dbConfig, {
|
||||
connection: {
|
||||
database: 'postgres',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Postgres connection config
|
||||
*
|
||||
* @param dbConfig The database config
|
||||
* @param parseConnectionString Flag to explicitly control connection string parsing
|
||||
* @param parseConnectionString Flag to explictly control connection string parsing
|
||||
*/
|
||||
export function getPgConnectionConfig(
|
||||
dbConfig: Config,
|
||||
@@ -101,25 +77,6 @@ export function getPgConnectionConfig(
|
||||
: connection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the missing Postgres database if it does not exist
|
||||
*
|
||||
* @param admin The administrative database connection, defaulting to the `postgres` database
|
||||
* @param database The name of the database to create
|
||||
*/
|
||||
async function ensurePgDatabase(admin: knex, database: string) {
|
||||
const result = await admin
|
||||
.from('pg_database')
|
||||
.where('datname', database)
|
||||
.count<Record<string, { count: string }>>();
|
||||
|
||||
if (parseInt(result[0].count, 10) > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
await admin.raw(`CREATE DATABASE ??`, [database]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a connection string using pg-connection-string
|
||||
*
|
||||
@@ -138,3 +95,39 @@ function requirePgConnectionString() {
|
||||
throw new Error(`${message}\n${e.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the missing Postgres database if it does not exist
|
||||
*
|
||||
* @param dbConfig The database config
|
||||
* @param databases The name of the databases to create
|
||||
*/
|
||||
export async function ensurePgDatabaseExists(
|
||||
dbConfig: Config,
|
||||
...databases: Array<string>
|
||||
) {
|
||||
const admin = createPgDatabaseClient(dbConfig, {
|
||||
connection: {
|
||||
database: 'postgres',
|
||||
},
|
||||
});
|
||||
|
||||
try {
|
||||
const ensureDatabase = async (database: string) => {
|
||||
const result = await admin
|
||||
.from('pg_database')
|
||||
.where('datname', database)
|
||||
.count<Record<string, { count: string }>>();
|
||||
|
||||
if (parseInt(result[0].count, 10) > 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
await admin.raw(`CREATE DATABASE ??`, [database]);
|
||||
};
|
||||
|
||||
await Promise.all(databases.map(ensureDatabase));
|
||||
} finally {
|
||||
await admin.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user