Merge pull request #813 from spotify/freben/inventory_move_migrations
Move inventory migrations into the plugin itself
This commit is contained in:
@@ -1 +0,0 @@
|
||||
dev.sqlite3
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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());
|
||||
|
||||
|
||||
@@ -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 });
|
||||
}
|
||||
|
||||
@@ -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<DatabaseInventory> {
|
||||
await database.migrate.latest({
|
||||
directory: path.resolve(__dirname, '..', 'migrations'),
|
||||
loadExtensions: ['.js'],
|
||||
});
|
||||
|
||||
return new DatabaseInventory(database);
|
||||
}
|
||||
|
||||
constructor(private readonly database: Knex) {}
|
||||
|
||||
async components(): Promise<Component[]> {
|
||||
|
||||
Reference in New Issue
Block a user