Skip to content

Commit b79a073

Browse files
committed
chore: trim functions before comparing
1 parent 6bdb8ea commit b79a073

File tree

2 files changed

+125
-3
lines changed

2 files changed

+125
-3
lines changed

.scripts/compare-database.js

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,18 @@ export const deepSort = (obj) => {
8484
return obj;
8585
};
8686

87+
// Function to normalize function/trigger definitions by stripping whitespace from each line
88+
export const normalizeDefinition = (definition) => {
89+
if (typeof definition !== 'string') {
90+
return definition;
91+
}
92+
93+
return definition
94+
.split('\n')
95+
.map(line => line.trim())
96+
.join('\n');
97+
};
98+
8799
const schemas = ['cloud', 'public'];
88100
const schemasArray = `(${schemas.map((schema) => `'${schema}'`).join(', ')})`;
89101

@@ -236,6 +248,18 @@ const queryDatabaseManifest = async (database) => {
236248
return { policyname, ...rest };
237249
};
238250

251+
// Normalize function definitions by stripping whitespace from each line
252+
const normalizeFuncDefinition = ({ definition, ...rest }) => ({
253+
...rest,
254+
definition: normalizeDefinition(definition),
255+
});
256+
257+
// Normalize trigger action statements by stripping whitespace from each line
258+
const normalizeTriggerAction = ({ action_statement, ...rest }) => ({
259+
...rest,
260+
action_statement: normalizeDefinition(action_statement),
261+
});
262+
239263
// Omit generated ids and values
240264
return {
241265
tables: omitArray(tables, 'table_catalog'),
@@ -270,8 +294,8 @@ const queryDatabaseManifest = async (database) => {
270294
'conexclop'
271295
),
272296
indexes,
273-
funcs,
274-
triggers: omitArray(triggers, 'trigger_catalog', 'event_object_catalog'),
297+
funcs: funcs.map(normalizeFuncDefinition),
298+
triggers: omitArray(triggers, 'trigger_catalog', 'event_object_catalog').map(normalizeTriggerAction),
275299
policies: policies.map(normalizeRoles).map(normalizePolicyname),
276300
columnGrants: omitArray(columnGrants, 'table_catalog').map(
277301
normalizeGrantee

.scripts/compare-database.test.js

Lines changed: 99 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*/
99

1010
import assert from 'node:assert';
11-
import { autoCompare, deepSort } from './compare-database.js';
11+
import { autoCompare, deepSort, normalizeDefinition } from './compare-database.js';
1212

1313
// Test helper function
1414
const runTest = (testName, testFn) => {
@@ -212,3 +212,101 @@ runTest('deepSort - mixed types in array', () => {
212212

213213
assert.deepStrictEqual(result, expected);
214214
});
215+
216+
// Test cases for normalizeDefinition function
217+
runTest('normalizeDefinition - single line without leading/trailing spaces', () => {
218+
const input = 'CREATE FUNCTION test() RETURNS void';
219+
const expected = 'CREATE FUNCTION test() RETURNS void';
220+
const result = normalizeDefinition(input);
221+
222+
assert.strictEqual(result, expected);
223+
});
224+
225+
runTest('normalizeDefinition - single line with leading/trailing spaces', () => {
226+
const input = ' CREATE FUNCTION test() RETURNS void ';
227+
const expected = 'CREATE FUNCTION test() RETURNS void';
228+
const result = normalizeDefinition(input);
229+
230+
assert.strictEqual(result, expected);
231+
});
232+
233+
runTest('normalizeDefinition - multi-line with various indentation', () => {
234+
const input = `CREATE OR REPLACE FUNCTION test_function()
235+
RETURNS trigger
236+
LANGUAGE plpgsql
237+
AS $function$
238+
BEGIN
239+
IF NEW.status IS NULL THEN
240+
NEW.status := 'active';
241+
END IF;
242+
RETURN NEW;
243+
END;
244+
$function$`;
245+
246+
const expected = `CREATE OR REPLACE FUNCTION test_function()
247+
RETURNS trigger
248+
LANGUAGE plpgsql
249+
AS $function$
250+
BEGIN
251+
IF NEW.status IS NULL THEN
252+
NEW.status := 'active';
253+
END IF;
254+
RETURN NEW;
255+
END;
256+
$function$`;
257+
258+
const result = normalizeDefinition(input);
259+
assert.strictEqual(result, expected);
260+
});
261+
262+
runTest('normalizeDefinition - trigger action statement', () => {
263+
const input = ` BEGIN
264+
UPDATE table1 SET updated_at = NOW() WHERE id = NEW.id;
265+
INSERT INTO audit_log (table_name, action) VALUES ('table1', 'UPDATE');
266+
RETURN NEW;
267+
END `;
268+
269+
const expected = `BEGIN
270+
UPDATE table1 SET updated_at = NOW() WHERE id = NEW.id;
271+
INSERT INTO audit_log (table_name, action) VALUES ('table1', 'UPDATE');
272+
RETURN NEW;
273+
END`;
274+
275+
const result = normalizeDefinition(input);
276+
assert.strictEqual(result, expected);
277+
});
278+
279+
runTest('normalizeDefinition - empty lines and mixed spacing', () => {
280+
const input = ` CREATE FUNCTION complex_func()
281+
282+
RETURNS TABLE(id integer, name text)
283+
AS $$
284+
SELECT id, name
285+
FROM users
286+
WHERE active = true;
287+
$$`;
288+
289+
const expected = `CREATE FUNCTION complex_func()
290+
291+
RETURNS TABLE(id integer, name text)
292+
AS $$
293+
SELECT id, name
294+
FROM users
295+
WHERE active = true;
296+
$$`;
297+
298+
const result = normalizeDefinition(input);
299+
assert.strictEqual(result, expected);
300+
});
301+
302+
runTest('normalizeDefinition - non-string input', () => {
303+
assert.strictEqual(normalizeDefinition(null), null);
304+
assert.strictEqual(normalizeDefinition(undefined), undefined);
305+
assert.strictEqual(normalizeDefinition(123), 123);
306+
assert.deepStrictEqual(normalizeDefinition({ key: 'value' }), { key: 'value' });
307+
});
308+
309+
runTest('normalizeDefinition - empty string', () => {
310+
assert.strictEqual(normalizeDefinition(''), '');
311+
assert.strictEqual(normalizeDefinition(' '), '');
312+
});

0 commit comments

Comments
 (0)