@open-pencil/cloud 0.17.0-beta.2 → 0.17.0-beta.4
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.
- package/README.md +1 -1
- package/dist/bin/{ai-relay.mjs → services.mjs} +8 -8
- package/dist/bin/services.mjs.map +1 -0
- package/dist/client.d.ts +236 -46
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +649 -400
- package/dist/client.js.map +1 -1
- package/dist/contract.d.ts +2 -2
- package/dist/contract.js +2 -2
- package/dist/contract2.js +680 -443
- package/dist/contract2.js.map +1 -1
- package/dist/email.d.ts +9 -1
- package/dist/email.d.ts.map +1 -1
- package/dist/email.js +99 -58
- package/dist/email.js.map +1 -1
- package/dist/index.d.ts +469 -65
- package/dist/index.d.ts.map +1 -1
- package/dist/listen.mjs +505 -787
- package/dist/listen.mjs.map +1 -1
- package/dist/runtime-node.d.mts +3920 -2524
- package/dist/runtime-node.d.mts.map +1 -1
- package/dist/runtime-node.mjs +6 -5
- package/dist/runtime-node.mjs.map +1 -1
- package/dist/server.d.ts +3586 -1897
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +5216 -4272
- package/dist/server.js.map +1 -1
- package/dist/server.mjs +5179 -3623
- package/dist/server.mjs.map +1 -1
- package/dist/server2.mjs +101 -79
- package/dist/server2.mjs.map +1 -1
- package/package.json +7 -6
- package/src/admin/api/routes.ts +59 -0
- package/src/admin/audit/service.ts +25 -6
- package/src/admin/documents/service.ts +361 -0
- package/src/admin/errors.ts +9 -1
- package/src/admin/index.ts +6 -0
- package/src/admin/operations/service.ts +7 -0
- package/src/admin/reports/service.ts +147 -0
- package/src/admin/schema.ts +6 -0
- package/src/client/api.ts +36 -21
- package/src/client/auth/navigation.ts +6 -5
- package/src/client/index.ts +7 -7
- package/src/client/portal/account.ts +25 -0
- package/src/client/portal/administration.ts +56 -0
- package/src/client/{ai.ts → services.ts} +44 -27
- package/src/contract/account.ts +58 -2
- package/src/contract/admin/index.ts +67 -3
- package/src/contract/collaboration.ts +6 -1
- package/src/contract/discovery.ts +22 -4
- package/src/contract/documents.ts +27 -0
- package/src/contract/entitlements.ts +2 -2
- package/src/contract/index.ts +63 -37
- package/src/contract/relay.ts +6 -0
- package/src/contract/reports.ts +42 -0
- package/src/contract/services.ts +233 -0
- package/src/contract/sharing.ts +3 -9
- package/src/email/render.ts +26 -0
- package/src/email/schema.ts +11 -1
- package/src/email/templates/brand.ts +1 -1
- package/src/runtime/cloudflare/environment.ts +22 -11
- package/src/runtime/cloudflare/index.ts +1 -1
- package/src/runtime/cloudflare/logging.ts +15 -0
- package/src/runtime/cloudflare/maintenance.ts +31 -0
- package/src/runtime/cloudflare/relay/index.ts +62 -14
- package/src/runtime/cloudflare/worker.ts +39 -13
- package/src/runtime/node/application.ts +5 -3
- package/src/runtime/node/bootstrap.ts +15 -4
- package/src/runtime/node/cleanup.ts +16 -3
- package/src/runtime/node/entitlements/main.ts +14 -11
- package/src/runtime/node/index.ts +1 -1
- package/src/runtime/node/migrate.ts +5 -1
- package/src/runtime/node/relay.ts +8 -2
- package/src/runtime/node/request-id.ts +13 -0
- package/src/runtime/node/server.ts +39 -15
- package/src/runtime/node/{ai-relay → services}/main.ts +6 -6
- package/src/runtime/node/{ai-relay → services}/server.ts +18 -18
- package/src/runtime/postgres/database.ts +20 -1
- package/src/runtime/posthog/errors.ts +15 -31
- package/src/runtime/s3/objects.ts +24 -1
- package/src/runtime/site/server.ts +32 -5
- package/src/server/account/actions.ts +5 -0
- package/src/server/account/deletion.ts +302 -0
- package/src/server/account/routes.ts +47 -3
- package/src/server/account/terms.ts +42 -0
- package/src/server/api/router.ts +9 -5
- package/src/server/api/types.ts +2 -0
- package/src/server/app.ts +90 -16
- package/src/server/auth/access-control.ts +63 -0
- package/src/server/auth/adapter.ts +11 -0
- package/src/server/auth/endpoints.ts +5 -1
- package/src/server/auth/factory.ts +30 -3
- package/src/server/auth/index.ts +10 -1
- package/src/server/cleanup/configured.ts +12 -5
- package/src/server/cleanup/documents.ts +165 -71
- package/src/server/cleanup/revisions.ts +16 -5
- package/src/server/cleanup/uploads.ts +8 -2
- package/src/server/cleanup/worker.ts +5 -2
- package/src/server/collaboration/relay/connection.ts +4 -3
- package/src/server/collaboration/relay/documents.ts +13 -10
- package/src/server/collaboration/relay/hub.ts +97 -25
- package/src/server/collaboration/relay/log.ts +5 -2
- package/src/server/collaboration/service.ts +27 -31
- package/src/server/config/deployment.ts +24 -4
- package/src/server/config/public.ts +5 -4
- package/src/server/config/schema.ts +82 -16
- package/src/server/db/expressions.ts +15 -1
- package/src/server/db/migrate.ts +34 -116
- package/src/server/db/migrations/001_accounts.ts +79 -0
- package/src/server/db/migrations/002_workspaces.ts +48 -0
- package/src/server/db/migrations/003_storage.ts +44 -0
- package/src/server/db/migrations/004_documents.ts +193 -0
- package/src/server/db/migrations/005_sharing.ts +95 -0
- package/src/server/db/migrations/006_operations.ts +110 -0
- package/src/server/db/migrations/007_trash.ts +33 -0
- package/src/server/db/migrations/008_terms.ts +18 -0
- package/src/server/db/migrations/schema.ts +71 -0
- package/src/server/db/tables.ts +52 -3
- package/src/server/documents/access.ts +66 -55
- package/src/server/documents/errors.ts +7 -0
- package/src/server/documents/images.ts +2 -1
- package/src/server/documents/limits.ts +9 -9
- package/src/server/documents/purge.ts +75 -0
- package/src/server/documents/repository.ts +2 -0
- package/src/server/documents/routes.ts +26 -0
- package/src/server/documents/service.ts +18 -4
- package/src/server/documents/summary.ts +1 -0
- package/src/server/documents/trash.ts +203 -0
- package/src/server/documents/versions/checkpoint.ts +2 -1
- package/src/server/documents/versions/history.ts +11 -7
- package/src/server/documents/versions/service.ts +13 -4
- package/src/server/errors/index.ts +59 -16
- package/src/server/errors/posthog.ts +80 -0
- package/src/server/index.ts +46 -17
- package/src/server/internal/index.ts +3 -3
- package/src/server/internal/routes.ts +7 -10
- package/src/server/internal/schema.ts +1 -1
- package/src/server/internal/service.ts +17 -23
- package/src/server/invitations/budget.ts +66 -0
- package/src/server/invitations/index.ts +1 -0
- package/src/server/logging.ts +76 -0
- package/src/server/pictures/service.ts +2 -1
- package/src/server/policy/database.ts +39 -25
- package/src/server/policy/index.ts +2 -1
- package/src/server/policy/keys.ts +5 -2
- package/src/server/policy/policy.ts +1 -0
- package/src/server/policy/schema.ts +2 -2
- package/src/server/policy/service.ts +12 -9
- package/src/server/policy/static.ts +5 -5
- package/src/server/policy/workspace.ts +41 -0
- package/src/server/quota/reconcile.ts +2 -0
- package/src/server/quota/storage.ts +69 -31
- package/src/server/rate-limit/cleanup.ts +17 -5
- package/src/server/rate-limit/middleware.ts +2 -2
- package/src/server/reports/index.ts +8 -0
- package/src/server/reports/routes.ts +37 -0
- package/src/server/reports/service.ts +123 -0
- package/src/server/services/errors.ts +7 -0
- package/src/server/services/index.ts +8 -0
- package/src/server/{ai → services}/keys.ts +11 -11
- package/src/server/services/routes.ts +47 -0
- package/src/server/{ai/service.ts → services/tokens.ts} +23 -20
- package/src/server/sharing/capabilities/service.ts +68 -22
- package/src/server/sharing/grants/service.ts +11 -18
- package/src/server/sharing/http.ts +4 -0
- package/src/server/sharing/invitations/service.ts +21 -11
- package/src/server/sharing/policy.ts +9 -14
- package/src/server/sharing/routes.ts +0 -16
- package/src/server/sharing/service.ts +1 -17
- package/src/server/validation.ts +1 -0
- package/src/server/workspaces/index.ts +1 -0
- package/src/server/workspaces/members.ts +89 -38
- package/src/server/workspaces/repository.ts +2 -1
- package/src/server/workspaces/routes.ts +4 -0
- package/src/{ai-relay → services}/app.ts +48 -44
- package/src/{ai-relay → services}/config.ts +25 -19
- package/src/services/db/index.ts +10 -0
- package/src/services/db/migrate.ts +37 -0
- package/src/{ai-relay/db/migrations/001_usage.ts → services/db/migrations/001_baseline.ts} +18 -7
- package/src/services/db/schema.ts +22 -0
- package/src/services/index.ts +18 -0
- package/src/{ai-relay → services}/metering.ts +2 -2
- package/src/{ai-relay → services}/usage.ts +43 -19
- package/dist/bin/ai-relay.mjs.map +0 -1
- package/src/ai-relay/db/index.ts +0 -10
- package/src/ai-relay/db/migrate.ts +0 -32
- package/src/ai-relay/db/schema.ts +0 -20
- package/src/ai-relay/index.ts +0 -18
- package/src/contract/ai.ts +0 -150
- package/src/server/ai/errors.ts +0 -7
- package/src/server/ai/index.ts +0 -4
- package/src/server/ai/routes.ts +0 -43
- package/src/server/auth/workspace-access.ts +0 -23
- package/src/server/db/migrations/001_foundation.ts +0 -164
- package/src/server/db/migrations/002_upload_cleanup_claims.ts +0 -34
- package/src/server/db/migrations/003_document_cleanup_claims.ts +0 -23
- package/src/server/db/migrations/004_document_sharing.ts +0 -82
- package/src/server/db/migrations/005_document_collaboration_epoch.ts +0 -13
- package/src/server/db/migrations/006_invitation_continuation.ts +0 -27
- package/src/server/db/migrations/007_collaboration_state.ts +0 -25
- package/src/server/db/migrations/008_storage_reservations.ts +0 -37
- package/src/server/db/migrations/009_workspace_entitlements.ts +0 -22
- package/src/server/db/migrations/010_upload_finalization.ts +0 -22
- package/src/server/db/migrations/011_upload_finalization_lease.ts +0 -12
- package/src/server/db/migrations/012_transactional_email.ts +0 -48
- package/src/server/db/migrations/013_admin_enrollment.ts +0 -54
- package/src/server/db/migrations/014_enrollment_email_kinds.ts +0 -30
- package/src/server/db/migrations/015_cloud_rate_limit.ts +0 -22
- package/src/server/db/migrations/016_auth_schema.ts +0 -16
- package/src/server/db/migrations/017_authentication_email_kinds.ts +0 -32
- package/src/server/db/migrations/018_authentication_mfa.ts +0 -78
- package/src/server/db/migrations/019_better_auth_account_issuer.ts +0 -42
- package/src/server/db/migrations/020_workspace_invitations.ts +0 -57
- package/src/server/db/migrations/021_document_versions.ts +0 -66
- package/src/server/db/migrations/022_entitlement_external_version.ts +0 -17
- package/src/server/db/migrations/023_document_images.ts +0 -61
- package/src/server/db/migrations/024_account_activity.ts +0 -66
- package/src/server/db/migrations/025_table_names.ts +0 -58
- package/src/server/db/migrations/026_user_picture.ts +0 -30
- package/src/server/db/migrations/027_document_kind.ts +0 -19
- package/src/server/db/migrations/028_collaboration_updates.ts +0 -84
- package/src/server/db/migrations/helpers.ts +0 -78
- /package/dist/bin/{ai-relay.d.mts → services.d.mts} +0 -0
package/dist/listen.mjs
CHANGED
|
@@ -19,48 +19,55 @@ var __exportAll = (all, no_symbols) => {
|
|
|
19
19
|
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
20
20
|
return target;
|
|
21
21
|
};
|
|
22
|
-
/** The `aud` of every token Cloud signs for
|
|
23
|
-
const
|
|
24
|
-
/** Where Cloud publishes the public keys
|
|
25
|
-
const
|
|
22
|
+
/** The `aud` of every token Cloud signs for its services. */
|
|
23
|
+
const CLOUD_SERVICES_TOKEN_AUDIENCE = "openpencil-services";
|
|
24
|
+
/** Where Cloud publishes the public keys a service verifies its tokens with. */
|
|
25
|
+
const CLOUD_SERVICES_JWKS_PATH = "/.well-known/jwks.json";
|
|
26
26
|
/**
|
|
27
27
|
* Where a service answers, below the discovered `endpoint`. Model requests follow the OpenAI chat
|
|
28
28
|
* completions API, so an OpenAI-compatible client takes `${endpoint}${base}` as its base URL.
|
|
29
29
|
*/
|
|
30
|
-
const
|
|
30
|
+
const CLOUD_SERVICES_API_PATHS = {
|
|
31
31
|
base: "/v1",
|
|
32
32
|
chatCompletions: "/v1/chat/completions",
|
|
33
33
|
catalog: "/v1/catalog",
|
|
34
34
|
wallet: "/v1/wallet",
|
|
35
|
-
usage: "/v1/usage"
|
|
35
|
+
usage: "/v1/usage",
|
|
36
|
+
search: "/v1/search"
|
|
36
37
|
};
|
|
37
38
|
const urlSchema$1 = v.pipe(v.string(), v.url());
|
|
38
39
|
const creditsSchema = v.pipe(v.number(), v.integer(), v.minValue(0));
|
|
39
|
-
const
|
|
40
|
+
const cloudServicesDiscoverySchema = v.object({
|
|
40
41
|
endpoint: urlSchema$1,
|
|
41
42
|
contractVersion: v.literal("1")
|
|
42
43
|
});
|
|
43
44
|
const cloudBillingDiscoverySchema = v.object({ portalURL: urlSchema$1 });
|
|
44
|
-
const
|
|
45
|
-
function
|
|
46
|
-
return v.parse(
|
|
45
|
+
const createServicesTokenSchema = v.object({ workspaceId: v.pipe(v.string(), v.uuid()) });
|
|
46
|
+
function parseCreateServicesToken(input) {
|
|
47
|
+
return v.parse(createServicesTokenSchema, input);
|
|
47
48
|
}
|
|
48
49
|
v.object({
|
|
49
50
|
token: v.pipe(v.string(), v.minLength(1)),
|
|
50
51
|
expiresAt: v.pipe(v.string(), v.isoTimestamp())
|
|
51
52
|
});
|
|
52
|
-
/** What
|
|
53
|
-
const
|
|
53
|
+
/** What a service may rely on once a token's signature, issuer, audience, and expiry check. */
|
|
54
|
+
const servicesTokenClaimsSchema = v.object({
|
|
54
55
|
iss: urlSchema$1,
|
|
55
|
-
aud: v.literal(
|
|
56
|
+
aud: v.literal(CLOUD_SERVICES_TOKEN_AUDIENCE),
|
|
56
57
|
sub: v.pipe(v.string(), v.minLength(1)),
|
|
57
58
|
workspaceId: v.pipe(v.string(), v.uuid()),
|
|
58
|
-
/**
|
|
59
|
+
/**
|
|
60
|
+
* The account whose credits the request spends: the workspace's owner, whose allowance every
|
|
61
|
+
* workspace they own shares. Absent from tokens a Cloud from before account plans issued, which
|
|
62
|
+
* a relay takes as the workspace's own allowance, as such a Cloud meant.
|
|
63
|
+
*/
|
|
64
|
+
accountId: v.optional(v.pipe(v.string(), v.minLength(1))),
|
|
65
|
+
/** Credits the owner's plan grants each month; `null` when Cloud sets no allowance. */
|
|
59
66
|
monthlyCreditAllowance: v.nullable(creditsSchema),
|
|
60
67
|
iat: v.pipe(v.number(), v.integer()),
|
|
61
68
|
exp: v.pipe(v.number(), v.integer())
|
|
62
69
|
});
|
|
63
|
-
const
|
|
70
|
+
const chatModelSchema = v.object({
|
|
64
71
|
id: v.pipe(v.string(), v.minLength(1)),
|
|
65
72
|
label: v.pipe(v.string(), v.minLength(1)),
|
|
66
73
|
/** Credits per million input and output tokens. */
|
|
@@ -73,11 +80,14 @@ const aiModelSchema = v.object({
|
|
|
73
80
|
tools: v.boolean()
|
|
74
81
|
})
|
|
75
82
|
});
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
83
|
+
/** What one web search costs; a service without search leaves it out of its catalog. */
|
|
84
|
+
const searchPricingSchema = v.object({ creditsPerRequest: creditsSchema });
|
|
85
|
+
const serviceCatalogSchema = v.object({
|
|
86
|
+
models: v.array(chatModelSchema),
|
|
87
|
+
defaultModelID: v.pipe(v.string(), v.minLength(1)),
|
|
88
|
+
search: v.optional(searchPricingSchema)
|
|
79
89
|
});
|
|
80
|
-
const
|
|
90
|
+
const creditBucketSchema = v.object({
|
|
81
91
|
kind: v.picklist([
|
|
82
92
|
"allowance",
|
|
83
93
|
"purchased",
|
|
@@ -90,9 +100,9 @@ v.object({
|
|
|
90
100
|
/** Credits left to spend; `null` when the service sets no limit. */
|
|
91
101
|
balance: v.nullable(creditsSchema),
|
|
92
102
|
/** Spent first to last. */
|
|
93
|
-
buckets: v.array(
|
|
103
|
+
buckets: v.array(creditBucketSchema)
|
|
94
104
|
});
|
|
95
|
-
const
|
|
105
|
+
const usageEntrySchema = v.object({
|
|
96
106
|
id: v.pipe(v.string(), v.minLength(1)),
|
|
97
107
|
createdAt: v.pipe(v.string(), v.isoTimestamp()),
|
|
98
108
|
kind: v.picklist([
|
|
@@ -104,31 +114,49 @@ const aiUsageEntrySchema = v.object({
|
|
|
104
114
|
userId: v.pipe(v.string(), v.minLength(1)),
|
|
105
115
|
credits: creditsSchema
|
|
106
116
|
});
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
117
|
+
v.object({
|
|
118
|
+
query: v.pipe(v.string(), v.trim(), v.minLength(1), v.maxLength(1e3)),
|
|
119
|
+
numResults: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(10)), 5)
|
|
120
|
+
});
|
|
121
|
+
/** One page a search found, with the part of its text that answers the query. */
|
|
122
|
+
const searchResultSchema = v.object({
|
|
123
|
+
url: v.pipe(urlSchema$1, v.regex(/^https?:\/\//i, "A search result links to an HTTP(S) page")),
|
|
124
|
+
title: v.string(),
|
|
125
|
+
publishedAt: v.nullable(v.pipe(v.string(), v.isoTimestamp())),
|
|
126
|
+
author: v.nullable(v.string()),
|
|
127
|
+
text: v.string()
|
|
128
|
+
});
|
|
129
|
+
v.object({ results: v.array(searchResultSchema) });
|
|
130
|
+
const usageQuerySchema = v.object({ cursor: v.optional(v.pipe(v.string(), v.minLength(1))) });
|
|
131
|
+
function parseUsageQuery(input) {
|
|
132
|
+
return v.parse(usageQuerySchema, input);
|
|
110
133
|
}
|
|
111
134
|
v.object({
|
|
112
|
-
entries: v.array(
|
|
135
|
+
entries: v.array(usageEntrySchema),
|
|
113
136
|
next: v.nullable(v.string())
|
|
114
137
|
});
|
|
115
|
-
/** Errors
|
|
116
|
-
const
|
|
138
|
+
/** Errors a service answers with, beside the upstream provider's own. */
|
|
139
|
+
const serviceErrorCodeSchema = v.picklist([
|
|
140
|
+
"invalid_request",
|
|
117
141
|
"unauthorized",
|
|
118
142
|
"insufficient_credits",
|
|
119
143
|
"model_not_allowed",
|
|
144
|
+
"capability_unavailable",
|
|
120
145
|
"rate_limited"
|
|
121
146
|
]);
|
|
122
147
|
v.object({ error: v.object({
|
|
123
|
-
code:
|
|
148
|
+
code: serviceErrorCodeSchema,
|
|
124
149
|
message: v.optional(v.string()),
|
|
125
150
|
/** With `insufficient_credits`: when the workspace's allowance renews, if the service knows. */
|
|
126
151
|
resetsAt: v.optional(v.pipe(v.string(), v.isoTimestamp()))
|
|
127
152
|
}) });
|
|
128
|
-
const
|
|
153
|
+
const SERVICE_ERROR_STATUS = {
|
|
154
|
+
invalid_request: 400,
|
|
129
155
|
unauthorized: 401,
|
|
130
156
|
insufficient_credits: 402,
|
|
131
157
|
model_not_allowed: 403,
|
|
158
|
+
/** A capability, such as search, the service does not offer. */
|
|
159
|
+
capability_unavailable: 404,
|
|
132
160
|
rate_limited: 429
|
|
133
161
|
};
|
|
134
162
|
//#endregion
|
|
@@ -137,6 +165,13 @@ const CLOUD_PROTOCOL_MAX_UPLOAD_BYTES = 1073741824;
|
|
|
137
165
|
const checksumSchema = v.pipe(v.string(), v.regex(/^[A-Za-z0-9+/]{43}=$/));
|
|
138
166
|
/** The most images one revision, or one request for image uploads, may name. */
|
|
139
167
|
const CLOUD_MAX_DOCUMENT_IMAGES = 1e4;
|
|
168
|
+
/**
|
|
169
|
+
* The most a Cloud API request body may hold; a server refuses anything past it before reading
|
|
170
|
+
* it. Files go to the object store, never through the API, so only lists grow large: a save's
|
|
171
|
+
* images fit with room to spare, and a client commits image uploads, whose part lists can run
|
|
172
|
+
* long, in batches under half of it.
|
|
173
|
+
*/
|
|
174
|
+
const CLOUD_API_MAX_BODY_BYTES = 8388608;
|
|
140
175
|
const imageNameSchema = v.pipe(v.string(), v.regex(/^[A-Za-z0-9._-]{1,128}$/));
|
|
141
176
|
const objectDownloadSchema = v.object({
|
|
142
177
|
url: v.pipe(v.string(), v.url()),
|
|
@@ -189,6 +224,22 @@ v.object({
|
|
|
189
224
|
sharedBy: v.nullable(v.object({ name: v.string() })),
|
|
190
225
|
sharedAt: v.string()
|
|
191
226
|
});
|
|
227
|
+
/**
|
|
228
|
+
* A document in Trash the person may restore: who deleted it and when, and when it goes for good.
|
|
229
|
+
* `canPurge` says whether they may also delete it for good before then.
|
|
230
|
+
*/
|
|
231
|
+
const trashedDocumentSchema = v.object({
|
|
232
|
+
id: v.pipe(v.string(), v.uuid()),
|
|
233
|
+
workspaceId: v.pipe(v.string(), v.uuid()),
|
|
234
|
+
workspaceName: v.string(),
|
|
235
|
+
name: v.string(),
|
|
236
|
+
kind: v.optional(documentKindSchema, "design"),
|
|
237
|
+
deletedAt: v.string(),
|
|
238
|
+
deletedBy: v.nullable(v.object({ name: v.string() })),
|
|
239
|
+
purgeAt: v.string(),
|
|
240
|
+
canPurge: v.boolean()
|
|
241
|
+
});
|
|
242
|
+
v.object({ documents: v.array(trashedDocumentSchema) });
|
|
192
243
|
const documentDownloadSchema = v.object({
|
|
193
244
|
document: documentSummarySchema,
|
|
194
245
|
revisionId: v.pipe(v.string(), v.uuid()),
|
|
@@ -387,6 +438,14 @@ const RELAY_CLOSE = {
|
|
|
387
438
|
/** The document was restored to a version, so its room started over under a new ID. */
|
|
388
439
|
restored: 4410
|
|
389
440
|
};
|
|
441
|
+
/**
|
|
442
|
+
* The text a peer sends to learn whether the relay still hears it, and the relay's answer. Relay
|
|
443
|
+
* frames are binary, so neither is taken for one, and a Durable Object answers without waking.
|
|
444
|
+
*/
|
|
445
|
+
const RELAY_HEARTBEAT = {
|
|
446
|
+
ping: "ping",
|
|
447
|
+
pong: "pong"
|
|
448
|
+
};
|
|
390
449
|
const CLIENT_AUTH = 0;
|
|
391
450
|
const CLIENT_MESSAGE = 1;
|
|
392
451
|
const CLIENT_FRAGMENT = 2;
|
|
@@ -599,6 +658,20 @@ const cloudEditorVersionSchema = v.pipe(v.string(), v.regex(/^\d+\.\d+\.\d+$/));
|
|
|
599
658
|
* and an editor older than the minimum keeps them hidden.
|
|
600
659
|
*/
|
|
601
660
|
const cloudListingSchema = v.object({ minimumEditorVersion: cloudEditorVersionSchema });
|
|
661
|
+
const legalURLSchema = v.pipe(v.string(), v.url());
|
|
662
|
+
/**
|
|
663
|
+
* The operator's legal links, which clients show at sign-up and in account pages. A server that
|
|
664
|
+
* names none shows none; `requireAcceptance` means an account accepts `termsVersion` before it uses
|
|
665
|
+
* the server.
|
|
666
|
+
*/
|
|
667
|
+
const cloudLegalDiscoverySchema = v.object({
|
|
668
|
+
termsURL: v.optional(legalURLSchema),
|
|
669
|
+
privacyURL: v.optional(legalURLSchema),
|
|
670
|
+
termsVersion: v.optional(v.string()),
|
|
671
|
+
requireAcceptance: v.boolean(),
|
|
672
|
+
abuseEmail: v.optional(v.pipe(v.string(), v.email())),
|
|
673
|
+
minimumAge: v.optional(v.pipe(v.number(), v.integer()))
|
|
674
|
+
});
|
|
602
675
|
const cloudDiscoverySchema = v.object({
|
|
603
676
|
protocolVersion: v.literal("1"),
|
|
604
677
|
deployment: cloudDeploymentSchema,
|
|
@@ -609,10 +682,11 @@ const cloudDiscoverySchema = v.object({
|
|
|
609
682
|
authentication: cloudAuthenticationSchema,
|
|
610
683
|
capabilities: cloudCapabilitiesSchema,
|
|
611
684
|
listing: v.optional(cloudListingSchema),
|
|
612
|
-
/** The
|
|
613
|
-
|
|
685
|
+
/** The services editors reach with Cloud-signed tokens; absent when the deployment has none. */
|
|
686
|
+
services: v.optional(cloudServicesDiscoverySchema),
|
|
614
687
|
/** Where people manage their plan and buy credits; absent when the deployment does not bill. */
|
|
615
|
-
billing: v.optional(cloudBillingDiscoverySchema)
|
|
688
|
+
billing: v.optional(cloudBillingDiscoverySchema),
|
|
689
|
+
legal: v.optional(cloudLegalDiscoverySchema)
|
|
616
690
|
});
|
|
617
691
|
function parseCloudDiscovery(input) {
|
|
618
692
|
return v.parse(cloudDiscoverySchema, input);
|
|
@@ -679,6 +753,7 @@ const workspaceMemberParams = validatedParams(v.object({
|
|
|
679
753
|
workspaceId: uuidParamSchema,
|
|
680
754
|
userId: textParamSchema
|
|
681
755
|
}));
|
|
756
|
+
const accountParams = validatedParams(v.object({ userId: uuidParamSchema }));
|
|
682
757
|
const shareParams = validatedParams(v.object({ shareId: uuidParamSchema }));
|
|
683
758
|
const invitationParams = validatedParams(v.object({ invitationId: uuidParamSchema }));
|
|
684
759
|
const invitationContinuationParams = validatedParams(v.object({ continuationId: textParamSchema }));
|
|
@@ -710,7 +785,7 @@ const staticEntitlementsSchema = v.object({
|
|
|
710
785
|
enabled: v.optional(v.boolean()),
|
|
711
786
|
maximumParticipants: v.optional(positiveIntegerSchema$1)
|
|
712
787
|
}), {}),
|
|
713
|
-
|
|
788
|
+
services: v.optional(v.object({
|
|
714
789
|
enabled: v.optional(v.boolean()),
|
|
715
790
|
monthlyCreditAllowance: v.optional(nonNegativeIntegerSchema)
|
|
716
791
|
}), {})
|
|
@@ -731,7 +806,7 @@ const staticEntitlementsTOMLSchema = v.strictObject({
|
|
|
731
806
|
enabled: v.optional(v.boolean()),
|
|
732
807
|
maximum_participants: v.optional(positiveIntegerSchema$1)
|
|
733
808
|
})),
|
|
734
|
-
|
|
809
|
+
services: v.optional(v.strictObject({
|
|
735
810
|
enabled: v.optional(v.boolean()),
|
|
736
811
|
monthly_credit_allowance: v.optional(nonNegativeIntegerSchema)
|
|
737
812
|
}))
|
|
@@ -751,6 +826,11 @@ const websocketURLSchema = v.pipe(v.string(), v.url(), v.check((value) => {
|
|
|
751
826
|
return protocol === "ws:" || protocol === "wss:";
|
|
752
827
|
}, "URL must use WS or WSS"));
|
|
753
828
|
const optionalTextSchema = v.optional(v.pipe(v.string(), v.trim(), v.minLength(1)));
|
|
829
|
+
/**
|
|
830
|
+
* A prefix deleted files move under: one lowercase path segment and a slash, which no object key
|
|
831
|
+
* the server makes starts with (they start with `documents/`).
|
|
832
|
+
*/
|
|
833
|
+
const deletedPrefixSchema = v.pipe(v.string(), v.regex(/^[a-z0-9][a-z0-9-]*\/$/), v.check((prefix) => prefix !== "documents/", "deleted_prefix cannot be documents/"));
|
|
754
834
|
const rawCloudServerConfigSchema = v.object({
|
|
755
835
|
deployment: v.picklist(["official", "self-hosted"]),
|
|
756
836
|
publicURL: httpURLSchema,
|
|
@@ -814,6 +894,8 @@ const rawCloudServerConfigSchema = v.object({
|
|
|
814
894
|
s3ChecksumVerification: v.optional(v.picklist(["native", "metadata"]), "native"),
|
|
815
895
|
s3ServerSideEncryption: v.optional(v.picklist(["AES256", "aws:kms"])),
|
|
816
896
|
s3KmsKeyId: optionalTextSchema,
|
|
897
|
+
/** Where a deleted file is kept until the bucket's lifecycle rule expires it; see `deleted_prefix`. */
|
|
898
|
+
s3DeletedPrefix: v.optional(deletedPrefixSchema),
|
|
817
899
|
smtpHost: optionalTextSchema,
|
|
818
900
|
smtpPort: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(65535))),
|
|
819
901
|
smtpSecure: v.optional(v.boolean()),
|
|
@@ -834,14 +916,32 @@ const rawCloudServerConfigSchema = v.object({
|
|
|
834
916
|
cleanupIntervalMs: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1e3)), 6e4),
|
|
835
917
|
cleanupLeaseDurationMs: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1e3)), 3e5),
|
|
836
918
|
documentRetentionMs: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 2592e6),
|
|
837
|
-
/**
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
919
|
+
/**
|
|
920
|
+
* How long a document deleted forever keeps its files and rows, out of everyone's sight, so a
|
|
921
|
+
* server administrator can still bring it back; 0 destroys it at once.
|
|
922
|
+
*/
|
|
923
|
+
documentPurgeGraceMs: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 6048e5),
|
|
924
|
+
/** The OpenPencil services editors reach with tokens this server signs; see `src/contract/services.ts`. */
|
|
925
|
+
servicesEndpoint: v.optional(httpURLSchema),
|
|
926
|
+
/** The PKCS #8 PEM of the P-256 key that signs service tokens. */
|
|
927
|
+
servicesSigningKey: optionalTextSchema,
|
|
928
|
+
servicesTokenLifetimeSeconds: v.optional(v.pipe(v.number(), v.integer(), v.minValue(60), v.maxValue(3600)), 600),
|
|
842
929
|
billingPortalURL: v.optional(httpURLSchema),
|
|
843
930
|
/** The bearer token services such as billing present to the internal API. */
|
|
844
931
|
internalServiceToken: v.optional(v.pipe(v.string(), v.minLength(32))),
|
|
932
|
+
/**
|
|
933
|
+
* The operator's own terms, privacy policy, abuse contact, and minimum age; without it the
|
|
934
|
+
* server shows none. With `requireAcceptance`, an account uses the server only once it accepted
|
|
935
|
+
* `termsVersion`.
|
|
936
|
+
*/
|
|
937
|
+
legal: v.optional(v.object({
|
|
938
|
+
termsURL: v.optional(httpURLSchema),
|
|
939
|
+
privacyURL: v.optional(httpURLSchema),
|
|
940
|
+
termsVersion: optionalTextSchema,
|
|
941
|
+
requireAcceptance: v.boolean(),
|
|
942
|
+
abuseEmail: v.optional(v.pipe(v.string(), v.email())),
|
|
943
|
+
minimumAge: v.optional(v.pipe(v.number(), v.integer(), v.minValue(13), v.maxValue(21)))
|
|
944
|
+
})),
|
|
845
945
|
/** Where server errors are reported; without it the server reports nothing. */
|
|
846
946
|
errorTracking: v.optional(v.object({
|
|
847
947
|
provider: v.literal("posthog"),
|
|
@@ -863,6 +963,7 @@ function validateAuthenticationConfig(config) {
|
|
|
863
963
|
if (config.emailPasswordMinimumLength > config.emailPasswordMaximumLength) throw new CloudConfigError("Minimum password length cannot exceed maximum password length");
|
|
864
964
|
if (config.emailPasswordEnabled && config.emailTransport === "none") throw new CloudConfigError("Email and password authentication requires transactional email delivery");
|
|
865
965
|
if (config.deploymentAdminMFARequired && !config.totpEnabled && !config.passkeysEnabled) throw new CloudConfigError("Deployment administrator MFA requires TOTP or passkeys");
|
|
966
|
+
if (config.deployment === "official" && !config.deploymentAdminMFARequired) throw new CloudConfigError("An official deployment requires MFA for deployment administrators");
|
|
866
967
|
if (config.recoveryCodesEnabled && !config.totpEnabled) throw new CloudConfigError("MFA recovery codes require TOTP");
|
|
867
968
|
requireTogether(config, "CAPTCHA", [
|
|
868
969
|
"captchaProvider",
|
|
@@ -887,9 +988,13 @@ function validateEmailConfig(config) {
|
|
|
887
988
|
if (config.deployment === "official" && config.smtpHost && config.smtpSecure === false) throw new CloudConfigError("Official SMTP delivery must use a secure connection");
|
|
888
989
|
}
|
|
889
990
|
function validateServiceConfig(config) {
|
|
890
|
-
requireTogether(config, "
|
|
991
|
+
requireTogether(config, "Services", ["servicesEndpoint", "servicesSigningKey"]);
|
|
891
992
|
if (config.internalServiceToken && config.staticEntitlements) throw new CloudConfigError("The internal API writes database entitlements, which static entitlements would hide");
|
|
892
|
-
if (config.deployment === "official" && config.
|
|
993
|
+
if (config.deployment === "official" && config.servicesEndpoint && new URL(config.servicesEndpoint).protocol !== "https:") throw new CloudConfigError("Official services endpoint must use HTTPS");
|
|
994
|
+
}
|
|
995
|
+
function validateLegalConfig(config) {
|
|
996
|
+
const legal = config.legal;
|
|
997
|
+
if (legal?.requireAcceptance && !(legal.termsURL && legal.termsVersion)) throw new CloudConfigError("[legal] require_acceptance needs terms_url and terms_version");
|
|
893
998
|
}
|
|
894
999
|
function validateObjectStorageConfig(config) {
|
|
895
1000
|
if (config.s3KmsKeyId && config.s3ServerSideEncryption !== "aws:kms") throw new CloudConfigError("S3 KMS key ID requires aws:kms server-side encryption");
|
|
@@ -904,10 +1009,11 @@ function parseCloudServerConfig(input) {
|
|
|
904
1009
|
validateEmailConfig(config);
|
|
905
1010
|
validateServiceConfig(config);
|
|
906
1011
|
validateObjectStorageConfig(config);
|
|
1012
|
+
validateLegalConfig(config);
|
|
907
1013
|
return config;
|
|
908
1014
|
}
|
|
909
|
-
/** PostHog's
|
|
910
|
-
const POSTHOG_DEFAULT_HOST = "https://
|
|
1015
|
+
/** PostHog's EU Cloud ingestion host, where OpenPencil's own project is. */
|
|
1016
|
+
const POSTHOG_DEFAULT_HOST = "https://eu.i.posthog.com";
|
|
911
1017
|
const DEFAULT_SECRET_NAMES = {
|
|
912
1018
|
databaseURL: "DATABASE_URL",
|
|
913
1019
|
authSecret: "BETTER_AUTH_SECRET",
|
|
@@ -920,7 +1026,7 @@ const DEFAULT_SECRET_NAMES = {
|
|
|
920
1026
|
smtpUser: "OPENPENCIL_CLOUD_SMTP_USER",
|
|
921
1027
|
smtpPassword: "OPENPENCIL_CLOUD_SMTP_PASSWORD",
|
|
922
1028
|
turnstileSecretKey: "TURNSTILE_SECRET_KEY",
|
|
923
|
-
|
|
1029
|
+
servicesSigningKey: "OPENPENCIL_CLOUD_SERVICES_SIGNING_KEY",
|
|
924
1030
|
internalServiceToken: "OPENPENCIL_CLOUD_SERVICE_TOKEN",
|
|
925
1031
|
posthogProjectAPIKey: "POSTHOG_PROJECT_API_KEY"
|
|
926
1032
|
};
|
|
@@ -1055,6 +1161,11 @@ const cloudDeploymentConfigSchema = v.strictObject({
|
|
|
1055
1161
|
checksum_verification: v.optional(v.picklist(["native", "metadata"]), "native"),
|
|
1056
1162
|
server_side_encryption: v.optional(v.picklist(["AES256", "aws:kms"])),
|
|
1057
1163
|
kms_key_id: v.optional(textSchema),
|
|
1164
|
+
/**
|
|
1165
|
+
* Keeps each file cleanup deletes under this prefix, such as `deleted/`, so a database restored
|
|
1166
|
+
* to an earlier time still finds it; a lifecycle rule on the prefix expires them.
|
|
1167
|
+
*/
|
|
1168
|
+
deleted_prefix: v.optional(deletedPrefixSchema),
|
|
1058
1169
|
maximum_upload_bytes: v.optional(positiveIntegerSchema)
|
|
1059
1170
|
}),
|
|
1060
1171
|
collaboration: v.optional(v.strictObject({
|
|
@@ -1062,7 +1173,13 @@ const cloudDeploymentConfigSchema = v.strictObject({
|
|
|
1062
1173
|
maximum_message_bytes: v.optional(positiveIntegerSchema),
|
|
1063
1174
|
maximum_connections_per_room: v.optional(positiveIntegerSchema)
|
|
1064
1175
|
}), {}),
|
|
1065
|
-
documents: v.optional(v.strictObject({
|
|
1176
|
+
documents: v.optional(v.strictObject({
|
|
1177
|
+
retention_days: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 30),
|
|
1178
|
+
purge_grace_days: v.optional(v.pipe(v.number(), v.integer(), v.minValue(0)), 7)
|
|
1179
|
+
}), {
|
|
1180
|
+
retention_days: 30,
|
|
1181
|
+
purge_grace_days: 7
|
|
1182
|
+
}),
|
|
1066
1183
|
email: emailSchema,
|
|
1067
1184
|
workers: v.optional(v.strictObject({
|
|
1068
1185
|
email: v.optional(v.strictObject({
|
|
@@ -1092,13 +1209,25 @@ const cloudDeploymentConfigSchema = v.strictObject({
|
|
|
1092
1209
|
source: v.literal("static"),
|
|
1093
1210
|
...staticEntitlementsTOMLSchema.entries
|
|
1094
1211
|
}), v.strictObject({ source: v.literal("database") })])),
|
|
1095
|
-
|
|
1212
|
+
services: v.optional(v.strictObject({
|
|
1096
1213
|
endpoint: urlSchema,
|
|
1097
|
-
signing_key: defaultReference(DEFAULT_SECRET_NAMES.
|
|
1214
|
+
signing_key: defaultReference(DEFAULT_SECRET_NAMES.servicesSigningKey),
|
|
1098
1215
|
token_lifetime_minutes: v.optional(v.pipe(v.number(), v.integer(), v.minValue(1), v.maxValue(60)), 10)
|
|
1099
1216
|
})),
|
|
1100
1217
|
billing: v.optional(v.strictObject({ portal_url: urlSchema })),
|
|
1101
1218
|
internal_api: v.optional(v.strictObject({ service_token: defaultReference(DEFAULT_SECRET_NAMES.internalServiceToken) })),
|
|
1219
|
+
/**
|
|
1220
|
+
* Optional: the operator's terms and privacy policy, linked at sign-up and in the portal, and
|
|
1221
|
+
* with `require_acceptance`, accepted once per `terms_version` before an account uses the server.
|
|
1222
|
+
*/
|
|
1223
|
+
legal: v.optional(v.strictObject({
|
|
1224
|
+
terms_url: v.optional(urlSchema),
|
|
1225
|
+
privacy_url: v.optional(urlSchema),
|
|
1226
|
+
terms_version: v.optional(textSchema),
|
|
1227
|
+
require_acceptance: v.optional(v.boolean(), false),
|
|
1228
|
+
abuse_email: v.optional(v.pipe(v.string(), v.email())),
|
|
1229
|
+
minimum_age: v.optional(v.pipe(v.number(), v.integer(), v.minValue(13), v.maxValue(21)))
|
|
1230
|
+
})),
|
|
1102
1231
|
/** Optional: reports server errors to PostHog error tracking. Leave it out to send nothing. */
|
|
1103
1232
|
error_tracking: v.optional(v.strictObject({
|
|
1104
1233
|
provider: v.literal("posthog"),
|
|
@@ -1115,838 +1244,427 @@ function checkColumnIn(column, values) {
|
|
|
1115
1244
|
if (values.length === 0 || values.some((value) => !CHECK_VALUE_PATTERN.test(value))) throw new Error("Check constraint values must be non-empty lowercase identifiers");
|
|
1116
1245
|
return sql`${sql.ref(column)} in (${sql.join(values.map((value) => sql.lit(value)))})`;
|
|
1117
1246
|
}
|
|
1118
|
-
//#endregion
|
|
1119
|
-
//#region src/server/db/migrations/helpers.ts
|
|
1120
|
-
async function addCheckConstraint(database, table, constraint, column, values) {
|
|
1121
|
-
await database.schema.alterTable(table).addCheckConstraint(constraint, checkColumnIn(column, values)).execute();
|
|
1122
|
-
}
|
|
1123
|
-
async function replaceCheckConstraint(database, table, constraint, column, values) {
|
|
1124
|
-
await database.schema.alterTable(table).dropConstraint(constraint).execute();
|
|
1125
|
-
await addCheckConstraint(database, table, constraint, column, values);
|
|
1126
|
-
}
|
|
1127
|
-
async function addCurrentTimestampColumns(database, table, columns) {
|
|
1128
|
-
for (const column of columns) await database.schema.alterTable(table).addColumn(column, "timestamptz", (definition) => definition.notNull().defaultTo(CURRENT_TIMESTAMP)).execute();
|
|
1129
|
-
}
|
|
1130
1247
|
/**
|
|
1131
|
-
*
|
|
1132
|
-
*
|
|
1248
|
+
* Holds a lock named `key` until the transaction ends, for a check and a write that must not
|
|
1249
|
+
* interleave with another transaction's, such as reserving storage from an account's allowance,
|
|
1250
|
+
* which spans rows no single one of which could be locked for it.
|
|
1133
1251
|
*/
|
|
1134
|
-
function
|
|
1135
|
-
|
|
1136
|
-
}
|
|
1137
|
-
//#endregion
|
|
1138
|
-
//#region src/server/db/migrations/001_foundation.ts
|
|
1139
|
-
var _001_foundation_exports = /* @__PURE__ */ __exportAll({
|
|
1140
|
-
down: () => down$27,
|
|
1141
|
-
up: () => up$27
|
|
1142
|
-
});
|
|
1143
|
-
const UPDATED_AT_TRIGGER = "openpencil_set_updated_at";
|
|
1144
|
-
async function up$27(database) {
|
|
1145
|
-
await database.schema.createTable("workspace").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("name", "text", (column) => column.notNull()).addColumn("slug", "text", (column) => column.notNull().unique()).addColumn("created_by", "text", (column) => column.notNull()).execute();
|
|
1146
|
-
await addCurrentTimestampColumns(database, "workspace", ["created_at", "updated_at"]);
|
|
1147
|
-
await database.schema.createTable("workspace_member").addColumn("workspace_id", "uuid", (column) => column.references("workspace.id").onDelete("cascade").notNull()).addColumn("user_id", "text", (column) => column.notNull()).addColumn("role", "text", (column) => column.notNull()).addPrimaryKeyConstraint("workspace_member_primary", ["workspace_id", "user_id"]).addCheckConstraint("workspace_member_role_check", checkColumnIn("role", [
|
|
1148
|
-
"admin",
|
|
1149
|
-
"editor",
|
|
1150
|
-
"viewer"
|
|
1151
|
-
])).execute();
|
|
1152
|
-
await addCurrentTimestampColumns(database, "workspace_member", ["created_at"]);
|
|
1153
|
-
await database.schema.createTable("document").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("workspace_id", "uuid", (column) => column.references("workspace.id").onDelete("cascade").notNull()).addColumn("name", "text", (column) => column.notNull()).addColumn("current_revision_id", "uuid").addColumn("version", "integer", (column) => column.notNull().defaultTo(0)).addColumn("created_by", "text", (column) => column.notNull()).addColumn("deleted_at", "timestamptz").execute();
|
|
1154
|
-
await addCurrentTimestampColumns(database, "document", ["created_at", "updated_at"]);
|
|
1155
|
-
await database.schema.createTable("storage_object").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("object_key", "text", (column) => column.notNull().unique()).addColumn("checksum", "text", (column) => column.notNull()).addColumn("byte_size", "bigint", (column) => column.notNull()).addColumn("content_type", "text", (column) => column.notNull()).addColumn("created_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).execute();
|
|
1156
|
-
await database.schema.createTable("document_revision").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.references("document.id").onDelete("cascade").notNull()).addColumn("parent_revision_id", "uuid", (column) => column.references("document_revision.id").onDelete("set null")).addColumn("storage_object_id", "uuid", (column) => column.references("storage_object.id").onDelete("restrict").notNull()).addColumn("created_by", "text", (column) => column.notNull()).addColumn("created_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).execute();
|
|
1157
|
-
await database.schema.alterTable("document").addForeignKeyConstraint("document_current_revision_foreign", ["current_revision_id"], "document_revision", ["id"], (constraint) => constraint.onDelete("set null")).execute();
|
|
1158
|
-
await database.schema.createTable("upload").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.references("document.id").onDelete("cascade").notNull()).addColumn("base_revision_id", "uuid", (column) => column.references("document_revision.id").onDelete("set null")).addColumn("object_key", "text", (column) => column.notNull().unique()).addColumn("status", "text", (column) => column.notNull()).addColumn("checksum", "text", (column) => column.notNull()).addColumn("created_by", "text", (column) => column.notNull()).addColumn("byte_size", "bigint", (column) => column.notNull()).addColumn("created_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).addColumn("content_type", "text", (column) => column.notNull()).addColumn("multipart_upload_id", "text").addColumn("expires_at", "timestamptz", (column) => column.notNull()).addCheckConstraint("upload_status_check", checkColumnIn("status", [
|
|
1159
|
-
"pending",
|
|
1160
|
-
"committed",
|
|
1161
|
-
"abandoned"
|
|
1162
|
-
])).execute();
|
|
1163
|
-
await database.schema.createIndex("workspace_member_user_index").on("workspace_member").column("user_id").execute();
|
|
1164
|
-
await database.schema.createIndex("document_workspace_index").on("document").columns(["workspace_id", "updated_at"]).execute();
|
|
1165
|
-
await database.schema.createIndex("document_revision_document_index").on("document_revision").columns(["document_id", "created_at"]).execute();
|
|
1166
|
-
await database.schema.createIndex("upload_expiration_index").on("upload").columns(["status", "expires_at"]).execute();
|
|
1167
|
-
await sql`
|
|
1168
|
-
create function ${sql.id(UPDATED_AT_TRIGGER)}() returns trigger as $$
|
|
1169
|
-
begin
|
|
1170
|
-
new.updated_at = now();
|
|
1171
|
-
return new;
|
|
1172
|
-
end;
|
|
1173
|
-
$$ language plpgsql
|
|
1174
|
-
`.execute(database);
|
|
1175
|
-
for (const table of ["workspace", "document"]) await sql`
|
|
1176
|
-
create trigger ${sql.id(`${table}_updated_at`)}
|
|
1177
|
-
before update on ${sql.table(table)}
|
|
1178
|
-
for each row execute function ${sql.id(UPDATED_AT_TRIGGER)}()
|
|
1179
|
-
`.execute(database);
|
|
1180
|
-
}
|
|
1181
|
-
async function down$27(database) {
|
|
1182
|
-
await database.schema.dropTable("upload").ifExists().execute();
|
|
1183
|
-
await database.schema.alterTable("document").dropConstraint("document_current_revision_foreign").execute();
|
|
1184
|
-
await database.schema.dropTable("document_revision").ifExists().execute();
|
|
1185
|
-
await database.schema.dropTable("storage_object").ifExists().execute();
|
|
1186
|
-
await database.schema.dropTable("document").ifExists().execute();
|
|
1187
|
-
await database.schema.dropTable("workspace_member").ifExists().execute();
|
|
1188
|
-
await database.schema.dropTable("workspace").ifExists().execute();
|
|
1189
|
-
await sql`drop function if exists ${sql.id(UPDATED_AT_TRIGGER)}()`.execute(database);
|
|
1190
|
-
}
|
|
1191
|
-
//#endregion
|
|
1192
|
-
//#region src/server/db/migrations/002_upload_cleanup_claims.ts
|
|
1193
|
-
var _002_upload_cleanup_claims_exports = /* @__PURE__ */ __exportAll({
|
|
1194
|
-
down: () => down$26,
|
|
1195
|
-
up: () => up$26
|
|
1196
|
-
});
|
|
1197
|
-
async function up$26(database) {
|
|
1198
|
-
await database.schema.alterTable("upload").addColumn("cleanup_claim_id", "uuid").addColumn("cleanup_claimed_at", "timestamptz").execute();
|
|
1199
|
-
await database.schema.alterTable("upload").dropConstraint("upload_status_check").execute();
|
|
1200
|
-
await database.schema.alterTable("upload").addCheckConstraint("upload_status_check", checkColumnIn("status", [
|
|
1201
|
-
"pending",
|
|
1202
|
-
"cleaning",
|
|
1203
|
-
"committed",
|
|
1204
|
-
"abandoned"
|
|
1205
|
-
])).execute();
|
|
1206
|
-
}
|
|
1207
|
-
async function down$26(database) {
|
|
1208
|
-
await database.schema.alterTable("upload").dropConstraint("upload_status_check").execute();
|
|
1209
|
-
await database.schema.alterTable("upload").addCheckConstraint("upload_status_check", checkColumnIn("status", [
|
|
1210
|
-
"pending",
|
|
1211
|
-
"committed",
|
|
1212
|
-
"abandoned"
|
|
1213
|
-
])).execute();
|
|
1214
|
-
await database.schema.alterTable("upload").dropColumn("cleanup_claimed_at").dropColumn("cleanup_claim_id").execute();
|
|
1215
|
-
}
|
|
1216
|
-
//#endregion
|
|
1217
|
-
//#region src/server/db/migrations/003_document_cleanup_claims.ts
|
|
1218
|
-
var _003_document_cleanup_claims_exports = /* @__PURE__ */ __exportAll({
|
|
1219
|
-
down: () => down$25,
|
|
1220
|
-
up: () => up$25
|
|
1221
|
-
});
|
|
1222
|
-
async function up$25(database) {
|
|
1223
|
-
await database.schema.alterTable("document").addColumn("cleanup_claim_id", "uuid").addColumn("cleanup_claimed_at", "timestamptz").execute();
|
|
1224
|
-
await database.schema.createIndex("document_cleanup_index").on("document").columns(["deleted_at", "cleanup_claimed_at"]).execute();
|
|
1225
|
-
}
|
|
1226
|
-
async function down$25(database) {
|
|
1227
|
-
await database.schema.dropIndex("document_cleanup_index").ifExists().execute();
|
|
1228
|
-
await database.schema.alterTable("document").dropColumn("cleanup_claimed_at").dropColumn("cleanup_claim_id").execute();
|
|
1229
|
-
}
|
|
1230
|
-
//#endregion
|
|
1231
|
-
//#region src/server/db/migrations/004_document_sharing.ts
|
|
1232
|
-
var _004_document_sharing_exports = /* @__PURE__ */ __exportAll({
|
|
1233
|
-
down: () => down$24,
|
|
1234
|
-
up: () => up$24
|
|
1235
|
-
});
|
|
1236
|
-
async function up$24(database) {
|
|
1237
|
-
await database.schema.createTable("document_share").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.references("document.id").onDelete("cascade").notNull()).addColumn("permission", "text", (column) => column.notNull()).addColumn("secret_hash", "text", (column) => column.notNull()).addColumn("room_epoch", "integer", (column) => column.notNull().defaultTo(0)).addColumn("created_by", "text", (column) => column.notNull()).addColumn("expires_at", "timestamptz").addColumn("revoked_at", "timestamptz").addColumn("last_used_at", "timestamptz").addCheckConstraint("document_share_permission_check", checkColumnIn("permission", ["view", "edit"])).execute();
|
|
1238
|
-
await addCurrentTimestampColumns(database, "document_share", ["created_at", "updated_at"]);
|
|
1239
|
-
await database.schema.createTable("document_grant").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.references("document.id").onDelete("cascade").notNull()).addColumn("user_id", "text", (column) => column.notNull()).addColumn("permission", "text", (column) => column.notNull()).addColumn("created_by", "text", (column) => column.notNull()).addColumn("revoked_at", "timestamptz").addUniqueConstraint("document_grant_document_user_unique", ["document_id", "user_id"]).addCheckConstraint("document_grant_permission_check", checkColumnIn("permission", ["view", "edit"])).execute();
|
|
1240
|
-
await addCurrentTimestampColumns(database, "document_grant", ["created_at", "updated_at"]);
|
|
1241
|
-
await addInvitationColumns(database.schema.createTable("document_invitation").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.references("document.id").onDelete("cascade").notNull()), "permission").addCheckConstraint("document_invitation_permission_check", checkColumnIn("permission", ["view", "edit"])).execute();
|
|
1242
|
-
await addCurrentTimestampColumns(database, "document_invitation", ["invited_at"]);
|
|
1243
|
-
await database.schema.createIndex("document_share_document_index").on("document_share").columns(["document_id", "revoked_at"]).execute();
|
|
1244
|
-
await database.schema.createIndex("document_grant_user_index").on("document_grant").columns(["user_id", "document_id"]).execute();
|
|
1245
|
-
await database.schema.createIndex("document_invitation_document_index").on("document_invitation").columns(["document_id", "revoked_at"]).execute();
|
|
1246
|
-
}
|
|
1247
|
-
async function down$24(database) {
|
|
1248
|
-
await database.schema.dropTable("document_invitation").ifExists().execute();
|
|
1249
|
-
await database.schema.dropTable("document_grant").ifExists().execute();
|
|
1250
|
-
await database.schema.dropTable("document_share").ifExists().execute();
|
|
1251
|
-
}
|
|
1252
|
-
//#endregion
|
|
1253
|
-
//#region src/server/db/migrations/005_document_collaboration_epoch.ts
|
|
1254
|
-
var _005_document_collaboration_epoch_exports = /* @__PURE__ */ __exportAll({
|
|
1255
|
-
down: () => down$23,
|
|
1256
|
-
up: () => up$23
|
|
1257
|
-
});
|
|
1258
|
-
async function up$23(database) {
|
|
1259
|
-
await database.schema.alterTable("document").addColumn("collaborationEpoch", "integer", (column) => column.notNull().defaultTo(0)).execute();
|
|
1260
|
-
}
|
|
1261
|
-
async function down$23(database) {
|
|
1262
|
-
await database.schema.alterTable("document").dropColumn("collaborationEpoch").execute();
|
|
1263
|
-
}
|
|
1264
|
-
//#endregion
|
|
1265
|
-
//#region src/server/db/migrations/006_invitation_continuation.ts
|
|
1266
|
-
var _006_invitation_continuation_exports = /* @__PURE__ */ __exportAll({
|
|
1267
|
-
down: () => down$22,
|
|
1268
|
-
up: () => up$22
|
|
1269
|
-
});
|
|
1270
|
-
async function up$22(database) {
|
|
1271
|
-
await database.schema.createTable("invitationContinuation").addColumn("id", "text", (column) => column.primaryKey()).addColumn("invitationId", "uuid", (column) => column.notNull().references("documentInvitation.id").onDelete("cascade")).addColumn("tokenEncrypted", "text", (column) => column.notNull()).addColumn("expiresAt", "timestamptz", (column) => column.notNull()).addColumn("consumedAt", "timestamptz").addColumn("createdAt", "timestamptz", (column) => column.notNull().defaultTo(database.fn("now"))).execute();
|
|
1272
|
-
await database.schema.createIndex("invitationContinuationExpiryIndex").on("invitationContinuation").column("expiresAt").execute();
|
|
1273
|
-
}
|
|
1274
|
-
async function down$22(database) {
|
|
1275
|
-
await database.schema.dropTable("invitationContinuation").execute();
|
|
1276
|
-
}
|
|
1277
|
-
//#endregion
|
|
1278
|
-
//#region src/server/db/migrations/007_collaboration_state.ts
|
|
1279
|
-
var _007_collaboration_state_exports = /* @__PURE__ */ __exportAll({
|
|
1280
|
-
down: () => down$21,
|
|
1281
|
-
up: () => up$21
|
|
1282
|
-
});
|
|
1283
|
-
async function up$21(database) {
|
|
1284
|
-
await database.schema.createTable("documentCollaborationState").addColumn("documentId", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("roomEpoch", "integer", (column) => column.notNull()).addColumn("state", "bytea", (column) => column.notNull()).addColumn("version", "integer", (column) => column.notNull().defaultTo(1)).addColumn("createdAt", "timestamptz", (column) => column.notNull().defaultTo(database.fn("now"))).addColumn("updatedAt", "timestamptz", (column) => column.notNull().defaultTo(database.fn("now"))).addPrimaryKeyConstraint("documentCollaborationStatePrimary", ["documentId", "roomEpoch"]).execute();
|
|
1285
|
-
}
|
|
1286
|
-
async function down$21(database) {
|
|
1287
|
-
await database.schema.dropTable("documentCollaborationState").execute();
|
|
1288
|
-
}
|
|
1289
|
-
//#endregion
|
|
1290
|
-
//#region src/server/db/migrations/008_storage_reservations.ts
|
|
1291
|
-
var _008_storage_reservations_exports = /* @__PURE__ */ __exportAll({
|
|
1292
|
-
down: () => down$20,
|
|
1293
|
-
up: () => up$20
|
|
1294
|
-
});
|
|
1295
|
-
async function up$20(database) {
|
|
1296
|
-
await database.schema.createTable("workspaceStorageUsage").addColumn("workspaceId", "uuid", (column) => column.primaryKey().references("workspace.id").onDelete("cascade")).addColumn("committedBytes", "bigint", (column) => column.notNull().defaultTo(0)).addColumn("updatedAt", "timestamptz", (column) => column.notNull().defaultTo(database.fn("now"))).execute();
|
|
1297
|
-
await database.schema.createTable("uploadStorageReservation").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("workspaceId", "uuid", (column) => column.notNull().references("workspace.id").onDelete("cascade")).addColumn("uploadId", "uuid", (column) => column.notNull().unique().references("upload.id").onDelete("cascade")).addColumn("bytes", "bigint", (column) => column.notNull()).addColumn("expiresAt", "timestamptz", (column) => column.notNull()).addColumn("committedAt", "timestamptz").addColumn("releasedAt", "timestamptz").addColumn("createdAt", "timestamptz", (column) => column.notNull().defaultTo(database.fn("now"))).execute();
|
|
1298
|
-
}
|
|
1299
|
-
async function down$20(database) {
|
|
1300
|
-
await database.schema.dropTable("uploadStorageReservation").execute();
|
|
1301
|
-
await database.schema.dropTable("workspaceStorageUsage").execute();
|
|
1302
|
-
}
|
|
1303
|
-
//#endregion
|
|
1304
|
-
//#region src/server/db/migrations/009_workspace_entitlements.ts
|
|
1305
|
-
var _009_workspace_entitlements_exports = /* @__PURE__ */ __exportAll({
|
|
1306
|
-
down: () => down$19,
|
|
1307
|
-
up: () => up$19
|
|
1308
|
-
});
|
|
1309
|
-
async function up$19(database) {
|
|
1310
|
-
await database.schema.createTable("workspaceEntitlement").addColumn("workspaceId", "uuid", (column) => column.primaryKey().references("workspace.id").onDelete("cascade")).addColumn("values", "jsonb", (column) => column.notNull()).addColumn("source", "text", (column) => column.notNull()).addColumn("revision", "integer", (column) => column.notNull().defaultTo(1)).addColumn("updatedAt", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).execute();
|
|
1311
|
-
}
|
|
1312
|
-
async function down$19(database) {
|
|
1313
|
-
await database.schema.dropTable("workspaceEntitlement").execute();
|
|
1314
|
-
}
|
|
1315
|
-
//#endregion
|
|
1316
|
-
//#region src/server/db/migrations/010_upload_finalization.ts
|
|
1317
|
-
var _010_upload_finalization_exports = /* @__PURE__ */ __exportAll({
|
|
1318
|
-
down: () => down$18,
|
|
1319
|
-
up: () => up$18
|
|
1320
|
-
});
|
|
1321
|
-
async function up$18(database) {
|
|
1322
|
-
await replaceCheckConstraint(database, "upload", "upload_status_check", "status", [
|
|
1323
|
-
"pending",
|
|
1324
|
-
"finalizing",
|
|
1325
|
-
"cleaning",
|
|
1326
|
-
"committed",
|
|
1327
|
-
"abandoned"
|
|
1328
|
-
]);
|
|
1329
|
-
}
|
|
1330
|
-
async function down$18(database) {
|
|
1331
|
-
await replaceCheckConstraint(database, "upload", "upload_status_check", "status", [
|
|
1332
|
-
"pending",
|
|
1333
|
-
"cleaning",
|
|
1334
|
-
"committed",
|
|
1335
|
-
"abandoned"
|
|
1336
|
-
]);
|
|
1337
|
-
}
|
|
1338
|
-
//#endregion
|
|
1339
|
-
//#region src/server/db/migrations/011_upload_finalization_lease.ts
|
|
1340
|
-
var _011_upload_finalization_lease_exports = /* @__PURE__ */ __exportAll({
|
|
1341
|
-
down: () => down$17,
|
|
1342
|
-
up: () => up$17
|
|
1343
|
-
});
|
|
1344
|
-
async function up$17(database) {
|
|
1345
|
-
await database.schema.alterTable("upload").addColumn("finalization_started_at", "timestamptz").execute();
|
|
1346
|
-
}
|
|
1347
|
-
async function down$17(database) {
|
|
1348
|
-
await database.schema.alterTable("upload").dropColumn("finalization_started_at").execute();
|
|
1349
|
-
}
|
|
1350
|
-
//#endregion
|
|
1351
|
-
//#region src/server/db/migrations/012_transactional_email.ts
|
|
1352
|
-
var _012_transactional_email_exports = /* @__PURE__ */ __exportAll({
|
|
1353
|
-
down: () => down$16,
|
|
1354
|
-
up: () => up$16
|
|
1355
|
-
});
|
|
1356
|
-
async function up$16(database) {
|
|
1357
|
-
await database.schema.createTable("transactional_email").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("idempotency_key", "text", (column) => column.notNull().unique()).addColumn("kind", "text", (column) => column.notNull()).addColumn("recipient_email_normalized", "text", (column) => column.notNull()).addColumn("payload_encrypted", "text").addColumn("status", "text", (column) => column.notNull().defaultTo("pending")).addColumn("attempt_count", "integer", (column) => column.notNull().defaultTo(0)).addColumn("next_attempt_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).addColumn("claim_id", "uuid").addColumn("claimed_at", "timestamptz").addColumn("transport", "text").addColumn("transport_message_id", "text").addColumn("last_error_code", "text").addColumn("created_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).addColumn("updated_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).addColumn("accepted_at", "timestamptz").addCheckConstraint("transactional_email_kind_check", checkColumnIn("kind", ["document-invitation"])).addCheckConstraint("transactional_email_status_check", checkColumnIn("status", [
|
|
1358
|
-
"pending",
|
|
1359
|
-
"sending",
|
|
1360
|
-
"accepted",
|
|
1361
|
-
"failed",
|
|
1362
|
-
"suppressed"
|
|
1363
|
-
])).execute();
|
|
1364
|
-
await database.schema.createIndex("transactional_email_delivery_index").on("transactional_email").columns(["status", "next_attempt_at"]).execute();
|
|
1365
|
-
}
|
|
1366
|
-
async function down$16(database) {
|
|
1367
|
-
await database.schema.dropTable("transactional_email").ifExists().execute();
|
|
1368
|
-
}
|
|
1369
|
-
//#endregion
|
|
1370
|
-
//#region src/server/db/migrations/013_admin_enrollment.ts
|
|
1371
|
-
var _013_admin_enrollment_exports = /* @__PURE__ */ __exportAll({
|
|
1372
|
-
down: () => down$15,
|
|
1373
|
-
up: () => up$15
|
|
1374
|
-
});
|
|
1375
|
-
async function up$15(database) {
|
|
1376
|
-
await database.schema.createTable("cloud_enrollment").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("email_normalized", "text", (column) => column.notNull().unique()).addColumn("name", "text").addColumn("reason", "text").addColumn("status", "text", (column) => column.notNull().defaultTo("pending")).addColumn("requested_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).addColumn("reviewed_at", "timestamptz").addColumn("reviewed_by", "text").addColumn("review_note", "text").addColumn("approved_user_id", "text").addCheckConstraint("cloud_enrollment_status_check", checkColumnIn("status", [
|
|
1377
|
-
"pending",
|
|
1378
|
-
"approved",
|
|
1379
|
-
"rejected",
|
|
1380
|
-
"revoked"
|
|
1381
|
-
])).execute();
|
|
1382
|
-
await database.schema.createIndex("cloud_enrollment_status_index").on("cloud_enrollment").columns(["status", "requested_at"]).execute();
|
|
1383
|
-
await database.schema.createTable("cloud_admin_audit_event").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("actor_user_id", "text", (column) => column.notNull()).addColumn("action", "text", (column) => column.notNull()).addColumn("subject_type", "text", (column) => column.notNull()).addColumn("subject_id", "text", (column) => column.notNull()).addColumn("metadata", "jsonb", (column) => column.notNull().defaultTo(EMPTY_JSON_OBJECT)).addColumn("created_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).execute();
|
|
1384
|
-
await database.schema.createIndex("cloud_admin_audit_created_index").on("cloud_admin_audit_event").column("created_at").execute();
|
|
1385
|
-
}
|
|
1386
|
-
async function down$15(database) {
|
|
1387
|
-
await database.schema.dropTable("cloud_admin_audit_event").ifExists().execute();
|
|
1388
|
-
await database.schema.dropTable("cloud_enrollment").ifExists().execute();
|
|
1389
|
-
}
|
|
1390
|
-
//#endregion
|
|
1391
|
-
//#region src/server/db/migrations/014_enrollment_email_kinds.ts
|
|
1392
|
-
var _014_enrollment_email_kinds_exports = /* @__PURE__ */ __exportAll({
|
|
1393
|
-
down: () => down$14,
|
|
1394
|
-
up: () => up$14
|
|
1395
|
-
});
|
|
1396
|
-
async function up$14(database) {
|
|
1397
|
-
await replaceCheckConstraint(database, "transactional_email", "transactional_email_kind_check", "kind", [
|
|
1398
|
-
"document-invitation",
|
|
1399
|
-
"enrollment-requested",
|
|
1400
|
-
"admin-enrollment-notification",
|
|
1401
|
-
"enrollment-approved",
|
|
1402
|
-
"enrollment-rejected",
|
|
1403
|
-
"enrollment-revoked"
|
|
1404
|
-
]);
|
|
1405
|
-
}
|
|
1406
|
-
async function down$14(database) {
|
|
1407
|
-
await replaceCheckConstraint(database, "transactional_email", "transactional_email_kind_check", "kind", ["document-invitation"]);
|
|
1408
|
-
}
|
|
1409
|
-
//#endregion
|
|
1410
|
-
//#region src/server/db/migrations/015_cloud_rate_limit.ts
|
|
1411
|
-
var _015_cloud_rate_limit_exports = /* @__PURE__ */ __exportAll({
|
|
1412
|
-
down: () => down$13,
|
|
1413
|
-
up: () => up$13
|
|
1414
|
-
});
|
|
1415
|
-
async function up$13(database) {
|
|
1416
|
-
await database.schema.alterTable("cloud_enrollment").addColumn("request_revision", "integer", (column) => column.notNull().defaultTo(1)).execute();
|
|
1417
|
-
await database.schema.createTable("cloud_rate_limit").addColumn("key_hash", "text", (column) => column.primaryKey()).addColumn("window_started_at", "timestamptz", (column) => column.notNull()).addColumn("request_count", "integer", (column) => column.notNull().defaultTo(1)).execute();
|
|
1418
|
-
await addCurrentTimestampColumns(database, "cloud_rate_limit", ["updated_at"]);
|
|
1419
|
-
}
|
|
1420
|
-
async function down$13(database) {
|
|
1421
|
-
await database.schema.dropTable("cloud_rate_limit").ifExists().execute();
|
|
1422
|
-
await database.schema.alterTable("cloud_enrollment").dropColumn("request_revision").execute();
|
|
1423
|
-
}
|
|
1424
|
-
//#endregion
|
|
1425
|
-
//#region src/server/db/migrations/016_auth_schema.ts
|
|
1426
|
-
var _016_auth_schema_exports = /* @__PURE__ */ __exportAll({
|
|
1427
|
-
down: () => down$12,
|
|
1428
|
-
up: () => up$12
|
|
1429
|
-
});
|
|
1430
|
-
async function up$12(database) {
|
|
1431
|
-
await database.schema.createTable("cloud_auth_schema").addColumn("id", "text", (column) => column.primaryKey()).addColumn("version", "text", (column) => column.notNull()).execute();
|
|
1432
|
-
await addCurrentTimestampColumns(database, "cloud_auth_schema", ["updated_at"]);
|
|
1433
|
-
}
|
|
1434
|
-
async function down$12(database) {
|
|
1435
|
-
await database.schema.dropTable("cloud_auth_schema").ifExists().execute();
|
|
1252
|
+
async function lockForTransaction(transaction, key) {
|
|
1253
|
+
await sql`select pg_advisory_xact_lock(hashtextextended(${key}, 0))`.execute(transaction);
|
|
1436
1254
|
}
|
|
1437
1255
|
//#endregion
|
|
1438
|
-
//#region src/server/db/migrations/
|
|
1439
|
-
var _017_authentication_email_kinds_exports = /* @__PURE__ */ __exportAll({
|
|
1440
|
-
down: () => down$11,
|
|
1441
|
-
up: () => up$11
|
|
1442
|
-
});
|
|
1443
|
-
const kinds = [
|
|
1444
|
-
"document-invitation",
|
|
1445
|
-
"enrollment-requested",
|
|
1446
|
-
"admin-enrollment-notification",
|
|
1447
|
-
"enrollment-approved",
|
|
1448
|
-
"enrollment-rejected",
|
|
1449
|
-
"enrollment-revoked"
|
|
1450
|
-
];
|
|
1451
|
-
async function up$11(database) {
|
|
1452
|
-
await replaceCheckConstraint(database, "transactional_email", "transactional_email_kind_check", "kind", [
|
|
1453
|
-
...kinds,
|
|
1454
|
-
"email-verification",
|
|
1455
|
-
"password-reset",
|
|
1456
|
-
"password-changed"
|
|
1457
|
-
]);
|
|
1458
|
-
}
|
|
1459
|
-
async function down$11(database) {
|
|
1460
|
-
await replaceCheckConstraint(database, "transactional_email", "transactional_email_kind_check", "kind", kinds);
|
|
1461
|
-
}
|
|
1462
|
-
//#endregion
|
|
1463
|
-
//#region src/server/db/migrations/018_authentication_mfa.ts
|
|
1464
|
-
var _018_authentication_mfa_exports = /* @__PURE__ */ __exportAll({
|
|
1465
|
-
down: () => down$10,
|
|
1466
|
-
up: () => up$10
|
|
1467
|
-
});
|
|
1468
|
-
async function tableNames(database) {
|
|
1469
|
-
return new Set((await database.introspection.getTables()).map((table) => table.name));
|
|
1470
|
-
}
|
|
1471
|
-
async function up$10(database) {
|
|
1472
|
-
const cloud = database;
|
|
1473
|
-
const tables = await tableNames(database);
|
|
1474
|
-
await database.schema.createTable("cloud_mfa_assurance").addColumn("session_id", "uuid", (column) => column.primaryKey()).addColumn("user_id", "uuid", (column) => column.notNull()).addColumn("method", "text", (column) => column.notNull()).addColumn("verified_at", "timestamptz", (column) => column.notNull()).execute();
|
|
1475
|
-
if (!tables.has("user")) return;
|
|
1476
|
-
await database.schema.alterTable("user").addColumn("two_factor_enabled", "boolean", (column) => column.notNull().defaultTo(false)).execute();
|
|
1477
|
-
await database.schema.createTable("two_factor").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("secret", "text", (column) => column.notNull().unique()).addColumn("backup_codes", "text", (column) => column.notNull()).addColumn("user_id", "uuid", (column) => column.notNull().references("user.id").onDelete("cascade")).addColumn("verified", "boolean", (column) => column.notNull().defaultTo(true)).addColumn("failed_verification_count", "integer", (column) => column.notNull().defaultTo(0)).addColumn("locked_until", "timestamptz").execute();
|
|
1478
|
-
await database.schema.createIndex("two_factor_user_index").on("two_factor").column("user_id").execute();
|
|
1479
|
-
await database.schema.createTable("passkey").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("name", "text").addColumn("public_key", "text", (column) => column.notNull()).addColumn("user_id", "uuid", (column) => column.notNull().references("user.id").onDelete("cascade")).addColumn("credential_id", "text", (column) => column.notNull().unique()).addColumn("counter", "integer", (column) => column.notNull()).addColumn("device_type", "text", (column) => column.notNull()).addColumn("backed_up", "boolean", (column) => column.notNull()).addColumn("transports", "text").addColumn("created_at", "timestamptz", (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP)).addColumn("aaguid", "text").execute();
|
|
1480
|
-
await database.schema.createIndex("passkey_user_index").on("passkey").column("user_id").execute();
|
|
1481
|
-
await cloud.updateTable("cloudAuthSchema").set({
|
|
1482
|
-
version: "1.7.2+mfa.1",
|
|
1483
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
1484
|
-
}).where("id", "=", "better-auth").where("version", "=", "1.7.2").execute();
|
|
1485
|
-
}
|
|
1486
|
-
async function down$10(database) {
|
|
1487
|
-
await database.schema.dropTable("cloud_mfa_assurance").ifExists().execute();
|
|
1488
|
-
await database.schema.dropTable("passkey").ifExists().execute();
|
|
1489
|
-
await database.schema.dropTable("two_factor").ifExists().execute();
|
|
1490
|
-
if ((await tableNames(database)).has("user")) await database.schema.alterTable("user").dropColumn("two_factor_enabled").execute();
|
|
1491
|
-
}
|
|
1492
|
-
//#endregion
|
|
1493
|
-
//#region src/server/db/migrations/019_better_auth_account_issuer.ts
|
|
1494
|
-
var _019_better_auth_account_issuer_exports = /* @__PURE__ */ __exportAll({
|
|
1495
|
-
down: () => down$9,
|
|
1496
|
-
up: () => up$9
|
|
1497
|
-
});
|
|
1498
|
-
/** The unique index Better Auth 1.7.0 through 1.7.2 created on `account (issuer, accountId)`. */
|
|
1499
|
-
const ISSUER_INDEX = "account_issuer_accountId_uidx";
|
|
1256
|
+
//#region src/server/db/migrations/schema.ts
|
|
1500
1257
|
/**
|
|
1501
|
-
*
|
|
1502
|
-
*
|
|
1503
|
-
*
|
|
1504
|
-
*
|
|
1258
|
+
* What Cloud's migrations build tables with. Names follow PostgreSQL's own: `<table>_pkey`,
|
|
1259
|
+
* `<table>_<columns>_key` for unique, `<table>_<columns>_fkey`, and `<table>_<column>_check`;
|
|
1260
|
+
* indexes are `<table>_<columns>_idx`. Account IDs are text, since Better Auth's tables come after
|
|
1261
|
+
* Cloud's (see `migrateCloudDatabase`), and never foreign keys.
|
|
1505
1262
|
*/
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
version: "1.7.7+mfa.1",
|
|
1514
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
1515
|
-
}).where("id", "=", "better-auth").where("version", "=", "1.7.2+mfa.1").execute();
|
|
1516
|
-
}
|
|
1517
|
-
async function down$9(database) {
|
|
1518
|
-
await database.updateTable("cloudAuthSchema").set({
|
|
1519
|
-
version: "1.7.2+mfa.1",
|
|
1520
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
1521
|
-
}).where("id", "=", "better-auth").where("version", "=", "1.7.7+mfa.1").execute();
|
|
1522
|
-
}
|
|
1523
|
-
//#endregion
|
|
1524
|
-
//#region src/server/db/migrations/020_workspace_invitations.ts
|
|
1525
|
-
var _020_workspace_invitations_exports = /* @__PURE__ */ __exportAll({
|
|
1526
|
-
down: () => down$8,
|
|
1527
|
-
up: () => up$8
|
|
1528
|
-
});
|
|
1529
|
-
const emailKinds$1 = [
|
|
1530
|
-
"document-invitation",
|
|
1531
|
-
"enrollment-requested",
|
|
1532
|
-
"admin-enrollment-notification",
|
|
1533
|
-
"enrollment-approved",
|
|
1534
|
-
"enrollment-rejected",
|
|
1535
|
-
"enrollment-revoked",
|
|
1536
|
-
"email-verification",
|
|
1537
|
-
"password-reset",
|
|
1538
|
-
"password-changed"
|
|
1263
|
+
const now = (column) => column.notNull().defaultTo(CURRENT_TIMESTAMP);
|
|
1264
|
+
const required = (column) => column.notNull();
|
|
1265
|
+
const PERMISSIONS = ["view", "edit"];
|
|
1266
|
+
const ROLES = [
|
|
1267
|
+
"admin",
|
|
1268
|
+
"editor",
|
|
1269
|
+
"viewer"
|
|
1539
1270
|
];
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1271
|
+
/** What every emailed invitation keeps: whom it went to, its access, its secret, and its life. */
|
|
1272
|
+
function withInvitation(table, access) {
|
|
1273
|
+
return table.addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("email_normalized", "text", required).addColumn(access, "text", required).addColumn("token_hash", "text", required).addColumn("invited_by", "text", required).addColumn("invited_at", "timestamptz", now).addColumn("expires_at", "timestamptz", required).addColumn("accepted_at", "timestamptz").addColumn("revoked_at", "timestamptz");
|
|
1274
|
+
}
|
|
1275
|
+
/** Text columns that take one of a few values, as `[table, column, values]`. */
|
|
1276
|
+
async function addChecks(database, checks) {
|
|
1277
|
+
for (const [table, column, values] of checks) await database.schema.alterTable(table).addCheckConstraint(`${table}_${column}_check`, checkColumnIn(column, values)).execute();
|
|
1278
|
+
}
|
|
1279
|
+
/** Lookups beyond primary keys and unique columns, as `[table, columns]`; `desc` sorts newest first. */
|
|
1280
|
+
async function addIndexes(database, indexes) {
|
|
1281
|
+
for (const [table, columns] of indexes) {
|
|
1282
|
+
const name = columns.map((column) => column.split(" ")[0]).join("_");
|
|
1283
|
+
await database.schema.createIndex(`${table}_${name}_idx`).on(table).columns([...columns]).execute();
|
|
1284
|
+
}
|
|
1549
1285
|
}
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
await database.schema.dropTable(
|
|
1286
|
+
/** Drops a migration's tables, last created first. */
|
|
1287
|
+
async function dropTables(database, tables) {
|
|
1288
|
+
for (const table of tables.toReversed()) await database.schema.dropTable(table).ifExists().cascade().execute();
|
|
1553
1289
|
}
|
|
1554
1290
|
//#endregion
|
|
1555
|
-
//#region src/server/db/migrations/
|
|
1556
|
-
var
|
|
1291
|
+
//#region src/server/db/migrations/001_accounts.ts
|
|
1292
|
+
var _001_accounts_exports = /* @__PURE__ */ __exportAll({
|
|
1557
1293
|
down: () => down$7,
|
|
1558
|
-
keepCurrentRevisions: () => keepCurrentRevisions,
|
|
1559
1294
|
up: () => up$7
|
|
1560
1295
|
});
|
|
1561
|
-
/**
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1296
|
+
/** What Cloud keeps about an account beside Better Auth's tables: its plan, sign-ins, and picture. */
|
|
1297
|
+
const TABLES$5 = [
|
|
1298
|
+
"auth_schema_version",
|
|
1299
|
+
"account_entitlement",
|
|
1300
|
+
"mfa_assurance",
|
|
1301
|
+
"sign_in",
|
|
1302
|
+
"user_picture",
|
|
1303
|
+
"api_rate_limit"
|
|
1304
|
+
];
|
|
1565
1305
|
async function up$7(database) {
|
|
1566
|
-
await database.schema.createTable("
|
|
1567
|
-
await
|
|
1568
|
-
await database.schema.
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1306
|
+
await database.schema.createTable("auth_schema_version").addColumn("id", "text", (column) => column.primaryKey()).addColumn("version", "text", required).addColumn("updated_at", "timestamptz", now).execute();
|
|
1307
|
+
await database.schema.createTable("account_entitlement").addColumn("user_id", "text", (column) => column.primaryKey()).addColumn("values", "jsonb", required).addColumn("source", "text", required).addColumn("revision", "integer", (column) => column.notNull().defaultTo(1)).addColumn("external_version", "bigint").addColumn("updated_at", "timestamptz", now).execute();
|
|
1308
|
+
await database.schema.createTable("mfa_assurance").addColumn("session_id", "uuid", (column) => column.primaryKey()).addColumn("user_id", "uuid", required).addColumn("method", "text", required).addColumn("verified_at", "timestamptz", required).execute();
|
|
1309
|
+
await database.schema.createTable("sign_in").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("user_id", "uuid", required).addColumn("session_id", "uuid").addColumn("method", "text", required).addColumn("ip_address", "text").addColumn("user_agent", "text").addColumn("created_at", "timestamptz", now).execute();
|
|
1310
|
+
await database.schema.createTable("user_picture").addColumn("user_id", "uuid", (column) => column.primaryKey()).addColumn("bytes", "bytea", required).addColumn("format", "text", required).addColumn("checksum", "text", required).addColumn("source", "text", required).addColumn("created_at", "timestamptz", now).execute();
|
|
1311
|
+
await database.schema.createTable("api_rate_limit").addColumn("key_hash", "text", (column) => column.primaryKey()).addColumn("window_started_at", "timestamptz", required).addColumn("request_count", "integer", (column) => column.notNull().defaultTo(1)).addColumn("updated_at", "timestamptz", now).execute();
|
|
1312
|
+
await addChecks(database, [[
|
|
1313
|
+
"user_picture",
|
|
1314
|
+
"format",
|
|
1315
|
+
["png", "jpeg"]
|
|
1316
|
+
], [
|
|
1317
|
+
"user_picture",
|
|
1318
|
+
"source",
|
|
1319
|
+
["upload", "provider"]
|
|
1320
|
+
]]);
|
|
1321
|
+
await addIndexes(database, [["sign_in", ["user_id", "created_at"]], ["sign_in", ["session_id"]]]);
|
|
1574
1322
|
}
|
|
1575
1323
|
async function down$7(database) {
|
|
1576
|
-
await database
|
|
1577
|
-
}
|
|
1578
|
-
/** Keeps each document's current revision as the first checkpoint of its history. */
|
|
1579
|
-
async function keepCurrentRevisions(database) {
|
|
1580
|
-
await database.insertInto("documentVersion").columns([
|
|
1581
|
-
"id",
|
|
1582
|
-
"documentId",
|
|
1583
|
-
"revisionId",
|
|
1584
|
-
"kind",
|
|
1585
|
-
"createdBy",
|
|
1586
|
-
"createdAt"
|
|
1587
|
-
]).expression((expression) => expression.selectFrom("document").innerJoin("documentRevision", "documentRevision.id", "document.currentRevisionId").select((select) => [
|
|
1588
|
-
select.fn("gen_random_uuid").as("id"),
|
|
1589
|
-
"document.id as documentId",
|
|
1590
|
-
"documentRevision.id as revisionId",
|
|
1591
|
-
select.val("checkpoint").as("kind"),
|
|
1592
|
-
"documentRevision.createdBy",
|
|
1593
|
-
"documentRevision.createdAt"
|
|
1594
|
-
])).execute();
|
|
1324
|
+
await dropTables(database, TABLES$5);
|
|
1595
1325
|
}
|
|
1596
1326
|
//#endregion
|
|
1597
|
-
//#region src/server/db/migrations/
|
|
1598
|
-
var
|
|
1327
|
+
//#region src/server/db/migrations/002_workspaces.ts
|
|
1328
|
+
var _002_workspaces_exports = /* @__PURE__ */ __exportAll({
|
|
1599
1329
|
down: () => down$6,
|
|
1600
1330
|
up: () => up$6
|
|
1601
1331
|
});
|
|
1602
|
-
/**
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1332
|
+
/** Workspaces, their members, and the people invited to join. */
|
|
1333
|
+
const TABLES$4 = [
|
|
1334
|
+
"workspace",
|
|
1335
|
+
"workspace_member",
|
|
1336
|
+
"workspace_invitation"
|
|
1337
|
+
];
|
|
1606
1338
|
async function up$6(database) {
|
|
1607
|
-
await database.schema.
|
|
1339
|
+
await database.schema.createTable("workspace").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("name", "text", required).addColumn("slug", "text", (column) => column.notNull().unique()).addColumn("created_by", "text", required).addColumn("owner_id", "text", required).addColumn("created_at", "timestamptz", now).addColumn("updated_at", "timestamptz", now).execute();
|
|
1340
|
+
await database.schema.createTable("workspace_member").addColumn("workspace_id", "uuid", (column) => column.notNull().references("workspace.id").onDelete("cascade")).addColumn("user_id", "text", required).addColumn("role", "text", required).addColumn("created_at", "timestamptz", now).addPrimaryKeyConstraint("workspace_member_pkey", ["workspace_id", "user_id"]).execute();
|
|
1341
|
+
await withInvitation(database.schema.createTable("workspace_invitation"), "role").addColumn("workspace_id", "uuid", (column) => column.notNull().references("workspace.id").onDelete("cascade")).execute();
|
|
1342
|
+
await addChecks(database, [[
|
|
1343
|
+
"workspace_member",
|
|
1344
|
+
"role",
|
|
1345
|
+
ROLES
|
|
1346
|
+
], [
|
|
1347
|
+
"workspace_invitation",
|
|
1348
|
+
"role",
|
|
1349
|
+
ROLES
|
|
1350
|
+
]]);
|
|
1351
|
+
await addIndexes(database, [
|
|
1352
|
+
["workspace", ["owner_id"]],
|
|
1353
|
+
["workspace_member", ["user_id"]],
|
|
1354
|
+
["workspace_invitation", ["workspace_id", "revoked_at"]]
|
|
1355
|
+
]);
|
|
1608
1356
|
}
|
|
1609
1357
|
async function down$6(database) {
|
|
1610
|
-
await database
|
|
1358
|
+
await dropTables(database, TABLES$4);
|
|
1611
1359
|
}
|
|
1612
1360
|
//#endregion
|
|
1613
|
-
//#region src/server/db/migrations/
|
|
1614
|
-
var
|
|
1361
|
+
//#region src/server/db/migrations/003_storage.ts
|
|
1362
|
+
var _003_storage_exports = /* @__PURE__ */ __exportAll({
|
|
1615
1363
|
down: () => down$5,
|
|
1616
1364
|
up: () => up$5
|
|
1617
1365
|
});
|
|
1618
|
-
/**
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1366
|
+
/** Files in the object store, a workspace's images among them, and how much each workspace uses. */
|
|
1367
|
+
const TABLES$3 = [
|
|
1368
|
+
"storage_object",
|
|
1369
|
+
"workspace_storage_usage",
|
|
1370
|
+
"workspace_image"
|
|
1371
|
+
];
|
|
1624
1372
|
async function up$5(database) {
|
|
1625
|
-
await database.schema.
|
|
1626
|
-
await
|
|
1627
|
-
await database.schema.createTable("workspace_image").addColumn("workspace_id", "uuid", (column) => column.references("workspace.id").onDelete("cascade")
|
|
1628
|
-
await addCurrentTimestampColumns(database, "workspace_image", ["touched_at"]);
|
|
1629
|
-
await database.schema.createTable("revision_image").addColumn("revision_id", "uuid", (column) => column.references("document_revision.id").onDelete("cascade").notNull()).addColumn("name", "text", (column) => column.notNull()).addColumn("workspace_id", "uuid", (column) => column.notNull()).addColumn("checksum", "text", (column) => column.notNull()).addPrimaryKeyConstraint("revision_image_pkey", ["revision_id", "name"]).addForeignKeyConstraint("revision_image_image_fkey", ["workspace_id", "checksum"], "workspace_image", ["workspace_id", "checksum"]).execute();
|
|
1630
|
-
await database.schema.createIndex("revision_image_image_index").on("revision_image").columns(["workspace_id", "checksum"]).execute();
|
|
1373
|
+
await database.schema.createTable("storage_object").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("object_key", "text", (column) => column.notNull().unique()).addColumn("checksum", "text", required).addColumn("byte_size", "bigint", required).addColumn("content_type", "text", required).addColumn("created_at", "timestamptz", now).execute();
|
|
1374
|
+
await database.schema.createTable("workspace_storage_usage").addColumn("workspace_id", "uuid", (column) => column.primaryKey().references("workspace.id").onDelete("cascade")).addColumn("committed_bytes", "bigint", (column) => column.notNull().defaultTo(0)).addColumn("updated_at", "timestamptz", now).execute();
|
|
1375
|
+
await database.schema.createTable("workspace_image").addColumn("workspace_id", "uuid", (column) => column.notNull().references("workspace.id").onDelete("cascade")).addColumn("checksum", "text", required).addColumn("storage_object_id", "uuid", (column) => column.notNull().unique().references("storage_object.id")).addColumn("touched_at", "timestamptz", now).addPrimaryKeyConstraint("workspace_image_pkey", ["workspace_id", "checksum"]).execute();
|
|
1631
1376
|
}
|
|
1632
1377
|
async function down$5(database) {
|
|
1633
|
-
await database
|
|
1634
|
-
await database.schema.dropTable("workspace_image").execute();
|
|
1635
|
-
await database.schema.alterTable("upload").dropConstraint("upload_kind_check").execute();
|
|
1636
|
-
await database.schema.alterTable("upload").dropColumn("kind").execute();
|
|
1378
|
+
await dropTables(database, TABLES$3);
|
|
1637
1379
|
}
|
|
1638
1380
|
//#endregion
|
|
1639
|
-
//#region src/server/db/migrations/
|
|
1640
|
-
var
|
|
1381
|
+
//#region src/server/db/migrations/004_documents.ts
|
|
1382
|
+
var _004_documents_exports = /* @__PURE__ */ __exportAll({
|
|
1641
1383
|
down: () => down$4,
|
|
1642
1384
|
up: () => up$4
|
|
1643
1385
|
});
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
"
|
|
1647
|
-
"
|
|
1648
|
-
"
|
|
1649
|
-
"
|
|
1650
|
-
"
|
|
1651
|
-
"
|
|
1652
|
-
"
|
|
1653
|
-
"
|
|
1654
|
-
"password-changed"
|
|
1386
|
+
/** Documents and what they hold: revisions, versions, images, collaboration rooms, and uploads. */
|
|
1387
|
+
const TABLES$2 = [
|
|
1388
|
+
"document",
|
|
1389
|
+
"document_revision",
|
|
1390
|
+
"document_version",
|
|
1391
|
+
"revision_image",
|
|
1392
|
+
"document_room_snapshot",
|
|
1393
|
+
"document_room_update",
|
|
1394
|
+
"upload",
|
|
1395
|
+
"upload_storage_reservation"
|
|
1655
1396
|
];
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1660
|
-
|
|
1397
|
+
async function createRevisions(database) {
|
|
1398
|
+
await database.schema.createTable("document").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("workspace_id", "uuid", (column) => column.notNull().references("workspace.id").onDelete("cascade")).addColumn("name", "text", required).addColumn("kind", "text", (column) => column.notNull().defaultTo("design")).addColumn("current_revision_id", "uuid").addColumn("version", "integer", (column) => column.notNull().defaultTo(0)).addColumn("collaboration_epoch", "integer", (column) => column.notNull().defaultTo(0)).addColumn("created_by", "text", required).addColumn("created_at", "timestamptz", now).addColumn("updated_at", "timestamptz", now).addColumn("deleted_at", "timestamptz").addColumn("cleanup_claim_id", "uuid").addColumn("cleanup_claimed_at", "timestamptz").addColumn("disabled_at", "timestamptz").addColumn("disabled_reason", "text").execute();
|
|
1399
|
+
await database.schema.createTable("document_revision").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("parent_revision_id", "uuid", (column) => column.references("document_revision.id").onDelete("set null")).addColumn("storage_object_id", "uuid", (column) => column.notNull().references("storage_object.id").onDelete("restrict")).addColumn("created_by", "text", required).addColumn("created_at", "timestamptz", now).execute();
|
|
1400
|
+
await database.schema.alterTable("document").addForeignKeyConstraint("document_current_revision_id_fkey", ["current_revision_id"], "document_revision", ["id"], (constraint) => constraint.onDelete("set null")).execute();
|
|
1401
|
+
await database.schema.createTable("document_version").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("revision_id", "uuid", (column) => column.notNull().unique().references("document_revision.id").onDelete("cascade")).addColumn("kind", "text", required).addColumn("name", "text").addColumn("description", "text").addColumn("restored_from_version_id", "uuid", (column) => column.references("document_version.id").onDelete("set null")).addColumn("created_by", "text", required).addColumn("created_at", "timestamptz", now).execute();
|
|
1402
|
+
await database.schema.createTable("revision_image").addColumn("revision_id", "uuid", (column) => column.notNull().references("document_revision.id").onDelete("cascade")).addColumn("name", "text", required).addColumn("workspace_id", "uuid", required).addColumn("checksum", "text", required).addPrimaryKeyConstraint("revision_image_pkey", ["revision_id", "name"]).addForeignKeyConstraint("revision_image_workspace_id_checksum_fkey", ["workspace_id", "checksum"], "workspace_image", ["workspace_id", "checksum"]).execute();
|
|
1403
|
+
}
|
|
1404
|
+
async function createRooms(database) {
|
|
1405
|
+
await database.schema.createTable("document_room_snapshot").addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("room_epoch", "integer", required).addColumn("snapshot_object_id", "uuid", (column) => column.notNull().references("storage_object.id")).addColumn("through_sequence", "integer", required).addColumn("created_at", "timestamptz", now).addColumn("updated_at", "timestamptz", now).addPrimaryKeyConstraint("document_room_snapshot_pkey", ["document_id", "room_epoch"]).execute();
|
|
1406
|
+
await database.schema.createTable("document_room_update").addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("room_epoch", "integer", required).addColumn("sequence", "integer", required).addColumn("data", "bytea", required).addColumn("created_at", "timestamptz", now).addPrimaryKeyConstraint("document_room_update_pkey", [
|
|
1407
|
+
"document_id",
|
|
1408
|
+
"room_epoch",
|
|
1409
|
+
"sequence"
|
|
1410
|
+
]).execute();
|
|
1411
|
+
}
|
|
1412
|
+
async function createUploads(database) {
|
|
1413
|
+
await database.schema.createTable("upload").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("kind", "text", (column) => column.notNull().defaultTo("revision")).addColumn("base_revision_id", "uuid", (column) => column.references("document_revision.id").onDelete("set null")).addColumn("object_key", "text", (column) => column.notNull().unique()).addColumn("status", "text", required).addColumn("checksum", "text", required).addColumn("byte_size", "bigint", required).addColumn("content_type", "text", required).addColumn("multipart_upload_id", "text").addColumn("created_by", "text", required).addColumn("created_at", "timestamptz", now).addColumn("expires_at", "timestamptz", required).addColumn("finalization_started_at", "timestamptz").addColumn("cleanup_claim_id", "uuid").addColumn("cleanup_claimed_at", "timestamptz").execute();
|
|
1414
|
+
await database.schema.createTable("upload_storage_reservation").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("workspace_id", "uuid", (column) => column.notNull().references("workspace.id").onDelete("cascade")).addColumn("upload_id", "uuid", (column) => column.notNull().unique().references("upload.id").onDelete("cascade")).addColumn("bytes", "bigint", required).addColumn("created_at", "timestamptz", now).addColumn("expires_at", "timestamptz", required).addColumn("committed_at", "timestamptz").addColumn("released_at", "timestamptz").execute();
|
|
1415
|
+
}
|
|
1661
1416
|
async function up$4(database) {
|
|
1662
|
-
await database
|
|
1663
|
-
await database
|
|
1664
|
-
await database
|
|
1665
|
-
await
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1417
|
+
await createRevisions(database);
|
|
1418
|
+
await createRooms(database);
|
|
1419
|
+
await createUploads(database);
|
|
1420
|
+
await addChecks(database, [
|
|
1421
|
+
[
|
|
1422
|
+
"document",
|
|
1423
|
+
"kind",
|
|
1424
|
+
["design", "board"]
|
|
1425
|
+
],
|
|
1426
|
+
[
|
|
1427
|
+
"document_version",
|
|
1428
|
+
"kind",
|
|
1429
|
+
["checkpoint", "named"]
|
|
1430
|
+
],
|
|
1431
|
+
[
|
|
1432
|
+
"upload",
|
|
1433
|
+
"kind",
|
|
1434
|
+
["revision", "image"]
|
|
1435
|
+
],
|
|
1436
|
+
[
|
|
1437
|
+
"upload",
|
|
1438
|
+
"status",
|
|
1439
|
+
[
|
|
1440
|
+
"pending",
|
|
1441
|
+
"finalizing",
|
|
1442
|
+
"cleaning",
|
|
1443
|
+
"committed",
|
|
1444
|
+
"abandoned"
|
|
1445
|
+
]
|
|
1446
|
+
]
|
|
1447
|
+
]);
|
|
1448
|
+
await addIndexes(database, [
|
|
1449
|
+
["document", ["workspace_id", "updated_at"]],
|
|
1450
|
+
["document", ["deleted_at", "cleanup_claimed_at"]],
|
|
1451
|
+
["document_revision", ["document_id", "created_at"]],
|
|
1452
|
+
["document_version", [
|
|
1453
|
+
"document_id",
|
|
1454
|
+
"created_at desc",
|
|
1455
|
+
"id desc"
|
|
1456
|
+
]],
|
|
1457
|
+
["revision_image", ["workspace_id", "checksum"]],
|
|
1458
|
+
["upload", ["status", "expires_at"]]
|
|
1670
1459
|
]);
|
|
1671
1460
|
}
|
|
1672
1461
|
async function down$4(database) {
|
|
1673
|
-
await
|
|
1674
|
-
await database.schema.dropTable("sign_in").execute();
|
|
1462
|
+
await dropTables(database, TABLES$2);
|
|
1675
1463
|
}
|
|
1676
1464
|
//#endregion
|
|
1677
|
-
//#region src/server/db/migrations/
|
|
1678
|
-
var
|
|
1465
|
+
//#region src/server/db/migrations/005_sharing.ts
|
|
1466
|
+
var _005_sharing_exports = /* @__PURE__ */ __exportAll({
|
|
1679
1467
|
down: () => down$3,
|
|
1680
1468
|
up: () => up$3
|
|
1681
1469
|
});
|
|
1682
|
-
/**
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
["cloud_mfa_assurance", "mfa_assurance"],
|
|
1690
|
-
["cloud_admin_audit_event", "admin_audit_event"],
|
|
1691
|
-
["cloud_auth_schema", "auth_schema_version"],
|
|
1692
|
-
["cloud_rate_limit", "api_rate_limit"]
|
|
1470
|
+
/** Who a document is shared with: people, invitations, links, and the invitations budget. */
|
|
1471
|
+
const TABLES$1 = [
|
|
1472
|
+
"document_grant",
|
|
1473
|
+
"document_invitation",
|
|
1474
|
+
"invitation_continuation",
|
|
1475
|
+
"document_share",
|
|
1476
|
+
"invitation_send"
|
|
1693
1477
|
];
|
|
1694
|
-
const CONSTRAINTS$1 = [
|
|
1695
|
-
[
|
|
1696
|
-
"enrollment",
|
|
1697
|
-
"cloud_enrollment_pkey",
|
|
1698
|
-
"enrollment_pkey"
|
|
1699
|
-
],
|
|
1700
|
-
[
|
|
1701
|
-
"enrollment",
|
|
1702
|
-
"cloud_enrollment_email_normalized_key",
|
|
1703
|
-
"enrollment_email_normalized_key"
|
|
1704
|
-
],
|
|
1705
|
-
[
|
|
1706
|
-
"enrollment",
|
|
1707
|
-
"cloud_enrollment_status_check",
|
|
1708
|
-
"enrollment_status_check"
|
|
1709
|
-
],
|
|
1710
|
-
[
|
|
1711
|
-
"mfa_assurance",
|
|
1712
|
-
"cloud_mfa_assurance_pkey",
|
|
1713
|
-
"mfa_assurance_pkey"
|
|
1714
|
-
],
|
|
1715
|
-
[
|
|
1716
|
-
"admin_audit_event",
|
|
1717
|
-
"cloud_admin_audit_event_pkey",
|
|
1718
|
-
"admin_audit_event_pkey"
|
|
1719
|
-
],
|
|
1720
|
-
[
|
|
1721
|
-
"auth_schema_version",
|
|
1722
|
-
"cloud_auth_schema_pkey",
|
|
1723
|
-
"auth_schema_version_pkey"
|
|
1724
|
-
],
|
|
1725
|
-
[
|
|
1726
|
-
"api_rate_limit",
|
|
1727
|
-
"cloud_rate_limit_pkey",
|
|
1728
|
-
"api_rate_limit_pkey"
|
|
1729
|
-
]
|
|
1730
|
-
];
|
|
1731
|
-
const INDEXES = [["cloud_enrollment_status_index", "enrollment_status_index"], ["cloud_admin_audit_created_index", "admin_audit_event_created_index"]];
|
|
1732
|
-
async function rename(database, direction) {
|
|
1733
|
-
const forward = direction === "up";
|
|
1734
|
-
if (forward) for (const [from, to] of TABLES) await database.schema.alterTable(from).renameTo(to).execute();
|
|
1735
|
-
for (const [table, from, to] of CONSTRAINTS$1) await database.schema.alterTable(table).renameConstraint(forward ? from : to, forward ? to : from).execute();
|
|
1736
|
-
for (const [from, to] of INDEXES) {
|
|
1737
|
-
const [source, target] = forward ? [from, to] : [to, from];
|
|
1738
|
-
await sql`alter index ${sql.id(source)} rename to ${sql.id(target)}`.execute(database);
|
|
1739
|
-
}
|
|
1740
|
-
if (!forward) for (const [from, to] of TABLES) await database.schema.alterTable(to).renameTo(from).execute();
|
|
1741
|
-
}
|
|
1742
1478
|
async function up$3(database) {
|
|
1743
|
-
await
|
|
1479
|
+
await database.schema.createTable("document_grant").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("user_id", "text", required).addColumn("permission", "text", required).addColumn("created_by", "text", required).addColumn("created_at", "timestamptz", now).addColumn("updated_at", "timestamptz", now).addColumn("revoked_at", "timestamptz").addUniqueConstraint("document_grant_document_id_user_id_key", ["document_id", "user_id"]).execute();
|
|
1480
|
+
await withInvitation(database.schema.createTable("document_invitation"), "permission").addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).execute();
|
|
1481
|
+
await database.schema.createTable("invitation_continuation").addColumn("id", "text", (column) => column.primaryKey()).addColumn("invitation_id", "uuid", (column) => column.notNull().references("document_invitation.id").onDelete("cascade")).addColumn("token_encrypted", "text", required).addColumn("created_at", "timestamptz", now).addColumn("expires_at", "timestamptz", required).addColumn("consumed_at", "timestamptz").execute();
|
|
1482
|
+
await database.schema.createTable("document_share").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("permission", "text", required).addColumn("secret_hash", "text", required).addColumn("room_epoch", "integer", (column) => column.notNull().defaultTo(0)).addColumn("created_by", "text", required).addColumn("created_at", "timestamptz", now).addColumn("updated_at", "timestamptz", now).addColumn("expires_at", "timestamptz").addColumn("revoked_at", "timestamptz").addColumn("last_used_at", "timestamptz").execute();
|
|
1483
|
+
await database.schema.createTable("invitation_send").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("sender_id", "text", required).addColumn("email_normalized", "text", required).addColumn("sent_at", "timestamptz", now).execute();
|
|
1484
|
+
await addChecks(database, [
|
|
1485
|
+
[
|
|
1486
|
+
"document_grant",
|
|
1487
|
+
"permission",
|
|
1488
|
+
PERMISSIONS
|
|
1489
|
+
],
|
|
1490
|
+
[
|
|
1491
|
+
"document_invitation",
|
|
1492
|
+
"permission",
|
|
1493
|
+
PERMISSIONS
|
|
1494
|
+
],
|
|
1495
|
+
[
|
|
1496
|
+
"document_share",
|
|
1497
|
+
"permission",
|
|
1498
|
+
PERMISSIONS
|
|
1499
|
+
]
|
|
1500
|
+
]);
|
|
1501
|
+
await addIndexes(database, [
|
|
1502
|
+
["document_grant", ["user_id", "document_id"]],
|
|
1503
|
+
["document_invitation", ["document_id", "revoked_at"]],
|
|
1504
|
+
["document_share", ["document_id", "revoked_at"]],
|
|
1505
|
+
["invitation_continuation", ["expires_at"]],
|
|
1506
|
+
["invitation_send", ["sender_id", "sent_at"]],
|
|
1507
|
+
["invitation_send", ["email_normalized", "sent_at"]]
|
|
1508
|
+
]);
|
|
1744
1509
|
}
|
|
1745
1510
|
async function down$3(database) {
|
|
1746
|
-
await
|
|
1511
|
+
await dropTables(database, TABLES$1);
|
|
1747
1512
|
}
|
|
1748
1513
|
//#endregion
|
|
1749
|
-
//#region src/server/db/migrations/
|
|
1750
|
-
var
|
|
1514
|
+
//#region src/server/db/migrations/006_operations.ts
|
|
1515
|
+
var _006_operations_exports = /* @__PURE__ */ __exportAll({
|
|
1751
1516
|
down: () => down$2,
|
|
1752
1517
|
up: () => up$2
|
|
1753
1518
|
});
|
|
1754
|
-
/**
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1519
|
+
/** Running the deployment: the email outbox, enrollment, reports, and the administrators' log. */
|
|
1520
|
+
const TABLES = [
|
|
1521
|
+
"transactional_email",
|
|
1522
|
+
"enrollment",
|
|
1523
|
+
"document_report",
|
|
1524
|
+
"admin_audit_event"
|
|
1525
|
+
];
|
|
1758
1526
|
async function up$2(database) {
|
|
1759
|
-
await database.schema.createTable("
|
|
1527
|
+
await database.schema.createTable("transactional_email").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("idempotency_key", "text", (column) => column.notNull().unique()).addColumn("kind", "text", required).addColumn("recipient_email_normalized", "text", required).addColumn("payload_encrypted", "text").addColumn("status", "text", (column) => column.notNull().defaultTo("pending")).addColumn("attempt_count", "integer", (column) => column.notNull().defaultTo(0)).addColumn("next_attempt_at", "timestamptz", now).addColumn("claim_id", "uuid").addColumn("claimed_at", "timestamptz").addColumn("transport", "text").addColumn("transport_message_id", "text").addColumn("last_error_code", "text").addColumn("created_at", "timestamptz", now).addColumn("updated_at", "timestamptz", now).addColumn("accepted_at", "timestamptz").execute();
|
|
1528
|
+
await database.schema.createTable("enrollment").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("email_normalized", "text", (column) => column.notNull().unique()).addColumn("name", "text").addColumn("reason", "text").addColumn("status", "text", (column) => column.notNull().defaultTo("pending")).addColumn("request_revision", "integer", (column) => column.notNull().defaultTo(1)).addColumn("requested_at", "timestamptz", now).addColumn("reviewed_at", "timestamptz").addColumn("reviewed_by", "text").addColumn("review_note", "text").addColumn("approved_user_id", "text").execute();
|
|
1529
|
+
await database.schema.createTable("document_report").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("document_id", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("share_id", "uuid").addColumn("reporter_user_id", "text").addColumn("reporter_guest_id", "text").addColumn("reply_email", "text").addColumn("reason", "text", required).addColumn("description", "text", required).addColumn("status", "text", (column) => column.notNull().defaultTo("open")).addColumn("created_at", "timestamptz", now).addColumn("resolved_at", "timestamptz").addColumn("resolved_by", "text").execute();
|
|
1530
|
+
await database.schema.createTable("admin_audit_event").addColumn("id", "uuid", (column) => column.primaryKey()).addColumn("actor_user_id", "text", required).addColumn("action", "text", required).addColumn("subject_type", "text", required).addColumn("subject_id", "text", required).addColumn("metadata", "jsonb", (column) => column.notNull().defaultTo(EMPTY_JSON_OBJECT)).addColumn("created_at", "timestamptz", now).execute();
|
|
1531
|
+
await addChecks(database, [
|
|
1532
|
+
[
|
|
1533
|
+
"transactional_email",
|
|
1534
|
+
"kind",
|
|
1535
|
+
[
|
|
1536
|
+
"document-invitation",
|
|
1537
|
+
"workspace-invitation",
|
|
1538
|
+
"enrollment-requested",
|
|
1539
|
+
"admin-enrollment-notification",
|
|
1540
|
+
"enrollment-approved",
|
|
1541
|
+
"enrollment-rejected",
|
|
1542
|
+
"enrollment-revoked",
|
|
1543
|
+
"email-verification",
|
|
1544
|
+
"password-reset",
|
|
1545
|
+
"password-changed",
|
|
1546
|
+
"email-change-requested",
|
|
1547
|
+
"new-sign-in",
|
|
1548
|
+
"two-step-changed",
|
|
1549
|
+
"document-report"
|
|
1550
|
+
]
|
|
1551
|
+
],
|
|
1552
|
+
[
|
|
1553
|
+
"transactional_email",
|
|
1554
|
+
"status",
|
|
1555
|
+
[
|
|
1556
|
+
"pending",
|
|
1557
|
+
"sending",
|
|
1558
|
+
"accepted",
|
|
1559
|
+
"failed",
|
|
1560
|
+
"suppressed"
|
|
1561
|
+
]
|
|
1562
|
+
],
|
|
1563
|
+
[
|
|
1564
|
+
"enrollment",
|
|
1565
|
+
"status",
|
|
1566
|
+
[
|
|
1567
|
+
"pending",
|
|
1568
|
+
"approved",
|
|
1569
|
+
"rejected",
|
|
1570
|
+
"revoked"
|
|
1571
|
+
]
|
|
1572
|
+
],
|
|
1573
|
+
[
|
|
1574
|
+
"document_report",
|
|
1575
|
+
"reason",
|
|
1576
|
+
[
|
|
1577
|
+
"spam-phishing",
|
|
1578
|
+
"illegal",
|
|
1579
|
+
"copyright",
|
|
1580
|
+
"harassment",
|
|
1581
|
+
"other"
|
|
1582
|
+
]
|
|
1583
|
+
],
|
|
1584
|
+
[
|
|
1585
|
+
"document_report",
|
|
1586
|
+
"status",
|
|
1587
|
+
[
|
|
1588
|
+
"open",
|
|
1589
|
+
"dismissed",
|
|
1590
|
+
"actioned"
|
|
1591
|
+
]
|
|
1592
|
+
]
|
|
1593
|
+
]);
|
|
1594
|
+
await addIndexes(database, [
|
|
1595
|
+
["transactional_email", ["status", "next_attempt_at"]],
|
|
1596
|
+
["enrollment", ["status", "requested_at"]],
|
|
1597
|
+
["document_report", ["document_id"]],
|
|
1598
|
+
["document_report", ["share_id", "created_at"]],
|
|
1599
|
+
["document_report", ["status", "created_at"]],
|
|
1600
|
+
["admin_audit_event", ["created_at"]]
|
|
1601
|
+
]);
|
|
1760
1602
|
}
|
|
1761
1603
|
async function down$2(database) {
|
|
1762
|
-
await database
|
|
1604
|
+
await dropTables(database, TABLES);
|
|
1763
1605
|
}
|
|
1764
1606
|
//#endregion
|
|
1765
|
-
//#region src/server/db/migrations/
|
|
1766
|
-
var
|
|
1607
|
+
//#region src/server/db/migrations/007_trash.ts
|
|
1608
|
+
var _007_trash_exports = /* @__PURE__ */ __exportAll({
|
|
1767
1609
|
down: () => down$1,
|
|
1768
1610
|
up: () => up$1
|
|
1769
1611
|
});
|
|
1770
1612
|
/**
|
|
1771
|
-
*
|
|
1772
|
-
*
|
|
1613
|
+
* Trash: who deleted a document, and who deleted it forever, when, and the storage that took off
|
|
1614
|
+
* its workspace's usage while its files wait out the grace before cleanup destroys them.
|
|
1773
1615
|
*/
|
|
1774
1616
|
async function up$1(database) {
|
|
1775
|
-
await database.schema.alterTable("document").addColumn("
|
|
1776
|
-
await
|
|
1617
|
+
await database.schema.alterTable("document").addColumn("deleted_by", "text").addColumn("purged_at", "timestamptz").addColumn("purged_by", "text").addColumn("purged_bytes", "bigint", (column) => column.notNull().defaultTo(0)).execute();
|
|
1618
|
+
await addIndexes(database, [["document", ["workspace_id", "deleted_at desc"]], ["document", ["purged_at"]]]);
|
|
1777
1619
|
}
|
|
1778
1620
|
async function down$1(database) {
|
|
1779
|
-
await database.schema.
|
|
1621
|
+
await database.schema.dropIndex("document_purged_at_idx").execute();
|
|
1622
|
+
await database.schema.dropIndex("document_workspace_id_deleted_at_idx").execute();
|
|
1623
|
+
await database.schema.alterTable("document").dropColumn("purged_bytes").dropColumn("purged_by").dropColumn("purged_at").dropColumn("deleted_by").execute();
|
|
1780
1624
|
}
|
|
1781
1625
|
//#endregion
|
|
1782
|
-
//#region src/server/db/migrations/
|
|
1783
|
-
var
|
|
1626
|
+
//#region src/server/db/migrations/008_terms.ts
|
|
1627
|
+
var _008_terms_exports = /* @__PURE__ */ __exportAll({
|
|
1784
1628
|
down: () => down,
|
|
1785
|
-
startRoomsOver: () => startRoomsOver,
|
|
1786
1629
|
up: () => up
|
|
1787
1630
|
});
|
|
1788
|
-
/**
|
|
1789
|
-
const CONSTRAINTS = [["documentCollaborationStatePrimary", "documentRoomSnapshotPrimary"], ["documentCollaborationStateDocumentIdFkey", "documentRoomSnapshotDocumentIdFkey"]];
|
|
1790
|
-
async function renameConstraints(database, direction) {
|
|
1791
|
-
for (const [old, current] of CONSTRAINTS) {
|
|
1792
|
-
const [from, to] = direction === "up" ? [old, current] : [current, old];
|
|
1793
|
-
await database.schema.alterTable("documentRoomSnapshot").renameConstraint(from, to).execute();
|
|
1794
|
-
}
|
|
1795
|
-
}
|
|
1796
|
-
/** Moves every document with a saved room to a new epoch, and drops the saved rooms. */
|
|
1797
|
-
async function startRoomsOver(database) {
|
|
1798
|
-
await database.updateTable("document").set((expression) => ({ collaborationEpoch: expression("collaborationEpoch", "+", 1) })).where("id", "in", database.selectFrom("documentRoomSnapshot").select("documentId")).execute();
|
|
1799
|
-
await database.deleteFrom("documentRoomSnapshot").execute();
|
|
1800
|
-
}
|
|
1801
|
-
/**
|
|
1802
|
-
* A room's edits as an append-only log beside a snapshot kept in the object store, so a save
|
|
1803
|
-
* writes what changed instead of the whole room, and the room's bulk lives with the documents'
|
|
1804
|
-
* files rather than in the database. The snapshot row names its stored file and the last logged
|
|
1805
|
-
* update it holds; compaction points it at a new file and drops the updates it covers.
|
|
1806
|
-
*
|
|
1807
|
-
* Rooms saved before held their snapshots in the row itself. Those rows go, and each such
|
|
1808
|
-
* document moves to a new collaboration epoch, so its next room starts from its last saved
|
|
1809
|
-
* revision and no tab's local copy of the earlier room joins it.
|
|
1810
|
-
*/
|
|
1631
|
+
/** Which version of the deployment's terms each account accepted, and when. */
|
|
1811
1632
|
async function up(database) {
|
|
1812
|
-
await database.schema.
|
|
1813
|
-
await renameConstraints(database, "up");
|
|
1814
|
-
await startRoomsOver(database);
|
|
1815
|
-
await database.schema.alterTable("documentRoomSnapshot").dropColumn("state").dropColumn("version").addColumn("snapshotObjectId", "uuid", (column) => column.notNull().references("storageObject.id")).addColumn("throughSequence", "integer", (column) => column.notNull()).execute();
|
|
1816
|
-
await database.schema.createTable("documentRoomUpdate").addColumn("documentId", "uuid", (column) => column.notNull().references("document.id").onDelete("cascade")).addColumn("roomEpoch", "integer", (column) => column.notNull()).addColumn("sequence", "integer", (column) => column.notNull()).addColumn("data", "bytea", (column) => column.notNull()).addColumn("createdAt", "timestamptz", (column) => column.notNull().defaultTo(database.fn("now"))).addPrimaryKeyConstraint("documentRoomUpdatePrimary", [
|
|
1817
|
-
"documentId",
|
|
1818
|
-
"roomEpoch",
|
|
1819
|
-
"sequence"
|
|
1820
|
-
]).execute();
|
|
1633
|
+
await database.schema.createTable("terms_acceptance").addColumn("user_id", "text", required).addColumn("version", "text", required).addColumn("accepted_at", "timestamptz", now).addPrimaryKeyConstraint("terms_acceptance_pkey", ["user_id", "version"]).execute();
|
|
1821
1634
|
}
|
|
1822
|
-
/** Back to snapshots in rows; the rooms saved meanwhile start over, as they did going up. */
|
|
1823
1635
|
async function down(database) {
|
|
1824
|
-
await database.schema.dropTable("
|
|
1825
|
-
await database.deleteFrom("documentRoomSnapshot").execute();
|
|
1826
|
-
await database.schema.alterTable("documentRoomSnapshot").dropColumn("snapshotObjectId").dropColumn("throughSequence").addColumn("state", "bytea", (column) => column.notNull()).addColumn("version", "integer", (column) => column.notNull().defaultTo(1)).execute();
|
|
1827
|
-
await renameConstraints(database, "down");
|
|
1828
|
-
await database.schema.alterTable("documentRoomSnapshot").renameTo("documentCollaborationState").execute();
|
|
1636
|
+
await database.schema.dropTable("terms_acceptance").execute();
|
|
1829
1637
|
}
|
|
1830
1638
|
//#endregion
|
|
1831
1639
|
//#region src/server/db/migrate.ts
|
|
1832
1640
|
const migrations = {
|
|
1833
|
-
"
|
|
1834
|
-
"
|
|
1835
|
-
"
|
|
1836
|
-
"
|
|
1837
|
-
"
|
|
1838
|
-
"
|
|
1839
|
-
"
|
|
1840
|
-
"
|
|
1841
|
-
"009_workspace_entitlements": _009_workspace_entitlements_exports,
|
|
1842
|
-
"010_upload_finalization": _010_upload_finalization_exports,
|
|
1843
|
-
"011_upload_finalization_lease": _011_upload_finalization_lease_exports,
|
|
1844
|
-
"012_transactional_email": _012_transactional_email_exports,
|
|
1845
|
-
"013_admin_enrollment": _013_admin_enrollment_exports,
|
|
1846
|
-
"014_enrollment_email_kinds": _014_enrollment_email_kinds_exports,
|
|
1847
|
-
"015_cloud_rate_limit": _015_cloud_rate_limit_exports,
|
|
1848
|
-
"016_auth_schema": _016_auth_schema_exports,
|
|
1849
|
-
"017_authentication_email_kinds": _017_authentication_email_kinds_exports,
|
|
1850
|
-
"018_authentication_mfa": _018_authentication_mfa_exports,
|
|
1851
|
-
"019_better_auth_account_issuer": _019_better_auth_account_issuer_exports,
|
|
1852
|
-
"020_workspace_invitations": _020_workspace_invitations_exports,
|
|
1853
|
-
"021_document_versions": _021_document_versions_exports,
|
|
1854
|
-
"022_entitlement_external_version": _022_entitlement_external_version_exports,
|
|
1855
|
-
"023_document_images": _023_document_images_exports,
|
|
1856
|
-
"024_account_activity": _024_account_activity_exports,
|
|
1857
|
-
"025_table_names": _025_table_names_exports,
|
|
1858
|
-
"026_user_picture": _026_user_picture_exports,
|
|
1859
|
-
"027_document_kind": _027_document_kind_exports,
|
|
1860
|
-
"028_collaboration_updates": _028_collaboration_updates_exports
|
|
1641
|
+
"001_accounts": _001_accounts_exports,
|
|
1642
|
+
"002_workspaces": _002_workspaces_exports,
|
|
1643
|
+
"003_storage": _003_storage_exports,
|
|
1644
|
+
"004_documents": _004_documents_exports,
|
|
1645
|
+
"005_sharing": _005_sharing_exports,
|
|
1646
|
+
"006_operations": _006_operations_exports,
|
|
1647
|
+
"007_trash": _007_trash_exports,
|
|
1648
|
+
"008_terms": _008_terms_exports
|
|
1861
1649
|
};
|
|
1650
|
+
/** Cloud's migrations, in order. */
|
|
1862
1651
|
var CloudMigrationProvider = class {
|
|
1863
1652
|
async getMigrations() {
|
|
1864
1653
|
return migrations;
|
|
1865
1654
|
}
|
|
1866
1655
|
};
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
"email_verified",
|
|
1873
|
-
"created_at",
|
|
1874
|
-
"updated_at",
|
|
1875
|
-
"role",
|
|
1876
|
-
"banned",
|
|
1877
|
-
"two_factor_enabled"
|
|
1878
|
-
],
|
|
1879
|
-
session: [
|
|
1880
|
-
"id",
|
|
1881
|
-
"expires_at",
|
|
1882
|
-
"token",
|
|
1883
|
-
"user_id",
|
|
1884
|
-
"impersonated_by"
|
|
1885
|
-
],
|
|
1886
|
-
account: [
|
|
1887
|
-
"id",
|
|
1888
|
-
"account_id",
|
|
1889
|
-
"provider_id",
|
|
1890
|
-
"user_id"
|
|
1891
|
-
],
|
|
1892
|
-
verification: [
|
|
1893
|
-
"id",
|
|
1894
|
-
"identifier",
|
|
1895
|
-
"value",
|
|
1896
|
-
"expires_at"
|
|
1897
|
-
],
|
|
1898
|
-
device_code: [
|
|
1899
|
-
"id",
|
|
1900
|
-
"device_code",
|
|
1901
|
-
"user_code",
|
|
1902
|
-
"status",
|
|
1903
|
-
"client_id"
|
|
1904
|
-
],
|
|
1905
|
-
rate_limit: [
|
|
1906
|
-
"id",
|
|
1907
|
-
"key",
|
|
1908
|
-
"count",
|
|
1909
|
-
"last_request"
|
|
1910
|
-
],
|
|
1911
|
-
two_factor: [
|
|
1912
|
-
"id",
|
|
1913
|
-
"secret",
|
|
1914
|
-
"backup_codes",
|
|
1915
|
-
"user_id",
|
|
1916
|
-
"verified",
|
|
1917
|
-
"failed_verification_count",
|
|
1918
|
-
"locked_until"
|
|
1919
|
-
],
|
|
1920
|
-
passkey: [
|
|
1921
|
-
"id",
|
|
1922
|
-
"public_key",
|
|
1923
|
-
"user_id",
|
|
1924
|
-
"credential_id",
|
|
1925
|
-
"counter",
|
|
1926
|
-
"device_type",
|
|
1927
|
-
"backed_up"
|
|
1928
|
-
]
|
|
1929
|
-
};
|
|
1930
|
-
async function existingAuthSchema(database) {
|
|
1931
|
-
const tables = await database.introspection.getTables();
|
|
1932
|
-
return new Map(tables.filter((table) => Object.hasOwn(REQUIRED_AUTH_SCHEMA, table.name)).map((table) => [table.name, new Set(table.columns.map((column) => column.name))]));
|
|
1933
|
-
}
|
|
1656
|
+
/**
|
|
1657
|
+
* Better Auth's tables come from its own migration API, once, after Cloud's: the version they
|
|
1658
|
+
* were built for is recorded, and a different one stops startup until someone migrates it
|
|
1659
|
+
* deliberately, since Better Auth cannot say what an upgrade between its versions changes.
|
|
1660
|
+
*/
|
|
1934
1661
|
async function migrateAuthSchema(database, migration) {
|
|
1935
1662
|
const recorded = await database.selectFrom("authSchemaVersion").select("version").where("id", "=", "better-auth").executeTakeFirst();
|
|
1936
1663
|
if (recorded) {
|
|
1937
1664
|
if (recorded.version !== migration.schemaVersion) throw new Error(`Better Auth schema ${recorded.version} requires an explicit migration to ${migration.schemaVersion}`);
|
|
1938
1665
|
return;
|
|
1939
1666
|
}
|
|
1940
|
-
|
|
1941
|
-
if (existing.size === 0) await migration.run();
|
|
1942
|
-
else {
|
|
1943
|
-
const missing = Object.entries(REQUIRED_AUTH_SCHEMA).flatMap(([table, requiredColumns]) => {
|
|
1944
|
-
const columns = existing.get(table);
|
|
1945
|
-
if (!columns) return [table];
|
|
1946
|
-
return requiredColumns.filter((column) => !columns.has(column)).map((column) => `${table}.${column}`);
|
|
1947
|
-
});
|
|
1948
|
-
if (missing.length > 0) throw new Error(`Existing Better Auth schema is incomplete: ${missing.join(", ")}`);
|
|
1949
|
-
}
|
|
1667
|
+
await migration.run();
|
|
1950
1668
|
await database.insertInto("authSchemaVersion").values({
|
|
1951
1669
|
id: "better-auth",
|
|
1952
1670
|
version: migration.schemaVersion,
|
|
@@ -1995,6 +1713,6 @@ async function listen(fetch, port, hostname) {
|
|
|
1995
1713
|
};
|
|
1996
1714
|
}
|
|
1997
1715
|
//#endregion
|
|
1998
|
-
export {
|
|
1716
|
+
export { CLOUD_API_MAX_BODY_BYTES, CLOUD_DISCOVERY_PATH, CLOUD_PROTOCOL_MAX_UPLOAD_BYTES, CLOUD_SERVICES_API_PATHS, CLOUD_SERVICES_JWKS_PATH, CLOUD_SERVICES_TOKEN_AUDIENCE, COLLAB_ACTIONS, CURRENT_TIMESTAMP, CloudConfigError, MAX_COLLAB_MESSAGE_BYTES, RELAY_CLOSE, RELAY_DOCUMENT_PARAM, RELAY_HEARTBEAT, RELAY_PATH, RELAY_SERVER_PEER, SERVICE_ERROR_STATUS, __exportAll, accountParams, cloudDeploymentConfigSchema, createRelayClientAssembler, decodeRelayClientFrame, decodeRoomUpdate, defaultRelayURL, documentDownloadSchema, documentInvitationParams, documentParams, documentPermissionSchema, documentShareParams, documentSummarySchema, documentUserParams, documentVersionParams, encodeSyncReply, httpURLSchema, idParams, invitationContinuationParams, invitationParams, listen, lockForTransaction, migrateCloudDatabase, migrateToLatest, parseCloudDiscovery, parseCloudServerConfig, parseCommitImageUploads, parseCommitUpload, parseCreateDocument, parseCreateServicesToken, parseCreateUpload, parseRequestImageLinks, parseRequestImageUploads, parseUsageQuery, pictureParams, presenceName, relayServerMessages, requestImageLinksSchema, serviceCatalogSchema, servicesTokenClaimsSchema, shareParams, staticEntitlementsSchema, uploadParams, validatedJSON, validatedQuery, workspaceInvitationParams, workspaceMemberParams, workspaceParams };
|
|
1999
1717
|
|
|
2000
1718
|
//# sourceMappingURL=listen.mjs.map
|