@vrplatform/kysely 1.3.45-6410 → 1.3.45-6439
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,399 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import { sql } from 'kysely';
|
|
3
|
+
|
|
4
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
5
|
+
await sql`set local lock_timeout = '5s'`.execute(db);
|
|
6
|
+
await sql`
|
|
7
|
+
create table public.line_classification_usage_capture (
|
|
8
|
+
id uuid primary key default gen_random_uuid(),
|
|
9
|
+
tenant_id uuid not null references public.tenant(id) on delete cascade,
|
|
10
|
+
kind text not null,
|
|
11
|
+
name text not null,
|
|
12
|
+
first_seen_at timestamptz,
|
|
13
|
+
last_seen_at timestamptz,
|
|
14
|
+
created_at timestamptz not null default now(),
|
|
15
|
+
constraint line_classification_usage_capture_kind_check
|
|
16
|
+
check (kind in ('paymentLine', 'reservationLine')),
|
|
17
|
+
constraint line_classification_usage_capture_observation_check
|
|
18
|
+
check (
|
|
19
|
+
(first_seen_at is null and last_seen_at is null) or
|
|
20
|
+
(
|
|
21
|
+
first_seen_at is not null and
|
|
22
|
+
last_seen_at is not null and
|
|
23
|
+
first_seen_at <= last_seen_at
|
|
24
|
+
)
|
|
25
|
+
)
|
|
26
|
+
);
|
|
27
|
+
`.execute(db);
|
|
28
|
+
|
|
29
|
+
await sql`
|
|
30
|
+
create index line_classification_usage_capture_drain_idx
|
|
31
|
+
on public.line_classification_usage_capture (created_at, id)
|
|
32
|
+
`.execute(db);
|
|
33
|
+
await sql`
|
|
34
|
+
create index line_classification_usage_capture_tenant_kind_name_idx
|
|
35
|
+
on public.line_classification_usage_capture (tenant_id, kind, name)
|
|
36
|
+
`.execute(db);
|
|
37
|
+
|
|
38
|
+
await sql`
|
|
39
|
+
create or replace function public.enqueue_line_classification_usage()
|
|
40
|
+
returns trigger
|
|
41
|
+
language plpgsql
|
|
42
|
+
as $$
|
|
43
|
+
begin
|
|
44
|
+
if tg_op = 'INSERT' then
|
|
45
|
+
insert into public.line_classification_usage_capture (
|
|
46
|
+
tenant_id,
|
|
47
|
+
kind,
|
|
48
|
+
name,
|
|
49
|
+
first_seen_at,
|
|
50
|
+
last_seen_at
|
|
51
|
+
)
|
|
52
|
+
select distinct
|
|
53
|
+
line.tenant_id,
|
|
54
|
+
case
|
|
55
|
+
when line.reservation_id is not null and line.payment_id is null
|
|
56
|
+
then 'reservationLine'
|
|
57
|
+
when line.payment_id is not null then 'paymentLine'
|
|
58
|
+
when classification.type in ('paymentLine', 'reservationLine')
|
|
59
|
+
then classification.type
|
|
60
|
+
end,
|
|
61
|
+
coalesce(line.effective_type, line.type),
|
|
62
|
+
statement_timestamp(),
|
|
63
|
+
statement_timestamp()
|
|
64
|
+
from new_payment_lines as line
|
|
65
|
+
left join public.payment_line_classification as classification
|
|
66
|
+
on classification.name = coalesce(line.effective_type, line.type)
|
|
67
|
+
where line.tenant_id is not null
|
|
68
|
+
and coalesce(line.effective_type, line.type) is not null
|
|
69
|
+
and coalesce(line.effective_type, line.type) != ''
|
|
70
|
+
and (
|
|
71
|
+
(line.reservation_id is not null and line.payment_id is null) or
|
|
72
|
+
line.payment_id is not null or
|
|
73
|
+
classification.type in ('paymentLine', 'reservationLine')
|
|
74
|
+
);
|
|
75
|
+
else
|
|
76
|
+
insert into public.line_classification_usage_capture (
|
|
77
|
+
tenant_id,
|
|
78
|
+
kind,
|
|
79
|
+
name,
|
|
80
|
+
first_seen_at,
|
|
81
|
+
last_seen_at
|
|
82
|
+
)
|
|
83
|
+
select distinct
|
|
84
|
+
line.tenant_id,
|
|
85
|
+
case
|
|
86
|
+
when line.reservation_id is not null and line.payment_id is null
|
|
87
|
+
then 'reservationLine'
|
|
88
|
+
when line.payment_id is not null then 'paymentLine'
|
|
89
|
+
when classification.type in ('paymentLine', 'reservationLine')
|
|
90
|
+
then classification.type
|
|
91
|
+
end,
|
|
92
|
+
coalesce(line.effective_type, line.type),
|
|
93
|
+
statement_timestamp(),
|
|
94
|
+
statement_timestamp()
|
|
95
|
+
from new_payment_lines as line
|
|
96
|
+
inner join old_payment_lines as old_line on old_line.id = line.id
|
|
97
|
+
left join public.payment_line_classification as classification
|
|
98
|
+
on classification.name = coalesce(line.effective_type, line.type)
|
|
99
|
+
where row(
|
|
100
|
+
line.tenant_id,
|
|
101
|
+
line.effective_type,
|
|
102
|
+
line.type,
|
|
103
|
+
line.reservation_id,
|
|
104
|
+
line.payment_id
|
|
105
|
+
) is distinct from row(
|
|
106
|
+
old_line.tenant_id,
|
|
107
|
+
old_line.effective_type,
|
|
108
|
+
old_line.type,
|
|
109
|
+
old_line.reservation_id,
|
|
110
|
+
old_line.payment_id
|
|
111
|
+
)
|
|
112
|
+
and line.tenant_id is not null
|
|
113
|
+
and coalesce(line.effective_type, line.type) is not null
|
|
114
|
+
and coalesce(line.effective_type, line.type) != ''
|
|
115
|
+
and (
|
|
116
|
+
(line.reservation_id is not null and line.payment_id is null) or
|
|
117
|
+
line.payment_id is not null or
|
|
118
|
+
classification.type in ('paymentLine', 'reservationLine')
|
|
119
|
+
);
|
|
120
|
+
end if;
|
|
121
|
+
return null;
|
|
122
|
+
end;
|
|
123
|
+
$$;
|
|
124
|
+
`.execute(db);
|
|
125
|
+
|
|
126
|
+
await sql`
|
|
127
|
+
create or replace function
|
|
128
|
+
public.enqueue_transaction_line_classification_usage()
|
|
129
|
+
returns trigger
|
|
130
|
+
language plpgsql
|
|
131
|
+
as $$
|
|
132
|
+
begin
|
|
133
|
+
if tg_op = 'INSERT' then
|
|
134
|
+
insert into public.line_classification_usage_capture (
|
|
135
|
+
tenant_id,
|
|
136
|
+
kind,
|
|
137
|
+
name,
|
|
138
|
+
first_seen_at,
|
|
139
|
+
last_seen_at
|
|
140
|
+
)
|
|
141
|
+
select distinct
|
|
142
|
+
line.tenant_id,
|
|
143
|
+
'paymentLine',
|
|
144
|
+
line.match_line_type_classification,
|
|
145
|
+
statement_timestamp(),
|
|
146
|
+
statement_timestamp()
|
|
147
|
+
from new_transaction_lines as line
|
|
148
|
+
where line.match_line_type_classification is not null
|
|
149
|
+
and line.match_line_type_classification != '';
|
|
150
|
+
else
|
|
151
|
+
insert into public.line_classification_usage_capture (
|
|
152
|
+
tenant_id,
|
|
153
|
+
kind,
|
|
154
|
+
name,
|
|
155
|
+
first_seen_at,
|
|
156
|
+
last_seen_at
|
|
157
|
+
)
|
|
158
|
+
select distinct
|
|
159
|
+
line.tenant_id,
|
|
160
|
+
'paymentLine',
|
|
161
|
+
line.match_line_type_classification,
|
|
162
|
+
statement_timestamp(),
|
|
163
|
+
statement_timestamp()
|
|
164
|
+
from new_transaction_lines as line
|
|
165
|
+
inner join old_transaction_lines as old_line on old_line.id = line.id
|
|
166
|
+
where row(
|
|
167
|
+
line.tenant_id,
|
|
168
|
+
line.match_line_type_classification
|
|
169
|
+
) is distinct from row(
|
|
170
|
+
old_line.tenant_id,
|
|
171
|
+
old_line.match_line_type_classification
|
|
172
|
+
)
|
|
173
|
+
and line.match_line_type_classification is not null
|
|
174
|
+
and line.match_line_type_classification != '';
|
|
175
|
+
end if;
|
|
176
|
+
return null;
|
|
177
|
+
end;
|
|
178
|
+
$$;
|
|
179
|
+
`.execute(db);
|
|
180
|
+
|
|
181
|
+
await sql`
|
|
182
|
+
create or replace function
|
|
183
|
+
public.enqueue_reservation_line_mapping_usage()
|
|
184
|
+
returns trigger
|
|
185
|
+
language plpgsql
|
|
186
|
+
as $$
|
|
187
|
+
begin
|
|
188
|
+
if tg_op = 'INSERT' then
|
|
189
|
+
insert into public.line_classification_usage_capture (
|
|
190
|
+
tenant_id,
|
|
191
|
+
kind,
|
|
192
|
+
name,
|
|
193
|
+
first_seen_at,
|
|
194
|
+
last_seen_at
|
|
195
|
+
)
|
|
196
|
+
select distinct
|
|
197
|
+
mapping.tenant_id,
|
|
198
|
+
'reservationLine',
|
|
199
|
+
mapping.line_type,
|
|
200
|
+
null::timestamptz,
|
|
201
|
+
null::timestamptz
|
|
202
|
+
from new_reservation_line_mappings as mapping
|
|
203
|
+
where mapping.booking_channel is null
|
|
204
|
+
and mapping.line_type is not null
|
|
205
|
+
and mapping.line_type != '';
|
|
206
|
+
elsif tg_op = 'UPDATE' then
|
|
207
|
+
insert into public.line_classification_usage_capture (
|
|
208
|
+
tenant_id,
|
|
209
|
+
kind,
|
|
210
|
+
name,
|
|
211
|
+
first_seen_at,
|
|
212
|
+
last_seen_at
|
|
213
|
+
)
|
|
214
|
+
select distinct
|
|
215
|
+
changed.tenant_id,
|
|
216
|
+
'reservationLine',
|
|
217
|
+
changed.line_type,
|
|
218
|
+
null::timestamptz,
|
|
219
|
+
null::timestamptz
|
|
220
|
+
from (
|
|
221
|
+
select
|
|
222
|
+
mapping.tenant_id,
|
|
223
|
+
mapping.line_type,
|
|
224
|
+
mapping.booking_channel
|
|
225
|
+
from new_reservation_line_mappings as mapping
|
|
226
|
+
inner join old_reservation_line_mappings as old_mapping
|
|
227
|
+
on old_mapping.id = mapping.id
|
|
228
|
+
where row(
|
|
229
|
+
mapping.tenant_id,
|
|
230
|
+
mapping.line_type,
|
|
231
|
+
mapping.booking_channel,
|
|
232
|
+
mapping.account_id
|
|
233
|
+
) is distinct from row(
|
|
234
|
+
old_mapping.tenant_id,
|
|
235
|
+
old_mapping.line_type,
|
|
236
|
+
old_mapping.booking_channel,
|
|
237
|
+
old_mapping.account_id
|
|
238
|
+
)
|
|
239
|
+
union
|
|
240
|
+
select
|
|
241
|
+
old_mapping.tenant_id,
|
|
242
|
+
old_mapping.line_type,
|
|
243
|
+
old_mapping.booking_channel
|
|
244
|
+
from old_reservation_line_mappings as old_mapping
|
|
245
|
+
inner join new_reservation_line_mappings as mapping
|
|
246
|
+
on mapping.id = old_mapping.id
|
|
247
|
+
where row(
|
|
248
|
+
mapping.tenant_id,
|
|
249
|
+
mapping.line_type,
|
|
250
|
+
mapping.booking_channel,
|
|
251
|
+
mapping.account_id
|
|
252
|
+
) is distinct from row(
|
|
253
|
+
old_mapping.tenant_id,
|
|
254
|
+
old_mapping.line_type,
|
|
255
|
+
old_mapping.booking_channel,
|
|
256
|
+
old_mapping.account_id
|
|
257
|
+
)
|
|
258
|
+
) as changed
|
|
259
|
+
where changed.booking_channel is null
|
|
260
|
+
and changed.line_type is not null
|
|
261
|
+
and changed.line_type != '';
|
|
262
|
+
else
|
|
263
|
+
insert into public.line_classification_usage_capture (
|
|
264
|
+
tenant_id,
|
|
265
|
+
kind,
|
|
266
|
+
name,
|
|
267
|
+
first_seen_at,
|
|
268
|
+
last_seen_at
|
|
269
|
+
)
|
|
270
|
+
select distinct
|
|
271
|
+
mapping.tenant_id,
|
|
272
|
+
'reservationLine',
|
|
273
|
+
mapping.line_type,
|
|
274
|
+
null::timestamptz,
|
|
275
|
+
null::timestamptz
|
|
276
|
+
from old_reservation_line_mappings as mapping
|
|
277
|
+
where mapping.booking_channel is null
|
|
278
|
+
and mapping.line_type is not null
|
|
279
|
+
and mapping.line_type != '';
|
|
280
|
+
end if;
|
|
281
|
+
return null;
|
|
282
|
+
end;
|
|
283
|
+
$$;
|
|
284
|
+
`.execute(db);
|
|
285
|
+
|
|
286
|
+
await sql`drop trigger payment_line_classification_usage_insert
|
|
287
|
+
on public.payment_line`.execute(db);
|
|
288
|
+
await sql`drop trigger payment_line_classification_usage_update
|
|
289
|
+
on public.payment_line`.execute(db);
|
|
290
|
+
await sql`drop trigger transaction_line_classification_usage_insert
|
|
291
|
+
on accounting.transaction_line`.execute(db);
|
|
292
|
+
await sql`drop trigger transaction_line_classification_usage_update
|
|
293
|
+
on accounting.transaction_line`.execute(db);
|
|
294
|
+
await sql`
|
|
295
|
+
create trigger payment_line_classification_usage_insert
|
|
296
|
+
after insert on public.payment_line
|
|
297
|
+
referencing new table as new_payment_lines
|
|
298
|
+
for each statement
|
|
299
|
+
execute function public.enqueue_line_classification_usage()
|
|
300
|
+
`.execute(db);
|
|
301
|
+
await sql`
|
|
302
|
+
create trigger payment_line_classification_usage_update
|
|
303
|
+
after update on public.payment_line
|
|
304
|
+
referencing old table as old_payment_lines new table as new_payment_lines
|
|
305
|
+
for each statement
|
|
306
|
+
execute function public.enqueue_line_classification_usage()
|
|
307
|
+
`.execute(db);
|
|
308
|
+
await sql`
|
|
309
|
+
create trigger transaction_line_classification_usage_insert
|
|
310
|
+
after insert on accounting.transaction_line
|
|
311
|
+
referencing new table as new_transaction_lines
|
|
312
|
+
for each statement
|
|
313
|
+
execute function public.enqueue_transaction_line_classification_usage()
|
|
314
|
+
`.execute(db);
|
|
315
|
+
await sql`
|
|
316
|
+
create trigger transaction_line_classification_usage_update
|
|
317
|
+
after update on accounting.transaction_line
|
|
318
|
+
referencing old table as old_transaction_lines
|
|
319
|
+
new table as new_transaction_lines
|
|
320
|
+
for each statement
|
|
321
|
+
execute function public.enqueue_transaction_line_classification_usage()
|
|
322
|
+
`.execute(db);
|
|
323
|
+
await sql`
|
|
324
|
+
create trigger reservation_line_mapping_usage_insert
|
|
325
|
+
after insert on accounting.account_reservation_line_type
|
|
326
|
+
referencing new table as new_reservation_line_mappings
|
|
327
|
+
for each statement
|
|
328
|
+
execute function public.enqueue_reservation_line_mapping_usage()
|
|
329
|
+
`.execute(db);
|
|
330
|
+
await sql`
|
|
331
|
+
create trigger reservation_line_mapping_usage_update
|
|
332
|
+
after update on accounting.account_reservation_line_type
|
|
333
|
+
referencing old table as old_reservation_line_mappings
|
|
334
|
+
new table as new_reservation_line_mappings
|
|
335
|
+
for each statement
|
|
336
|
+
execute function public.enqueue_reservation_line_mapping_usage()
|
|
337
|
+
`.execute(db);
|
|
338
|
+
await sql`
|
|
339
|
+
create trigger reservation_line_mapping_usage_delete
|
|
340
|
+
after delete on accounting.account_reservation_line_type
|
|
341
|
+
referencing old table as old_reservation_line_mappings
|
|
342
|
+
for each statement
|
|
343
|
+
execute function public.enqueue_reservation_line_mapping_usage()
|
|
344
|
+
`.execute(db);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
348
|
+
await sql`set local lock_timeout = '5s'`.execute(db);
|
|
349
|
+
await sql`drop trigger reservation_line_mapping_usage_delete
|
|
350
|
+
on accounting.account_reservation_line_type`.execute(db);
|
|
351
|
+
await sql`drop trigger reservation_line_mapping_usage_update
|
|
352
|
+
on accounting.account_reservation_line_type`.execute(db);
|
|
353
|
+
await sql`drop trigger reservation_line_mapping_usage_insert
|
|
354
|
+
on accounting.account_reservation_line_type`.execute(db);
|
|
355
|
+
await sql`drop trigger payment_line_classification_usage_insert
|
|
356
|
+
on public.payment_line`.execute(db);
|
|
357
|
+
await sql`drop trigger payment_line_classification_usage_update
|
|
358
|
+
on public.payment_line`.execute(db);
|
|
359
|
+
await sql`drop trigger transaction_line_classification_usage_insert
|
|
360
|
+
on accounting.transaction_line`.execute(db);
|
|
361
|
+
await sql`drop trigger transaction_line_classification_usage_update
|
|
362
|
+
on accounting.transaction_line`.execute(db);
|
|
363
|
+
await sql`
|
|
364
|
+
create trigger payment_line_classification_usage_insert
|
|
365
|
+
after insert on public.payment_line
|
|
366
|
+
referencing new table as new_payment_lines
|
|
367
|
+
for each statement
|
|
368
|
+
execute function public.capture_line_classification_usage()
|
|
369
|
+
`.execute(db);
|
|
370
|
+
await sql`
|
|
371
|
+
create trigger payment_line_classification_usage_update
|
|
372
|
+
after update on public.payment_line
|
|
373
|
+
referencing new table as new_payment_lines
|
|
374
|
+
for each statement
|
|
375
|
+
execute function public.capture_line_classification_usage()
|
|
376
|
+
`.execute(db);
|
|
377
|
+
await sql`
|
|
378
|
+
create trigger transaction_line_classification_usage_insert
|
|
379
|
+
after insert on accounting.transaction_line
|
|
380
|
+
referencing new table as new_transaction_lines
|
|
381
|
+
for each statement
|
|
382
|
+
execute function public.capture_transaction_line_classification_usage()
|
|
383
|
+
`.execute(db);
|
|
384
|
+
await sql`
|
|
385
|
+
create trigger transaction_line_classification_usage_update
|
|
386
|
+
after update on accounting.transaction_line
|
|
387
|
+
referencing new table as new_transaction_lines
|
|
388
|
+
for each statement
|
|
389
|
+
execute function public.capture_transaction_line_classification_usage()
|
|
390
|
+
`.execute(db);
|
|
391
|
+
await sql`drop function
|
|
392
|
+
public.enqueue_reservation_line_mapping_usage()`.execute(db);
|
|
393
|
+
await sql`drop function
|
|
394
|
+
public.enqueue_transaction_line_classification_usage()`.execute(db);
|
|
395
|
+
await sql`drop function public.enqueue_line_classification_usage()`.execute(
|
|
396
|
+
db
|
|
397
|
+
);
|
|
398
|
+
await sql`drop table public.line_classification_usage_capture`.execute(db);
|
|
399
|
+
}
|
|
@@ -1399,6 +1399,15 @@ export interface PublicLatestDuplicateEmails {
|
|
|
1399
1399
|
name: string | null;
|
|
1400
1400
|
status: string | null;
|
|
1401
1401
|
}
|
|
1402
|
+
export interface PublicLineClassificationUsageCapture {
|
|
1403
|
+
createdAt: Generated<Timestamp>;
|
|
1404
|
+
firstSeenAt: Timestamp | null;
|
|
1405
|
+
id: Generated<string>;
|
|
1406
|
+
kind: string;
|
|
1407
|
+
lastSeenAt: Timestamp | null;
|
|
1408
|
+
name: string;
|
|
1409
|
+
tenantId: string;
|
|
1410
|
+
}
|
|
1402
1411
|
export interface PublicLineClassificationUsage {
|
|
1403
1412
|
appId: string | null;
|
|
1404
1413
|
createdAt: Generated<Timestamp>;
|
|
@@ -2452,6 +2461,7 @@ export interface DB {
|
|
|
2452
2461
|
"public.latestDuplicateEmails": PublicLatestDuplicateEmails;
|
|
2453
2462
|
"public.lineClassificationUsage": PublicLineClassificationUsage;
|
|
2454
2463
|
"public.lineClassificationUsageBackfill": PublicLineClassificationUsageBackfill;
|
|
2464
|
+
"public.lineClassificationUsageCapture": PublicLineClassificationUsageCapture;
|
|
2455
2465
|
"public.listing": PublicListing;
|
|
2456
2466
|
"public.listingCollection": PublicListingCollection;
|
|
2457
2467
|
"public.listingConnection": PublicListingConnection;
|