From 52c48ce1a7e3daac1c08647e2166a0021cfd64fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Mon, 11 May 2020 22:06:38 +0200 Subject: [PATCH] Move inventory migrations into the plugin itself --- packages/backend/.gitignore | 1 - packages/backend/src/database.ts | 39 ------------------- packages/backend/src/index.ts | 22 ++++++----- packages/backend/src/plugins/inventory.ts | 2 +- .../src/inventory/DatabaseInventory.ts | 10 +++++ .../src/migrations/20200511113813_init.ts | 0 6 files changed, 24 insertions(+), 50 deletions(-) delete mode 100644 packages/backend/.gitignore delete mode 100644 packages/backend/src/database.ts rename {packages/backend => plugins/inventory-backend}/src/migrations/20200511113813_init.ts (100%) diff --git a/packages/backend/.gitignore b/packages/backend/.gitignore deleted file mode 100644 index 1097a1ff3c..0000000000 --- a/packages/backend/.gitignore +++ /dev/null @@ -1 +0,0 @@ -dev.sqlite3 diff --git a/packages/backend/src/database.ts b/packages/backend/src/database.ts deleted file mode 100644 index afd46b87e7..0000000000 --- a/packages/backend/src/database.ts +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 2020 Spotify AB - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import knex from 'knex'; -import path from 'path'; -import { Logger } from 'winston'; - -export async function buildDatabase(logger: Logger) { - logger.info('Connecting to database'); - const db = knex({ - client: 'sqlite3', - connection: { - filename: './dev.sqlite3', - }, - useNullAsDefault: true, - }); - - logger.info('Running database migrations'); - await db.migrate.latest({ - directory: path.resolve(__dirname, 'migrations'), - loadExtensions: ['.js'], - }); - - logger.info('Database initialization complete'); - return db; -} diff --git a/packages/backend/src/index.ts b/packages/backend/src/index.ts index 3033bc095e..b917c6c559 100644 --- a/packages/backend/src/index.ts +++ b/packages/backend/src/index.ts @@ -32,7 +32,7 @@ import compression from 'compression'; import cors from 'cors'; import express from 'express'; import helmet from 'helmet'; -import { buildDatabase } from './database'; +import knex from 'knex'; import inventory from './plugins/inventory'; import scaffolder from './plugins/scaffolder'; import { PluginEnvironment } from './types'; @@ -40,14 +40,18 @@ import { PluginEnvironment } from './types'; const DEFAULT_PORT = 7000; const PORT = parseInt(process.env.PORT ?? '', 10) || DEFAULT_PORT; -async function main() { - const database = await buildDatabase(getRootLogger()); - - const pluginEnvironment: PluginEnvironment = { - logger: getRootLogger().child({ type: 'plugin' }), - database, +function createEnv(plugin: string): PluginEnvironment { + return { + logger: getRootLogger().child({ type: 'plugin', plugin }), + database: knex({ + client: 'sqlite3', + connection: ':memory:', + useNullAsDefault: true, + }), }; +} +async function main() { const app = express(); app.use(helmet()); @@ -55,8 +59,8 @@ async function main() { app.use(compression()); app.use(express.json()); app.use(requestLoggingHandler()); - app.use('/inventory', await inventory(pluginEnvironment)); - app.use('/scaffolder', await scaffolder(pluginEnvironment)); + app.use('/inventory', await inventory(createEnv('inventory'))); + app.use('/scaffolder', await scaffolder(createEnv('scaffolder'))); app.use(notFoundHandler()); app.use(errorHandler()); diff --git a/packages/backend/src/plugins/inventory.ts b/packages/backend/src/plugins/inventory.ts index bf382df2ce..96d3fc52e7 100644 --- a/packages/backend/src/plugins/inventory.ts +++ b/packages/backend/src/plugins/inventory.ts @@ -21,6 +21,6 @@ import { import { PluginEnvironment } from '../types'; export default async function({ logger, database }: PluginEnvironment) { - const inventory = new DatabaseInventory(database); + const inventory = await DatabaseInventory.create(database); return await createRouter({ inventory, logger }); } diff --git a/plugins/inventory-backend/src/inventory/DatabaseInventory.ts b/plugins/inventory-backend/src/inventory/DatabaseInventory.ts index 0afa1a6bcf..58e2666552 100644 --- a/plugins/inventory-backend/src/inventory/DatabaseInventory.ts +++ b/plugins/inventory-backend/src/inventory/DatabaseInventory.ts @@ -15,11 +15,21 @@ */ import { NotFoundError } from '@backstage/backend-common'; +import path from 'path'; import Knex from 'knex'; import { v4 as uuidv4 } from 'uuid'; import { AddLocationRequest, Component, Inventory, Location } from './types'; export class DatabaseInventory implements Inventory { + static async create(database: Knex): Promise { + await database.migrate.latest({ + directory: path.resolve(__dirname, '..', 'migrations'), + loadExtensions: ['.js'], + }); + + return new DatabaseInventory(database); + } + constructor(private readonly database: Knex) {} async components(): Promise { diff --git a/packages/backend/src/migrations/20200511113813_init.ts b/plugins/inventory-backend/src/migrations/20200511113813_init.ts similarity index 100% rename from packages/backend/src/migrations/20200511113813_init.ts rename to plugins/inventory-backend/src/migrations/20200511113813_init.ts