-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathtriggers.ts
150 lines (124 loc) · 4.3 KB
/
triggers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import type { MigrationOptions } from '../types';
import { escapeValue } from '../utils';
import { createFunction, dropFunction } from './functions';
import type { FunctionOptions } from './functionsTypes';
import type { DropOptions, Name, Value } from './generalTypes';
import type {
CreateTrigger,
DropTrigger,
RenameTrigger,
TriggerOptions,
} from './triggersTypes';
export type { CreateTrigger, DropTrigger, RenameTrigger };
export function dropTrigger(mOptions: MigrationOptions): DropTrigger {
const _drop: DropTrigger = (tableName, triggerName, options = {}) => {
const { ifExists, cascade } = options;
const ifExistsStr = ifExists ? ' IF EXISTS' : '';
const cascadeStr = cascade ? ' CASCADE' : '';
const triggerNameStr = mOptions.literal(triggerName);
const tableNameStr = mOptions.literal(tableName);
return `DROP TRIGGER${ifExistsStr} ${triggerNameStr} ON ${tableNameStr}${cascadeStr};`;
};
return _drop;
}
export function createTrigger(mOptions: MigrationOptions): CreateTrigger {
const _create: CreateTrigger = (
tableName: Name,
triggerName: string,
triggerOptions:
| (TriggerOptions & DropOptions)
| (TriggerOptions & FunctionOptions & DropOptions),
definition?: Value
) => {
const {
constraint,
condition,
operation,
deferrable,
deferred,
functionParams = [],
} = triggerOptions;
let { when, level = 'STATEMENT', function: functionName } = triggerOptions;
const operations = Array.isArray(operation)
? operation.join(' OR ')
: operation;
if (constraint) {
when = 'AFTER';
}
if (!when) {
throw new Error('"when" (BEFORE/AFTER/INSTEAD OF) have to be specified');
}
const isInsteadOf = /instead\s+of/i.test(when);
if (isInsteadOf) {
level = 'ROW';
}
if (definition) {
functionName = functionName === undefined ? triggerName : functionName;
}
if (!functionName) {
throw new Error("Can't determine function name");
}
if (isInsteadOf && condition) {
throw new Error("INSTEAD OF trigger can't have condition specified");
}
if (!operations) {
throw new Error(
'"operation" (INSERT/UPDATE[ OF ...]/DELETE/TRUNCATE) have to be specified'
);
}
const defferStr = constraint
? `${deferrable ? `DEFERRABLE INITIALLY ${deferred ? 'DEFERRED' : 'IMMEDIATE'}` : 'NOT DEFERRABLE'}\n `
: '';
const conditionClause = condition ? `WHEN (${condition})\n ` : '';
const constraintStr = constraint ? ' CONSTRAINT' : '';
const paramsStr = functionParams.map(escapeValue).join(', ');
const triggerNameStr = mOptions.literal(triggerName);
const tableNameStr = mOptions.literal(tableName);
const functionNameStr = mOptions.literal(functionName);
const triggerSQL = `CREATE${constraintStr} TRIGGER ${triggerNameStr}
${when} ${operations} ON ${tableNameStr}
${defferStr}FOR EACH ${level}
${conditionClause}EXECUTE PROCEDURE ${functionNameStr}(${paramsStr});`;
const fnSQL = definition
? `${createFunction(mOptions)(
functionName,
[],
{ ...(triggerOptions as FunctionOptions), returns: 'trigger' },
definition
)}\n`
: '';
return `${fnSQL}${triggerSQL}`;
};
_create.reverse = (
tableName: Name,
triggerName: string,
triggerOptions: TriggerOptions & DropOptions,
definition?: Value
) => {
const triggerSQL = dropTrigger(mOptions)(
tableName,
triggerName,
triggerOptions
);
const fnSQL = definition
? `\n${dropFunction(mOptions)(triggerOptions.function || triggerName, [], triggerOptions)}`
: '';
return `${triggerSQL}${fnSQL}`;
};
return _create;
}
export function renameTrigger(mOptions: MigrationOptions): RenameTrigger {
const _rename: RenameTrigger = (
tableName,
oldTriggerName,
newTriggerName
) => {
const oldTriggerNameStr = mOptions.literal(oldTriggerName);
const tableNameStr = mOptions.literal(tableName);
const newTriggerNameStr = mOptions.literal(newTriggerName);
return `ALTER TRIGGER ${oldTriggerNameStr} ON ${tableNameStr} RENAME TO ${newTriggerNameStr};`;
};
_rename.reverse = (tableName, oldTriggerName, newTriggerName) =>
_rename(tableName, newTriggerName, oldTriggerName);
return _rename;
}