hoja-cli 0.21.4__py3-none-any.whl
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.
- VERSION +1 -0
- google_workspace/__init__.py +19 -0
- google_workspace/client.py +253 -0
- google_workspace/config.py +39 -0
- google_workspace/errors.py +19 -0
- google_workspace/mcp_server.py +100 -0
- google_workspace/oauth.py +131 -0
- google_workspace/principal.py +83 -0
- google_workspace/py.typed +0 -0
- google_workspace/tools.py +64 -0
- google_workspace/writes.py +44 -0
- hoja/__init__.py +29 -0
- hoja/__main__.py +6 -0
- hoja/auth.py +42 -0
- hoja/cli.py +161 -0
- hoja/cli_state.py +55 -0
- hoja/client.py +268 -0
- hoja/commands/__init__.py +4 -0
- hoja/commands/auth_cmd.py +271 -0
- hoja/commands/comment.py +149 -0
- hoja/commands/cycle.py +75 -0
- hoja/commands/init.py +108 -0
- hoja/commands/issue.py +398 -0
- hoja/commands/label.py +163 -0
- hoja/commands/memory.py +240 -0
- hoja/commands/project.py +56 -0
- hoja/commands/raw.py +50 -0
- hoja/commands/team.py +102 -0
- hoja/commands/update.py +162 -0
- hoja/config.py +362 -0
- hoja/errors.py +107 -0
- hoja/filters.py +150 -0
- hoja/graphql/__init__.py +5 -0
- hoja/graphql/fragments.py +138 -0
- hoja/graphql/mutations.py +160 -0
- hoja/graphql/queries.py +245 -0
- hoja/mcp/__init__.py +27 -0
- hoja/mcp/content.py +151 -0
- hoja/mcp/prompts.py +298 -0
- hoja/mcp/resources.py +213 -0
- hoja/mcp/server.py +189 -0
- hoja/memory_board.py +370 -0
- hoja/models/__init__.py +37 -0
- hoja/models/base.py +64 -0
- hoja/models/cycle.py +32 -0
- hoja/models/enums.py +62 -0
- hoja/models/issue.py +99 -0
- hoja/models/label.py +17 -0
- hoja/models/project.py +60 -0
- hoja/models/team.py +31 -0
- hoja/models/user.py +32 -0
- hoja/models/workflow.py +65 -0
- hoja/output.py +410 -0
- hoja/skill_content/commands.md +222 -0
- hoja/skill_content/examples.md +193 -0
- hoja/skill_content/filters.md +101 -0
- hoja/skill_content/output-formats.md +92 -0
- hoja/skill_content/overview.md +123 -0
- hoja/skill_content/workflows.md +174 -0
- hoja_cli-0.21.4.dist-info/METADATA +279 -0
- hoja_cli-0.21.4.dist-info/RECORD +107 -0
- hoja_cli-0.21.4.dist-info/WHEEL +4 -0
- hoja_cli-0.21.4.dist-info/entry_points.txt +4 -0
- hoja_cli-0.21.4.dist-info/licenses/LICENSE +21 -0
- hoja_server/README.md +135 -0
- hoja_server/__init__.py +11 -0
- hoja_server/abac.py +116 -0
- hoja_server/agent_chat.py +280 -0
- hoja_server/agent_conversations.py +91 -0
- hoja_server/agent_durable.py +332 -0
- hoja_server/agent_prompt.py +117 -0
- hoja_server/agent_threads.py +96 -0
- hoja_server/agent_uploads.py +60 -0
- hoja_server/app.py +2189 -0
- hoja_server/cadence.py +231 -0
- hoja_server/cli.py +191 -0
- hoja_server/cloudflare_realtime.py +110 -0
- hoja_server/comment_policy.py +70 -0
- hoja_server/connector_actions.py +119 -0
- hoja_server/connectors_api.py +375 -0
- hoja_server/db.py +1816 -0
- hoja_server/denials.py +18 -0
- hoja_server/deployment.py +28 -0
- hoja_server/email_adapter.py +94 -0
- hoja_server/google_login.py +217 -0
- hoja_server/google_oidc.py +342 -0
- hoja_server/identity_transfer.py +210 -0
- hoja_server/mcp_oauth.py +623 -0
- hoja_server/mcp_tools.py +579 -0
- hoja_server/meeting_policy.py +179 -0
- hoja_server/meetings.py +1332 -0
- hoja_server/meetings_api.py +909 -0
- hoja_server/model_providers.py +205 -0
- hoja_server/realtime.py +690 -0
- hoja_server/resolvers.py +1281 -0
- hoja_server/schema.graphql +477 -0
- hoja_server/schema_sdl.py +8 -0
- hoja_server/secrets_box.py +39 -0
- hoja_server/signup_config.py +292 -0
- hoja_server/signup_gate.py +393 -0
- hoja_server/signup_policy.py +97 -0
- hoja_server/signup_store.py +577 -0
- hoja_server/store.py +2179 -0
- hoja_server/sync.py +249 -0
- hoja_server/tenant_context.py +131 -0
- hoja_server/turnstile.py +81 -0
- hoja_server/writer.py +1262 -0
hoja_server/db.py
ADDED
|
@@ -0,0 +1,1816 @@
|
|
|
1
|
+
"""SQLAlchemy Core storage, engine configuration, migration, and seeding."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import hashlib
|
|
6
|
+
import os
|
|
7
|
+
import secrets
|
|
8
|
+
import time
|
|
9
|
+
import uuid
|
|
10
|
+
from datetime import datetime, timezone
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
from sqlalchemy import (
|
|
14
|
+
BigInteger,
|
|
15
|
+
Boolean,
|
|
16
|
+
Column,
|
|
17
|
+
Float,
|
|
18
|
+
ForeignKey,
|
|
19
|
+
Integer,
|
|
20
|
+
MetaData,
|
|
21
|
+
String,
|
|
22
|
+
Table,
|
|
23
|
+
Text,
|
|
24
|
+
UniqueConstraint,
|
|
25
|
+
create_engine,
|
|
26
|
+
event,
|
|
27
|
+
func,
|
|
28
|
+
inspect,
|
|
29
|
+
select,
|
|
30
|
+
)
|
|
31
|
+
from sqlalchemy.engine import Engine, make_url
|
|
32
|
+
from sqlalchemy.exc import IntegrityError
|
|
33
|
+
|
|
34
|
+
metadata = MetaData()
|
|
35
|
+
CURRENT_SCHEMA_REVISION = 37
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def now_iso() -> str:
|
|
39
|
+
return datetime.now(timezone.utc).isoformat()
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def new_id() -> str:
|
|
43
|
+
return str(uuid.uuid4())
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def token_hash(token: str) -> str:
|
|
47
|
+
return hashlib.sha256(token.encode()).hexdigest()
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def gen_token() -> str:
|
|
51
|
+
return "lin_api_" + secrets.token_urlsafe(30)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def gen_session_token() -> str:
|
|
55
|
+
return secrets.token_urlsafe(48)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
# --------------------------------------------------------------------------
|
|
59
|
+
# Tables
|
|
60
|
+
# --------------------------------------------------------------------------
|
|
61
|
+
organization = Table(
|
|
62
|
+
"organization",
|
|
63
|
+
metadata,
|
|
64
|
+
Column("id", String, primary_key=True),
|
|
65
|
+
Column("name", String, nullable=False),
|
|
66
|
+
Column("url_key", String, nullable=False, unique=True),
|
|
67
|
+
Column("created_at", String),
|
|
68
|
+
Column("updated_at", String),
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
global_identity = Table(
|
|
72
|
+
"global_identity",
|
|
73
|
+
metadata,
|
|
74
|
+
Column("id", String, primary_key=True),
|
|
75
|
+
Column("email", String, nullable=False, index=True),
|
|
76
|
+
Column("created_at", String, nullable=False),
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
organization_membership = Table(
|
|
80
|
+
"organization_membership",
|
|
81
|
+
metadata,
|
|
82
|
+
Column("id", String, primary_key=True),
|
|
83
|
+
Column("identity_id", String, ForeignKey("global_identity.id"), nullable=False, index=True),
|
|
84
|
+
Column("organization_id", String, ForeignKey("organization.id"), nullable=False, index=True),
|
|
85
|
+
Column("profile_user_id", String, ForeignKey("user.id"), nullable=False, unique=True),
|
|
86
|
+
Column("active", Boolean, nullable=False, default=True),
|
|
87
|
+
Column("created_at", String, nullable=False),
|
|
88
|
+
UniqueConstraint("identity_id", "organization_id", name="uq_membership_identity_org"),
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
identity_credential = Table(
|
|
92
|
+
"identity_credential",
|
|
93
|
+
metadata,
|
|
94
|
+
Column("identity_id", String, ForeignKey("global_identity.id"), primary_key=True),
|
|
95
|
+
Column("password_hash", String, nullable=False),
|
|
96
|
+
Column("created_at", String, nullable=False),
|
|
97
|
+
Column("updated_at", String, nullable=False),
|
|
98
|
+
)
|
|
99
|
+
|
|
100
|
+
membership_invitation = Table(
|
|
101
|
+
"membership_invitation",
|
|
102
|
+
metadata,
|
|
103
|
+
Column("id", String, primary_key=True),
|
|
104
|
+
Column("token_hash", String, nullable=False, unique=True, index=True),
|
|
105
|
+
Column("organization_id", String, ForeignKey("organization.id"), nullable=False, index=True),
|
|
106
|
+
Column("profile_user_id", String, ForeignKey("user.id"), nullable=False, unique=True),
|
|
107
|
+
Column("created_by_profile_id", String, ForeignKey("user.id"), nullable=False),
|
|
108
|
+
Column("created_at", String, nullable=False),
|
|
109
|
+
Column("expires_at", String, nullable=False),
|
|
110
|
+
Column("accepted_at", String),
|
|
111
|
+
Column("accepted_identity_id", String, ForeignKey("global_identity.id")),
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
api_key = Table(
|
|
115
|
+
"api_key",
|
|
116
|
+
metadata,
|
|
117
|
+
Column("id", String, primary_key=True),
|
|
118
|
+
Column("token_hash", String, nullable=False, index=True),
|
|
119
|
+
Column("label", String),
|
|
120
|
+
Column("access", String, nullable=False, server_default="read_write"),
|
|
121
|
+
Column("hint", String),
|
|
122
|
+
Column("expires_at", String),
|
|
123
|
+
Column("last_used_at", String),
|
|
124
|
+
Column("user_id", String, ForeignKey("user.id")),
|
|
125
|
+
Column("organization_id", String, ForeignKey("organization.id")),
|
|
126
|
+
Column("created_at", String),
|
|
127
|
+
Column("revoked_at", String),
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
password_credential = Table(
|
|
131
|
+
"password_credential",
|
|
132
|
+
metadata,
|
|
133
|
+
Column("user_id", String, ForeignKey("user.id"), primary_key=True),
|
|
134
|
+
Column("password_hash", String, nullable=False),
|
|
135
|
+
Column("created_at", String, nullable=False),
|
|
136
|
+
Column("updated_at", String, nullable=False),
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
browser_session = Table(
|
|
140
|
+
"browser_session",
|
|
141
|
+
metadata,
|
|
142
|
+
Column("id", String, primary_key=True),
|
|
143
|
+
Column("token_hash", String, nullable=False, unique=True, index=True),
|
|
144
|
+
Column("user_id", String, ForeignKey("user.id"), nullable=False, index=True),
|
|
145
|
+
Column("organization_id", String, ForeignKey("organization.id"), nullable=False, index=True),
|
|
146
|
+
Column("created_at", String, nullable=False),
|
|
147
|
+
Column("expires_at", String, nullable=False, index=True),
|
|
148
|
+
Column("revoked_at", String),
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
# --------------------------------------------------------------------------
|
|
152
|
+
# Hosted sign-up: Google-only identity, the pre-tenant session, and the gate
|
|
153
|
+
# rows the provisioner picks up. None of these tables is a workspace: an
|
|
154
|
+
# eligibility row is a *request*, not a database, container, route or DNS name.
|
|
155
|
+
# --------------------------------------------------------------------------
|
|
156
|
+
|
|
157
|
+
signup_identity = Table(
|
|
158
|
+
"signup_identity",
|
|
159
|
+
metadata,
|
|
160
|
+
Column("id", String, primary_key=True),
|
|
161
|
+
# `subject` is the Google `sub` claim: the stable identity key. `email` is
|
|
162
|
+
# display/policy only, because a Workspace address can be reassigned.
|
|
163
|
+
Column("provider", String, nullable=False, server_default="google"),
|
|
164
|
+
Column("subject", String, nullable=False),
|
|
165
|
+
Column("email", String, nullable=False),
|
|
166
|
+
Column("email_verified", Boolean, nullable=False, server_default="0"),
|
|
167
|
+
Column("name", String),
|
|
168
|
+
Column("picture", Text),
|
|
169
|
+
Column("hosted_domain", String), # the `hd` claim, when Google returns one
|
|
170
|
+
Column("google_verified_at", String, nullable=False),
|
|
171
|
+
Column("created_at", String, nullable=False),
|
|
172
|
+
Column("updated_at", String, nullable=False),
|
|
173
|
+
UniqueConstraint("provider", "subject", name="uq_signup_identity_provider_subject"),
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
signup_flow = Table(
|
|
177
|
+
"signup_flow",
|
|
178
|
+
metadata,
|
|
179
|
+
# One row per in-flight authorization-code flow. The browser holds only an
|
|
180
|
+
# opaque binding id in a short-lived HttpOnly cookie; the state hash, nonce
|
|
181
|
+
# and PKCE verifier stay server-side and are single use (consumed_at).
|
|
182
|
+
Column("id", String, primary_key=True),
|
|
183
|
+
Column("state_hash", String, nullable=False, unique=True, index=True),
|
|
184
|
+
Column("binding_hash", String, nullable=False, index=True),
|
|
185
|
+
Column("nonce", String, nullable=False),
|
|
186
|
+
Column("code_verifier", String, nullable=False),
|
|
187
|
+
Column("redirect_uri", Text, nullable=False),
|
|
188
|
+
Column("return_to", Text),
|
|
189
|
+
Column("created_at", String, nullable=False),
|
|
190
|
+
Column("expires_at", String, nullable=False, index=True),
|
|
191
|
+
Column("consumed_at", String),
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
signup_session = Table(
|
|
195
|
+
"signup_session",
|
|
196
|
+
metadata,
|
|
197
|
+
# Opaque server-side session for a google_verified person who has no
|
|
198
|
+
# workspace yet, so browser_session (user_id/organization_id NOT NULL) cannot
|
|
199
|
+
# hold it. Same machinery: a random token, only its hash stored, rotation on
|
|
200
|
+
# login, revocation by row update.
|
|
201
|
+
Column("id", String, primary_key=True),
|
|
202
|
+
Column("token_hash", String, nullable=False, unique=True, index=True),
|
|
203
|
+
Column("identity_id", String, ForeignKey("signup_identity.id"), nullable=False, index=True),
|
|
204
|
+
Column("created_at", String, nullable=False),
|
|
205
|
+
Column("expires_at", String, nullable=False, index=True),
|
|
206
|
+
Column("revoked_at", String),
|
|
207
|
+
Column("rotated_from", String),
|
|
208
|
+
Column("client_ip", String),
|
|
209
|
+
Column("user_agent", Text),
|
|
210
|
+
)
|
|
211
|
+
|
|
212
|
+
signup_invite = Table(
|
|
213
|
+
"signup_invite",
|
|
214
|
+
metadata,
|
|
215
|
+
Column("id", String, primary_key=True),
|
|
216
|
+
Column("email", String, nullable=False, unique=True, index=True),
|
|
217
|
+
Column("note", Text),
|
|
218
|
+
Column("created_by", String),
|
|
219
|
+
Column("created_at", String, nullable=False),
|
|
220
|
+
Column("expires_at", String),
|
|
221
|
+
Column("consumed_at", String),
|
|
222
|
+
Column("consumed_by_identity_id", String, ForeignKey("signup_identity.id")),
|
|
223
|
+
)
|
|
224
|
+
|
|
225
|
+
signup_eligibility = Table(
|
|
226
|
+
"signup_eligibility",
|
|
227
|
+
metadata,
|
|
228
|
+
# The outbox the provisioner reads. status='eligible' + claimed_at IS NULL
|
|
229
|
+
# is the queue; (subject, slug) is unique so a retried signup cannot enqueue
|
|
230
|
+
# two workspaces, and idempotency_key makes a repeated POST return the same row.
|
|
231
|
+
Column("id", String, primary_key=True),
|
|
232
|
+
Column("idempotency_key", String, nullable=False, unique=True, index=True),
|
|
233
|
+
Column("identity_id", String, ForeignKey("signup_identity.id"), nullable=False, index=True),
|
|
234
|
+
Column("google_sub", String, nullable=False, index=True),
|
|
235
|
+
Column("email", String, nullable=False),
|
|
236
|
+
Column("slug", String, nullable=False, unique=True, index=True),
|
|
237
|
+
Column("status", String, nullable=False, server_default="eligible"),
|
|
238
|
+
Column("client_ip", String, nullable=False, index=True),
|
|
239
|
+
Column("turnstile_hostname", String),
|
|
240
|
+
Column("created_at", String, nullable=False),
|
|
241
|
+
Column("claimed_at", String),
|
|
242
|
+
# Provisioning lease/audit fields. ``attempts`` is incremented when a worker
|
|
243
|
+
# claims the row; ``provisioned_at`` is written only after the tenant is
|
|
244
|
+
# healthy through the edge. ``last_error`` holds a stable code, never a
|
|
245
|
+
# secret or a raw subprocess error.
|
|
246
|
+
Column("attempts", Integer, nullable=False, server_default="0", default=0),
|
|
247
|
+
Column("provisioned_at", String),
|
|
248
|
+
Column("last_error", String),
|
|
249
|
+
UniqueConstraint("google_sub", "slug", name="uq_signup_eligibility_subject_slug"),
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
turnstile_use = Table(
|
|
253
|
+
"turnstile_use",
|
|
254
|
+
metadata,
|
|
255
|
+
# A Turnstile token is single use: the primary key IS the token hash, so a
|
|
256
|
+
# second attempt to spend it fails on insert instead of passing the gate.
|
|
257
|
+
Column("token_hash", String, primary_key=True),
|
|
258
|
+
Column("identity_id", String, ForeignKey("signup_identity.id"), index=True),
|
|
259
|
+
Column("client_ip", String),
|
|
260
|
+
Column("hostname", String),
|
|
261
|
+
Column("consumed_at", String, nullable=False),
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
oauth_client = Table(
|
|
265
|
+
"oauth_client",
|
|
266
|
+
metadata,
|
|
267
|
+
Column("client_id", String, primary_key=True),
|
|
268
|
+
Column("client_json", Text, nullable=False),
|
|
269
|
+
Column("created_at", String, nullable=False),
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
oauth_authorization = Table(
|
|
273
|
+
"oauth_authorization",
|
|
274
|
+
metadata,
|
|
275
|
+
Column("code_hash", String, primary_key=True),
|
|
276
|
+
Column("client_id", String, ForeignKey("oauth_client.client_id"), nullable=False, index=True),
|
|
277
|
+
Column("user_id", String, ForeignKey("user.id"), nullable=False, index=True),
|
|
278
|
+
Column("organization_id", String, ForeignKey("organization.id"), nullable=False, index=True),
|
|
279
|
+
Column("scopes", Text, nullable=False),
|
|
280
|
+
Column("code_challenge", String, nullable=False),
|
|
281
|
+
Column("redirect_uri", Text, nullable=False),
|
|
282
|
+
Column("expires_at", String, nullable=False, index=True),
|
|
283
|
+
Column("consumed_at", String),
|
|
284
|
+
Column("resource", Text),
|
|
285
|
+
)
|
|
286
|
+
|
|
287
|
+
oauth_pending = Table(
|
|
288
|
+
"oauth_pending", metadata,
|
|
289
|
+
Column("pending_hash", String, primary_key=True),
|
|
290
|
+
Column("client_id", String, ForeignKey("oauth_client.client_id"), nullable=False, index=True),
|
|
291
|
+
Column("user_id", String, ForeignKey("user.id"), index=True),
|
|
292
|
+
Column("organization_id", String, ForeignKey("organization.id"), index=True),
|
|
293
|
+
Column("scopes", Text, nullable=False), Column("code_challenge", String, nullable=False),
|
|
294
|
+
Column("redirect_uri", Text, nullable=False), Column("redirect_explicit", Boolean, nullable=False),
|
|
295
|
+
Column("state", Text), Column("csrf_token", String, nullable=False), Column("resource", Text),
|
|
296
|
+
Column("expires_at", String, nullable=False, index=True), Column("consumed_at", String),
|
|
297
|
+
)
|
|
298
|
+
|
|
299
|
+
oauth_access_token = Table(
|
|
300
|
+
"oauth_access_token",
|
|
301
|
+
metadata,
|
|
302
|
+
Column("token_hash", String, primary_key=True),
|
|
303
|
+
Column("client_id", String, ForeignKey("oauth_client.client_id"), nullable=False, index=True),
|
|
304
|
+
Column("user_id", String, ForeignKey("user.id"), nullable=False, index=True),
|
|
305
|
+
Column("organization_id", String, ForeignKey("organization.id"), nullable=False, index=True),
|
|
306
|
+
Column("scopes", Text, nullable=False),
|
|
307
|
+
Column("expires_at", String, nullable=False, index=True),
|
|
308
|
+
Column("grant_id", String, nullable=False, index=True),
|
|
309
|
+
Column("resource", Text),
|
|
310
|
+
Column("revoked_at", String),
|
|
311
|
+
)
|
|
312
|
+
|
|
313
|
+
oauth_refresh_token = Table(
|
|
314
|
+
"oauth_refresh_token",
|
|
315
|
+
metadata,
|
|
316
|
+
Column("token_hash", String, primary_key=True),
|
|
317
|
+
Column("client_id", String, ForeignKey("oauth_client.client_id"), nullable=False, index=True),
|
|
318
|
+
Column("user_id", String, ForeignKey("user.id"), nullable=False, index=True),
|
|
319
|
+
Column("organization_id", String, ForeignKey("organization.id"), nullable=False, index=True),
|
|
320
|
+
Column("scopes", Text, nullable=False),
|
|
321
|
+
Column("expires_at", String, nullable=False, index=True),
|
|
322
|
+
Column("grant_id", String, nullable=False, index=True),
|
|
323
|
+
Column("resource", Text),
|
|
324
|
+
Column("revoked_at", String),
|
|
325
|
+
)
|
|
326
|
+
|
|
327
|
+
user_preference = Table(
|
|
328
|
+
"user_preference",
|
|
329
|
+
metadata,
|
|
330
|
+
Column("user_id", String, ForeignKey("user.id"), primary_key=True),
|
|
331
|
+
Column("preference_key", String, primary_key=True),
|
|
332
|
+
Column("value", Text, nullable=False),
|
|
333
|
+
Column("updated_at", String, nullable=False),
|
|
334
|
+
)
|
|
335
|
+
|
|
336
|
+
connector_account = Table(
|
|
337
|
+
"connector_account",
|
|
338
|
+
metadata,
|
|
339
|
+
Column("id", String, primary_key=True),
|
|
340
|
+
Column("organization_id", String, nullable=False, index=True),
|
|
341
|
+
Column("owner_user_id", String, index=True),
|
|
342
|
+
Column("provider", String, nullable=False),
|
|
343
|
+
Column("external_account", String, nullable=False),
|
|
344
|
+
Column("scopes", Text, nullable=False),
|
|
345
|
+
Column("encrypted_refresh_token", Text, nullable=False),
|
|
346
|
+
Column("status", String, nullable=False),
|
|
347
|
+
Column("created_at", String, nullable=False),
|
|
348
|
+
Column("updated_at", String, nullable=False),
|
|
349
|
+
Column("revoked_at", String),
|
|
350
|
+
)
|
|
351
|
+
|
|
352
|
+
connector_action = Table(
|
|
353
|
+
"connector_action",
|
|
354
|
+
metadata,
|
|
355
|
+
Column("id", String, primary_key=True),
|
|
356
|
+
Column("organization_id", String, nullable=False, index=True),
|
|
357
|
+
Column("account_id", String, ForeignKey("connector_account.id"), nullable=False, index=True),
|
|
358
|
+
Column("requested_by_user_id", String, nullable=False),
|
|
359
|
+
Column("tool", String, nullable=False),
|
|
360
|
+
Column("args_json", Text, nullable=False),
|
|
361
|
+
Column("status", String, nullable=False),
|
|
362
|
+
Column("result_summary", Text),
|
|
363
|
+
Column("created_at", String, nullable=False),
|
|
364
|
+
Column("resolved_at", String),
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
model_provider = Table(
|
|
368
|
+
"model_provider",
|
|
369
|
+
metadata,
|
|
370
|
+
Column("id", String, primary_key=True),
|
|
371
|
+
Column("organization_id", String, nullable=False, index=True),
|
|
372
|
+
Column("name", String, nullable=False),
|
|
373
|
+
Column("provider", String, nullable=False),
|
|
374
|
+
Column("model_id", String, nullable=False),
|
|
375
|
+
Column("base_url", String),
|
|
376
|
+
Column("encrypted_api_key", String, nullable=False),
|
|
377
|
+
Column("enabled", Boolean, nullable=False, default=True),
|
|
378
|
+
Column("created_at", String, nullable=False),
|
|
379
|
+
Column("updated_at", String, nullable=False),
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
agent_conversation = Table(
|
|
383
|
+
"agent_conversation",
|
|
384
|
+
metadata,
|
|
385
|
+
Column("id", String, primary_key=True),
|
|
386
|
+
Column("organization_id", String, nullable=False, index=True),
|
|
387
|
+
Column("user_id", String, nullable=False, index=True),
|
|
388
|
+
Column("title", String),
|
|
389
|
+
Column("created_at", String, nullable=False),
|
|
390
|
+
Column("updated_at", String, nullable=False),
|
|
391
|
+
)
|
|
392
|
+
|
|
393
|
+
agent_message = Table(
|
|
394
|
+
"agent_message",
|
|
395
|
+
metadata,
|
|
396
|
+
Column("id", String, primary_key=True),
|
|
397
|
+
Column("conversation_id", String, ForeignKey("agent_conversation.id"), nullable=False, index=True),
|
|
398
|
+
Column("role", String, nullable=False),
|
|
399
|
+
Column("content", Text, nullable=False),
|
|
400
|
+
Column("created_at", String, nullable=False),
|
|
401
|
+
)
|
|
402
|
+
|
|
403
|
+
schema_revision = Table(
|
|
404
|
+
"schema_revision",
|
|
405
|
+
metadata,
|
|
406
|
+
Column("revision", Integer, primary_key=True),
|
|
407
|
+
Column("applied_at", String, nullable=False),
|
|
408
|
+
)
|
|
409
|
+
|
|
410
|
+
rate_limit_event = Table(
|
|
411
|
+
"rate_limit_event",
|
|
412
|
+
metadata,
|
|
413
|
+
Column("id", Integer, primary_key=True, autoincrement=True),
|
|
414
|
+
Column("bucket", String, nullable=False, index=True),
|
|
415
|
+
Column("key_hash", String, nullable=False, index=True),
|
|
416
|
+
Column("created_at", Float, nullable=False, index=True),
|
|
417
|
+
)
|
|
418
|
+
|
|
419
|
+
oauth_state_nonce = Table(
|
|
420
|
+
"oauth_state_nonce",
|
|
421
|
+
metadata,
|
|
422
|
+
Column("nonce_hash", String, primary_key=True),
|
|
423
|
+
Column("expires_at", Float, nullable=False, index=True),
|
|
424
|
+
)
|
|
425
|
+
|
|
426
|
+
user = Table(
|
|
427
|
+
"user",
|
|
428
|
+
metadata,
|
|
429
|
+
Column("id", String, primary_key=True),
|
|
430
|
+
Column("organization_id", String, ForeignKey("organization.id"), index=True),
|
|
431
|
+
Column("name", String),
|
|
432
|
+
Column("display_name", String),
|
|
433
|
+
Column("email", String),
|
|
434
|
+
Column("active", Boolean, default=True),
|
|
435
|
+
Column("admin", Boolean, default=False),
|
|
436
|
+
Column("avatar_url", String),
|
|
437
|
+
Column("url", String),
|
|
438
|
+
Column("timezone", String),
|
|
439
|
+
Column("status_emoji", String),
|
|
440
|
+
Column("status_label", String),
|
|
441
|
+
Column("created_at", String),
|
|
442
|
+
Column("updated_at", String),
|
|
443
|
+
Column("archived_at", String),
|
|
444
|
+
)
|
|
445
|
+
|
|
446
|
+
team = Table(
|
|
447
|
+
"team",
|
|
448
|
+
metadata,
|
|
449
|
+
Column("id", String, primary_key=True),
|
|
450
|
+
Column("organization_id", String, ForeignKey("organization.id"), index=True),
|
|
451
|
+
Column("key", String, nullable=False),
|
|
452
|
+
Column("name", String, nullable=False),
|
|
453
|
+
Column("description", String),
|
|
454
|
+
Column("color", String),
|
|
455
|
+
Column("icon", String),
|
|
456
|
+
# Departments are teams with children. Not an access boundary: visibility is.
|
|
457
|
+
Column("parent_team_id", String, ForeignKey("team.id"), index=True),
|
|
458
|
+
Column("visibility", String, nullable=False, default="public"),
|
|
459
|
+
Column("timezone", String),
|
|
460
|
+
Column("cadence_enabled", Boolean, nullable=False, default=False),
|
|
461
|
+
Column("cadence_weekday", Integer),
|
|
462
|
+
Column("cadence_anchor_at", String),
|
|
463
|
+
Column("cadence_policy_version", Integer),
|
|
464
|
+
Column("cadence_duration_weeks", Integer, nullable=False, default=1),
|
|
465
|
+
Column("cadence_cooldown_weeks", Integer, nullable=False, default=0),
|
|
466
|
+
Column("cadence_upcoming_count", Integer, nullable=False, default=2),
|
|
467
|
+
Column("issue_counter", Integer, default=0),
|
|
468
|
+
Column("active_cycle_id", String),
|
|
469
|
+
Column("auto_add_started", Boolean, nullable=False, default=True),
|
|
470
|
+
Column("auto_add_completed", Boolean, nullable=False, default=True),
|
|
471
|
+
Column("auto_add_backlog", Boolean, nullable=False, default=True),
|
|
472
|
+
Column("default_state_id", String),
|
|
473
|
+
Column("created_at", String),
|
|
474
|
+
Column("updated_at", String),
|
|
475
|
+
Column("archived_at", String),
|
|
476
|
+
UniqueConstraint("organization_id", "key", name="uq_team_org_key"),
|
|
477
|
+
)
|
|
478
|
+
|
|
479
|
+
workflow_state = Table(
|
|
480
|
+
"workflow_state",
|
|
481
|
+
metadata,
|
|
482
|
+
Column("id", String, primary_key=True),
|
|
483
|
+
Column("organization_id", String, ForeignKey("organization.id"), index=True),
|
|
484
|
+
Column("team_id", String, ForeignKey("team.id"), index=True),
|
|
485
|
+
Column("name", String, nullable=False),
|
|
486
|
+
Column("type", String, nullable=False),
|
|
487
|
+
Column("color", String),
|
|
488
|
+
Column("description", String),
|
|
489
|
+
Column("position", Float, default=0),
|
|
490
|
+
Column("created_at", String),
|
|
491
|
+
Column("updated_at", String),
|
|
492
|
+
Column("archived_at", String),
|
|
493
|
+
)
|
|
494
|
+
|
|
495
|
+
team_member = Table(
|
|
496
|
+
"team_member",
|
|
497
|
+
metadata,
|
|
498
|
+
Column("team_id", String, ForeignKey("team.id"), primary_key=True),
|
|
499
|
+
Column("user_id", String, ForeignKey("user.id"), primary_key=True),
|
|
500
|
+
Column("role", String, nullable=False, default="member"),
|
|
501
|
+
)
|
|
502
|
+
|
|
503
|
+
issue = Table(
|
|
504
|
+
"issue",
|
|
505
|
+
metadata,
|
|
506
|
+
Column("id", String, primary_key=True),
|
|
507
|
+
Column("organization_id", String, ForeignKey("organization.id"), index=True),
|
|
508
|
+
Column("team_id", String, ForeignKey("team.id"), index=True),
|
|
509
|
+
Column("number", Integer),
|
|
510
|
+
Column("identifier", String, index=True),
|
|
511
|
+
Column("title", String, nullable=False),
|
|
512
|
+
Column("description", Text),
|
|
513
|
+
Column("priority", Integer, default=0),
|
|
514
|
+
Column("priority_label", String),
|
|
515
|
+
Column("estimate", Float),
|
|
516
|
+
Column("state_id", String, ForeignKey("workflow_state.id"), index=True),
|
|
517
|
+
Column("assignee_id", String, ForeignKey("user.id"), index=True),
|
|
518
|
+
Column("creator_id", String, ForeignKey("user.id")),
|
|
519
|
+
Column("project_id", String, index=True),
|
|
520
|
+
Column("cycle_id", String),
|
|
521
|
+
Column("parent_id", String),
|
|
522
|
+
Column("url", String),
|
|
523
|
+
Column("branch_name", String),
|
|
524
|
+
Column("due_date", String),
|
|
525
|
+
Column("started_at", String),
|
|
526
|
+
Column("state_changed_at", String),
|
|
527
|
+
Column("completed_at", String),
|
|
528
|
+
Column("canceled_at", String),
|
|
529
|
+
Column("snoozed_until_at", String),
|
|
530
|
+
Column("created_at", String, index=True),
|
|
531
|
+
Column("updated_at", String, index=True),
|
|
532
|
+
Column("archived_at", String),
|
|
533
|
+
UniqueConstraint("team_id", "number", name="uq_issue_team_number"),
|
|
534
|
+
)
|
|
535
|
+
|
|
536
|
+
issue_label = Table(
|
|
537
|
+
"issue_label",
|
|
538
|
+
metadata,
|
|
539
|
+
Column("id", String, primary_key=True),
|
|
540
|
+
Column("organization_id", String, ForeignKey("organization.id"), index=True),
|
|
541
|
+
Column("team_id", String, ForeignKey("team.id")),
|
|
542
|
+
Column("name", String, nullable=False),
|
|
543
|
+
Column("color", String),
|
|
544
|
+
Column("description", String),
|
|
545
|
+
Column("created_at", String),
|
|
546
|
+
Column("updated_at", String),
|
|
547
|
+
Column("archived_at", String),
|
|
548
|
+
)
|
|
549
|
+
|
|
550
|
+
issue_label_link = Table(
|
|
551
|
+
"issue_label_link",
|
|
552
|
+
metadata,
|
|
553
|
+
Column("issue_id", String, ForeignKey("issue.id"), primary_key=True),
|
|
554
|
+
Column("label_id", String, ForeignKey("issue_label.id"), primary_key=True),
|
|
555
|
+
)
|
|
556
|
+
|
|
557
|
+
issue_subscriber = Table(
|
|
558
|
+
"issue_subscriber",
|
|
559
|
+
metadata,
|
|
560
|
+
Column("issue_id", String, ForeignKey("issue.id"), primary_key=True),
|
|
561
|
+
Column("user_id", String, ForeignKey("user.id"), primary_key=True),
|
|
562
|
+
)
|
|
563
|
+
|
|
564
|
+
comment = Table(
|
|
565
|
+
"comment",
|
|
566
|
+
metadata,
|
|
567
|
+
Column("id", String, primary_key=True),
|
|
568
|
+
Column("organization_id", String, ForeignKey("organization.id"), index=True),
|
|
569
|
+
Column("issue_id", String, ForeignKey("issue.id"), index=True),
|
|
570
|
+
Column("body", Text, nullable=False),
|
|
571
|
+
Column("user_id", String, ForeignKey("user.id")),
|
|
572
|
+
Column("url", String),
|
|
573
|
+
Column("created_at", String),
|
|
574
|
+
Column("updated_at", String),
|
|
575
|
+
Column("archived_at", String),
|
|
576
|
+
)
|
|
577
|
+
|
|
578
|
+
change_event = Table(
|
|
579
|
+
"change_event",
|
|
580
|
+
metadata,
|
|
581
|
+
Column("seq", Integer, primary_key=True, autoincrement=True),
|
|
582
|
+
Column("organization_id", String, ForeignKey("organization.id"), nullable=False, index=True),
|
|
583
|
+
Column("entity", String, nullable=False),
|
|
584
|
+
Column("entity_id", String, nullable=False),
|
|
585
|
+
Column("action", String, nullable=False),
|
|
586
|
+
Column("team_id", String),
|
|
587
|
+
Column("created_at", String, nullable=False),
|
|
588
|
+
# Set for per-user events (notifications): only this user is told about them.
|
|
589
|
+
Column("recipient_id", String),
|
|
590
|
+
)
|
|
591
|
+
|
|
592
|
+
issue_activity = Table(
|
|
593
|
+
"issue_activity",
|
|
594
|
+
metadata,
|
|
595
|
+
Column("id", String, primary_key=True),
|
|
596
|
+
Column("organization_id", String, ForeignKey("organization.id"), nullable=False, index=True),
|
|
597
|
+
Column("issue_id", String, ForeignKey("issue.id"), nullable=False, index=True),
|
|
598
|
+
Column("actor_id", String, ForeignKey("user.id")),
|
|
599
|
+
Column("event_type", String, nullable=False),
|
|
600
|
+
Column("changes", Text),
|
|
601
|
+
Column("created_at", String, nullable=False, index=True),
|
|
602
|
+
)
|
|
603
|
+
|
|
604
|
+
initiative = Table(
|
|
605
|
+
"initiative",
|
|
606
|
+
metadata,
|
|
607
|
+
Column("id", String, primary_key=True),
|
|
608
|
+
Column("organization_id", String, ForeignKey("organization.id"), nullable=False, index=True),
|
|
609
|
+
Column("name", String, nullable=False),
|
|
610
|
+
Column("summary", Text),
|
|
611
|
+
Column("status", String, nullable=False),
|
|
612
|
+
Column("priority", Integer, nullable=False, default=0),
|
|
613
|
+
Column("owner_id", String, ForeignKey("user.id")),
|
|
614
|
+
Column("creator_id", String, ForeignKey("user.id")),
|
|
615
|
+
Column("target_date", String),
|
|
616
|
+
Column("created_at", String, nullable=False),
|
|
617
|
+
Column("updated_at", String, nullable=False),
|
|
618
|
+
Column("archived_at", String),
|
|
619
|
+
)
|
|
620
|
+
|
|
621
|
+
initiative_project = Table(
|
|
622
|
+
"initiative_project",
|
|
623
|
+
metadata,
|
|
624
|
+
Column("organization_id", String, ForeignKey("organization.id"), primary_key=True),
|
|
625
|
+
Column("initiative_id", String, ForeignKey("initiative.id"), primary_key=True),
|
|
626
|
+
Column("project_id", String, ForeignKey("project.id"), primary_key=True),
|
|
627
|
+
)
|
|
628
|
+
|
|
629
|
+
inbox_notification = Table(
|
|
630
|
+
"inbox_notification",
|
|
631
|
+
metadata,
|
|
632
|
+
Column("id", String, primary_key=True),
|
|
633
|
+
Column("organization_id", String, ForeignKey("organization.id"), nullable=False, index=True),
|
|
634
|
+
Column("recipient_id", String, ForeignKey("user.id"), nullable=False, index=True),
|
|
635
|
+
Column("issue_id", String, ForeignKey("issue.id"), nullable=False, index=True),
|
|
636
|
+
Column("actor_id", String, ForeignKey("user.id")),
|
|
637
|
+
Column("kind", String, nullable=False),
|
|
638
|
+
Column("created_at", String, nullable=False, index=True),
|
|
639
|
+
Column("read_at", String),
|
|
640
|
+
Column("archived_at", String),
|
|
641
|
+
)
|
|
642
|
+
|
|
643
|
+
attachment = Table(
|
|
644
|
+
"attachment",
|
|
645
|
+
metadata,
|
|
646
|
+
Column("id", String, primary_key=True),
|
|
647
|
+
Column("organization_id", String, ForeignKey("organization.id"), index=True),
|
|
648
|
+
Column("issue_id", String, ForeignKey("issue.id"), index=True),
|
|
649
|
+
Column("creator_id", String, ForeignKey("user.id")),
|
|
650
|
+
Column("url", String, nullable=False),
|
|
651
|
+
Column("title", String, nullable=False),
|
|
652
|
+
Column("subtitle", String),
|
|
653
|
+
Column("created_at", String),
|
|
654
|
+
)
|
|
655
|
+
|
|
656
|
+
project = Table(
|
|
657
|
+
"project",
|
|
658
|
+
metadata,
|
|
659
|
+
Column("id", String, primary_key=True),
|
|
660
|
+
Column("organization_id", String, ForeignKey("organization.id"), index=True),
|
|
661
|
+
Column("name", String, nullable=False),
|
|
662
|
+
Column("description", String),
|
|
663
|
+
Column("slug_id", String),
|
|
664
|
+
Column("icon", String),
|
|
665
|
+
Column("color", String),
|
|
666
|
+
Column("state", String, default="backlog"),
|
|
667
|
+
Column("progress", Float, default=0),
|
|
668
|
+
Column("lead_id", String, ForeignKey("user.id")),
|
|
669
|
+
Column("creator_id", String, ForeignKey("user.id")),
|
|
670
|
+
Column("start_date", String),
|
|
671
|
+
Column("target_date", String),
|
|
672
|
+
Column("url", String),
|
|
673
|
+
Column("created_at", String),
|
|
674
|
+
Column("updated_at", String),
|
|
675
|
+
Column("archived_at", String),
|
|
676
|
+
)
|
|
677
|
+
|
|
678
|
+
project_update = Table(
|
|
679
|
+
"project_update",
|
|
680
|
+
metadata,
|
|
681
|
+
Column("id", String, primary_key=True),
|
|
682
|
+
Column("organization_id", String, ForeignKey("organization.id"), nullable=False, index=True),
|
|
683
|
+
Column("project_id", String, ForeignKey("project.id"), nullable=False, index=True),
|
|
684
|
+
Column("status", String),
|
|
685
|
+
Column("body", Text, nullable=False),
|
|
686
|
+
Column("created_at", String, nullable=False, index=True),
|
|
687
|
+
Column("actor_id", String, ForeignKey("user.id")),
|
|
688
|
+
Column("progress", Float),
|
|
689
|
+
Column("health", String),
|
|
690
|
+
)
|
|
691
|
+
|
|
692
|
+
project_member = Table(
|
|
693
|
+
"project_member",
|
|
694
|
+
metadata,
|
|
695
|
+
Column("project_id", String, ForeignKey("project.id"), primary_key=True),
|
|
696
|
+
Column("user_id", String, ForeignKey("user.id"), primary_key=True),
|
|
697
|
+
)
|
|
698
|
+
|
|
699
|
+
project_team = Table(
|
|
700
|
+
"project_team",
|
|
701
|
+
metadata,
|
|
702
|
+
Column("project_id", String, ForeignKey("project.id"), primary_key=True),
|
|
703
|
+
Column("team_id", String, ForeignKey("team.id"), primary_key=True),
|
|
704
|
+
)
|
|
705
|
+
|
|
706
|
+
cycle = Table(
|
|
707
|
+
"cycle",
|
|
708
|
+
metadata,
|
|
709
|
+
Column("id", String, primary_key=True),
|
|
710
|
+
Column("organization_id", String, ForeignKey("organization.id"), index=True),
|
|
711
|
+
Column("team_id", String, ForeignKey("team.id"), index=True),
|
|
712
|
+
Column("number", Integer),
|
|
713
|
+
Column("name", String),
|
|
714
|
+
Column("description", String),
|
|
715
|
+
Column("starts_at", String),
|
|
716
|
+
Column("ends_at", String),
|
|
717
|
+
Column("cadence_boundary_at", String),
|
|
718
|
+
Column("completed_at", String),
|
|
719
|
+
Column("progress", Float, default=0),
|
|
720
|
+
Column("created_at", String),
|
|
721
|
+
Column("updated_at", String),
|
|
722
|
+
Column("archived_at", String),
|
|
723
|
+
UniqueConstraint("team_id", "number", name="uq_cycle_team_number"),
|
|
724
|
+
UniqueConstraint("team_id", "cadence_boundary_at", name="uq_cycle_team_cadence_boundary"),
|
|
725
|
+
)
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
# --------------------------------------------------------------------------
|
|
729
|
+
# Seed workflow states (name, type, position)
|
|
730
|
+
# --------------------------------------------------------------------------
|
|
731
|
+
SEED_STATES = [
|
|
732
|
+
("Backlog", "backlog", 0, "#bec2c8"),
|
|
733
|
+
("Todo", "unstarted", 1, "#e2e2e2"),
|
|
734
|
+
("In Progress", "started", 2, "#f2c94c"),
|
|
735
|
+
("In Review", "started", 3, "#5e6ad2"),
|
|
736
|
+
("Done", "completed", 4, "#5e6ad2"),
|
|
737
|
+
("Canceled", "canceled", 5, "#95a2b3"),
|
|
738
|
+
]
|
|
739
|
+
|
|
740
|
+
PRIORITY_LABELS = {0: "No priority", 1: "Urgent", 2: "High", 3: "Medium", 4: "Low"}
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
def default_db_dir() -> Path:
|
|
744
|
+
base = os.environ.get("XDG_DATA_HOME") or str(Path.home() / ".local" / "share")
|
|
745
|
+
d = Path(base) / "hoja"
|
|
746
|
+
d.mkdir(parents=True, exist_ok=True)
|
|
747
|
+
return d
|
|
748
|
+
|
|
749
|
+
|
|
750
|
+
def db_path_for(tenant: str) -> Path:
|
|
751
|
+
base = Path(os.environ.get("XDG_DATA_HOME") or str(Path.home() / ".local" / "share"))
|
|
752
|
+
canonical = base / "hoja" / f"{tenant}.db"
|
|
753
|
+
canonical.parent.mkdir(parents=True, exist_ok=True)
|
|
754
|
+
return canonical
|
|
755
|
+
|
|
756
|
+
|
|
757
|
+
def resolve_database_target(
|
|
758
|
+
*,
|
|
759
|
+
database_url: str | None = None,
|
|
760
|
+
db_path: str | Path | None = None,
|
|
761
|
+
tenant: str = "default",
|
|
762
|
+
) -> str:
|
|
763
|
+
"""Resolve explicit URL, environment URL, then the SQLite path fallback."""
|
|
764
|
+
if database_url:
|
|
765
|
+
return database_url
|
|
766
|
+
if env_url := os.environ.get("HOJA_DATABASE_URL"):
|
|
767
|
+
return env_url
|
|
768
|
+
return str(db_path if db_path is not None else db_path_for(tenant))
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
def _as_sqlalchemy_url(target: str | Path) -> str:
|
|
772
|
+
value = os.fspath(target)
|
|
773
|
+
if value.startswith("postgres://"):
|
|
774
|
+
return f"postgresql+psycopg://{value.removeprefix('postgres://')}"
|
|
775
|
+
if value.startswith("postgresql://"):
|
|
776
|
+
return f"postgresql+psycopg://{value.removeprefix('postgresql://')}"
|
|
777
|
+
if "://" in value or value.startswith("sqlite:"):
|
|
778
|
+
return value
|
|
779
|
+
return f"sqlite:///{value}"
|
|
780
|
+
|
|
781
|
+
|
|
782
|
+
def display_database_target(target: str | Path) -> str:
|
|
783
|
+
"""Return a target safe for logs, hiding URL passwords."""
|
|
784
|
+
value = os.fspath(target)
|
|
785
|
+
if "://" not in value and not value.startswith("sqlite:"):
|
|
786
|
+
return value
|
|
787
|
+
return make_url(value).render_as_string(hide_password=True)
|
|
788
|
+
|
|
789
|
+
|
|
790
|
+
def _pool_setting(name: str, default: int, *, minimum: int) -> int:
|
|
791
|
+
"""A bounded integer from the environment, refusing nonsense loudly."""
|
|
792
|
+
raw = (os.environ.get(name) or "").strip()
|
|
793
|
+
if not raw:
|
|
794
|
+
return default
|
|
795
|
+
try:
|
|
796
|
+
value = int(raw)
|
|
797
|
+
except ValueError as exc:
|
|
798
|
+
raise ValueError(f"{name} must be an integer, got {raw!r}.") from exc
|
|
799
|
+
if value < minimum:
|
|
800
|
+
raise ValueError(f"{name} must be at least {minimum}, got {value}.")
|
|
801
|
+
return value
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
def make_engine(target: str | Path) -> Engine:
|
|
805
|
+
url = _as_sqlalchemy_url(target)
|
|
806
|
+
backend = make_url(url).get_backend_name()
|
|
807
|
+
kwargs = {"future": True}
|
|
808
|
+
if backend == "postgresql":
|
|
809
|
+
kwargs["pool_pre_ping"] = True
|
|
810
|
+
# Pool sizing is a deployment decision, not a constant: instances are
|
|
811
|
+
# per tenant and they all point at one server, so N * (size + overflow)
|
|
812
|
+
# must stay under the server's max_connections with room for psql and
|
|
813
|
+
# migrations. The old 40 + 20 let two instances alone exceed 100.
|
|
814
|
+
kwargs["pool_size"] = _pool_setting("HOJA_DB_POOL_SIZE", 5, minimum=1)
|
|
815
|
+
kwargs["max_overflow"] = _pool_setting("HOJA_DB_MAX_OVERFLOW", 5, minimum=0)
|
|
816
|
+
kwargs["pool_timeout"] = _pool_setting("HOJA_DB_POOL_TIMEOUT", 30, minimum=1)
|
|
817
|
+
eng = create_engine(url, **kwargs)
|
|
818
|
+
if backend == "postgresql":
|
|
819
|
+
from hoja_server.tenant_context import install
|
|
820
|
+
|
|
821
|
+
install(eng)
|
|
822
|
+
|
|
823
|
+
if backend == "sqlite":
|
|
824
|
+
|
|
825
|
+
@event.listens_for(eng, "connect")
|
|
826
|
+
def _set_pragma(dbapi_conn, _):
|
|
827
|
+
cur = dbapi_conn.cursor()
|
|
828
|
+
cur.execute("PRAGMA journal_mode=WAL")
|
|
829
|
+
cur.execute("PRAGMA foreign_keys=ON")
|
|
830
|
+
cur.close()
|
|
831
|
+
|
|
832
|
+
return eng
|
|
833
|
+
|
|
834
|
+
|
|
835
|
+
def _apply_token_hash_uniqueness(conn) -> None:
|
|
836
|
+
duplicate = conn.execute(
|
|
837
|
+
select(api_key.c.token_hash, func.count())
|
|
838
|
+
.group_by(api_key.c.token_hash)
|
|
839
|
+
.having(func.count() > 1)
|
|
840
|
+
.limit(1)
|
|
841
|
+
).first()
|
|
842
|
+
if duplicate:
|
|
843
|
+
raise RuntimeError(
|
|
844
|
+
"Cannot apply schema revision 2: duplicate API token hashes exist. "
|
|
845
|
+
"Revoke duplicate keys before migrating."
|
|
846
|
+
)
|
|
847
|
+
indexes = inspect(conn).get_indexes(api_key.name)
|
|
848
|
+
if not any(index["name"] == "uq_api_key_token_hash" for index in indexes):
|
|
849
|
+
conn.exec_driver_sql("CREATE UNIQUE INDEX uq_api_key_token_hash ON api_key (token_hash)")
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
def _apply_identity_memberships(conn) -> None:
|
|
853
|
+
"""Backfill a distinct identity and membership for every legacy profile."""
|
|
854
|
+
global_identity.create(conn, checkfirst=True)
|
|
855
|
+
organization_membership.create(conn, checkfirst=True)
|
|
856
|
+
identity_credential.create(conn, checkfirst=True)
|
|
857
|
+
profiles = conn.execute(select(user)).mappings().all()
|
|
858
|
+
for profile in profiles:
|
|
859
|
+
identity_id = new_id()
|
|
860
|
+
created_at = profile.get("created_at") or now_iso()
|
|
861
|
+
conn.execute(
|
|
862
|
+
global_identity.insert().values(
|
|
863
|
+
id=identity_id,
|
|
864
|
+
email=(profile.get("email") or "").strip().lower(),
|
|
865
|
+
created_at=created_at,
|
|
866
|
+
)
|
|
867
|
+
)
|
|
868
|
+
conn.execute(
|
|
869
|
+
organization_membership.insert().values(
|
|
870
|
+
id=new_id(),
|
|
871
|
+
identity_id=identity_id,
|
|
872
|
+
organization_id=profile["organization_id"],
|
|
873
|
+
profile_user_id=profile["id"],
|
|
874
|
+
active=bool(profile.get("active", True)) and profile.get("archived_at") is None,
|
|
875
|
+
created_at=created_at,
|
|
876
|
+
)
|
|
877
|
+
)
|
|
878
|
+
old_credential = (
|
|
879
|
+
conn.execute(
|
|
880
|
+
select(password_credential).where(password_credential.c.user_id == profile["id"])
|
|
881
|
+
)
|
|
882
|
+
.mappings()
|
|
883
|
+
.first()
|
|
884
|
+
)
|
|
885
|
+
if old_credential:
|
|
886
|
+
conn.execute(
|
|
887
|
+
identity_credential.insert().values(
|
|
888
|
+
identity_id=identity_id,
|
|
889
|
+
password_hash=old_credential["password_hash"],
|
|
890
|
+
created_at=old_credential["created_at"],
|
|
891
|
+
updated_at=old_credential["updated_at"],
|
|
892
|
+
)
|
|
893
|
+
)
|
|
894
|
+
|
|
895
|
+
|
|
896
|
+
def _apply_team_cadence(conn) -> None:
|
|
897
|
+
"""Add opt-in cadence policy and an idempotent cycle-boundary key."""
|
|
898
|
+
inspector = inspect(conn)
|
|
899
|
+
existing = {column["name"] for column in inspector.get_columns("team")}
|
|
900
|
+
additions = {
|
|
901
|
+
# FALSE, not 0: Postgres rejects an integer default on a boolean column
|
|
902
|
+
# ("column ... is of type boolean but default expression is of type integer").
|
|
903
|
+
"cadence_enabled": "BOOLEAN NOT NULL DEFAULT FALSE",
|
|
904
|
+
"cadence_weekday": "INTEGER",
|
|
905
|
+
"cadence_anchor_at": "VARCHAR",
|
|
906
|
+
"cadence_policy_version": "INTEGER",
|
|
907
|
+
"cadence_duration_weeks": "INTEGER NOT NULL DEFAULT 1",
|
|
908
|
+
"cadence_cooldown_weeks": "INTEGER NOT NULL DEFAULT 0",
|
|
909
|
+
"cadence_upcoming_count": "INTEGER NOT NULL DEFAULT 2",
|
|
910
|
+
}
|
|
911
|
+
for name, sql_type in additions.items():
|
|
912
|
+
if name not in existing:
|
|
913
|
+
conn.exec_driver_sql(f"ALTER TABLE team ADD COLUMN {name} {sql_type}")
|
|
914
|
+
issue_columns = {column["name"] for column in inspect(conn).get_columns("issue")}
|
|
915
|
+
if "state_changed_at" not in issue_columns:
|
|
916
|
+
conn.exec_driver_sql("ALTER TABLE issue ADD COLUMN state_changed_at VARCHAR")
|
|
917
|
+
conn.exec_driver_sql("UPDATE issue SET state_changed_at = COALESCE(updated_at, created_at)")
|
|
918
|
+
cycle_columns = {column["name"] for column in inspect(conn).get_columns("cycle")}
|
|
919
|
+
if "cadence_boundary_at" not in cycle_columns:
|
|
920
|
+
conn.exec_driver_sql("ALTER TABLE cycle ADD COLUMN cadence_boundary_at VARCHAR")
|
|
921
|
+
indexes = inspect(conn).get_indexes("cycle")
|
|
922
|
+
constraints = inspect(conn).get_unique_constraints("cycle")
|
|
923
|
+
names = {item.get("name") for item in indexes + constraints}
|
|
924
|
+
if "uq_cycle_team_cadence_boundary" not in names:
|
|
925
|
+
conn.exec_driver_sql(
|
|
926
|
+
"CREATE UNIQUE INDEX uq_cycle_team_cadence_boundary "
|
|
927
|
+
"ON cycle (team_id, cadence_boundary_at)"
|
|
928
|
+
)
|
|
929
|
+
|
|
930
|
+
|
|
931
|
+
def _apply_team_auto_add_backlog(conn) -> None:
|
|
932
|
+
"""Add the backlog auto-add-to-cycle toggle on the team table.
|
|
933
|
+
|
|
934
|
+
Requested by the workspace owner: backlog issues should join the
|
|
935
|
+
current cycle automatically, like started/completed ones. Defaults on.
|
|
936
|
+
"""
|
|
937
|
+
inspector = inspect(conn)
|
|
938
|
+
existing = {column["name"] for column in inspector.get_columns("team")}
|
|
939
|
+
if "auto_add_backlog" not in existing:
|
|
940
|
+
conn.exec_driver_sql("ALTER TABLE team ADD COLUMN auto_add_backlog BOOLEAN NOT NULL DEFAULT TRUE")
|
|
941
|
+
|
|
942
|
+
|
|
943
|
+
def _apply_oauth_security(conn):
|
|
944
|
+
oauth_pending.create(conn, checkfirst=True)
|
|
945
|
+
for table in (oauth_authorization, oauth_access_token, oauth_refresh_token):
|
|
946
|
+
if "resource" not in {c["name"] for c in inspect(conn).get_columns(table.name)}:
|
|
947
|
+
conn.exec_driver_sql(f"ALTER TABLE {table.name} ADD COLUMN resource TEXT")
|
|
948
|
+
|
|
949
|
+
|
|
950
|
+
def _apply_api_key_access(conn):
|
|
951
|
+
if "access" not in {c["name"] for c in inspect(conn).get_columns("api_key")}:
|
|
952
|
+
conn.exec_driver_sql(
|
|
953
|
+
"ALTER TABLE api_key ADD COLUMN access VARCHAR NOT NULL DEFAULT 'read_write'"
|
|
954
|
+
)
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
# Non-secret, org-scoped tables whose every write is published as a change event.
|
|
958
|
+
# Secret tables (api_key, sessions, credentials, OAuth, invitations, global identity)
|
|
959
|
+
# are deliberately absent so they can never produce an event.
|
|
960
|
+
_CHANGE_CAPTURE = (
|
|
961
|
+
# (table, entity, primary key, team_id expression, organization_id expression)
|
|
962
|
+
# Expressions use {row} for NEW/OLD. None means NULL (no team scope).
|
|
963
|
+
("issue", "issue", "id", "{row}.team_id", "{row}.organization_id"),
|
|
964
|
+
("comment", "comment", "id", None, "{row}.organization_id"),
|
|
965
|
+
("attachment", "attachment", "id", None, "{row}.organization_id"),
|
|
966
|
+
("project", "project", "id", None, "{row}.organization_id"),
|
|
967
|
+
("project_update", "projectUpdate", "id", None, "{row}.organization_id"),
|
|
968
|
+
("initiative", "initiative", "id", None, "{row}.organization_id"),
|
|
969
|
+
("cycle", "cycle", "id", "{row}.team_id", "{row}.organization_id"),
|
|
970
|
+
("issue_label", "label", "id", "{row}.team_id", "{row}.organization_id"),
|
|
971
|
+
("workflow_state", "workflowState", "id", "{row}.team_id", "{row}.organization_id"),
|
|
972
|
+
("team", "team", "id", "{row}.id", "{row}.organization_id"),
|
|
973
|
+
("team_member", "teamMember", "team_id", "{row}.team_id",
|
|
974
|
+
"(SELECT organization_id FROM team WHERE id = {row}.team_id)"),
|
|
975
|
+
("inbox_notification", "notification", "id", None, "{row}.organization_id"),
|
|
976
|
+
("user", "user", "id", None, "{row}.organization_id"),
|
|
977
|
+
("organization_membership", "member", "id", None, "{row}.organization_id"),
|
|
978
|
+
)
|
|
979
|
+
|
|
980
|
+
|
|
981
|
+
def _change_trigger_sql(dialect: str, table_name: str, entity: str, pk: str,
|
|
982
|
+
team_expr: str | None, org_expr: str):
|
|
983
|
+
"""Return one CREATE TRIGGER statement per insert/update/delete for a table."""
|
|
984
|
+
statements = []
|
|
985
|
+
for op, label in (("insert", "create"), ("update", "update"), ("delete", "delete")):
|
|
986
|
+
row = "OLD" if op == "delete" else "NEW"
|
|
987
|
+
team_value = team_expr.format(row=row) if team_expr else "NULL"
|
|
988
|
+
org = org_expr.format(row=row)
|
|
989
|
+
pk_value = f"{row}.{pk}"
|
|
990
|
+
body = (
|
|
991
|
+
"INSERT INTO change_event (organization_id, entity, entity_id, action, team_id, created_at) "
|
|
992
|
+
f"VALUES ({org}, '{entity}', CAST({pk_value} AS TEXT), '{label}', "
|
|
993
|
+
f"{team_value}, strftime('%Y-%m-%dT%H:%M:%fZ','now'));"
|
|
994
|
+
)
|
|
995
|
+
statements.append(
|
|
996
|
+
f"CREATE TRIGGER IF NOT EXISTS trg_chg_{table_name}_{label} "
|
|
997
|
+
f"AFTER {op.upper()} ON {table_name} FOR EACH ROW BEGIN {body} END;"
|
|
998
|
+
)
|
|
999
|
+
return statements
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
def _postgres_change_trigger_sql(table_name: str, entity: str, pk: str,
|
|
1003
|
+
team_expr: str | None, org_expr: str) -> list[str]:
|
|
1004
|
+
"""One PL/pgSQL function per table (shared body shape) and one trigger per operation."""
|
|
1005
|
+
function = f"chg_{table_name}"
|
|
1006
|
+
team_new = team_expr.format(row="NEW") if team_expr else "NULL"
|
|
1007
|
+
team_old = team_expr.format(row="OLD") if team_expr else "NULL"
|
|
1008
|
+
org_new = org_expr.format(row="NEW")
|
|
1009
|
+
org_old = org_expr.format(row="OLD")
|
|
1010
|
+
pk_new = f"NEW.{pk}"
|
|
1011
|
+
pk_old = f"OLD.{pk}"
|
|
1012
|
+
statements = [
|
|
1013
|
+
f"""CREATE OR REPLACE FUNCTION {function}() RETURNS trigger LANGUAGE plpgsql AS $fn$
|
|
1014
|
+
BEGIN
|
|
1015
|
+
IF TG_OP = 'DELETE' THEN
|
|
1016
|
+
INSERT INTO change_event (organization_id, entity, entity_id, action, team_id, created_at)
|
|
1017
|
+
VALUES ({org_old}, '{entity}', CAST({pk_old} AS TEXT), 'delete', {team_old},
|
|
1018
|
+
to_char(now() AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"+00:00"'));
|
|
1019
|
+
RETURN OLD;
|
|
1020
|
+
END IF;
|
|
1021
|
+
INSERT INTO change_event (organization_id, entity, entity_id, action, team_id, created_at)
|
|
1022
|
+
VALUES ({org_new}, '{entity}', CAST({pk_new} AS TEXT),
|
|
1023
|
+
CASE WHEN TG_OP = 'INSERT' THEN 'create' ELSE 'update' END, {team_new},
|
|
1024
|
+
to_char(now() AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"+00:00"'));
|
|
1025
|
+
RETURN NEW;
|
|
1026
|
+
END;
|
|
1027
|
+
$fn$;""",
|
|
1028
|
+
f'DROP TRIGGER IF EXISTS trg_chg_{table_name} ON "{table_name}";',
|
|
1029
|
+
f'CREATE TRIGGER trg_chg_{table_name} AFTER INSERT OR UPDATE OR DELETE ON "{table_name}" '
|
|
1030
|
+
f"FOR EACH ROW EXECUTE FUNCTION {function}();",
|
|
1031
|
+
]
|
|
1032
|
+
return statements
|
|
1033
|
+
|
|
1034
|
+
|
|
1035
|
+
def _apply_realtime_capture(conn):
|
|
1036
|
+
dialect = conn.dialect.name
|
|
1037
|
+
for table_name, entity, pk, team_expr, org_expr in _CHANGE_CAPTURE:
|
|
1038
|
+
if dialect == "sqlite":
|
|
1039
|
+
for statement in _change_trigger_sql(dialect, table_name, entity, pk, team_expr, org_expr):
|
|
1040
|
+
conn.exec_driver_sql(statement)
|
|
1041
|
+
elif dialect == "postgresql":
|
|
1042
|
+
for statement in _postgres_change_trigger_sql(table_name, entity, pk, team_expr, org_expr):
|
|
1043
|
+
conn.exec_driver_sql(statement)
|
|
1044
|
+
else:
|
|
1045
|
+
raise RuntimeError(f"Realtime capture is not implemented for {dialect}")
|
|
1046
|
+
|
|
1047
|
+
|
|
1048
|
+
# Link tables publish as an update of their parent issue. The event row is
|
|
1049
|
+
# selected from the issue itself, so a link removed alongside (or after) its
|
|
1050
|
+
# issue emits nothing instead of violating change_event's NOT NULL columns.
|
|
1051
|
+
_LINK_CAPTURE = ("issue_label_link", "issue_subscriber")
|
|
1052
|
+
|
|
1053
|
+
|
|
1054
|
+
def _apply_issue_link_capture(conn):
|
|
1055
|
+
dialect = conn.dialect.name
|
|
1056
|
+
for table_name in _LINK_CAPTURE:
|
|
1057
|
+
if dialect == "sqlite":
|
|
1058
|
+
for op in ("insert", "delete"):
|
|
1059
|
+
row = "OLD" if op == "delete" else "NEW"
|
|
1060
|
+
conn.exec_driver_sql(
|
|
1061
|
+
f"CREATE TRIGGER IF NOT EXISTS trg_chg_{table_name}_{op} "
|
|
1062
|
+
f"AFTER {op.upper()} ON {table_name} FOR EACH ROW BEGIN "
|
|
1063
|
+
"INSERT INTO change_event (organization_id, entity, entity_id, action, team_id, created_at) "
|
|
1064
|
+
"SELECT organization_id, 'issue', id, 'update', team_id, "
|
|
1065
|
+
f"strftime('%Y-%m-%dT%H:%M:%fZ','now') FROM issue WHERE id = {row}.issue_id; END;"
|
|
1066
|
+
)
|
|
1067
|
+
elif dialect == "postgresql":
|
|
1068
|
+
conn.exec_driver_sql(f"""CREATE OR REPLACE FUNCTION chg_{table_name}() RETURNS trigger LANGUAGE plpgsql AS $fn$
|
|
1069
|
+
DECLARE v_issue_id TEXT;
|
|
1070
|
+
BEGIN
|
|
1071
|
+
v_issue_id := CASE WHEN TG_OP = 'DELETE' THEN OLD.issue_id ELSE NEW.issue_id END;
|
|
1072
|
+
INSERT INTO change_event (organization_id, entity, entity_id, action, team_id, created_at)
|
|
1073
|
+
SELECT i.organization_id, 'issue', i.id, 'update', i.team_id,
|
|
1074
|
+
to_char(now() AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS.US"+00:00"')
|
|
1075
|
+
FROM issue i WHERE i.id = v_issue_id;
|
|
1076
|
+
RETURN NULL;
|
|
1077
|
+
END;
|
|
1078
|
+
$fn$;""")
|
|
1079
|
+
conn.exec_driver_sql(f'DROP TRIGGER IF EXISTS trg_chg_{table_name} ON "{table_name}";')
|
|
1080
|
+
conn.exec_driver_sql(
|
|
1081
|
+
f'CREATE TRIGGER trg_chg_{table_name} AFTER INSERT OR UPDATE OR DELETE ON "{table_name}" '
|
|
1082
|
+
f"FOR EACH ROW EXECUTE FUNCTION chg_{table_name}();")
|
|
1083
|
+
else:
|
|
1084
|
+
raise RuntimeError(f"Realtime capture is not implemented for {dialect}")
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
CHANGE_NOTIFY_CHANNEL = "hoja_change"
|
|
1088
|
+
_PG_CHANGE_TS = "to_char(now() AT TIME ZONE 'UTC', 'YYYY-MM-DD\"T\"HH24:MI:SS.US\"+00:00\"')"
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
def _pg_publish_body(org: str, entity: str, entity_id: str, action: str, team: str,
|
|
1092
|
+
recipient: str = "NULL") -> str:
|
|
1093
|
+
"""PL/pgSQL that records one change event and notifies listeners, commit-ordered per org.
|
|
1094
|
+
|
|
1095
|
+
It runs from a DEFERRED constraint trigger, i.e. at COMMIT after every
|
|
1096
|
+
statement of the transaction, and takes a per-org transaction advisory lock
|
|
1097
|
+
before allocating the seq. Concurrent writers of one org therefore get seqs in
|
|
1098
|
+
commit order, so a reader holding "everything up to seq N" never misses a
|
|
1099
|
+
lower seq that commits later. The lock is held only while committing, after
|
|
1100
|
+
all row locks were taken, so it cannot deadlock with them.
|
|
1101
|
+
"""
|
|
1102
|
+
return f"""
|
|
1103
|
+
v_org := {org};
|
|
1104
|
+
-- Evaluated at commit: the org (or a link's issue) may be gone by then.
|
|
1105
|
+
IF v_org IS NULL OR NOT EXISTS (SELECT 1 FROM organization WHERE id = v_org) THEN RETURN NULL; END IF;
|
|
1106
|
+
PERFORM pg_advisory_xact_lock(hashtext('hoja.change:' || v_org));
|
|
1107
|
+
INSERT INTO change_event (organization_id, entity, entity_id, action, team_id, created_at, recipient_id)
|
|
1108
|
+
VALUES (v_org, {entity}, {entity_id}, {action}, {team}, {_PG_CHANGE_TS}, {recipient})
|
|
1109
|
+
RETURNING seq INTO v_seq;
|
|
1110
|
+
PERFORM pg_notify('{CHANGE_NOTIFY_CHANNEL}',
|
|
1111
|
+
json_build_array(v_seq, v_org, {entity}, {entity_id}, {action}, {team}, {recipient})::text);"""
|
|
1112
|
+
|
|
1113
|
+
|
|
1114
|
+
# Updates that are not changes clients care about (issue number allocation).
|
|
1115
|
+
_PG_SKIP = {"team": " IF TG_OP = 'UPDATE' AND OLD.issue_counter IS DISTINCT FROM NEW.issue_counter "
|
|
1116
|
+
"THEN RETURN NULL; END IF;"}
|
|
1117
|
+
# Per-user entities: the event carries the recipient and only that user receives it.
|
|
1118
|
+
_RECIPIENT_CAPTURE = {"inbox_notification": "{row}.recipient_id"}
|
|
1119
|
+
|
|
1120
|
+
|
|
1121
|
+
def _apply_realtime_v2_capture(conn):
|
|
1122
|
+
"""Recipient-scoped notification events; on PostgreSQL also deferred,
|
|
1123
|
+
commit-ordered capture with LISTEN/NOTIFY wakeups."""
|
|
1124
|
+
if "recipient_id" not in {c["name"] for c in inspect(conn).get_columns("change_event")}:
|
|
1125
|
+
conn.exec_driver_sql("ALTER TABLE change_event ADD COLUMN recipient_id VARCHAR")
|
|
1126
|
+
if conn.dialect.name == "sqlite":
|
|
1127
|
+
# SQLite serializes writers, so its seqs are already commit-ordered; only
|
|
1128
|
+
# notification events change, to carry their recipient.
|
|
1129
|
+
for op, label in (("insert", "create"), ("update", "update"), ("delete", "delete")):
|
|
1130
|
+
row = "OLD" if op == "delete" else "NEW"
|
|
1131
|
+
conn.exec_driver_sql(f"DROP TRIGGER IF EXISTS trg_chg_inbox_notification_{label}")
|
|
1132
|
+
conn.exec_driver_sql(
|
|
1133
|
+
f"CREATE TRIGGER trg_chg_inbox_notification_{label} AFTER {op.upper()} ON inbox_notification "
|
|
1134
|
+
"FOR EACH ROW BEGIN INSERT INTO change_event "
|
|
1135
|
+
"(organization_id, entity, entity_id, action, team_id, created_at, recipient_id) "
|
|
1136
|
+
f"VALUES ({row}.organization_id, 'notification', CAST({row}.id AS TEXT), '{label}', NULL, "
|
|
1137
|
+
f"strftime('%Y-%m-%dT%H:%M:%fZ','now'), {row}.recipient_id); END;")
|
|
1138
|
+
# Allocating an issue number bumps team.issue_counter; that is not a team
|
|
1139
|
+
# change, and publishing it made every client re-bootstrap on issue create.
|
|
1140
|
+
conn.exec_driver_sql("DROP TRIGGER IF EXISTS trg_chg_team_update")
|
|
1141
|
+
conn.exec_driver_sql(
|
|
1142
|
+
"CREATE TRIGGER trg_chg_team_update AFTER UPDATE ON team FOR EACH ROW "
|
|
1143
|
+
"WHEN OLD.issue_counter IS NEW.issue_counter BEGIN INSERT INTO change_event "
|
|
1144
|
+
"(organization_id, entity, entity_id, action, team_id, created_at) "
|
|
1145
|
+
"VALUES (NEW.organization_id, 'team', CAST(NEW.id AS TEXT), 'update', NEW.id, "
|
|
1146
|
+
"strftime('%Y-%m-%dT%H:%M:%fZ','now')); END;")
|
|
1147
|
+
return
|
|
1148
|
+
if conn.dialect.name != "postgresql":
|
|
1149
|
+
raise RuntimeError(f"Realtime capture is not implemented for {conn.dialect.name}")
|
|
1150
|
+
for table_name, entity, pk, team_expr, org_expr in _CHANGE_CAPTURE:
|
|
1151
|
+
team_new = team_expr.format(row="NEW") if team_expr else "NULL"
|
|
1152
|
+
team_old = team_expr.format(row="OLD") if team_expr else "NULL"
|
|
1153
|
+
rcpt = _RECIPIENT_CAPTURE.get(table_name)
|
|
1154
|
+
rcpt_new = rcpt.format(row="NEW") if rcpt else "NULL"
|
|
1155
|
+
rcpt_old = rcpt.format(row="OLD") if rcpt else "NULL"
|
|
1156
|
+
conn.exec_driver_sql(f"""CREATE OR REPLACE FUNCTION chg_{table_name}() RETURNS trigger LANGUAGE plpgsql AS $fn$
|
|
1157
|
+
DECLARE v_org TEXT; v_seq BIGINT;
|
|
1158
|
+
BEGIN
|
|
1159
|
+
{_PG_SKIP.get(table_name, "")}
|
|
1160
|
+
IF TG_OP = 'DELETE' THEN
|
|
1161
|
+
{_pg_publish_body(org_expr.format(row="OLD"), f"'{entity}'", f"CAST(OLD.{pk} AS TEXT)", "'delete'",
|
|
1162
|
+
team_old, rcpt_old)}
|
|
1163
|
+
ELSE
|
|
1164
|
+
{_pg_publish_body(org_expr.format(row="NEW"), f"'{entity}'", f"CAST(NEW.{pk} AS TEXT)",
|
|
1165
|
+
"CASE WHEN TG_OP = 'INSERT' THEN 'create' ELSE 'update' END", team_new, rcpt_new)}
|
|
1166
|
+
END IF;
|
|
1167
|
+
RETURN NULL;
|
|
1168
|
+
END;
|
|
1169
|
+
$fn$;""")
|
|
1170
|
+
_pg_deferred_trigger(conn, table_name)
|
|
1171
|
+
for table_name in _LINK_CAPTURE:
|
|
1172
|
+
# A link publishes as an update of its issue, read at commit time; a link
|
|
1173
|
+
# removed together with its issue emits nothing (the issue delete does).
|
|
1174
|
+
conn.exec_driver_sql(f"""CREATE OR REPLACE FUNCTION chg_{table_name}() RETURNS trigger LANGUAGE plpgsql AS $fn$
|
|
1175
|
+
DECLARE v_org TEXT; v_seq BIGINT; v_issue_id TEXT; v_team TEXT;
|
|
1176
|
+
BEGIN
|
|
1177
|
+
v_issue_id := CASE WHEN TG_OP = 'DELETE' THEN OLD.issue_id ELSE NEW.issue_id END;
|
|
1178
|
+
SELECT i.organization_id, i.team_id INTO v_org, v_team FROM issue i WHERE i.id = v_issue_id;
|
|
1179
|
+
{_pg_publish_body("v_org", "'issue'", "v_issue_id", "'update'", "v_team")}
|
|
1180
|
+
RETURN NULL;
|
|
1181
|
+
END;
|
|
1182
|
+
$fn$;""")
|
|
1183
|
+
_pg_deferred_trigger(conn, table_name)
|
|
1184
|
+
|
|
1185
|
+
|
|
1186
|
+
def _pg_deferred_trigger(conn, table_name: str) -> None:
|
|
1187
|
+
conn.exec_driver_sql(f'DROP TRIGGER IF EXISTS trg_chg_{table_name} ON "{table_name}";')
|
|
1188
|
+
conn.exec_driver_sql(
|
|
1189
|
+
f'CREATE CONSTRAINT TRIGGER trg_chg_{table_name} AFTER INSERT OR UPDATE OR DELETE ON "{table_name}" '
|
|
1190
|
+
f"DEFERRABLE INITIALLY DEFERRED FOR EACH ROW EXECUTE FUNCTION chg_{table_name}();")
|
|
1191
|
+
|
|
1192
|
+
|
|
1193
|
+
def _apply_change_event_org_seq_index(conn):
|
|
1194
|
+
conn.exec_driver_sql(
|
|
1195
|
+
"CREATE INDEX IF NOT EXISTS ix_change_event_org_seq "
|
|
1196
|
+
"ON change_event (organization_id, seq)"
|
|
1197
|
+
)
|
|
1198
|
+
|
|
1199
|
+
|
|
1200
|
+
def _apply_issue_scale_indexes(conn):
|
|
1201
|
+
for stmt in (
|
|
1202
|
+
"CREATE INDEX IF NOT EXISTS ix_issue_cycle_id ON issue (cycle_id)",
|
|
1203
|
+
"CREATE INDEX IF NOT EXISTS ix_issue_parent_id ON issue (parent_id)",
|
|
1204
|
+
"CREATE INDEX IF NOT EXISTS ix_issue_creator_id ON issue (creator_id)",
|
|
1205
|
+
"CREATE INDEX IF NOT EXISTS ix_issue_org_created ON issue (organization_id, created_at)",
|
|
1206
|
+
"CREATE INDEX IF NOT EXISTS ix_issue_org_updated ON issue (organization_id, updated_at)",
|
|
1207
|
+
"CREATE INDEX IF NOT EXISTS ix_issue_archived_partial ON issue (organization_id, archived_at) "
|
|
1208
|
+
"WHERE archived_at IS NULL",
|
|
1209
|
+
"CREATE INDEX IF NOT EXISTS ix_inbox_notification_recipient_read ON inbox_notification (recipient_id, read_at)",
|
|
1210
|
+
"CREATE INDEX IF NOT EXISTS ix_issue_activity_issue_created ON issue_activity (issue_id, created_at)",
|
|
1211
|
+
):
|
|
1212
|
+
conn.exec_driver_sql(stmt)
|
|
1213
|
+
|
|
1214
|
+
|
|
1215
|
+
def _apply_api_key_lifecycle(conn):
|
|
1216
|
+
existing = {c["name"] for c in inspect(conn).get_columns("api_key")}
|
|
1217
|
+
for name, sql_type in (("hint", "VARCHAR"), ("expires_at", "VARCHAR"), ("last_used_at", "VARCHAR")):
|
|
1218
|
+
if name not in existing:
|
|
1219
|
+
conn.exec_driver_sql(f"ALTER TABLE api_key ADD COLUMN {name} {sql_type}")
|
|
1220
|
+
|
|
1221
|
+
def _apply_agent_conversation_title(conn) -> None:
|
|
1222
|
+
if "title" not in {c["name"] for c in inspect(conn).get_columns("agent_conversation")}:
|
|
1223
|
+
conn.exec_driver_sql("ALTER TABLE agent_conversation ADD COLUMN title VARCHAR")
|
|
1224
|
+
|
|
1225
|
+
|
|
1226
|
+
def rate_limit_key_hash(key: str) -> str:
|
|
1227
|
+
"""Rate-limit keys (emails, IPs) are stored only as SHA-256 digests."""
|
|
1228
|
+
return hashlib.sha256(key.encode()).hexdigest()
|
|
1229
|
+
|
|
1230
|
+
|
|
1231
|
+
def rate_limit_blocked(
|
|
1232
|
+
engine: Engine, bucket: str, keys: list[str], limit: int, window_seconds: float
|
|
1233
|
+
) -> bool:
|
|
1234
|
+
"""True when any key has `limit` or more events inside the sliding window."""
|
|
1235
|
+
hashes = [rate_limit_key_hash(key) for key in keys]
|
|
1236
|
+
cutoff = time.time() - window_seconds
|
|
1237
|
+
with engine.connect() as conn:
|
|
1238
|
+
counts = conn.execute(
|
|
1239
|
+
select(rate_limit_event.c.key_hash, func.count())
|
|
1240
|
+
.where(
|
|
1241
|
+
rate_limit_event.c.bucket == bucket,
|
|
1242
|
+
rate_limit_event.c.key_hash.in_(hashes),
|
|
1243
|
+
rate_limit_event.c.created_at > cutoff,
|
|
1244
|
+
)
|
|
1245
|
+
.group_by(rate_limit_event.c.key_hash)
|
|
1246
|
+
).all()
|
|
1247
|
+
return any(count >= limit for _, count in counts)
|
|
1248
|
+
|
|
1249
|
+
|
|
1250
|
+
def rate_limit_record(engine: Engine, bucket: str, keys: list[str], window_seconds: float) -> None:
|
|
1251
|
+
"""Record one event per key and prune this bucket's expired events."""
|
|
1252
|
+
now = time.time()
|
|
1253
|
+
with engine.begin() as conn:
|
|
1254
|
+
conn.execute(
|
|
1255
|
+
rate_limit_event.delete().where(
|
|
1256
|
+
rate_limit_event.c.bucket == bucket,
|
|
1257
|
+
rate_limit_event.c.created_at <= now - window_seconds,
|
|
1258
|
+
)
|
|
1259
|
+
)
|
|
1260
|
+
conn.execute(
|
|
1261
|
+
rate_limit_event.insert(),
|
|
1262
|
+
[
|
|
1263
|
+
{"bucket": bucket, "key_hash": rate_limit_key_hash(key), "created_at": now}
|
|
1264
|
+
for key in keys
|
|
1265
|
+
],
|
|
1266
|
+
)
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
def _apply_team_cycle_automations(conn) -> None:
|
|
1270
|
+
"""Add Linear-style auto-add-to-cycle toggles on the team table.
|
|
1271
|
+
|
|
1272
|
+
Defaults match Linear (both on). Structured so a future cooldown
|
|
1273
|
+
attribution rule can read the same flags.
|
|
1274
|
+
"""
|
|
1275
|
+
inspector = inspect(conn)
|
|
1276
|
+
existing = {column["name"] for column in inspector.get_columns("team")}
|
|
1277
|
+
additions = {
|
|
1278
|
+
"auto_add_started": "BOOLEAN NOT NULL DEFAULT TRUE",
|
|
1279
|
+
"auto_add_completed": "BOOLEAN NOT NULL DEFAULT TRUE",
|
|
1280
|
+
}
|
|
1281
|
+
for name, sql_type in additions.items():
|
|
1282
|
+
if name not in existing:
|
|
1283
|
+
conn.exec_driver_sql(f"ALTER TABLE team ADD COLUMN {name} {sql_type}")
|
|
1284
|
+
|
|
1285
|
+
|
|
1286
|
+
meeting = Table(
|
|
1287
|
+
"meeting",
|
|
1288
|
+
metadata,
|
|
1289
|
+
Column("id", String, primary_key=True),
|
|
1290
|
+
Column("organization_id", String, ForeignKey("organization.id"), nullable=False, index=True),
|
|
1291
|
+
Column("code", String, nullable=False, unique=True),
|
|
1292
|
+
Column("title", String, nullable=False),
|
|
1293
|
+
Column("description", Text),
|
|
1294
|
+
Column("host_id", String, ForeignKey("user.id"), nullable=False, index=True),
|
|
1295
|
+
Column("team_id", String, ForeignKey("team.id")),
|
|
1296
|
+
Column("issue_id", String),
|
|
1297
|
+
Column("project_id", String),
|
|
1298
|
+
Column("access", String, nullable=False, default="invited"),
|
|
1299
|
+
Column("passcode_hash", String),
|
|
1300
|
+
Column("link_enabled", Boolean, nullable=False, default=False),
|
|
1301
|
+
Column("waiting_room", Boolean, nullable=False, default=True),
|
|
1302
|
+
Column("join_before_host", Boolean, nullable=False, default=False),
|
|
1303
|
+
Column("mute_on_entry", Boolean, nullable=False, default=True),
|
|
1304
|
+
Column("allow_unmute", Boolean, nullable=False, default=True),
|
|
1305
|
+
Column("screen_share", String, nullable=False, default="all"),
|
|
1306
|
+
Column("chat_enabled", Boolean, nullable=False, default=True),
|
|
1307
|
+
Column("locked", Boolean, nullable=False, default=False),
|
|
1308
|
+
Column("recording", String, nullable=False, default="off"),
|
|
1309
|
+
Column("max_participants", Integer, nullable=False, default=25),
|
|
1310
|
+
Column("status", String, nullable=False, default="scheduled", index=True),
|
|
1311
|
+
Column("scheduled_start", String),
|
|
1312
|
+
Column("duration_minutes", Integer),
|
|
1313
|
+
Column("started_at", String),
|
|
1314
|
+
Column("ended_at", String),
|
|
1315
|
+
Column("reminded_at", String),
|
|
1316
|
+
Column("created_at", String, nullable=False),
|
|
1317
|
+
Column("updated_at", String, nullable=False),
|
|
1318
|
+
)
|
|
1319
|
+
|
|
1320
|
+
meeting_invitee = Table(
|
|
1321
|
+
"meeting_invitee",
|
|
1322
|
+
metadata,
|
|
1323
|
+
Column("id", String, primary_key=True),
|
|
1324
|
+
Column("meeting_id", String, ForeignKey("meeting.id"), nullable=False, index=True),
|
|
1325
|
+
Column("user_id", String, ForeignKey("user.id"), index=True),
|
|
1326
|
+
Column("email", String, index=True),
|
|
1327
|
+
Column("role", String, nullable=False, default="attendee"),
|
|
1328
|
+
Column("response", String, nullable=False, default="needsAction", server_default="needsAction"),
|
|
1329
|
+
Column("invited_by", String, ForeignKey("user.id")),
|
|
1330
|
+
Column("created_at", String, nullable=False),
|
|
1331
|
+
)
|
|
1332
|
+
|
|
1333
|
+
meeting_participant = Table(
|
|
1334
|
+
"meeting_participant",
|
|
1335
|
+
metadata,
|
|
1336
|
+
Column("id", String, primary_key=True),
|
|
1337
|
+
Column("meeting_id", String, ForeignKey("meeting.id"), nullable=False, index=True),
|
|
1338
|
+
Column("user_id", String, ForeignKey("user.id"), index=True),
|
|
1339
|
+
Column("kind", String, nullable=False, default="member"),
|
|
1340
|
+
Column("display_name", String, nullable=False),
|
|
1341
|
+
Column("role", String, nullable=False, default="attendee"),
|
|
1342
|
+
Column("state", String, nullable=False, default="waiting"),
|
|
1343
|
+
Column("token_hash", String, unique=True, index=True),
|
|
1344
|
+
Column("sfu_session_id", String),
|
|
1345
|
+
Column("sfu_sub_session_id", String),
|
|
1346
|
+
Column("device_id", String),
|
|
1347
|
+
Column("device_of", String),
|
|
1348
|
+
Column("device_label", String),
|
|
1349
|
+
Column("tracks", Text),
|
|
1350
|
+
Column("muted_by_host", Boolean, nullable=False, default=False),
|
|
1351
|
+
Column("hand_raised", Boolean, nullable=False, default=False),
|
|
1352
|
+
Column("audio_on", Boolean, nullable=False, default=False),
|
|
1353
|
+
Column("video_on", Boolean, nullable=False, default=False),
|
|
1354
|
+
Column("screen_on", Boolean, nullable=False, default=False),
|
|
1355
|
+
Column("bytes_in", BigInteger, nullable=False, default=0),
|
|
1356
|
+
Column("bytes_out", BigInteger, nullable=False, default=0),
|
|
1357
|
+
Column("joined_at", String),
|
|
1358
|
+
Column("left_at", String),
|
|
1359
|
+
Column("created_at", String, nullable=False),
|
|
1360
|
+
)
|
|
1361
|
+
|
|
1362
|
+
meeting_ring = Table(
|
|
1363
|
+
"meeting_ring",
|
|
1364
|
+
metadata,
|
|
1365
|
+
Column("id", String, primary_key=True),
|
|
1366
|
+
Column("meeting_id", String, ForeignKey("meeting.id"), nullable=False, index=True),
|
|
1367
|
+
Column("organization_id", String, ForeignKey("organization.id"), nullable=False),
|
|
1368
|
+
Column("from_id", String, ForeignKey("user.id"), nullable=False),
|
|
1369
|
+
Column("to_id", String, ForeignKey("user.id"), nullable=False, index=True),
|
|
1370
|
+
Column("state", String, nullable=False, default="ringing"),
|
|
1371
|
+
Column("created_at", String, nullable=False),
|
|
1372
|
+
Column("expires_at", String, nullable=False),
|
|
1373
|
+
Column("responded_at", String),
|
|
1374
|
+
)
|
|
1375
|
+
|
|
1376
|
+
meeting_chat = Table(
|
|
1377
|
+
"meeting_chat",
|
|
1378
|
+
metadata,
|
|
1379
|
+
Column("id", String, primary_key=True),
|
|
1380
|
+
Column("meeting_id", String, ForeignKey("meeting.id"), nullable=False, index=True),
|
|
1381
|
+
Column("sender_id", String, nullable=False),
|
|
1382
|
+
Column("recipient_id", String),
|
|
1383
|
+
Column("body", Text, nullable=False),
|
|
1384
|
+
Column("created_at", String, nullable=False),
|
|
1385
|
+
)
|
|
1386
|
+
|
|
1387
|
+
|
|
1388
|
+
def _apply_meeting_extras(conn) -> None:
|
|
1389
|
+
"""RSVPs, ringing, reminders, separate publisher/subscriber SFU sessions, media flags, usage."""
|
|
1390
|
+
meeting_ring.create(conn, checkfirst=True)
|
|
1391
|
+
inspector = inspect(conn)
|
|
1392
|
+
additions = {
|
|
1393
|
+
"meeting": {"reminded_at": "VARCHAR"},
|
|
1394
|
+
"meeting_invitee": {"response": "VARCHAR NOT NULL DEFAULT 'needsAction'"},
|
|
1395
|
+
"meeting_participant": {
|
|
1396
|
+
"sfu_sub_session_id": "VARCHAR",
|
|
1397
|
+
"audio_on": "BOOLEAN NOT NULL DEFAULT FALSE",
|
|
1398
|
+
"video_on": "BOOLEAN NOT NULL DEFAULT FALSE",
|
|
1399
|
+
"screen_on": "BOOLEAN NOT NULL DEFAULT FALSE",
|
|
1400
|
+
"bytes_in": "BIGINT NOT NULL DEFAULT 0",
|
|
1401
|
+
"bytes_out": "BIGINT NOT NULL DEFAULT 0",
|
|
1402
|
+
},
|
|
1403
|
+
}
|
|
1404
|
+
for table_name, columns in additions.items():
|
|
1405
|
+
existing = {column["name"] for column in inspector.get_columns(table_name)}
|
|
1406
|
+
for name, sql_type in columns.items():
|
|
1407
|
+
if name not in existing:
|
|
1408
|
+
conn.exec_driver_sql(f"ALTER TABLE {table_name} ADD COLUMN {name} {sql_type}")
|
|
1409
|
+
|
|
1410
|
+
|
|
1411
|
+
def _apply_meeting_devices(conn) -> None:
|
|
1412
|
+
"""Per-device seats: a person may hold a main seat plus companion seats (phone as mic, etc.)."""
|
|
1413
|
+
existing = {column["name"] for column in inspect(conn).get_columns("meeting_participant")}
|
|
1414
|
+
for name in ("device_id", "device_of", "device_label"):
|
|
1415
|
+
if name not in existing:
|
|
1416
|
+
conn.exec_driver_sql(f"ALTER TABLE meeting_participant ADD COLUMN {name} VARCHAR")
|
|
1417
|
+
|
|
1418
|
+
|
|
1419
|
+
def _apply_signup_provisioning(conn) -> None:
|
|
1420
|
+
"""Add the worker lease fields to an existing sign-up outbox."""
|
|
1421
|
+
existing = {column["name"] for column in inspect(conn).get_columns("signup_eligibility")}
|
|
1422
|
+
additions = {
|
|
1423
|
+
"attempts": "INTEGER NOT NULL DEFAULT 0",
|
|
1424
|
+
"provisioned_at": "VARCHAR",
|
|
1425
|
+
"last_error": "VARCHAR",
|
|
1426
|
+
}
|
|
1427
|
+
for name, sql_type in additions.items():
|
|
1428
|
+
if name not in existing:
|
|
1429
|
+
conn.exec_driver_sql(
|
|
1430
|
+
f"ALTER TABLE signup_eligibility ADD COLUMN {name} {sql_type}"
|
|
1431
|
+
)
|
|
1432
|
+
|
|
1433
|
+
|
|
1434
|
+
def _apply_team_hierarchy(conn) -> None:
|
|
1435
|
+
"""Departments (team.parent_team_id), team.visibility and team_member.role.
|
|
1436
|
+
|
|
1437
|
+
``private`` becomes ``visibility``: the two-state boolean could not express the
|
|
1438
|
+
'restricted' middle ground (parent-team members may see and join). The old column
|
|
1439
|
+
is left in place because SQLite cannot drop columns in place and nothing reads it
|
|
1440
|
+
once the data has been copied; a fresh database never gets it.
|
|
1441
|
+
"""
|
|
1442
|
+
columns = {column["name"] for column in inspect(conn).get_columns("team")}
|
|
1443
|
+
if "parent_team_id" not in columns:
|
|
1444
|
+
conn.exec_driver_sql("ALTER TABLE team ADD COLUMN parent_team_id VARCHAR")
|
|
1445
|
+
# DDL must come before the data copy below: Postgres refuses to run DDL on a table
|
|
1446
|
+
# that has pending trigger events, and the copy fires the change_event triggers.
|
|
1447
|
+
conn.exec_driver_sql(
|
|
1448
|
+
"CREATE INDEX IF NOT EXISTS ix_team_parent_team_id ON team (parent_team_id)")
|
|
1449
|
+
copy_private = False
|
|
1450
|
+
if "visibility" not in columns:
|
|
1451
|
+
conn.exec_driver_sql(
|
|
1452
|
+
"ALTER TABLE team ADD COLUMN visibility VARCHAR NOT NULL DEFAULT 'public'")
|
|
1453
|
+
copy_private = "private" in columns
|
|
1454
|
+
member_columns = {column["name"] for column in inspect(conn).get_columns("team_member")}
|
|
1455
|
+
if "role" not in member_columns:
|
|
1456
|
+
conn.exec_driver_sql(
|
|
1457
|
+
"ALTER TABLE team_member ADD COLUMN role VARCHAR NOT NULL DEFAULT 'member'")
|
|
1458
|
+
if copy_private:
|
|
1459
|
+
# Last statement on purpose. Postgres types `private` BOOLEAN so an integer
|
|
1460
|
+
# literal is a type error there; SQLite stores 0/1 and accepts either. This
|
|
1461
|
+
# branch only runs when upgrading a database created before `visibility`
|
|
1462
|
+
# existed, which is why a fresh-Postgres test never exercised it.
|
|
1463
|
+
truthy = "TRUE" if conn.dialect.name == "postgresql" else "1"
|
|
1464
|
+
conn.exec_driver_sql(
|
|
1465
|
+
f"UPDATE team SET visibility = 'private' WHERE private = {truthy}")
|
|
1466
|
+
|
|
1467
|
+
|
|
1468
|
+
_MIGRATIONS = {
|
|
1469
|
+
1: lambda _conn: None,
|
|
1470
|
+
2: _apply_token_hash_uniqueness,
|
|
1471
|
+
3: lambda conn: project_team.create(conn, checkfirst=True),
|
|
1472
|
+
4: lambda conn: attachment.create(conn, checkfirst=True),
|
|
1473
|
+
5: lambda conn: (
|
|
1474
|
+
password_credential.create(conn, checkfirst=True),
|
|
1475
|
+
browser_session.create(conn, checkfirst=True),
|
|
1476
|
+
),
|
|
1477
|
+
6: _apply_identity_memberships,
|
|
1478
|
+
7: lambda conn: membership_invitation.create(conn, checkfirst=True),
|
|
1479
|
+
8: lambda conn: issue_activity.create(conn, checkfirst=True),
|
|
1480
|
+
9: lambda conn: project_update.create(conn, checkfirst=True),
|
|
1481
|
+
10: lambda conn: inbox_notification.create(conn, checkfirst=True),
|
|
1482
|
+
11: lambda conn: (
|
|
1483
|
+
initiative.create(conn, checkfirst=True),
|
|
1484
|
+
initiative_project.create(conn, checkfirst=True),
|
|
1485
|
+
),
|
|
1486
|
+
12: _apply_team_cadence,
|
|
1487
|
+
13: lambda conn: user_preference.create(conn, checkfirst=True),
|
|
1488
|
+
14: lambda conn: (
|
|
1489
|
+
oauth_client.create(conn, checkfirst=True),
|
|
1490
|
+
oauth_authorization.create(conn, checkfirst=True),
|
|
1491
|
+
oauth_access_token.create(conn, checkfirst=True),
|
|
1492
|
+
oauth_refresh_token.create(conn, checkfirst=True),
|
|
1493
|
+
),
|
|
1494
|
+
15: _apply_oauth_security,
|
|
1495
|
+
16: _apply_api_key_access,
|
|
1496
|
+
17: _apply_api_key_lifecycle,
|
|
1497
|
+
18: lambda conn: change_event.create(conn, checkfirst=True),
|
|
1498
|
+
19: _apply_realtime_capture,
|
|
1499
|
+
20: lambda conn: model_provider.create(conn, checkfirst=True),
|
|
1500
|
+
21: lambda conn: (
|
|
1501
|
+
agent_conversation.create(conn, checkfirst=True),
|
|
1502
|
+
agent_message.create(conn, checkfirst=True),
|
|
1503
|
+
),
|
|
1504
|
+
22: _apply_agent_conversation_title,
|
|
1505
|
+
23: lambda conn: (
|
|
1506
|
+
connector_account.create(conn, checkfirst=True),
|
|
1507
|
+
connector_action.create(conn, checkfirst=True),
|
|
1508
|
+
),
|
|
1509
|
+
24: lambda conn: rate_limit_event.create(conn, checkfirst=True),
|
|
1510
|
+
25: lambda conn: oauth_state_nonce.create(conn, checkfirst=True),
|
|
1511
|
+
26: _apply_team_cycle_automations,
|
|
1512
|
+
27: _apply_team_auto_add_backlog,
|
|
1513
|
+
28: _apply_change_event_org_seq_index,
|
|
1514
|
+
29: _apply_issue_scale_indexes,
|
|
1515
|
+
30: _apply_issue_link_capture,
|
|
1516
|
+
31: _apply_realtime_v2_capture,
|
|
1517
|
+
32: lambda conn: (
|
|
1518
|
+
meeting.create(conn, checkfirst=True),
|
|
1519
|
+
meeting_invitee.create(conn, checkfirst=True),
|
|
1520
|
+
meeting_participant.create(conn, checkfirst=True),
|
|
1521
|
+
meeting_chat.create(conn, checkfirst=True),
|
|
1522
|
+
),
|
|
1523
|
+
33: _apply_meeting_extras,
|
|
1524
|
+
34: lambda conn: _apply_meeting_devices(conn),
|
|
1525
|
+
35: _apply_team_hierarchy,
|
|
1526
|
+
36: lambda conn: (
|
|
1527
|
+
signup_identity.create(conn, checkfirst=True),
|
|
1528
|
+
signup_flow.create(conn, checkfirst=True),
|
|
1529
|
+
signup_session.create(conn, checkfirst=True),
|
|
1530
|
+
signup_invite.create(conn, checkfirst=True),
|
|
1531
|
+
signup_eligibility.create(conn, checkfirst=True),
|
|
1532
|
+
turnstile_use.create(conn, checkfirst=True),
|
|
1533
|
+
),
|
|
1534
|
+
37: _apply_signup_provisioning,
|
|
1535
|
+
}
|
|
1536
|
+
|
|
1537
|
+
|
|
1538
|
+
def applied_schema_revision(engine: Engine) -> int:
|
|
1539
|
+
"""Return the latest recorded revision, or zero for an unmigrated database."""
|
|
1540
|
+
if not inspect(engine).has_table(schema_revision.name):
|
|
1541
|
+
return 0
|
|
1542
|
+
with engine.connect() as conn:
|
|
1543
|
+
revisions = list(
|
|
1544
|
+
conn.execute(
|
|
1545
|
+
select(schema_revision.c.revision).order_by(schema_revision.c.revision)
|
|
1546
|
+
).scalars()
|
|
1547
|
+
)
|
|
1548
|
+
if revisions != list(range(1, len(revisions) + 1)):
|
|
1549
|
+
raise RuntimeError("Schema revision ledger is not a contiguous prefix.")
|
|
1550
|
+
return revisions[-1] if revisions else 0
|
|
1551
|
+
|
|
1552
|
+
|
|
1553
|
+
def schema_is_current(engine: Engine) -> bool:
|
|
1554
|
+
return applied_schema_revision(engine) == CURRENT_SCHEMA_REVISION
|
|
1555
|
+
|
|
1556
|
+
|
|
1557
|
+
def migrate(engine: Engine) -> None:
|
|
1558
|
+
"""Create a fresh schema and apply every outstanding ordered revision."""
|
|
1559
|
+
current = applied_schema_revision(engine)
|
|
1560
|
+
if current == 0:
|
|
1561
|
+
metadata.create_all(engine)
|
|
1562
|
+
if current > CURRENT_SCHEMA_REVISION:
|
|
1563
|
+
raise RuntimeError(
|
|
1564
|
+
f"Database revision {current} is newer than this server supports "
|
|
1565
|
+
f"({CURRENT_SCHEMA_REVISION})."
|
|
1566
|
+
)
|
|
1567
|
+
for revision in range(current + 1, CURRENT_SCHEMA_REVISION + 1):
|
|
1568
|
+
with engine.begin() as conn:
|
|
1569
|
+
_MIGRATIONS[revision](conn)
|
|
1570
|
+
conn.execute(schema_revision.insert().values(revision=revision, applied_at=now_iso()))
|
|
1571
|
+
|
|
1572
|
+
|
|
1573
|
+
def seed_states_for_team(conn, org_id: str, team_id: str) -> str:
|
|
1574
|
+
"""Insert seed workflow states; return the default (first unstarted) state id."""
|
|
1575
|
+
default_state_id = None
|
|
1576
|
+
ts = now_iso()
|
|
1577
|
+
for name, stype, pos, color in SEED_STATES:
|
|
1578
|
+
sid = new_id()
|
|
1579
|
+
conn.execute(
|
|
1580
|
+
workflow_state.insert().values(
|
|
1581
|
+
id=sid,
|
|
1582
|
+
organization_id=org_id,
|
|
1583
|
+
team_id=team_id,
|
|
1584
|
+
name=name,
|
|
1585
|
+
type=stype,
|
|
1586
|
+
color=color,
|
|
1587
|
+
position=float(pos),
|
|
1588
|
+
created_at=ts,
|
|
1589
|
+
updated_at=ts,
|
|
1590
|
+
)
|
|
1591
|
+
)
|
|
1592
|
+
if stype == "unstarted" and default_state_id is None:
|
|
1593
|
+
default_state_id = sid
|
|
1594
|
+
return default_state_id
|
|
1595
|
+
|
|
1596
|
+
|
|
1597
|
+
def app_url() -> str:
|
|
1598
|
+
"""Browser-facing application base URL for generated entity links."""
|
|
1599
|
+
return os.environ.get("HOJA_APP_URL", "http://localhost:8787").rstrip("/")
|
|
1600
|
+
|
|
1601
|
+
|
|
1602
|
+
def entity_url(org_url_key: str, kind: str, entity_key: str) -> str:
|
|
1603
|
+
return f"{app_url()}/{org_url_key}/{kind}/{entity_key}"
|
|
1604
|
+
|
|
1605
|
+
|
|
1606
|
+
def _default_org_url_key(org_name: str) -> str:
|
|
1607
|
+
key = "-".join(org_name.lower().split())
|
|
1608
|
+
if not key:
|
|
1609
|
+
raise ValueError("Organization name must produce a non-empty URL key.")
|
|
1610
|
+
return key
|
|
1611
|
+
|
|
1612
|
+
|
|
1613
|
+
def seed_tenant(
|
|
1614
|
+
engine: Engine,
|
|
1615
|
+
*,
|
|
1616
|
+
org_name: str = "Local",
|
|
1617
|
+
org_url_key: str | None = None,
|
|
1618
|
+
team_key: str = "ENG",
|
|
1619
|
+
team_name: str = "Engineering",
|
|
1620
|
+
user_name: str = "Local User",
|
|
1621
|
+
user_email: str = "you@local",
|
|
1622
|
+
token: str | None = None,
|
|
1623
|
+
demo_issues: bool = True,
|
|
1624
|
+
) -> dict:
|
|
1625
|
+
"""Create one org, one admin user, one team (seeded states), an API token.
|
|
1626
|
+
Idempotent-ish: if the org already exists, returns existing token info is NOT
|
|
1627
|
+
possible (hash is one-way), so this is intended for fresh DBs.
|
|
1628
|
+
"""
|
|
1629
|
+
token = token or gen_token()
|
|
1630
|
+
org_url_key = org_url_key or _default_org_url_key(org_name)
|
|
1631
|
+
ts = now_iso()
|
|
1632
|
+
try:
|
|
1633
|
+
with engine.begin() as conn:
|
|
1634
|
+
existing = conn.execute(
|
|
1635
|
+
select(organization.c.id).where(organization.c.url_key == org_url_key)
|
|
1636
|
+
).first()
|
|
1637
|
+
if existing:
|
|
1638
|
+
raise ValueError(
|
|
1639
|
+
f"Organization URL key '{org_url_key}' already exists; "
|
|
1640
|
+
"choose a unique --org-key."
|
|
1641
|
+
)
|
|
1642
|
+
org_id = new_id()
|
|
1643
|
+
conn.execute(
|
|
1644
|
+
organization.insert().values(
|
|
1645
|
+
id=org_id,
|
|
1646
|
+
name=org_name,
|
|
1647
|
+
url_key=org_url_key,
|
|
1648
|
+
created_at=ts,
|
|
1649
|
+
updated_at=ts,
|
|
1650
|
+
)
|
|
1651
|
+
)
|
|
1652
|
+
uid = new_id()
|
|
1653
|
+
conn.execute(
|
|
1654
|
+
user.insert().values(
|
|
1655
|
+
id=uid,
|
|
1656
|
+
organization_id=org_id,
|
|
1657
|
+
name=user_name,
|
|
1658
|
+
display_name=user_name,
|
|
1659
|
+
email=user_email,
|
|
1660
|
+
active=True,
|
|
1661
|
+
admin=True,
|
|
1662
|
+
url=entity_url(org_url_key, "profiles", uid),
|
|
1663
|
+
timezone="UTC",
|
|
1664
|
+
created_at=ts,
|
|
1665
|
+
updated_at=ts,
|
|
1666
|
+
)
|
|
1667
|
+
)
|
|
1668
|
+
if inspect(conn).has_table(global_identity.name):
|
|
1669
|
+
identity_id = new_id()
|
|
1670
|
+
conn.execute(
|
|
1671
|
+
global_identity.insert().values(
|
|
1672
|
+
id=identity_id, email=user_email.strip().lower(), created_at=ts
|
|
1673
|
+
)
|
|
1674
|
+
)
|
|
1675
|
+
conn.execute(
|
|
1676
|
+
organization_membership.insert().values(
|
|
1677
|
+
id=new_id(),
|
|
1678
|
+
identity_id=identity_id,
|
|
1679
|
+
organization_id=org_id,
|
|
1680
|
+
profile_user_id=uid,
|
|
1681
|
+
active=True,
|
|
1682
|
+
created_at=ts,
|
|
1683
|
+
)
|
|
1684
|
+
)
|
|
1685
|
+
conn.execute(
|
|
1686
|
+
api_key.insert().values(
|
|
1687
|
+
id=new_id(),
|
|
1688
|
+
token_hash=token_hash(token),
|
|
1689
|
+
label="seed",
|
|
1690
|
+
user_id=uid,
|
|
1691
|
+
organization_id=org_id,
|
|
1692
|
+
created_at=ts,
|
|
1693
|
+
)
|
|
1694
|
+
)
|
|
1695
|
+
tid = new_id()
|
|
1696
|
+
conn.execute(
|
|
1697
|
+
team.insert().values(
|
|
1698
|
+
id=tid,
|
|
1699
|
+
organization_id=org_id,
|
|
1700
|
+
key=team_key.upper(),
|
|
1701
|
+
name=team_name,
|
|
1702
|
+
description=f"{team_name} team",
|
|
1703
|
+
color="#5e6ad2",
|
|
1704
|
+
visibility="public",
|
|
1705
|
+
timezone="UTC",
|
|
1706
|
+
issue_counter=0,
|
|
1707
|
+
created_at=ts,
|
|
1708
|
+
updated_at=ts,
|
|
1709
|
+
)
|
|
1710
|
+
)
|
|
1711
|
+
conn.execute(team_member.insert().values(team_id=tid, user_id=uid, role="owner"))
|
|
1712
|
+
default_state_id = seed_states_for_team(conn, org_id, tid)
|
|
1713
|
+
conn.execute(
|
|
1714
|
+
team.update().where(team.c.id == tid).values(default_state_id=default_state_id)
|
|
1715
|
+
)
|
|
1716
|
+
|
|
1717
|
+
if demo_issues:
|
|
1718
|
+
_seed_demo_issues(
|
|
1719
|
+
conn, org_id, tid, team_key.upper(), uid, default_state_id, org_url_key
|
|
1720
|
+
)
|
|
1721
|
+
except IntegrityError as exc:
|
|
1722
|
+
if "token_hash" in str(exc).lower() or "uq_api_key_token_hash" in str(exc):
|
|
1723
|
+
raise ValueError("API token already exists; choose a different token.") from exc
|
|
1724
|
+
raise ValueError(
|
|
1725
|
+
f"Organization URL key '{org_url_key}' already exists; choose a unique --org-key."
|
|
1726
|
+
) from exc
|
|
1727
|
+
|
|
1728
|
+
return {
|
|
1729
|
+
"token": token,
|
|
1730
|
+
"org_id": org_id,
|
|
1731
|
+
"user_id": uid,
|
|
1732
|
+
"team_id": tid,
|
|
1733
|
+
"team_key": team_key.upper(),
|
|
1734
|
+
}
|
|
1735
|
+
|
|
1736
|
+
|
|
1737
|
+
def _seed_demo_issues(conn, org_id, team_id, team_key, uid, state_id, org_url_key):
|
|
1738
|
+
ts = now_iso()
|
|
1739
|
+
# bump counter to 2, create ENG-1, ENG-2
|
|
1740
|
+
samples = [
|
|
1741
|
+
("Set up local backend", "First seeded issue", 2),
|
|
1742
|
+
("Wire Hoja base_url", "Point the CLI at the local server", 1),
|
|
1743
|
+
]
|
|
1744
|
+
n = 0
|
|
1745
|
+
for title, desc, prio in samples:
|
|
1746
|
+
n += 1
|
|
1747
|
+
iid = new_id()
|
|
1748
|
+
ident = f"{team_key}-{n}"
|
|
1749
|
+
conn.execute(
|
|
1750
|
+
issue.insert().values(
|
|
1751
|
+
id=iid,
|
|
1752
|
+
organization_id=org_id,
|
|
1753
|
+
team_id=team_id,
|
|
1754
|
+
number=n,
|
|
1755
|
+
identifier=ident,
|
|
1756
|
+
title=title,
|
|
1757
|
+
description=desc,
|
|
1758
|
+
priority=prio,
|
|
1759
|
+
priority_label=PRIORITY_LABELS[prio],
|
|
1760
|
+
state_id=state_id,
|
|
1761
|
+
creator_id=uid,
|
|
1762
|
+
assignee_id=uid,
|
|
1763
|
+
url=entity_url(org_url_key, "issue", ident),
|
|
1764
|
+
branch_name=f"{uid[:8]}/{ident.lower()}-{title.lower().replace(' ', '-')[:20]}",
|
|
1765
|
+
created_at=ts,
|
|
1766
|
+
updated_at=ts,
|
|
1767
|
+
)
|
|
1768
|
+
)
|
|
1769
|
+
conn.execute(team.update().where(team.c.id == team_id).values(issue_counter=n))
|
|
1770
|
+
|
|
1771
|
+
|
|
1772
|
+
def resolve_token_identity(
|
|
1773
|
+
engine: Engine,
|
|
1774
|
+
*,
|
|
1775
|
+
organization_ref: str | None = None,
|
|
1776
|
+
user_ref: str | None = None,
|
|
1777
|
+
email: str | None = None,
|
|
1778
|
+
) -> tuple[str, str]:
|
|
1779
|
+
"""Resolve the exact (user, organization) for a newly minted token."""
|
|
1780
|
+
if user_ref and email:
|
|
1781
|
+
raise ValueError("Choose either --user or --email, not both.")
|
|
1782
|
+
shared_database = engine.dialect.name != "sqlite"
|
|
1783
|
+
if shared_database and not organization_ref:
|
|
1784
|
+
raise ValueError("--organization is required for a shared database.")
|
|
1785
|
+
if shared_database and not (user_ref or email):
|
|
1786
|
+
raise ValueError("--user or --email is required for a shared database.")
|
|
1787
|
+
|
|
1788
|
+
with engine.connect() as conn:
|
|
1789
|
+
if organization_ref:
|
|
1790
|
+
org_rows = conn.execute(
|
|
1791
|
+
select(organization.c.id)
|
|
1792
|
+
.where(
|
|
1793
|
+
(organization.c.id == organization_ref)
|
|
1794
|
+
| (organization.c.url_key == organization_ref)
|
|
1795
|
+
)
|
|
1796
|
+
.limit(2)
|
|
1797
|
+
).all()
|
|
1798
|
+
else:
|
|
1799
|
+
org_rows = conn.execute(select(organization.c.id).limit(2)).all()
|
|
1800
|
+
if not org_rows:
|
|
1801
|
+
raise ValueError("No matching organization found; run 'hoja-serve seed' first.")
|
|
1802
|
+
if len(org_rows) != 1:
|
|
1803
|
+
raise ValueError("Multiple organizations found; specify --organization.")
|
|
1804
|
+
org_id = org_rows[0][0]
|
|
1805
|
+
|
|
1806
|
+
query = select(user.c.id).where(user.c.organization_id == org_id)
|
|
1807
|
+
if user_ref:
|
|
1808
|
+
query = query.where(user.c.id == user_ref)
|
|
1809
|
+
elif email:
|
|
1810
|
+
query = query.where(user.c.email == email)
|
|
1811
|
+
user_rows = conn.execute(query.limit(2)).all()
|
|
1812
|
+
if not user_rows:
|
|
1813
|
+
raise ValueError("No matching user found in the selected organization.")
|
|
1814
|
+
if len(user_rows) != 1:
|
|
1815
|
+
raise ValueError("Multiple users found; specify --user or --email.")
|
|
1816
|
+
return user_rows[0][0], org_id
|