@vrplatform/kysely 1.3.45-stage.371 → 1.3.45-stage.392

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,136 @@
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`
6
+ create temporary table reservation_journal_full_refresh_repair_scope (
7
+ invalidation_id uuid primary key,
8
+ tenant_id uuid not null,
9
+ reservation_id uuid not null,
10
+ applied_at timestamptz not null,
11
+ covering_effect_id uuid not null
12
+ ) on commit drop
13
+ `.execute(db);
14
+
15
+ await sql`
16
+ insert into reservation_journal_full_refresh_repair_scope (
17
+ invalidation_id,
18
+ tenant_id,
19
+ reservation_id,
20
+ applied_at,
21
+ covering_effect_id
22
+ )
23
+ select
24
+ pending.id,
25
+ pending.tenant_id,
26
+ pending.reservation_id,
27
+ covering.applied_at,
28
+ covering.effect_id
29
+ from accounting.reservation_journal_invalidation as pending
30
+ inner join lateral (
31
+ select
32
+ applied.applied_at,
33
+ applied.effect_id
34
+ from accounting.reservation_journal_invalidation as applied
35
+ inner join audit.effect as effect
36
+ on effect.tenant_id = applied.tenant_id
37
+ and effect.id = applied.effect_id
38
+ where applied.tenant_id = pending.tenant_id
39
+ and applied.reservation_id = pending.reservation_id
40
+ and applied.revision >= pending.revision
41
+ and applied.applied_at is not null
42
+ and applied.covered_by_effect_id = applied.effect_id
43
+ and effect.status = 'completed'
44
+ and coalesce(effect.payload ->> 'mode', 'full') = 'full'
45
+ and (
46
+ select attempt.status
47
+ from audit.effect_attempt as attempt
48
+ where attempt.tenant_id = effect.tenant_id
49
+ and attempt.effect_id = effect.id
50
+ order by attempt.attempt_num desc
51
+ limit 1
52
+ ) is distinct from 'skipped_locked'
53
+ order by applied.revision desc
54
+ limit 1
55
+ ) as covering on true
56
+ where pending.applied_at is null
57
+ `.execute(db);
58
+
59
+ await sql`
60
+ update accounting.reservation_journal_invalidation as invalidation
61
+ set
62
+ applied_at = scope.applied_at,
63
+ covered_by_effect_id = scope.covering_effect_id
64
+ from reservation_journal_full_refresh_repair_scope as scope
65
+ where invalidation.id = scope.invalidation_id
66
+ and invalidation.applied_at is null
67
+ `.execute(db);
68
+
69
+ await sql`
70
+ select state.tenant_id
71
+ from accounting.reservation_journal_state as state
72
+ inner join (
73
+ select distinct tenant_id, reservation_id
74
+ from reservation_journal_full_refresh_repair_scope
75
+ ) as scope
76
+ on scope.tenant_id = state.tenant_id
77
+ and scope.reservation_id = state.reservation_id
78
+ order by state.tenant_id, state.reservation_id
79
+ for update of state
80
+ `.execute(db);
81
+
82
+ await sql`
83
+ with advancement as (
84
+ select
85
+ state.tenant_id,
86
+ state.reservation_id,
87
+ coalesce(
88
+ min(invalidation.revision) filter (
89
+ where invalidation.applied_at is null
90
+ ) - 1,
91
+ max(invalidation.revision),
92
+ state.applied_revision
93
+ ) as applied_revision,
94
+ min(invalidation.created_at) filter (
95
+ where invalidation.applied_at is null
96
+ ) as stale_since,
97
+ max(invalidation.applied_at) as calculated_at
98
+ from accounting.reservation_journal_state as state
99
+ inner join (
100
+ select distinct tenant_id, reservation_id
101
+ from reservation_journal_full_refresh_repair_scope
102
+ ) as scope
103
+ on scope.tenant_id = state.tenant_id
104
+ and scope.reservation_id = state.reservation_id
105
+ left join accounting.reservation_journal_invalidation as invalidation
106
+ on invalidation.tenant_id = state.tenant_id
107
+ and invalidation.reservation_id = state.reservation_id
108
+ group by
109
+ state.tenant_id,
110
+ state.reservation_id,
111
+ state.applied_revision
112
+ )
113
+ update accounting.reservation_journal_state as state
114
+ set
115
+ applied_revision = greatest(
116
+ state.applied_revision,
117
+ advancement.applied_revision
118
+ ),
119
+ stale_since = advancement.stale_since,
120
+ calculated_at = case
121
+ when advancement.applied_revision > state.applied_revision
122
+ then greatest(state.calculated_at, advancement.calculated_at)
123
+ else state.calculated_at
124
+ end,
125
+ updated_at = now()
126
+ from advancement
127
+ where state.tenant_id = advancement.tenant_id
128
+ and state.reservation_id = advancement.reservation_id
129
+ and (
130
+ state.applied_revision < advancement.applied_revision
131
+ or state.stale_since is distinct from advancement.stale_since
132
+ )
133
+ `.execute(db);
134
+ }
135
+
136
+ export async function down(_db: Kysely<unknown>): Promise<void> {}
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.3.45-stage.371",
6
+ "version": "1.3.45-stage.392",
7
7
  "description": "",
8
8
  "main": "build/main/index.js",
9
9
  "module": "build/module/index.js",