From 9c42092dd4618095659277565388d08fe38edb6f Mon Sep 17 00:00:00 2001 From: obmagnusson Date: Wed, 23 Oct 2024 10:22:57 +0000 Subject: [PATCH] add a validation check on down --- ...2145836-payment-alter-column-definition-text.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/apps/application-system/api/migrations/20241022145836-payment-alter-column-definition-text.js b/apps/application-system/api/migrations/20241022145836-payment-alter-column-definition-text.js index 1eadfc953d8c..373432c0e3e3 100644 --- a/apps/application-system/api/migrations/20241022145836-payment-alter-column-definition-text.js +++ b/apps/application-system/api/migrations/20241022145836-payment-alter-column-definition-text.js @@ -4,14 +4,24 @@ module.exports = { up: async (queryInterface, Sequelize) => { await queryInterface.changeColumn('payment', 'definition', { type: Sequelize.TEXT, - allowNull: true, // Keep the same nullability as before + allowNull: true, }) }, down: async (queryInterface, Sequelize) => { + // Check for records that might be truncated + const records = await queryInterface.sequelize.query( + `SELECT id FROM payment WHERE LENGTH(CAST(definition AS TEXT)) > 255`, + ) + if (records[0].length > 0) { + throw new Error( + 'Cannot safely rollback: Some records exceed STRING length limit', + ) + } + await queryInterface.changeColumn('payment', 'definition', { type: Sequelize.STRING, - allowNull: true, // Revert to the original type and nullability + allowNull: true, }) }, }