Fixed migration util dropping foreign keys before columns

- if we add a column with a foreign key reference, the `down` migration
  will try to remove that column
- you can't remove a column without deleting the foreign key reference
  first
- our migration utils didn't take that into account and there's nothing
  in Knex to do this for us
- this commit deletes the foreign key before removing the column if we
  have one referenced in the column spec
- also updates the code to pass the column spec into the util
This commit is contained in:
Daniel Lockyer 2022-04-04 13:03:38 +01:00
parent 08701aa7a8
commit eddb77e204
No known key found for this signature in database
GPG Key ID: D21186F0B47295AD
2 changed files with 8 additions and 2 deletions

View File

@ -375,7 +375,8 @@ function createAddColumnMigration(table, column, columnDefinition) {
column,
dbIsInCorrectState: hasColumn => hasColumn === false,
operation: commands.dropColumn,
operationVerb: 'Removing'
operationVerb: 'Removing',
columnDefinition
})
);
}

View File

@ -65,7 +65,12 @@ function addColumn(tableName, column, transaction = db.knex, columnSpec) {
});
}
function dropColumn(tableName, column, transaction = db.knex) {
async function dropColumn(tableName, column, transaction = db.knex, columnSpec = {}) {
if (Object.prototype.hasOwnProperty.call(columnSpec, 'references')) {
const [toTable, toColumn] = columnSpec.references.split('.');
await dropForeign({fromTable: tableName, fromColumn: column, toTable, toColumn, transaction});
}
return transaction.schema.table(tableName, function (table) {
table.dropColumn(column);
});