@vrplatform/kysely 1.3.45-4697 → 1.3.45-4704

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,231 @@
1
+ import type { Kysely } from 'kysely';
2
+ import { sql } from 'kysely';
3
+
4
+ export async function backfillCanonicalJsonbShapes(
5
+ db: Kysely<unknown>
6
+ ): Promise<void> {
7
+ await sql`
8
+ update public.connection
9
+ set credentials = (credentials #>> '{}')::jsonb
10
+ where jsonb_typeof(credentials) = 'string'
11
+ `.execute(db);
12
+ await sql`
13
+ update public.connection
14
+ set persistent_state = (persistent_state #>> '{}')::jsonb
15
+ where jsonb_typeof(persistent_state) = 'string'
16
+ `.execute(db);
17
+ await sql`
18
+ update public.feature
19
+ set required_approvals = (required_approvals #>> '{}')::jsonb
20
+ where jsonb_typeof(required_approvals) = 'string'
21
+ `.execute(db);
22
+ await sql`
23
+ update accounting.transaction
24
+ set recurring_pattern = (recurring_pattern #>> '{}')::jsonb
25
+ where jsonb_typeof(recurring_pattern) = 'string'
26
+ `.execute(db);
27
+ await sql`
28
+ update public.source
29
+ set json = (json #>> '{}')::jsonb
30
+ where type = 'ownerStatement'
31
+ and jsonb_typeof(json) = 'string'
32
+ `.execute(db);
33
+ await sql`
34
+ update accounting.owner_statement_projection
35
+ set payload = (payload #>> '{}')::jsonb
36
+ where jsonb_typeof(payload) = 'string'
37
+ `.execute(db);
38
+ }
39
+
40
+ export const canonicalJsonbShapeConstraintTargets = [
41
+ 'connection',
42
+ 'feature',
43
+ 'transaction',
44
+ 'source',
45
+ 'ownerStatementProjection',
46
+ ] as const;
47
+
48
+ type CanonicalJsonbShapeConstraintTarget =
49
+ (typeof canonicalJsonbShapeConstraintTargets)[number];
50
+
51
+ export async function addCanonicalJsonbShapeConstraints(
52
+ db: Kysely<unknown>,
53
+ target: CanonicalJsonbShapeConstraintTarget
54
+ ): Promise<void> {
55
+ if (target === 'connection') {
56
+ await sql`
57
+ do $$
58
+ begin
59
+ if not exists (
60
+ select 1 from pg_constraint
61
+ where conname = 'connection_credentials_object_check'
62
+ and conrelid = 'public.connection'::regclass
63
+ ) then
64
+ alter table public.connection
65
+ add constraint connection_credentials_object_check
66
+ check (
67
+ credentials is null
68
+ or jsonb_typeof(credentials) = 'object'
69
+ ) not valid;
70
+ end if;
71
+ if not exists (
72
+ select 1 from pg_constraint
73
+ where conname = 'connection_persistent_state_object_check'
74
+ and conrelid = 'public.connection'::regclass
75
+ ) then
76
+ alter table public.connection
77
+ add constraint connection_persistent_state_object_check
78
+ check (
79
+ persistent_state is null
80
+ or jsonb_typeof(persistent_state) = 'object'
81
+ ) not valid;
82
+ end if;
83
+ end
84
+ $$
85
+ `.execute(db);
86
+ } else if (target === 'feature') {
87
+ await sql`
88
+ do $$
89
+ begin
90
+ if not exists (
91
+ select 1 from pg_constraint
92
+ where conname = 'feature_required_approvals_array_check'
93
+ and conrelid = 'public.feature'::regclass
94
+ ) then
95
+ alter table public.feature
96
+ add constraint feature_required_approvals_array_check
97
+ check (
98
+ required_approvals is null
99
+ or jsonb_typeof(required_approvals) = 'array'
100
+ ) not valid;
101
+ end if;
102
+ end
103
+ $$
104
+ `.execute(db);
105
+ } else if (target === 'transaction') {
106
+ await sql`
107
+ do $$
108
+ begin
109
+ if not exists (
110
+ select 1 from pg_constraint
111
+ where conname = 'transaction_recurring_pattern_object_check'
112
+ and conrelid = 'accounting.transaction'::regclass
113
+ ) then
114
+ alter table accounting.transaction
115
+ add constraint transaction_recurring_pattern_object_check
116
+ check (
117
+ recurring_pattern is null
118
+ or jsonb_typeof(recurring_pattern) = 'object'
119
+ ) not valid;
120
+ end if;
121
+ end
122
+ $$
123
+ `.execute(db);
124
+ } else if (target === 'source') {
125
+ await sql`
126
+ do $$
127
+ begin
128
+ if not exists (
129
+ select 1 from pg_constraint
130
+ where conname = 'source_owner_statement_json_object_check'
131
+ and conrelid = 'public.source'::regclass
132
+ ) then
133
+ alter table public.source
134
+ add constraint source_owner_statement_json_object_check
135
+ check (
136
+ type <> 'ownerStatement'
137
+ or json is null
138
+ or jsonb_typeof(json) = 'object'
139
+ ) not valid;
140
+ end if;
141
+ end
142
+ $$
143
+ `.execute(db);
144
+ } else {
145
+ await sql`
146
+ do $$
147
+ begin
148
+ if not exists (
149
+ select 1 from pg_constraint
150
+ where conname = 'owner_statement_projection_payload_container_check'
151
+ and conrelid = 'accounting.owner_statement_projection'::regclass
152
+ ) then
153
+ alter table accounting.owner_statement_projection
154
+ add constraint owner_statement_projection_payload_container_check
155
+ check (jsonb_typeof(payload) in ('array', 'object')) not valid;
156
+ end if;
157
+ end
158
+ $$
159
+ `.execute(db);
160
+ }
161
+ }
162
+
163
+ export async function validateCanonicalJsonbShapeConstraints(
164
+ db: Kysely<unknown>,
165
+ target: CanonicalJsonbShapeConstraintTarget
166
+ ): Promise<void> {
167
+ if (target === 'connection') {
168
+ await sql`
169
+ alter table public.connection
170
+ validate constraint connection_credentials_object_check
171
+ `.execute(db);
172
+ await sql`
173
+ alter table public.connection
174
+ validate constraint connection_persistent_state_object_check
175
+ `.execute(db);
176
+ } else if (target === 'feature') {
177
+ await sql`
178
+ alter table public.feature
179
+ validate constraint feature_required_approvals_array_check
180
+ `.execute(db);
181
+ } else if (target === 'transaction') {
182
+ await sql`
183
+ alter table accounting.transaction
184
+ validate constraint transaction_recurring_pattern_object_check
185
+ `.execute(db);
186
+ } else if (target === 'source') {
187
+ await sql`
188
+ alter table public.source
189
+ validate constraint source_owner_statement_json_object_check
190
+ `.execute(db);
191
+ } else {
192
+ await sql`
193
+ alter table accounting.owner_statement_projection
194
+ validate constraint owner_statement_projection_payload_container_check
195
+ `.execute(db);
196
+ }
197
+ }
198
+
199
+ export async function up(db: Kysely<unknown>): Promise<void> {
200
+ await backfillCanonicalJsonbShapes(db);
201
+ for (const target of canonicalJsonbShapeConstraintTargets) {
202
+ await addCanonicalJsonbShapeConstraints(db, target);
203
+ }
204
+ for (const target of canonicalJsonbShapeConstraintTargets) {
205
+ await validateCanonicalJsonbShapeConstraints(db, target);
206
+ }
207
+ }
208
+
209
+ export async function down(db: Kysely<unknown>): Promise<void> {
210
+ await sql`
211
+ alter table accounting.owner_statement_projection
212
+ drop constraint if exists owner_statement_projection_payload_container_check
213
+ `.execute(db);
214
+ await sql`
215
+ alter table public.source
216
+ drop constraint if exists source_owner_statement_json_object_check
217
+ `.execute(db);
218
+ await sql`
219
+ alter table accounting.transaction
220
+ drop constraint if exists transaction_recurring_pattern_object_check
221
+ `.execute(db);
222
+ await sql`
223
+ alter table public.feature
224
+ drop constraint if exists feature_required_approvals_array_check
225
+ `.execute(db);
226
+ await sql`
227
+ alter table public.connection
228
+ drop constraint if exists connection_persistent_state_object_check,
229
+ drop constraint if exists connection_credentials_object_check
230
+ `.execute(db);
231
+ }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.3.45-4697",
6
+ "version": "1.3.45-4704",
7
7
  "description": "",
8
8
  "main": "build/main/index.js",
9
9
  "module": "build/module/index.js",