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());
|
||||
|
||||
|
||||
@@ -1,45 +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 * as Knex from 'knex';
|
||||
|
||||
export async function up(knex: Knex): Promise<any> {
|
||||
return knex.schema
|
||||
.createTable('locations', table => {
|
||||
table
|
||||
.uuid('id')
|
||||
.primary()
|
||||
.comment('Auto-generated ID of the location');
|
||||
table
|
||||
.string('type')
|
||||
.notNullable()
|
||||
.comment('The type of location');
|
||||
table
|
||||
.string('target')
|
||||
.notNullable()
|
||||
.comment('The actual target of the location');
|
||||
})
|
||||
.createTable('components', table => {
|
||||
table
|
||||
.string('name')
|
||||
.primary()
|
||||
.comment('The name of the component');
|
||||
});
|
||||
}
|
||||
|
||||
export async function down(knex: Knex): Promise<any> {
|
||||
return knex.schema.dropTable('components').dropTable('locations');
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user