2025-07-03 10:59:17 -04:00
|
|
|
import { transformTriggers } from 'src/sql-tools/transformers/trigger.transformer';
|
2025-04-07 15:12:12 -04:00
|
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
|
|
|
|
|
|
describe(transformTriggers.name, () => {
|
2025-07-03 10:59:17 -04:00
|
|
|
describe('TriggerCreate', () => {
|
2025-04-07 15:12:12 -04:00
|
|
|
it('should work', () => {
|
|
|
|
|
expect(
|
|
|
|
|
transformTriggers({
|
2025-07-03 10:59:17 -04:00
|
|
|
type: 'TriggerCreate',
|
2025-04-07 15:12:12 -04:00
|
|
|
trigger: {
|
|
|
|
|
name: 'trigger1',
|
|
|
|
|
tableName: 'table1',
|
|
|
|
|
timing: 'before',
|
|
|
|
|
actions: ['update'],
|
|
|
|
|
scope: 'row',
|
|
|
|
|
functionName: 'function1',
|
|
|
|
|
synchronize: true,
|
|
|
|
|
},
|
|
|
|
|
reason: 'unknown',
|
|
|
|
|
}),
|
|
|
|
|
).toEqual(
|
|
|
|
|
`CREATE OR REPLACE TRIGGER "trigger1"
|
|
|
|
|
BEFORE UPDATE ON "table1"
|
|
|
|
|
FOR EACH ROW
|
|
|
|
|
EXECUTE FUNCTION function1();`,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should work with multiple actions', () => {
|
|
|
|
|
expect(
|
|
|
|
|
transformTriggers({
|
2025-07-03 10:59:17 -04:00
|
|
|
type: 'TriggerCreate',
|
2025-04-07 15:12:12 -04:00
|
|
|
trigger: {
|
|
|
|
|
name: 'trigger1',
|
|
|
|
|
tableName: 'table1',
|
|
|
|
|
timing: 'before',
|
|
|
|
|
actions: ['update', 'delete'],
|
|
|
|
|
scope: 'row',
|
|
|
|
|
functionName: 'function1',
|
|
|
|
|
synchronize: true,
|
|
|
|
|
},
|
|
|
|
|
reason: 'unknown',
|
|
|
|
|
}),
|
|
|
|
|
).toEqual(
|
|
|
|
|
`CREATE OR REPLACE TRIGGER "trigger1"
|
|
|
|
|
BEFORE UPDATE OR DELETE ON "table1"
|
|
|
|
|
FOR EACH ROW
|
|
|
|
|
EXECUTE FUNCTION function1();`,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('should work with old/new reference table aliases', () => {
|
|
|
|
|
expect(
|
|
|
|
|
transformTriggers({
|
2025-07-03 10:59:17 -04:00
|
|
|
type: 'TriggerCreate',
|
2025-04-07 15:12:12 -04:00
|
|
|
trigger: {
|
|
|
|
|
name: 'trigger1',
|
|
|
|
|
tableName: 'table1',
|
|
|
|
|
timing: 'before',
|
|
|
|
|
actions: ['update'],
|
|
|
|
|
referencingNewTableAs: 'new',
|
|
|
|
|
referencingOldTableAs: 'old',
|
|
|
|
|
scope: 'row',
|
|
|
|
|
functionName: 'function1',
|
|
|
|
|
synchronize: true,
|
|
|
|
|
},
|
|
|
|
|
reason: 'unknown',
|
|
|
|
|
}),
|
|
|
|
|
).toEqual(
|
|
|
|
|
`CREATE OR REPLACE TRIGGER "trigger1"
|
|
|
|
|
BEFORE UPDATE ON "table1"
|
|
|
|
|
REFERENCING OLD TABLE AS "old" NEW TABLE AS "new"
|
|
|
|
|
FOR EACH ROW
|
|
|
|
|
EXECUTE FUNCTION function1();`,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2025-07-03 10:59:17 -04:00
|
|
|
describe('TriggerDrop', () => {
|
2025-04-07 15:12:12 -04:00
|
|
|
it('should work', () => {
|
|
|
|
|
expect(
|
|
|
|
|
transformTriggers({
|
2025-07-03 10:59:17 -04:00
|
|
|
type: 'TriggerDrop',
|
2025-04-07 15:12:12 -04:00
|
|
|
tableName: 'table1',
|
|
|
|
|
triggerName: 'trigger1',
|
|
|
|
|
reason: 'unknown',
|
|
|
|
|
}),
|
|
|
|
|
).toEqual(`DROP TRIGGER "trigger1" ON "table1";`);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|