Skip to content

Commit 65cbe6c

Browse files
committed
chore: update compare DB script data sorting logics
1 parent f2c0a05 commit 65cbe6c

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

.scripts/compare-database.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,24 @@ const queryDatabaseData = async (database) => {
251251
user: 'postgres',
252252
password: 'postgres',
253253
});
254+
255+
// Deep sort function to handle arrays within data
256+
const deepSort = (obj) => {
257+
if (Array.isArray(obj)) {
258+
return obj.map(deepSort).sort(autoCompare);
259+
}
260+
261+
if (obj && typeof obj === 'object') {
262+
const sorted = {};
263+
Object.keys(obj).sort().forEach(key => {
264+
sorted[key] = deepSort(obj[key]);
265+
});
266+
return sorted;
267+
}
268+
269+
return obj;
270+
};
271+
254272
const result = await Promise.all(
255273
manifests[0].tables.map(async ({ table_schema, table_name }) => {
256274
const { rows } = await pool.query(
@@ -262,7 +280,7 @@ const queryDatabaseData = async (database) => {
262280
const data = omitArray(rows, 'value');
263281
return [
264282
table_name,
265-
data.sort(buildSortByKeys(Object.keys(data[0] ?? {}))),
283+
data.map(deepSort).sort(buildSortByKeys(Object.keys(data[0] ?? {}))),
266284
];
267285
}
268286

@@ -281,7 +299,7 @@ const queryDatabaseData = async (database) => {
281299
'add_on_sku_id'
282300
);
283301

284-
return [table_name, data.sort(buildSortByKeys(Object.keys(data[0] ?? {})))];
302+
return [table_name, data.map(deepSort).sort(buildSortByKeys(Object.keys(data[0] ?? {})))];
285303
})
286304
);
287305

0 commit comments

Comments
 (0)