From 9beb4360a2586bfad72409d94eb777a59fc69493 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fredrik=20Adel=C3=B6w?= Date: Thu, 6 Aug 2020 15:49:07 +0200 Subject: [PATCH] fix(catalog-backend): update migration to delete dependent view first --- ...904_location_update_log_duplication_fix.js | 48 ++++++++++++------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/plugins/catalog-backend/migrations/20200805163904_location_update_log_duplication_fix.js b/plugins/catalog-backend/migrations/20200805163904_location_update_log_duplication_fix.js index aa59b54ac8..05c1658641 100644 --- a/plugins/catalog-backend/migrations/20200805163904_location_update_log_duplication_fix.js +++ b/plugins/catalog-backend/migrations/20200805163904_location_update_log_duplication_fix.js @@ -21,6 +21,7 @@ */ exports.up = function up(knex) { return knex.schema + .raw('DROP VIEW location_update_log_latest;') .dropTable('location_update_log') .createTable('location_update_log', table => { table.bigIncrements('id').primary(); // instead of uuid, so we can MAX it @@ -35,8 +36,6 @@ exports.up = function up(knex) { .onDelete('CASCADE'); table.string('entity_name').nullable(); }).raw(` - DROP VIEW location_update_log_latest; - `).raw(` CREATE VIEW location_update_log_latest AS SELECT t1.* FROM location_update_log t1 JOIN @@ -56,20 +55,33 @@ exports.up = function up(knex) { * @param {import('knex')} knex */ exports.down = function down(knex) { - return knex.schema.raw(` - DROP VIEW location_update_log_latest; - `).raw(` - CREATE VIEW location_update_log_latest AS - SELECT t1.* FROM location_update_log t1 - JOIN - ( - SELECT location_id, MAX(created_at) AS MAXDATE - FROM location_update_log - GROUP BY location_id - ) t2 - ON t1.location_id = t2.location_id - AND t1.created_at = t2.MAXDATE - GROUP BY t1.location_id, t1.id - ORDER BY created_at DESC; - `); + return knex.schema + .raw('DROP VIEW location_update_log_latest;') + .dropTable('location_update_log') + .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.string('message'); + table + .uuid('location_id') + .references('id') + .inTable('locations') + .onUpdate('CASCADE') + .onDelete('CASCADE'); + table.string('entity_name').nullable(); + }).raw(` + CREATE VIEW location_update_log_latest AS + SELECT t1.* FROM location_update_log t1 + JOIN + ( + SELECT location_id, MAX(created_at) AS MAXDATE + FROM location_update_log + GROUP BY location_id + ) t2 + ON t1.location_id = t2.location_id + AND t1.created_at = t2.MAXDATE + GROUP BY t1.location_id, t1.id + ORDER BY created_at DESC; + `); };