From 5fcf4a9bd6f1cd953bb2b7a1b230bbedc28a24e3 Mon Sep 17 00:00:00 2001 From: bholmesdev Date: Thu, 1 Feb 2024 18:40:23 -0500 Subject: [PATCH] refactor: objShallowEqual -> deep diff --- packages/db/src/core/cli/migration-queries.ts | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/packages/db/src/core/cli/migration-queries.ts b/packages/db/src/core/cli/migration-queries.ts index 22c9644bb9..d871d71c65 100644 --- a/packages/db/src/core/cli/migration-queries.ts +++ b/packages/db/src/core/cli/migration-queries.ts @@ -530,18 +530,20 @@ function getUpdatedFields(oldFields: DBFields, newFields: DBFields): UpdatedFiel for (const [key, newField] of Object.entries(newFields)) { const oldField = oldFields[key]; if (!oldField) continue; - if (objShallowEqual(oldField, newField)) continue; - // TODO: refactor to deep-diff with a prefilter on `type` - const oldFieldSqlType = { ...oldField, type: schemaTypeToSqlType(oldField.type) }; - const newFieldSqlType = { ...newField, type: schemaTypeToSqlType(newField.type) }; - const isSafeTypeUpdate = - objShallowEqual(oldFieldSqlType, newFieldSqlType) && - canChangeTypeWithoutQuery(oldField, newField); + const diff = deepDiff(oldField, newField, (path, objKey) => { + const isTypeKey = objKey === 'type' && path.length === 0; + return ( + // If we can safely update the type without a SQL query, ignore the diff + isTypeKey && + oldField.type !== newField.type && + canChangeTypeWithoutQuery(oldField, newField) + ); + }); - if (isSafeTypeUpdate) continue; - - updated[key] = { old: oldField, new: newField }; + if (diff) { + updated[key] = { old: oldField, new: newField }; + } } return updated; } @@ -576,13 +578,3 @@ type DBFieldWithDefault = function hasRuntimeDefault(field: DBField): field is DBFieldWithDefault { return field.type === 'date' && field.default === 'now'; } - -function objShallowEqual(a: Record, b: Record) { - if (Object.keys(a).length !== Object.keys(b).length) return false; - for (const [key, value] of Object.entries(a)) { - if (JSON.stringify(b[key]) !== JSON.stringify(value)) { - return false; - } - } - return true; -}