Skip to content

Commit

Permalink
add a validation check on down
Browse files Browse the repository at this point in the history
  • Loading branch information
obmagnusson committed Oct 23, 2024
1 parent 48d8f39 commit 16e8726
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
})
},
}

0 comments on commit 16e8726

Please sign in to comment.