Skip to content

Commit

Permalink
fix(schema): improve detection of renamed columns
Browse files Browse the repository at this point in the history
Previously, we only cared about the column type, now we also check if the column is/was a FK or not.
  • Loading branch information
B4nan committed Apr 3, 2024
1 parent fa69276 commit 4d13c58
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/knex/src/schema/SchemaComparator.ts
Expand Up @@ -339,6 +339,8 @@ export class SchemaComparator {
*/
private detectColumnRenamings(tableDifferences: TableDifference, inverseTableDiff?: TableDifference): void {
const renameCandidates: Dictionary<[Column, Column][]> = {};
const oldFKs = Object.values(tableDifferences.fromTable.getForeignKeys());
const newFKs = Object.values(tableDifferences.toTable.getForeignKeys());

for (const addedColumn of Object.values(tableDifferences.addedColumns)) {
for (const removedColumn of Object.values(tableDifferences.removedColumns)) {
Expand All @@ -348,6 +350,13 @@ export class SchemaComparator {
continue;
}

const wasFK = oldFKs.some(fk => fk.columnNames.includes(removedColumn.name));
const isFK = newFKs.some(fk => fk.columnNames.includes(addedColumn.name));

if (wasFK !== isFK) {
continue;
}

const renamedColumn = inverseTableDiff?.renamedColumns[addedColumn.name];

if (renamedColumn && renamedColumn?.name !== removedColumn.name) {
Expand Down

0 comments on commit 4d13c58

Please sign in to comment.