0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-02-03 22:29:08 -05:00

refactor: objShallowEqual -> deep diff

This commit is contained in:
bholmesdev 2024-02-01 18:40:23 -05:00
parent 06f96661af
commit 5fcf4a9bd6

View file

@ -530,19 +530,21 @@ 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);
if (isSafeTypeUpdate) continue;
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 (diff) {
updated[key] = { old: oldField, new: newField };
}
}
return updated;
}
const typeChangesWithoutQuery: Array<{ from: FieldType; to: FieldType }> = [
@ -576,13 +578,3 @@ type DBFieldWithDefault =
function hasRuntimeDefault(field: DBField): field is DBFieldWithDefault {
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;
}