@opengeni/db 0.12.6 → 0.13.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,140 @@
1
+ import { sql } from "drizzle-orm";
2
+ import {
3
+ bigint,
4
+ check,
5
+ index,
6
+ pgTable,
7
+ text,
8
+ timestamp,
9
+ uniqueIndex,
10
+ uuid,
11
+ } from "drizzle-orm/pg-core";
12
+
13
+ // Foreign keys and the cross-row revision/head integrity triggers live in the
14
+ // SQL migration. Keeping this additive schema leaf independent avoids a cycle
15
+ // back into schema.ts while retaining one canonical Drizzle namespace export.
16
+
17
+ export const workspaceInstructionPolicyRevisions = pgTable(
18
+ "workspace_instruction_policy_revisions",
19
+ {
20
+ id: uuid("id").primaryKey().defaultRandom(),
21
+ accountId: uuid("account_id").notNull(),
22
+ workspaceId: uuid("workspace_id").notNull(),
23
+ revision: bigint("revision", { mode: "number" })
24
+ .notNull()
25
+ .default(sql`nextval('workspace_instruction_policy_revision_seq')`),
26
+ kind: text("kind").notNull(),
27
+ scope: text("scope").notNull(),
28
+ roleKey: text("role_key"),
29
+ content: text("content").notNull(),
30
+ contentHash: text("content_hash").notNull(),
31
+ provenanceSource: text("provenance_source").notNull(),
32
+ provenanceSourceId: text("provenance_source_id"),
33
+ supersedesRevisionId: uuid("supersedes_revision_id"),
34
+ createdBySubjectId: text("created_by_subject_id").notNull(),
35
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
36
+ },
37
+ (table) => ({
38
+ workspaceRevision: uniqueIndex(
39
+ "workspace_instruction_policy_revisions_workspace_revision_uq",
40
+ ).on(table.workspaceId, table.revision),
41
+ workspaceHistory: index("workspace_instruction_policy_revisions_workspace_history_idx").on(
42
+ table.workspaceId,
43
+ table.kind,
44
+ table.scope,
45
+ table.roleKey,
46
+ table.revision,
47
+ ),
48
+ target: check(
49
+ "workspace_instruction_policy_revisions_target_chk",
50
+ sql`(
51
+ (${table.kind} = 'charter' and ${table.scope} = 'global' and ${table.roleKey} is null)
52
+ or (${table.kind} = 'policy' and ${table.scope} = 'global' and ${table.roleKey} is null)
53
+ or (${table.kind} = 'policy' and ${table.scope} = 'role' and ${table.roleKey} is not null)
54
+ )`,
55
+ ),
56
+ }),
57
+ );
58
+
59
+ export const workspaceInstructionPolicyHeads = pgTable(
60
+ "workspace_instruction_policy_heads",
61
+ {
62
+ id: uuid("id").primaryKey().defaultRandom(),
63
+ accountId: uuid("account_id").notNull(),
64
+ workspaceId: uuid("workspace_id").notNull(),
65
+ kind: text("kind").notNull(),
66
+ scope: text("scope").notNull(),
67
+ roleKey: text("role_key"),
68
+ revisionId: uuid("revision_id").notNull(),
69
+ revision: bigint("revision", { mode: "number" }).notNull(),
70
+ contentHash: text("content_hash").notNull(),
71
+ activationVersion: bigint("activation_version", { mode: "number" }).notNull(),
72
+ activatedAt: timestamp("activated_at", { withTimezone: true }).notNull().defaultNow(),
73
+ },
74
+ (table) => ({
75
+ charter: uniqueIndex("workspace_instruction_policy_heads_charter_uq")
76
+ .on(table.workspaceId)
77
+ .where(sql`${table.kind} = 'charter'`),
78
+ globalPolicy: uniqueIndex("workspace_instruction_policy_heads_global_policy_uq")
79
+ .on(table.workspaceId)
80
+ .where(sql`${table.kind} = 'policy' and ${table.scope} = 'global'`),
81
+ rolePolicy: uniqueIndex("workspace_instruction_policy_heads_role_policy_uq")
82
+ .on(table.workspaceId, table.roleKey)
83
+ .where(sql`${table.kind} = 'policy' and ${table.scope} = 'role'`),
84
+ target: check(
85
+ "workspace_instruction_policy_heads_target_chk",
86
+ sql`(
87
+ (${table.kind} = 'charter' and ${table.scope} = 'global' and ${table.roleKey} is null)
88
+ or (${table.kind} = 'policy' and ${table.scope} = 'global' and ${table.roleKey} is null)
89
+ or (${table.kind} = 'policy' and ${table.scope} = 'role' and ${table.roleKey} is not null)
90
+ )`,
91
+ ),
92
+ }),
93
+ );
94
+
95
+ export const workspaceInstructionPolicyActivationEvents = pgTable(
96
+ "workspace_instruction_policy_activation_events",
97
+ {
98
+ id: uuid("id").primaryKey().defaultRandom(),
99
+ accountId: uuid("account_id").notNull(),
100
+ workspaceId: uuid("workspace_id").notNull(),
101
+ kind: text("kind").notNull(),
102
+ scope: text("scope").notNull(),
103
+ roleKey: text("role_key"),
104
+ type: text("type").notNull(),
105
+ activationVersion: bigint("activation_version", { mode: "number" }).notNull(),
106
+ oldRevisionId: uuid("old_revision_id"),
107
+ oldRevision: bigint("old_revision", { mode: "number" }),
108
+ oldContentHash: text("old_content_hash"),
109
+ newRevisionId: uuid("new_revision_id").notNull(),
110
+ newRevision: bigint("new_revision", { mode: "number" }).notNull(),
111
+ newContentHash: text("new_content_hash").notNull(),
112
+ actorSubjectId: text("actor_subject_id").notNull(),
113
+ reason: text("reason").notNull(),
114
+ createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
115
+ },
116
+ (table) => ({
117
+ workspaceActivationVersion: uniqueIndex(
118
+ "workspace_instruction_policy_events_target_version_uq",
119
+ ).on(
120
+ table.workspaceId,
121
+ table.kind,
122
+ table.scope,
123
+ sql`coalesce(${table.roleKey}, '')`,
124
+ table.activationVersion,
125
+ ),
126
+ workspaceTimeline: index("workspace_instruction_policy_events_workspace_time_idx").on(
127
+ table.workspaceId,
128
+ table.createdAt,
129
+ table.id,
130
+ ),
131
+ target: check(
132
+ "workspace_instruction_policy_activation_events_target_chk",
133
+ sql`(
134
+ (${table.kind} = 'charter' and ${table.scope} = 'global' and ${table.roleKey} is null)
135
+ or (${table.kind} = 'policy' and ${table.scope} = 'global' and ${table.roleKey} is null)
136
+ or (${table.kind} = 'policy' and ${table.scope} = 'role' and ${table.roleKey} is not null)
137
+ )`,
138
+ ),
139
+ }),
140
+ );