feat: sql-tools overrides (#19796)

This commit is contained in:
Jason Rasmussen
2025-07-08 08:17:40 -04:00
committed by GitHub
parent 1f9813a28e
commit df4a27e8a7
114 changed files with 775 additions and 289 deletions

View File

@@ -0,0 +1,69 @@
import { compareOverrides } from 'src/sql-tools/comparers/override.comparer';
import { DatabaseOverride, Reason } from 'src/sql-tools/types';
import { describe, expect, it } from 'vitest';
const testOverride: DatabaseOverride = {
name: 'test',
value: { type: 'function', name: 'test_func', sql: 'func implementation' },
synchronize: true,
};
describe('compareOverrides', () => {
describe('onExtra', () => {
it('should work', () => {
expect(compareOverrides.onExtra(testOverride)).toEqual([
{
type: 'OverrideDrop',
overrideName: 'test',
reason: Reason.MissingInSource,
},
]);
});
});
describe('onMissing', () => {
it('should work', () => {
expect(compareOverrides.onMissing(testOverride)).toEqual([
{
type: 'OverrideCreate',
override: testOverride,
reason: Reason.MissingInTarget,
},
]);
});
});
describe('onCompare', () => {
it('should work', () => {
expect(compareOverrides.onCompare(testOverride, testOverride)).toEqual([]);
});
it('should drop and recreate when the value changes', () => {
const source: DatabaseOverride = {
name: 'test',
value: {
type: 'function',
name: 'test_func',
sql: 'func implementation',
},
synchronize: true,
};
const target: DatabaseOverride = {
name: 'test',
value: {
type: 'function',
name: 'test_func',
sql: 'func implementation2',
},
synchronize: true,
};
expect(compareOverrides.onCompare(source, target)).toEqual([
{
override: source,
type: 'OverrideUpdate',
reason: expect.stringContaining('value is different'),
},
]);
});
});
});

View File

@@ -0,0 +1,29 @@
import { Comparer, DatabaseOverride, Reason } from 'src/sql-tools/types';
export const compareOverrides: Comparer<DatabaseOverride> = {
onMissing: (source) => [
{
type: 'OverrideCreate',
override: source,
reason: Reason.MissingInTarget,
},
],
onExtra: (target) => [
{
type: 'OverrideDrop',
overrideName: target.name,
reason: Reason.MissingInSource,
},
],
onCompare: (source, target) => {
if (source.value.name !== target.value.name || source.value.sql !== target.value.sql) {
const sourceValue = JSON.stringify(source.value);
const targetValue = JSON.stringify(target.value);
return [
{ type: 'OverrideUpdate', override: source, reason: `value is different (${sourceValue} vs ${targetValue})` },
];
}
return [];
},
};