mirror of
https://github.com/withastro/astro.git
synced 2025-02-10 22:38:53 -05:00
refactor: objShallowEqual -> deep diff
This commit is contained in:
parent
06f96661af
commit
5fcf4a9bd6
1 changed files with 12 additions and 20 deletions
|
@ -530,18 +530,20 @@ function getUpdatedFields(oldFields: DBFields, newFields: DBFields): UpdatedFiel
|
||||||
for (const [key, newField] of Object.entries(newFields)) {
|
for (const [key, newField] of Object.entries(newFields)) {
|
||||||
const oldField = oldFields[key];
|
const oldField = oldFields[key];
|
||||||
if (!oldField) continue;
|
if (!oldField) continue;
|
||||||
if (objShallowEqual(oldField, newField)) continue;
|
|
||||||
|
|
||||||
// TODO: refactor to deep-diff with a prefilter on `type`
|
const diff = deepDiff(oldField, newField, (path, objKey) => {
|
||||||
const oldFieldSqlType = { ...oldField, type: schemaTypeToSqlType(oldField.type) };
|
const isTypeKey = objKey === 'type' && path.length === 0;
|
||||||
const newFieldSqlType = { ...newField, type: schemaTypeToSqlType(newField.type) };
|
return (
|
||||||
const isSafeTypeUpdate =
|
// If we can safely update the type without a SQL query, ignore the diff
|
||||||
objShallowEqual(oldFieldSqlType, newFieldSqlType) &&
|
isTypeKey &&
|
||||||
canChangeTypeWithoutQuery(oldField, newField);
|
oldField.type !== newField.type &&
|
||||||
|
canChangeTypeWithoutQuery(oldField, newField)
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
if (isSafeTypeUpdate) continue;
|
if (diff) {
|
||||||
|
updated[key] = { old: oldField, new: newField };
|
||||||
updated[key] = { old: oldField, new: newField };
|
}
|
||||||
}
|
}
|
||||||
return updated;
|
return updated;
|
||||||
}
|
}
|
||||||
|
@ -576,13 +578,3 @@ type DBFieldWithDefault =
|
||||||
function hasRuntimeDefault(field: DBField): field is DBFieldWithDefault {
|
function hasRuntimeDefault(field: DBField): field is DBFieldWithDefault {
|
||||||
return field.type === 'date' && field.default === 'now';
|
return field.type === 'date' && field.default === 'now';
|
||||||
}
|
}
|
||||||
|
|
||||||
function objShallowEqual(a: Record<string, unknown>, b: Record<string, unknown>) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue