@vrplatform/kysely 1.3.45-40371378523 → 1.3.45-40436448893
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,60 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import { sql } from 'kysely';
|
|
3
|
+
|
|
4
|
+
// PRO-19885: authentication reads tokens only from control_plane.token. The
|
|
5
|
+
// regional table has no reader or writer left. Crunchy keeps its frozen copy:
|
|
6
|
+
// it is the only database that holds VRIntegrations teams, and the only one
|
|
7
|
+
// where the baseline partner is not the sole team with that realm. Dropped
|
|
8
|
+
// without cascade, so a missed dependency fails the migration.
|
|
9
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
10
|
+
await sql`set local lock_timeout = '5s'`.execute(db);
|
|
11
|
+
await sql`set local statement_timeout = '60s'`.execute(db);
|
|
12
|
+
|
|
13
|
+
const holdsVrIntegrationTeams = await sql<{ holds: boolean }>`
|
|
14
|
+
select exists (
|
|
15
|
+
select 1
|
|
16
|
+
from public.tenant
|
|
17
|
+
where storage_realm = 'vrintegration'
|
|
18
|
+
and id <> 'd6ab2bbe-e0fc-48d9-89a3-c3c05f4e79b5'
|
|
19
|
+
) as holds
|
|
20
|
+
`.execute(db);
|
|
21
|
+
if (holdsVrIntegrationTeams.rows[0]?.holds) return;
|
|
22
|
+
|
|
23
|
+
await sql`drop table public.token`.execute(db);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Recreates the empty table as the baseline defined it. Rows are not restored.
|
|
27
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
28
|
+
await sql`
|
|
29
|
+
create table if not exists public.token (
|
|
30
|
+
id text primary key default gen_random_uuid(),
|
|
31
|
+
payload jsonb not null default jsonb_build_object(),
|
|
32
|
+
created_at timestamptz not null default now(),
|
|
33
|
+
expires_at timestamptz,
|
|
34
|
+
type text,
|
|
35
|
+
nano_id text not null default gen_random_uuid(),
|
|
36
|
+
tenant_id uuid references public.tenant (id)
|
|
37
|
+
on update restrict on delete set null,
|
|
38
|
+
user_id uuid references public."user" (id)
|
|
39
|
+
on update restrict on delete set null
|
|
40
|
+
)
|
|
41
|
+
`.execute(db);
|
|
42
|
+
await sql`
|
|
43
|
+
create index if not exists token_id_type_idx on public.token (id, type)
|
|
44
|
+
`.execute(db);
|
|
45
|
+
await sql`
|
|
46
|
+
create index if not exists token_nano_id_idx on public.token (nano_id)
|
|
47
|
+
`.execute(db);
|
|
48
|
+
await sql`
|
|
49
|
+
create unique index if not exists token_partner_secret_unique_idx
|
|
50
|
+
on public.token (nano_id)
|
|
51
|
+
where type = any (array['partner_api_key', 'embedded_bootstrap'])
|
|
52
|
+
`.execute(db);
|
|
53
|
+
await sql`
|
|
54
|
+
create index if not exists token_tenant_id_id_type_idx
|
|
55
|
+
on public.token (tenant_id, id, type)
|
|
56
|
+
`.execute(db);
|
|
57
|
+
await sql`
|
|
58
|
+
create index if not exists token_tenant_id_key on public.token (tenant_id)
|
|
59
|
+
`.execute(db);
|
|
60
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type { Kysely } from 'kysely';
|
|
2
|
+
import { sql } from 'kysely';
|
|
3
|
+
|
|
4
|
+
// GET /transactions looks up who created each listed transaction in
|
|
5
|
+
// audit.mutation. The general subject index forces a heap read for every
|
|
6
|
+
// mutation of the transaction (updates included) before the INSERT filter, and
|
|
7
|
+
// cold reads took 330-445 ms for 100 rows on the largest teams. This partial index holds only the
|
|
8
|
+
// creation rows and carries the columns the lookup reads. On large databases
|
|
9
|
+
// run scripts/ben-2/pro-19855-create-audit-index.ts first: it builds the index
|
|
10
|
+
// concurrently, and this migration then skips it. The lookup must
|
|
11
|
+
// keep `subject_type` and `op` as constants so a generic plan can use the index.
|
|
12
|
+
export async function up(db: Kysely<unknown>): Promise<void> {
|
|
13
|
+
await sql`set local lock_timeout = '5s'`.execute(db);
|
|
14
|
+
await sql`set local statement_timeout = '30min'`.execute(db);
|
|
15
|
+
const existing = await sql<{ valid: boolean }>`
|
|
16
|
+
select (i.indisvalid and i.indisready) as valid
|
|
17
|
+
from pg_index as i
|
|
18
|
+
inner join pg_class as index_class on index_class.oid = i.indexrelid
|
|
19
|
+
where index_class.relname = 'audit_mutation_transaction_insert_idx'
|
|
20
|
+
`.execute(db);
|
|
21
|
+
// `create index if not exists` locks the table before it finds the index, which
|
|
22
|
+
// times out on a busy database, so skip a valid pre-built index here.
|
|
23
|
+
if (existing.rows[0]?.valid) return;
|
|
24
|
+
// A failed concurrent build leaves an invalid index that `if not exists`
|
|
25
|
+
// would keep, so drop it first.
|
|
26
|
+
if (existing.rows[0]) {
|
|
27
|
+
await sql`
|
|
28
|
+
drop index audit.audit_mutation_transaction_insert_idx
|
|
29
|
+
`.execute(db);
|
|
30
|
+
}
|
|
31
|
+
await sql`
|
|
32
|
+
create index audit_mutation_transaction_insert_idx
|
|
33
|
+
on audit.mutation (tenant_id, subject_id) include (action_id, created_at)
|
|
34
|
+
where subject_type = 'transaction' and op = 'INSERT'
|
|
35
|
+
`.execute(db);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export async function down(db: Kysely<unknown>): Promise<void> {
|
|
39
|
+
await sql`set local lock_timeout = '5s'`.execute(db);
|
|
40
|
+
await sql`set local statement_timeout = '60s'`.execute(db);
|
|
41
|
+
await sql`
|
|
42
|
+
drop index if exists audit.audit_mutation_transaction_insert_idx
|
|
43
|
+
`.execute(db);
|
|
44
|
+
}
|