@balena/abstract-sql-compiler 7.17.0 → 7.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,206 @@
1
+ const typeVocab = require('fs').readFileSync(
2
+ require.resolve('@balena/sbvr-types/Type.sbvr'),
3
+ );
4
+ const modifiedAtTrigger = (tableName) => `\
5
+ DO
6
+ $$
7
+ BEGIN
8
+ IF NOT EXISTS(
9
+ SELECT 1
10
+ FROM "information_schema"."triggers"
11
+ WHERE "event_object_table" = '${tableName}'
12
+ AND "trigger_name" = '${tableName}_trigger_update_modified_at'
13
+ ) THEN
14
+ CREATE TRIGGER "${tableName}_trigger_update_modified_at"
15
+ BEFORE UPDATE ON "${tableName}"
16
+ FOR EACH ROW
17
+ EXECUTE PROCEDURE "trigger_update_modified_at"();
18
+ END IF;
19
+ END;
20
+ $$`;
21
+
22
+ describe('reference type', function () {
23
+ let test;
24
+ beforeEach(() => {
25
+ test = require('./test')(typeVocab);
26
+ });
27
+
28
+ it('informative - no foreignKey for reference field', async () => {
29
+ test(
30
+ `\
31
+
32
+ Term: term
33
+ Term: term history
34
+ Fact Type: term history references term
35
+ Necessity: each term history references exactly one term
36
+ Reference Type: informative
37
+ `,
38
+ [
39
+ `\
40
+ DO $$
41
+ BEGIN
42
+ PERFORM '"trigger_update_modified_at"()'::regprocedure;
43
+ EXCEPTION WHEN undefined_function THEN
44
+ CREATE FUNCTION "trigger_update_modified_at"()
45
+ RETURNS TRIGGER AS $fn$
46
+ BEGIN
47
+ NEW."modified at" = NOW();
48
+ RETURN NEW;
49
+ END;
50
+ $fn$ LANGUAGE plpgsql;
51
+ END;
52
+ $$;`,
53
+
54
+ `\
55
+ CREATE TABLE IF NOT EXISTS "term" (
56
+ "created at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
57
+ , "modified at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
58
+ , "id" SERIAL NOT NULL PRIMARY KEY
59
+ );`,
60
+ modifiedAtTrigger('term'),
61
+ `\
62
+ CREATE TABLE IF NOT EXISTS "term history" (
63
+ "created at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
64
+ , "modified at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
65
+ , "id" SERIAL NOT NULL PRIMARY KEY
66
+ , "references-term" INTEGER NOT NULL
67
+ );`,
68
+ modifiedAtTrigger('term history'),
69
+ ],
70
+ );
71
+ });
72
+
73
+ it('informative - no foreignKey for reference field - order of Reference Type and Rule is irrelevant ', function () {
74
+ test(
75
+ `\
76
+
77
+ Term: term
78
+ Term: term history
79
+ Fact Type: term history references term
80
+ Reference Type: informative
81
+ Necessity: each term history references exactly one term
82
+ `,
83
+ [
84
+ `\
85
+ DO $$
86
+ BEGIN
87
+ PERFORM '"trigger_update_modified_at"()'::regprocedure;
88
+ EXCEPTION WHEN undefined_function THEN
89
+ CREATE FUNCTION "trigger_update_modified_at"()
90
+ RETURNS TRIGGER AS $fn$
91
+ BEGIN
92
+ NEW."modified at" = NOW();
93
+ RETURN NEW;
94
+ END;
95
+ $fn$ LANGUAGE plpgsql;
96
+ END;
97
+ $$;`,
98
+
99
+ `\
100
+ CREATE TABLE IF NOT EXISTS "term" (
101
+ "created at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
102
+ , "modified at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
103
+ , "id" SERIAL NOT NULL PRIMARY KEY
104
+ );`,
105
+ modifiedAtTrigger('term'),
106
+ `\
107
+ CREATE TABLE IF NOT EXISTS "term history" (
108
+ "created at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
109
+ , "modified at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
110
+ , "id" SERIAL NOT NULL PRIMARY KEY
111
+ , "references-term" INTEGER NOT NULL
112
+ );`,
113
+ modifiedAtTrigger('term history'),
114
+ ],
115
+ );
116
+ });
117
+
118
+ it('strict - foreignKey for reference field', function () {
119
+ test(
120
+ `\
121
+ Term: term
122
+ Term: term history
123
+ Fact Type: term history references term
124
+ Necessity: each term history references exactly one term
125
+ Reference Type: strict
126
+ `,
127
+ [
128
+ `\
129
+ DO $$
130
+ BEGIN
131
+ PERFORM '"trigger_update_modified_at"()'::regprocedure;
132
+ EXCEPTION WHEN undefined_function THEN
133
+ CREATE FUNCTION "trigger_update_modified_at"()
134
+ RETURNS TRIGGER AS $fn$
135
+ BEGIN
136
+ NEW."modified at" = NOW();
137
+ RETURN NEW;
138
+ END;
139
+ $fn$ LANGUAGE plpgsql;
140
+ END;
141
+ $$;`,
142
+
143
+ `\
144
+ CREATE TABLE IF NOT EXISTS "term" (
145
+ "created at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
146
+ , "modified at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
147
+ , "id" SERIAL NOT NULL PRIMARY KEY
148
+ );`,
149
+ modifiedAtTrigger('term'),
150
+ `\
151
+ CREATE TABLE IF NOT EXISTS "term history" (
152
+ "created at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
153
+ , "modified at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
154
+ , "id" SERIAL NOT NULL PRIMARY KEY
155
+ , "references-term" INTEGER NOT NULL
156
+ , FOREIGN KEY ("references-term") REFERENCES "term" ("id")
157
+ );`,
158
+ modifiedAtTrigger('term history'),
159
+ ],
160
+ );
161
+ });
162
+
163
+ it('default (strict) - foreignKey for reference field', function () {
164
+ test(
165
+ `\
166
+ Term: term
167
+ Term: term history
168
+ Fact Type: term history references term
169
+ Necessity: each term history references exactly one term
170
+ `,
171
+ [
172
+ `\
173
+ DO $$
174
+ BEGIN
175
+ PERFORM '"trigger_update_modified_at"()'::regprocedure;
176
+ EXCEPTION WHEN undefined_function THEN
177
+ CREATE FUNCTION "trigger_update_modified_at"()
178
+ RETURNS TRIGGER AS $fn$
179
+ BEGIN
180
+ NEW."modified at" = NOW();
181
+ RETURN NEW;
182
+ END;
183
+ $fn$ LANGUAGE plpgsql;
184
+ END;
185
+ $$;`,
186
+
187
+ `\
188
+ CREATE TABLE IF NOT EXISTS "term" (
189
+ "created at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
190
+ , "modified at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
191
+ , "id" SERIAL NOT NULL PRIMARY KEY
192
+ );`,
193
+ modifiedAtTrigger('term'),
194
+ `\
195
+ CREATE TABLE IF NOT EXISTS "term history" (
196
+ "created at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
197
+ , "modified at" TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
198
+ , "id" SERIAL NOT NULL PRIMARY KEY
199
+ , "references-term" INTEGER NOT NULL
200
+ , FOREIGN KEY ("references-term") REFERENCES "term" ("id")
201
+ );`,
202
+ modifiedAtTrigger('term history'),
203
+ ],
204
+ );
205
+ });
206
+ });