diff --git a/plugins/catalog-backend/src/database/migrations/20200511113813_init.ts b/plugins/catalog-backend/migrations/20200511113813_init.js similarity index 95% rename from plugins/catalog-backend/src/database/migrations/20200511113813_init.ts rename to plugins/catalog-backend/migrations/20200511113813_init.js index 5f136670f4..1023107955 100644 --- a/plugins/catalog-backend/src/database/migrations/20200511113813_init.ts +++ b/plugins/catalog-backend/migrations/20200511113813_init.js @@ -14,9 +14,10 @@ * limitations under the License. */ -import * as Knex from 'knex'; - -export async function up(knex: Knex): Promise { +/** + * @param {import('knex')} knex + */ +exports.up = async function up(knex) { return ( knex.schema // @@ -114,9 +115,12 @@ export async function up(knex: Knex): Promise { .comment('The corresponding value to match on'); }) ); -} +}; -export async function down(knex: Knex): Promise { +/** + * @param {import('knex')} knex + */ +exports.down = async function down(knex) { return knex.schema .dropTable('entities_search') .alterTable('entities', table => { @@ -124,4 +128,4 @@ export async function down(knex: Knex): Promise { }) .dropTable('entities') .dropTable('locations'); -} +}; diff --git a/plugins/catalog-backend/src/database/migrations/20200520140700_location_update_log_table.ts b/plugins/catalog-backend/migrations/20200520140700_location_update_log_table.js similarity index 81% rename from plugins/catalog-backend/src/database/migrations/20200520140700_location_update_log_table.ts rename to plugins/catalog-backend/migrations/20200520140700_location_update_log_table.js index 6700f5748e..b9cf30a716 100644 --- a/plugins/catalog-backend/src/database/migrations/20200520140700_location_update_log_table.ts +++ b/plugins/catalog-backend/migrations/20200520140700_location_update_log_table.js @@ -13,16 +13,15 @@ * 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 { +/** + * @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 +31,11 @@ export async function up(knex: Knex): Promise { .onDelete('CASCADE'); table.string('entity_name').nullable(); }); -} +}; -export async function down(knex: Knex): Promise { +/** + * @param {import('knex')} knex + */ +exports.down = async function down(knex) { return knex.schema.dropTableIfExists('location_update_log'); -} +}; diff --git a/plugins/catalog-backend/src/database/migrations/20200527114117_location_update_log_latest_view.ts b/plugins/catalog-backend/migrations/20200527114117_location_update_log_latest_view.js similarity index 86% rename from plugins/catalog-backend/src/database/migrations/20200527114117_location_update_log_latest_view.ts rename to plugins/catalog-backend/migrations/20200527114117_location_update_log_latest_view.js index 6da9eeed9b..605b04bbce 100644 --- a/plugins/catalog-backend/src/database/migrations/20200527114117_location_update_log_latest_view.ts +++ b/plugins/catalog-backend/migrations/20200527114117_location_update_log_latest_view.js @@ -13,9 +13,11 @@ * 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 { +/** + * @param {import('knex')} knex + */ +exports.up = async function up(knex) { // Need to first order by date of creation const query = knex .select() @@ -28,8 +30,11 @@ export async function up(knex: Knex): Promise { await knex.schema.raw( `CREATE VIEW location_update_log_latest AS ${groupedQuery.toString()};`, ); -} +}; -export async function down(knex: Knex): Promise { +/** + * @param {import('knex')} knex + */ +exports.down = async function down(knex) { return knex.schema.raw(`DROP VIEW location_update_log_latest;`); -} +}; diff --git a/plugins/catalog-backend/package.json b/plugins/catalog-backend/package.json index 5df1c414d3..d98afbe4d3 100644 --- a/plugins/catalog-backend/package.json +++ b/plugins/catalog-backend/package.json @@ -45,7 +45,8 @@ "tsc-watch": "^4.2.3" }, "files": [ - "dist" + "dist", + "migrations" ], "nodemonConfig": { "watch": "./dist" diff --git a/plugins/catalog-backend/src/database/DatabaseManager.ts b/plugins/catalog-backend/src/database/DatabaseManager.ts index 8710aede4c..28751f4830 100644 --- a/plugins/catalog-backend/src/database/DatabaseManager.ts +++ b/plugins/catalog-backend/src/database/DatabaseManager.ts @@ -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 = {}, ): Promise { 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); diff --git a/plugins/catalog-backend/tsconfig.json b/plugins/catalog-backend/tsconfig.json index 015a967f76..95c3a656e6 100644 --- a/plugins/catalog-backend/tsconfig.json +++ b/plugins/catalog-backend/tsconfig.json @@ -9,6 +9,7 @@ "target": "es2019", "module": "commonjs", "esModuleInterop": true, + "allowJs": true, "lib": ["es2019"], "types": ["node", "jest"] }