Merge pull request #1280 from spotify/rugvip/migrate-js

plugins/catalog-backend: move migrations to JS with type annotations
This commit is contained in:
Patrik Oldsberg
2020-06-15 11:08:57 +02:00
committed by GitHub
8 changed files with 53 additions and 26 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ async function getConfig() {
for (const pkg of packages) {
const mainSrc = pkg.get('main:src');
if (mainSrc) {
moduleNameMapper[pkg.name] = path.resolve(pkg.location, mainSrc);
moduleNameMapper[`^${pkg.name}$`] = path.resolve(pkg.location, mainSrc);
}
}
}
@@ -14,9 +14,12 @@
* limitations under the License.
*/
import * as Knex from 'knex';
// @ts-check
export async function up(knex: Knex): Promise<any> {
/**
* @param {import('knex')} knex
*/
exports.up = async function up(knex) {
return (
knex.schema
//
@@ -114,9 +117,12 @@ export async function up(knex: Knex): Promise<any> {
.comment('The corresponding value to match on');
})
);
}
};
export async function down(knex: Knex): Promise<any> {
/**
* @param {import('knex')} knex
*/
exports.down = async function down(knex) {
return knex.schema
.dropTable('entities_search')
.alterTable('entities', table => {
@@ -124,4 +130,4 @@ export async function down(knex: Knex): Promise<any> {
})
.dropTable('entities')
.dropTable('locations');
}
};
@@ -13,16 +13,17 @@
* 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> {
// @ts-check
/**
* @param {import('knex')} knex
*/
exports.up = async function up(knex) {
return knex.schema.createTable('location_update_log', table => {
table.uuid('id').primary();
table.enum('status', ['success', 'fail']).notNullable();
table
.dateTime('created_at')
.defaultTo(knex.fn.now())
.notNullable();
table.dateTime('created_at').defaultTo(knex.fn.now()).notNullable();
table.string('message');
table
.uuid('location_id')
@@ -32,8 +33,11 @@ export async function up(knex: Knex): Promise<any> {
.onDelete('CASCADE');
table.string('entity_name').nullable();
});
}
};
export async function down(knex: Knex): Promise<any> {
/**
* @param {import('knex')} knex
*/
exports.down = async function down(knex) {
return knex.schema.dropTableIfExists('location_update_log');
}
};
@@ -13,9 +13,13 @@
* 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> {
// @ts-check
/**
* @param {import('knex')} knex
*/
exports.up = async function up(knex) {
// Need to first order by date of creation
const query = knex
.select()
@@ -28,8 +32,11 @@ export async function up(knex: Knex): Promise<any> {
await knex.schema.raw(
`CREATE VIEW location_update_log_latest AS ${groupedQuery.toString()};`,
);
}
};
export async function down(knex: Knex): Promise<any> {
/**
* @param {import('knex')} knex
*/
exports.down = async function down(knex) {
return knex.schema.raw(`DROP VIEW location_update_log_latest;`);
}
};
+2 -1
View File
@@ -45,7 +45,8 @@
"tsc-watch": "^4.2.3"
},
"files": [
"dist"
"dist",
"migrations"
],
"nodemonConfig": {
"watch": "./dist"
@@ -22,6 +22,11 @@ import { Logger } from 'winston';
import { CommonDatabase } from './CommonDatabase';
import { Database } from './types';
const migrationsDir = path.resolve(
require.resolve('@backstage/plugin-catalog-backend/package.json'),
'../migrations',
);
export type CreateDatabaseOptions = {
logger: Logger;
fieldNormalizer: (value: string) => string;
@@ -38,8 +43,7 @@ export class DatabaseManager {
options: Partial<CreateDatabaseOptions> = {},
): Promise<Database> {
await knex.migrate.latest({
directory: path.resolve(__dirname, 'migrations'),
loadExtensions: ['.js'],
directory: migrationsDir,
});
const { logger, fieldNormalizer } = { ...defaultOptions, ...options };
return new CommonDatabase(knex, fieldNormalizer, logger);
@@ -69,8 +73,7 @@ export class DatabaseManager {
resource.run('PRAGMA foreign_keys = ON', () => {});
});
await knex.migrate.latest({
directory: path.resolve(__dirname, 'migrations'),
loadExtensions: ['.ts'],
directory: migrationsDir,
});
const { logger, fieldNormalizer } = defaultOptions;
return new CommonDatabase(knex, fieldNormalizer, logger);
+1
View File
@@ -9,6 +9,7 @@
"target": "es2019",
"module": "commonjs",
"esModuleInterop": true,
"allowJs": true,
"lib": ["es2019"],
"types": ["node", "jest"]
}
+6 -1
View File
@@ -1,6 +1,11 @@
{
"extends": "@backstage/cli/config/tsconfig.json",
"include": ["packages/*/src", "plugins/*/src", "plugins/*/dev"],
"include": [
"packages/*/src",
"plugins/*/src",
"plugins/*/dev",
"plugins/*/migrations"
],
"compilerOptions": {
"outDir": "dist"
}