feat: support skip migration

Signed-off-by: Dominik Schwank <dominik.schwank@sda.se>
This commit is contained in:
Dominik Schwank
2022-09-01 11:03:32 +02:00
committed by Fredrik Adelöw
parent 81f0945f6e
commit 1e12c7f5f1
6 changed files with 36 additions and 28 deletions
+3 -6
View File
@@ -13,17 +13,14 @@ export function createRouter<T>(
options: RouterOptions<T>,
): Promise<express.Router>;
// @public (undocumented)
export function createUserSettingsStore(
database: PluginDatabaseManager,
): Promise<DatabaseUserSettingsStore>;
// @public
export class DatabaseUserSettingsStore
implements UserSettingsStore<Knex.Transaction>
{
// (undocumented)
static create(knex: Knex): Promise<DatabaseUserSettingsStore>;
static create(options: {
database: PluginDatabaseManager;
}): Promise<DatabaseUserSettingsStore>;
// (undocumented)
delete(
tx: Knex.Transaction<any, any[]>,
@@ -29,9 +29,18 @@ const databases = TestDatabases.create({
async function createStore(databaseId: TestDatabaseId) {
const knex = await databases.init(databaseId);
const databaseManager = {
getClient: async () => knex,
migrations: {
skip: false,
},
};
return {
knex,
storage: await DatabaseUserSettingsStore.create(knex),
storage: await DatabaseUserSettingsStore.create({
database: databaseManager,
}),
};
}
@@ -13,7 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { resolvePackagePath } from '@backstage/backend-common';
import {
PluginDatabaseManager,
resolvePackagePath,
} from '@backstage/backend-common';
import { NotFoundError } from '@backstage/errors';
import { Knex } from 'knex';
@@ -42,11 +45,19 @@ export type RawDbUserSettingsRow = {
export class DatabaseUserSettingsStore
implements UserSettingsStore<Knex.Transaction>
{
static async create(knex: Knex): Promise<DatabaseUserSettingsStore> {
await knex.migrate.latest({
directory: migrationsDir,
});
return new DatabaseUserSettingsStore(knex);
static async create(options: {
database: PluginDatabaseManager;
}): Promise<DatabaseUserSettingsStore> {
const { database } = options;
const client = await database.getClient();
if (!database.migrations?.skip) {
await client.migrate.latest({
directory: migrationsDir,
});
}
return new DatabaseUserSettingsStore(client);
}
private constructor(private readonly db: Knex) {}
@@ -13,8 +13,4 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export {
createRouter,
createUserSettingsStore,
type RouterOptions,
} from './router';
export { createRouter, type RouterOptions } from './router';
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { PluginDatabaseManager, errorHandler } from '@backstage/backend-common';
import { errorHandler } from '@backstage/backend-common';
import { AuthenticationError, InputError } from '@backstage/errors';
import {
getBearerTokenFromAuthorizationHeader,
@@ -22,14 +22,7 @@ import {
import express, { Request } from 'express';
import Router from 'express-promise-router';
import { DatabaseUserSettingsStore, UserSettingsStore } from '../database';
/**
* @public
*/
export async function createUserSettingsStore(database: PluginDatabaseManager) {
return await DatabaseUserSettingsStore.create(await database.getClient());
}
import { UserSettingsStore } from '../database';
/**
* @public
@@ -52,7 +52,9 @@ export async function startStandaloneServer(
} as IdentityClient;
const router = await createRouter({
userSettingsStore: await DatabaseUserSettingsStore.create(database),
userSettingsStore: await DatabaseUserSettingsStore.create({
database: { getClient: async () => database },
}),
identity: identityMock,
});