anbaric-cloud-hosting 1.43.0 → 1.44.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -74,3 +74,10 @@ they are persisted.
74
74
 
75
75
  Postgres schema management is automatic on boot (`ensureSchema`): workflow
76
76
  tables in `public`, platform tables in `anbaric_system`.
77
+
78
+ Schema changes are migrations. `src/data-store/PlatformMigrations.ts` lists them
79
+ in order, each with a stable id; `migrate` records the ids it has applied in
80
+ `anbaric_schema_migrations`, takes an advisory lock so two instances starting
81
+ together take turns, and runs each migration in its own transaction. A migration
82
+ that fails stops the boot. To change the schema, append a numbered entry —
83
+ never edit one that has shipped, and never reuse an id.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "anbaric-cloud-hosting",
3
- "version": "1.43.0",
3
+ "version": "1.44.0",
4
4
  "description": "Anbaric Cloud hosting service: Postgres-backed job persistence and queuing exposed over an HTTP API",
5
5
  "license": "MIT",
6
6
  "author": "chris@anbaric.ai",
@@ -18,18 +18,18 @@
18
18
  "@aws-sdk/client-s3": "^3.1110.0",
19
19
  "@aws-sdk/client-secrets-manager": "^3.700.0",
20
20
  "@aws-sdk/client-servicediscovery": "^3.1110.0",
21
- "anbaric-data-store": "^1.43.0",
22
- "anbaric-plugins": "^1.43.0",
23
- "anbaric-tsapi": "^1.43.0",
24
- "anbaric-web": "^1.43.0",
21
+ "anbaric-data-store": "^1.44.0",
22
+ "anbaric-plugins": "^1.44.0",
23
+ "anbaric-tsapi": "^1.44.0",
24
+ "anbaric-web": "^1.44.0",
25
25
  "esbuild": "^0.28.2",
26
26
  "pg": "^8.16.0"
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/node": "^26.2.0",
30
30
  "@types/pg": "^8.15.0",
31
- "anbaric-impl-cloud": "^1.43.0",
32
- "anbaric-state-machine": "^1.43.0",
31
+ "anbaric-impl-cloud": "^1.44.0",
32
+ "anbaric-state-machine": "^1.44.0",
33
33
  "tsx": "^4.20.0",
34
34
  "typescript": "^7.0.2"
35
35
  },
@@ -1,6 +1,10 @@
1
1
  /* What a person may do in the tenant this platform serves. The roles are
2
2
  assigned in Anbaric Cloud's central login, which owns the tenant definitions
3
- and the user-to-tenant mapping; the platform only enforces them. */
3
+ and the user-to-tenant mapping; the platform only enforces them.
4
+
5
+ Mirrored by that control plane's own tenants/TenantRole.ts rather than
6
+ shared with it, so the two repos need not move in lockstep over a four-value
7
+ union. Keep them in step. */
4
8
  type TenantRole = "OWNER" | "ADMIN" | "BUILDER" | "USER";
5
9
 
6
10
  const TENANT_ROLES : Array<TenantRole> = ["OWNER", "ADMIN", "BUILDER", "USER"];
@@ -0,0 +1,74 @@
1
+ import {Pool, PoolClient} from "pg";
2
+
3
+ type Migration = {
4
+
5
+ /* A stable identifier, recorded once the migration has run. Never reuse or
6
+ rename one: the record is the only thing standing between a second boot
7
+ and running the same change twice. Numbered prefixes keep the order
8
+ obvious to a reader ("003-tenant-roles"). */
9
+ id : string,
10
+
11
+ run : (client : PoolClient) => Promise<void>,
12
+
13
+ };
14
+
15
+ const TABLE = "anbaric_schema_migrations";
16
+
17
+ // One well-known lock, so two instances booting together take it in turns
18
+ // rather than racing to apply the same migration.
19
+ const LOCK_KEY = 8_233_120;
20
+
21
+ /* Applies whatever has not been applied yet, in order, each in its own
22
+ transaction. A migration that throws stops the boot: coming up against a
23
+ half-changed database is worse than not coming up at all. */
24
+ const migrate = async (pool : Pool, migrations : Array<Migration>) : Promise<Array<string>> => {
25
+ const duplicate = migrations.find((migration, at) => migrations.findIndex(other => other.id === migration.id) !== at);
26
+ if (duplicate) throw new Error(`Duplicate migration id "${duplicate.id}"`);
27
+
28
+ const client = await pool.connect();
29
+ const applied : Array<string> = [];
30
+
31
+ try {
32
+ await client.query(`
33
+ CREATE TABLE IF NOT EXISTS ${TABLE} (
34
+ id TEXT PRIMARY KEY,
35
+ applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
36
+ )
37
+ `);
38
+ await client.query("SELECT pg_advisory_lock($1)", [LOCK_KEY]);
39
+
40
+ const done = new Set((await client.query(`SELECT id FROM ${TABLE}`)).rows.map(row => row.id));
41
+
42
+ for (const migration of migrations) {
43
+ if (done.has(migration.id)) continue;
44
+
45
+ try {
46
+ await client.query("BEGIN");
47
+ await migration.run(client);
48
+ await client.query(`INSERT INTO ${TABLE} (id) VALUES ($1)`, [migration.id]);
49
+ await client.query("COMMIT");
50
+ } catch (error) {
51
+ await client.query("ROLLBACK").catch(() => {});
52
+ throw new Error(`Migration "${migration.id}" failed: ${error instanceof Error ? error.message : error}`);
53
+ }
54
+ applied.push(migration.id);
55
+ }
56
+
57
+ return applied;
58
+ } finally {
59
+ await client.query("SELECT pg_advisory_unlock($1)", [LOCK_KEY]).catch(() => {});
60
+ client.release();
61
+ }
62
+ };
63
+
64
+ /* The common case: a migration that is just SQL. Several statements may be
65
+ given, and they run in the one transaction the migration already has. */
66
+ const statements = (id : string, ...sql : Array<string>) : Migration => ({
67
+ id,
68
+ run: async (client) => {
69
+ for (const statement of sql) await client.query(statement);
70
+ },
71
+ });
72
+
73
+ export { migrate, statements };
74
+ export type { Migration };
@@ -0,0 +1,209 @@
1
+ import {Migration, statements} from "./Migrations";
2
+
3
+ /* Workflow data (jobs, queue, documents) lives in the default public schema;
4
+ platform system tables are encapsulated in the anbaric_system schema so the
5
+ two never mix.
6
+
7
+ The baseline is everything the schema had grown to before migrations were
8
+ recorded, so it is written defensively (IF NOT EXISTS, guarded DO blocks) and
9
+ is safe against a database that already has all of it. Everything after it is
10
+ a numbered step that runs exactly once: write new changes as plain SQL and
11
+ never edit a migration that has shipped. */
12
+ const PLATFORM_MIGRATIONS : Array<Migration> = [
13
+
14
+ statements("001-baseline",
15
+ `CREATE TABLE IF NOT EXISTS jobs (
16
+ id TEXT PRIMARY KEY,
17
+ state TEXT NOT NULL,
18
+ properties JSONB NOT NULL DEFAULT '{}',
19
+ workflow_id TEXT,
20
+ started_at TIMESTAMPTZ NOT NULL DEFAULT now(),
21
+ started_by TEXT NOT NULL DEFAULT 'system',
22
+ last_updated TIMESTAMPTZ NOT NULL DEFAULT now(),
23
+ transitions JSONB NOT NULL DEFAULT '[]',
24
+ status TEXT NOT NULL DEFAULT 'active',
25
+ inserted_at BIGINT GENERATED ALWAYS AS IDENTITY
26
+ )`,
27
+ "ALTER TABLE jobs ADD COLUMN IF NOT EXISTS workflow_id TEXT",
28
+ "ALTER TABLE jobs ADD COLUMN IF NOT EXISTS started_at TIMESTAMPTZ NOT NULL DEFAULT now()",
29
+ "ALTER TABLE jobs ADD COLUMN IF NOT EXISTS started_by TEXT NOT NULL DEFAULT 'system'",
30
+ "ALTER TABLE jobs ADD COLUMN IF NOT EXISTS last_updated TIMESTAMPTZ NOT NULL DEFAULT now()",
31
+ "ALTER TABLE jobs ADD COLUMN IF NOT EXISTS transitions JSONB NOT NULL DEFAULT '[]'",
32
+ "ALTER TABLE jobs ADD COLUMN IF NOT EXISTS killed BOOLEAN NOT NULL DEFAULT false",
33
+ "ALTER TABLE jobs ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'active'",
34
+ "ALTER TABLE jobs ADD COLUMN IF NOT EXISTS app_id TEXT",
35
+ `UPDATE jobs
36
+ SET app_id = split_part(workflow_id, '/', 1),
37
+ workflow_id = substring(workflow_id from position('/' in workflow_id) + 1)
38
+ WHERE app_id IS NULL AND workflow_id LIKE '%/%'`,
39
+ `CREATE TABLE IF NOT EXISTS awaits (
40
+ id UUID PRIMARY KEY,
41
+ metadata JSONB NOT NULL DEFAULT '{}',
42
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now()
43
+ )`,
44
+ "ALTER TABLE jobs ADD COLUMN IF NOT EXISTS waiting_for UUID REFERENCES awaits(id) ON DELETE SET NULL",
45
+ `CREATE TABLE IF NOT EXISTS documents (
46
+ app_id TEXT NOT NULL DEFAULT '',
47
+ collection TEXT NOT NULL,
48
+ id TEXT NOT NULL,
49
+ document JSONB NOT NULL,
50
+ inserted_at BIGINT GENERATED ALWAYS AS IDENTITY,
51
+ PRIMARY KEY (app_id, collection, id)
52
+ )`,
53
+ "ALTER TABLE documents ADD COLUMN IF NOT EXISTS app_id TEXT NOT NULL DEFAULT ''",
54
+ `DO $$ BEGIN
55
+ IF NOT EXISTS (SELECT 1 FROM information_schema.key_column_usage
56
+ WHERE table_name = 'documents' AND constraint_name = 'documents_pkey'
57
+ AND column_name = 'app_id') THEN
58
+ ALTER TABLE documents DROP CONSTRAINT IF EXISTS documents_pkey;
59
+ ALTER TABLE documents ADD PRIMARY KEY (app_id, collection, id);
60
+ END IF;
61
+ END $$`,
62
+ `CREATE TABLE IF NOT EXISTS queue (
63
+ position BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
64
+ job_id TEXT NOT NULL,
65
+ workflow_id TEXT NOT NULL,
66
+ due TIMESTAMPTZ,
67
+ leased_until TIMESTAMPTZ
68
+ )`,
69
+ "ALTER TABLE queue ADD COLUMN IF NOT EXISTS app_id TEXT",
70
+ "ALTER TABLE queue ADD COLUMN IF NOT EXISTS retry_at TIMESTAMPTZ",
71
+ "ALTER TABLE queue ADD COLUMN IF NOT EXISTS attempts INT NOT NULL DEFAULT 0",
72
+ `UPDATE queue
73
+ SET app_id = split_part(workflow_id, '/', 1),
74
+ workflow_id = substring(workflow_id from position('/' in workflow_id) + 1)
75
+ WHERE app_id IS NULL AND workflow_id LIKE '%/%'`,
76
+ `CREATE TABLE IF NOT EXISTS job_run_schedule (
77
+ id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
78
+ app_id TEXT NOT NULL DEFAULT '',
79
+ workflow_id TEXT NOT NULL,
80
+ run_at TIMESTAMPTZ NOT NULL,
81
+ claimed_at TIMESTAMPTZ
82
+ )`,
83
+ `CREATE UNIQUE INDEX IF NOT EXISTS job_run_schedule_run
84
+ ON job_run_schedule (app_id, workflow_id, run_at)`,
85
+ `CREATE INDEX IF NOT EXISTS job_run_schedule_due
86
+ ON job_run_schedule (run_at) WHERE claimed_at IS NULL`,
87
+ `CREATE TABLE IF NOT EXISTS app_docs (
88
+ app_id TEXT NOT NULL,
89
+ slug TEXT NOT NULL,
90
+ title TEXT NOT NULL DEFAULT '',
91
+ markdown TEXT NOT NULL,
92
+ parent_slug TEXT,
93
+ position INTEGER NOT NULL DEFAULT 0,
94
+ generated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
95
+ PRIMARY KEY (app_id, slug)
96
+ )`,
97
+ "ALTER TABLE app_docs ADD COLUMN IF NOT EXISTS parent_slug TEXT",
98
+ "ALTER TABLE app_docs ADD COLUMN IF NOT EXISTS position INTEGER NOT NULL DEFAULT 0",
99
+ `CREATE TABLE IF NOT EXISTS entitlements (
100
+ app_id TEXT,
101
+ entitlement_id TEXT NOT NULL,
102
+ notes TEXT NOT NULL DEFAULT '',
103
+ registered_at TIMESTAMPTZ NOT NULL DEFAULT now()
104
+ )`,
105
+ `CREATE UNIQUE INDEX IF NOT EXISTS entitlements_key
106
+ ON entitlements (COALESCE(app_id, ''), entitlement_id)`,
107
+ `CREATE TABLE IF NOT EXISTS entitlement_grants (
108
+ id TEXT PRIMARY KEY,
109
+ user_id TEXT NOT NULL,
110
+ app_id TEXT,
111
+ entitlement_id TEXT NOT NULL,
112
+ notes TEXT NOT NULL DEFAULT '',
113
+ granted_at TIMESTAMPTZ NOT NULL DEFAULT now(),
114
+ granted_by TEXT NOT NULL
115
+ )`,
116
+ `CREATE INDEX IF NOT EXISTS entitlement_grants_lookup
117
+ ON entitlement_grants (user_id, entitlement_id)`,
118
+ `INSERT INTO entitlements (app_id, entitlement_id, notes)
119
+ VALUES (NULL, 'access', 'Global access')
120
+ ON CONFLICT (COALESCE(app_id, ''), entitlement_id) DO NOTHING`,
121
+ `CREATE TABLE IF NOT EXISTS prompts (
122
+ app_id TEXT NOT NULL,
123
+ prompt_id TEXT NOT NULL,
124
+ version INTEGER NOT NULL,
125
+ instructions TEXT NOT NULL,
126
+ output_schema JSONB,
127
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
128
+ PRIMARY KEY (app_id, prompt_id, version)
129
+ )`,
130
+ "ALTER TABLE prompts DROP COLUMN IF EXISTS input_schema",
131
+ "CREATE SCHEMA IF NOT EXISTS anbaric_system",
132
+ `CREATE TABLE IF NOT EXISTS anbaric_system.cli_keys (
133
+ id TEXT PRIMARY KEY,
134
+ user_id TEXT NOT NULL,
135
+ client_name TEXT NOT NULL,
136
+ public_key TEXT NOT NULL,
137
+ tenant TEXT,
138
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now()
139
+ )`,
140
+ "ALTER TABLE anbaric_system.cli_keys ADD COLUMN IF NOT EXISTS tenant TEXT",
141
+ `CREATE TABLE IF NOT EXISTS anbaric_system.users (
142
+ id TEXT PRIMARY KEY,
143
+ name TEXT NOT NULL DEFAULT '',
144
+ email TEXT NOT NULL DEFAULT '',
145
+ picture TEXT,
146
+ first_seen TIMESTAMPTZ NOT NULL DEFAULT now(),
147
+ last_seen TIMESTAMPTZ NOT NULL DEFAULT now()
148
+ )`,
149
+ `DO $$ BEGIN
150
+ CREATE TYPE anbaric_system.audit_interaction AS ENUM
151
+ ('CREATE', 'UPDATE_PROPERTIES', 'CHANGE_STATE', 'DELETE', 'READ', 'LIST');
152
+ EXCEPTION WHEN duplicate_object THEN null;
153
+ END $$`,
154
+ `CREATE TABLE IF NOT EXISTS anbaric_system.audit_records (
155
+ id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
156
+ resource_type TEXT NOT NULL,
157
+ resource_id TEXT NOT NULL,
158
+ actor_id TEXT NOT NULL,
159
+ actor_type TEXT NOT NULL,
160
+ interaction TEXT[] NOT NULL,
161
+ description TEXT NOT NULL,
162
+ details JSONB,
163
+ at TIMESTAMPTZ NOT NULL DEFAULT now()
164
+ )`,
165
+ "ALTER TABLE anbaric_system.audit_records ADD COLUMN IF NOT EXISTS resource_type TEXT",
166
+ "ALTER TABLE anbaric_system.audit_records ADD COLUMN IF NOT EXISTS resource_id TEXT",
167
+ "ALTER TABLE anbaric_system.audit_records ADD COLUMN IF NOT EXISTS app_id TEXT",
168
+ "ALTER TABLE anbaric_system.audit_records ADD COLUMN IF NOT EXISTS interaction TEXT[]",
169
+ `DO $$ BEGIN
170
+ IF EXISTS (SELECT 1 FROM information_schema.columns
171
+ WHERE table_schema = 'anbaric_system' AND table_name = 'audit_records'
172
+ AND column_name = 'interaction' AND data_type <> 'ARRAY') THEN
173
+ ALTER TABLE anbaric_system.audit_records
174
+ ALTER COLUMN interaction TYPE anbaric_system.audit_interaction[] USING ARRAY[interaction];
175
+ END IF;
176
+ END $$`,
177
+ `DO $$ BEGIN
178
+ IF EXISTS (SELECT 1 FROM information_schema.columns
179
+ WHERE table_schema = 'anbaric_system' AND table_name = 'audit_records'
180
+ AND column_name = 'job_id') THEN
181
+ UPDATE anbaric_system.audit_records
182
+ SET resource_type = COALESCE(resource_type, 'job'),
183
+ resource_id = COALESCE(resource_id, job_id),
184
+ interaction = COALESCE(interaction, ARRAY[change::text::anbaric_system.audit_interaction])
185
+ WHERE resource_id IS NULL OR interaction IS NULL;
186
+ ALTER TABLE anbaric_system.audit_records DROP COLUMN job_id;
187
+ ALTER TABLE anbaric_system.audit_records DROP COLUMN change;
188
+ END IF;
189
+ END $$`,
190
+ `DO $$ BEGIN
191
+ IF EXISTS (SELECT 1 FROM information_schema.columns
192
+ WHERE table_schema = 'anbaric_system' AND table_name = 'audit_records'
193
+ AND column_name = 'interaction' AND udt_name = '_audit_interaction') THEN
194
+ ALTER TABLE anbaric_system.audit_records
195
+ ALTER COLUMN interaction TYPE TEXT[] USING interaction::text[];
196
+ END IF;
197
+ END $$`,
198
+ "UPDATE anbaric_system.audit_records SET resource_type = 'job' WHERE resource_type IS NULL",
199
+ "UPDATE anbaric_system.audit_records SET resource_id = 'unknown' WHERE resource_id IS NULL",
200
+ "UPDATE anbaric_system.audit_records SET interaction = ARRAY['UPDATE_PROPERTIES']::text[] WHERE interaction IS NULL",
201
+ "ALTER TABLE anbaric_system.audit_records ALTER COLUMN resource_type SET NOT NULL",
202
+ "ALTER TABLE anbaric_system.audit_records ALTER COLUMN resource_id SET NOT NULL",
203
+ "ALTER TABLE anbaric_system.audit_records ALTER COLUMN interaction SET NOT NULL",
204
+ "CREATE INDEX IF NOT EXISTS audit_records_resource ON anbaric_system.audit_records (resource_type, resource_id)",
205
+ ),
206
+
207
+ ];
208
+
209
+ export { PLATFORM_MIGRATIONS };
@@ -1,273 +1,40 @@
1
1
  import {Pool} from "pg";
2
+ import {migrate} from "./Migrations";
3
+ import {PLATFORM_MIGRATIONS} from "./PlatformMigrations";
2
4
 
3
- /* Workflow data (jobs, queue, documents) lives in the default public schema;
4
- platform system tables are encapsulated in the anbaric_system schema so the
5
- two never mix. */
5
+ /* Brings the database up to date at boot, then settles the app's SQL role.
6
+ The role is not a migration: its password comes from the environment and may
7
+ be rotated, so it is re-applied every time rather than recorded as done. */
6
8
  const ensureSchema = async (pool : Pool) : Promise<void> => {
7
- await pool.query(`
8
- CREATE TABLE IF NOT EXISTS jobs (
9
- id TEXT PRIMARY KEY,
10
- state TEXT NOT NULL,
11
- properties JSONB NOT NULL DEFAULT '{}',
12
- workflow_id TEXT,
13
- started_at TIMESTAMPTZ NOT NULL DEFAULT now(),
14
- started_by TEXT NOT NULL DEFAULT 'system',
15
- last_updated TIMESTAMPTZ NOT NULL DEFAULT now(),
16
- transitions JSONB NOT NULL DEFAULT '[]',
17
- status TEXT NOT NULL DEFAULT 'active',
18
- inserted_at BIGINT GENERATED ALWAYS AS IDENTITY
19
- )
20
- `);
21
- await pool.query("ALTER TABLE jobs ADD COLUMN IF NOT EXISTS workflow_id TEXT");
22
- await pool.query("ALTER TABLE jobs ADD COLUMN IF NOT EXISTS started_at TIMESTAMPTZ NOT NULL DEFAULT now()");
23
- await pool.query("ALTER TABLE jobs ADD COLUMN IF NOT EXISTS started_by TEXT NOT NULL DEFAULT 'system'");
24
- await pool.query("ALTER TABLE jobs ADD COLUMN IF NOT EXISTS last_updated TIMESTAMPTZ NOT NULL DEFAULT now()");
25
- await pool.query("ALTER TABLE jobs ADD COLUMN IF NOT EXISTS transitions JSONB NOT NULL DEFAULT '[]'");
26
- await pool.query("ALTER TABLE jobs ADD COLUMN IF NOT EXISTS killed BOOLEAN NOT NULL DEFAULT false");
27
- await pool.query("ALTER TABLE jobs ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'active'");
28
- // A workflow is keyed by the composite (app_id, workflow_id). Older rows
29
- // stored "appId/workflowId" in workflow_id; split that once into the two
30
- // columns (best effort - a bare workflow id with no app is left as-is).
31
- await pool.query("ALTER TABLE jobs ADD COLUMN IF NOT EXISTS app_id TEXT");
32
- await pool.query(`UPDATE jobs
33
- SET app_id = split_part(workflow_id, '/', 1),
34
- workflow_id = substring(workflow_id from position('/' in workflow_id) + 1)
35
- WHERE app_id IS NULL AND workflow_id LIKE '%/%'`);
36
- // The await a job is parked on; its metadata is normalised here rather than on the job.
37
- await pool.query(`
38
- CREATE TABLE IF NOT EXISTS awaits (
39
- id UUID PRIMARY KEY,
40
- metadata JSONB NOT NULL DEFAULT '{}',
41
- created_at TIMESTAMPTZ NOT NULL DEFAULT now()
42
- )
43
- `);
44
- await pool.query("ALTER TABLE jobs ADD COLUMN IF NOT EXISTS waiting_for UUID REFERENCES awaits(id) ON DELETE SET NULL");
45
- await pool.query(`
46
- CREATE TABLE IF NOT EXISTS documents (
47
- app_id TEXT NOT NULL DEFAULT '',
48
- collection TEXT NOT NULL,
49
- id TEXT NOT NULL,
50
- document JSONB NOT NULL,
51
- inserted_at BIGINT GENERATED ALWAYS AS IDENTITY,
52
- PRIMARY KEY (app_id, collection, id)
53
- )
54
- `);
55
- // Documents are owned by an app: widen the key to (app_id, collection, id).
56
- await pool.query("ALTER TABLE documents ADD COLUMN IF NOT EXISTS app_id TEXT NOT NULL DEFAULT ''");
57
- await pool.query(`DO $$ BEGIN
58
- IF NOT EXISTS (SELECT 1 FROM information_schema.key_column_usage
59
- WHERE table_name = 'documents' AND constraint_name = 'documents_pkey'
60
- AND column_name = 'app_id') THEN
61
- ALTER TABLE documents DROP CONSTRAINT IF EXISTS documents_pkey;
62
- ALTER TABLE documents ADD PRIMARY KEY (app_id, collection, id);
63
- END IF;
64
- END $$`);
65
- await pool.query(`
66
- CREATE TABLE IF NOT EXISTS queue (
67
- position BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
68
- job_id TEXT NOT NULL,
69
- workflow_id TEXT NOT NULL,
70
- due TIMESTAMPTZ,
71
- leased_until TIMESTAMPTZ
72
- )
73
- `);
74
- await pool.query("ALTER TABLE queue ADD COLUMN IF NOT EXISTS app_id TEXT");
75
- await pool.query("ALTER TABLE queue ADD COLUMN IF NOT EXISTS retry_at TIMESTAMPTZ");
76
- await pool.query("ALTER TABLE queue ADD COLUMN IF NOT EXISTS attempts INT NOT NULL DEFAULT 0");
77
- await pool.query(`UPDATE queue
78
- SET app_id = split_part(workflow_id, '/', 1),
79
- workflow_id = substring(workflow_id from position('/' in workflow_id) + 1)
80
- WHERE app_id IS NULL AND workflow_id LIKE '%/%'`);
81
- // Runs a scheduler has planned. Unique on (app_id, workflow_id, run_at) so
82
- // two schedulers planning the same window converge on one row rather than
83
- // starting a machine twice.
84
- await pool.query(`
85
- CREATE TABLE IF NOT EXISTS job_run_schedule (
86
- id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
87
- app_id TEXT NOT NULL DEFAULT '',
88
- workflow_id TEXT NOT NULL,
89
- run_at TIMESTAMPTZ NOT NULL,
90
- claimed_at TIMESTAMPTZ
91
- )
92
- `);
93
- await pool.query(`CREATE UNIQUE INDEX IF NOT EXISTS job_run_schedule_run
94
- ON job_run_schedule (app_id, workflow_id, run_at)`);
95
- await pool.query(`CREATE INDEX IF NOT EXISTS job_run_schedule_due
96
- ON job_run_schedule (run_at) WHERE claimed_at IS NULL`);
97
- // User documentation the platform generates from an app's source on deploy,
98
- // keyed by (app_id, slug) and held as a shallow tree: a root doc has a null
99
- // parent_slug, a child names its parent's slug, and position orders
100
- // siblings. A redeploy replaces the whole set for an app.
101
- await pool.query(`
102
- CREATE TABLE IF NOT EXISTS app_docs (
103
- app_id TEXT NOT NULL,
104
- slug TEXT NOT NULL,
105
- title TEXT NOT NULL DEFAULT '',
106
- markdown TEXT NOT NULL,
107
- parent_slug TEXT,
108
- position INTEGER NOT NULL DEFAULT 0,
109
- generated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
110
- PRIMARY KEY (app_id, slug)
111
- )
112
- `);
113
- await pool.query("ALTER TABLE app_docs ADD COLUMN IF NOT EXISTS parent_slug TEXT");
114
- await pool.query("ALTER TABLE app_docs ADD COLUMN IF NOT EXISTS position INTEGER NOT NULL DEFAULT 0");
115
- await pool.query(`
116
- CREATE TABLE IF NOT EXISTS entitlements (
117
- app_id TEXT,
118
- entitlement_id TEXT NOT NULL,
119
- notes TEXT NOT NULL DEFAULT '',
120
- registered_at TIMESTAMPTZ NOT NULL DEFAULT now()
121
- )
122
- `);
123
- await pool.query(`
124
- CREATE UNIQUE INDEX IF NOT EXISTS entitlements_key
125
- ON entitlements (COALESCE(app_id, ''), entitlement_id)
126
- `);
127
- await pool.query(`
128
- CREATE TABLE IF NOT EXISTS entitlement_grants (
129
- id TEXT PRIMARY KEY,
130
- user_id TEXT NOT NULL,
131
- app_id TEXT,
132
- entitlement_id TEXT NOT NULL,
133
- notes TEXT NOT NULL DEFAULT '',
134
- granted_at TIMESTAMPTZ NOT NULL DEFAULT now(),
135
- granted_by TEXT NOT NULL
136
- )
137
- `);
138
- await pool.query(`
139
- CREATE INDEX IF NOT EXISTS entitlement_grants_lookup
140
- ON entitlement_grants (user_id, entitlement_id)
141
- `);
142
- await pool.query(`
143
- INSERT INTO entitlements (app_id, entitlement_id, notes)
144
- VALUES (NULL, 'access', 'Global access')
145
- ON CONFLICT (COALESCE(app_id, ''), entitlement_id) DO NOTHING
146
- `);
147
- await pool.query(`
148
- CREATE TABLE IF NOT EXISTS prompts (
149
- app_id TEXT NOT NULL,
150
- prompt_id TEXT NOT NULL,
151
- version INTEGER NOT NULL,
152
- instructions TEXT NOT NULL,
153
- output_schema JSONB,
154
- created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
155
- PRIMARY KEY (app_id, prompt_id, version)
156
- )
157
- `);
158
- await pool.query("ALTER TABLE prompts DROP COLUMN IF EXISTS input_schema");
159
- await pool.query("CREATE SCHEMA IF NOT EXISTS anbaric_system");
160
- await pool.query(`
161
- CREATE TABLE IF NOT EXISTS anbaric_system.cli_keys (
162
- id TEXT PRIMARY KEY,
163
- user_id TEXT NOT NULL,
164
- client_name TEXT NOT NULL,
165
- public_key TEXT NOT NULL,
166
- tenant TEXT,
167
- created_at TIMESTAMPTZ NOT NULL DEFAULT now()
168
- )
169
- `);
170
- await pool.query("ALTER TABLE anbaric_system.cli_keys ADD COLUMN IF NOT EXISTS tenant TEXT");
171
- await pool.query(`
172
- CREATE TABLE IF NOT EXISTS anbaric_system.users (
173
- id TEXT PRIMARY KEY,
174
- name TEXT NOT NULL DEFAULT '',
175
- email TEXT NOT NULL DEFAULT '',
176
- picture TEXT,
177
- first_seen TIMESTAMPTZ NOT NULL DEFAULT now(),
178
- last_seen TIMESTAMPTZ NOT NULL DEFAULT now()
179
- )
180
- `);
181
- await pool.query(`
182
- DO $$ BEGIN
183
- CREATE TYPE anbaric_system.audit_interaction AS ENUM
184
- ('CREATE', 'UPDATE_PROPERTIES', 'CHANGE_STATE', 'DELETE', 'READ', 'LIST');
185
- EXCEPTION WHEN duplicate_object THEN null;
186
- END $$
187
- `);
188
- await pool.query(`
189
- CREATE TABLE IF NOT EXISTS anbaric_system.audit_records (
190
- id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
191
- resource_type TEXT NOT NULL,
192
- resource_id TEXT NOT NULL,
193
- actor_id TEXT NOT NULL,
194
- actor_type TEXT NOT NULL,
195
- interaction TEXT[] NOT NULL,
196
- description TEXT NOT NULL,
197
- details JSONB,
198
- at TIMESTAMPTZ NOT NULL DEFAULT now()
199
- )
200
- `);
201
- await pool.query("ALTER TABLE anbaric_system.audit_records ADD COLUMN IF NOT EXISTS resource_type TEXT");
202
- await pool.query("ALTER TABLE anbaric_system.audit_records ADD COLUMN IF NOT EXISTS resource_id TEXT");
203
- // The app that owns the audited resource; part of every resource's composite identity.
204
- await pool.query("ALTER TABLE anbaric_system.audit_records ADD COLUMN IF NOT EXISTS app_id TEXT");
205
- await pool.query("ALTER TABLE anbaric_system.audit_records ADD COLUMN IF NOT EXISTS interaction TEXT[]");
206
- await pool.query(`
207
- DO $$ BEGIN
208
- IF EXISTS (SELECT 1 FROM information_schema.columns
209
- WHERE table_schema = 'anbaric_system' AND table_name = 'audit_records'
210
- AND column_name = 'interaction' AND data_type <> 'ARRAY') THEN
211
- ALTER TABLE anbaric_system.audit_records
212
- ALTER COLUMN interaction TYPE anbaric_system.audit_interaction[] USING ARRAY[interaction];
213
- END IF;
214
- END $$
215
- `);
216
- await pool.query(`
217
- DO $$ BEGIN
218
- IF EXISTS (SELECT 1 FROM information_schema.columns
219
- WHERE table_schema = 'anbaric_system' AND table_name = 'audit_records'
220
- AND column_name = 'job_id') THEN
221
- UPDATE anbaric_system.audit_records
222
- SET resource_type = COALESCE(resource_type, 'job'),
223
- resource_id = COALESCE(resource_id, job_id),
224
- interaction = COALESCE(interaction, ARRAY[change::text::anbaric_system.audit_interaction])
225
- WHERE resource_id IS NULL OR interaction IS NULL;
226
- ALTER TABLE anbaric_system.audit_records DROP COLUMN job_id;
227
- ALTER TABLE anbaric_system.audit_records DROP COLUMN change;
228
- END IF;
229
- END $$
230
- `);
231
- await pool.query(`
232
- DO $$ BEGIN
233
- IF EXISTS (SELECT 1 FROM information_schema.columns
234
- WHERE table_schema = 'anbaric_system' AND table_name = 'audit_records'
235
- AND column_name = 'interaction' AND udt_name = '_audit_interaction') THEN
236
- ALTER TABLE anbaric_system.audit_records
237
- ALTER COLUMN interaction TYPE TEXT[] USING interaction::text[];
238
- END IF;
239
- END $$
240
- `);
241
- await pool.query("UPDATE anbaric_system.audit_records SET resource_type = 'job' WHERE resource_type IS NULL");
242
- await pool.query("UPDATE anbaric_system.audit_records SET resource_id = 'unknown' WHERE resource_id IS NULL");
243
- await pool.query("UPDATE anbaric_system.audit_records SET interaction = ARRAY['UPDATE_PROPERTIES']::text[] WHERE interaction IS NULL");
244
- await pool.query("ALTER TABLE anbaric_system.audit_records ALTER COLUMN resource_type SET NOT NULL");
245
- await pool.query("ALTER TABLE anbaric_system.audit_records ALTER COLUMN resource_id SET NOT NULL");
246
- await pool.query("ALTER TABLE anbaric_system.audit_records ALTER COLUMN interaction SET NOT NULL");
247
- await pool.query("CREATE INDEX IF NOT EXISTS audit_records_resource ON anbaric_system.audit_records (resource_type, resource_id)");
9
+ const applied = await migrate(pool, PLATFORM_MIGRATIONS);
10
+ if (applied.length > 0) console.log(`Applied ${applied.length} migration(s): ${applied.join(", ")}`);
11
+
12
+ await ensureAppRole(pool);
13
+ };
248
14
 
249
- // The shared SQL store for apps: a dedicated schema plus a login role scoped
250
- // to it, so deployed apps reach only anbaric_app_data - never the platform's
251
- // public (jobs/documents/queue) or anbaric_system schemas.
15
+ /* The shared SQL store for apps: a dedicated schema plus a login role scoped
16
+ to it, so deployed apps reach only anbaric_app_data - never the platform's
17
+ public (jobs/documents/queue) or anbaric_system schemas. */
18
+ const ensureAppRole = async (pool : Pool) : Promise<void> => {
252
19
  const appSqlSchema = process.env.ANBARIC_SQL_SCHEMA ?? "anbaric_app_data";
253
- if (!/^[a-z_][a-z0-9_]*$/i.test(appSqlSchema)) throw new Error(`Invalid ANBARIC_SQL_SCHEMA "${appSqlSchema}"`);
20
+ if (! /^[a-z_][a-z0-9_]*$/i.test(appSqlSchema)) throw new Error(`Invalid ANBARIC_SQL_SCHEMA "${appSqlSchema}"`);
254
21
  await pool.query(`CREATE SCHEMA IF NOT EXISTS ${appSqlSchema}`);
255
22
 
256
23
  const appDbPassword = process.env.ANBARIC_APP_DB_PASSWORD;
257
- if (appDbPassword) {
258
- await pool.query(`DO $$ BEGIN
259
- IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'anbaric_app') THEN CREATE ROLE anbaric_app LOGIN; END IF;
260
- END $$`);
261
- await pool.query(`ALTER ROLE anbaric_app LOGIN PASSWORD '${appDbPassword.replace(/'/g, "''")}'`);
262
- await pool.query(`ALTER ROLE anbaric_app SET search_path TO ${appSqlSchema}`);
263
- await pool.query(`GRANT USAGE, CREATE ON SCHEMA ${appSqlSchema} TO anbaric_app`);
264
- await pool.query(`GRANT ALL ON ALL TABLES IN SCHEMA ${appSqlSchema} TO anbaric_app`);
265
- await pool.query(`GRANT ALL ON ALL SEQUENCES IN SCHEMA ${appSqlSchema} TO anbaric_app`);
266
- await pool.query(`ALTER DEFAULT PRIVILEGES IN SCHEMA ${appSqlSchema} GRANT ALL ON TABLES TO anbaric_app`);
267
- await pool.query(`ALTER DEFAULT PRIVILEGES IN SCHEMA ${appSqlSchema} GRANT ALL ON SEQUENCES TO anbaric_app`);
268
- await pool.query("REVOKE ALL ON SCHEMA public FROM anbaric_app");
269
- await pool.query("REVOKE ALL ON SCHEMA anbaric_system FROM anbaric_app");
270
- }
24
+ if (! appDbPassword) return;
25
+
26
+ await pool.query(`DO $$ BEGIN
27
+ IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'anbaric_app') THEN CREATE ROLE anbaric_app LOGIN; END IF;
28
+ END $$`);
29
+ await pool.query(`ALTER ROLE anbaric_app LOGIN PASSWORD '${appDbPassword.replace(/'/g, "''")}'`);
30
+ await pool.query(`ALTER ROLE anbaric_app SET search_path TO ${appSqlSchema}`);
31
+ await pool.query(`GRANT USAGE, CREATE ON SCHEMA ${appSqlSchema} TO anbaric_app`);
32
+ await pool.query(`GRANT ALL ON ALL TABLES IN SCHEMA ${appSqlSchema} TO anbaric_app`);
33
+ await pool.query(`GRANT ALL ON ALL SEQUENCES IN SCHEMA ${appSqlSchema} TO anbaric_app`);
34
+ await pool.query(`ALTER DEFAULT PRIVILEGES IN SCHEMA ${appSqlSchema} GRANT ALL ON TABLES TO anbaric_app`);
35
+ await pool.query(`ALTER DEFAULT PRIVILEGES IN SCHEMA ${appSqlSchema} GRANT ALL ON SEQUENCES TO anbaric_app`);
36
+ await pool.query("REVOKE ALL ON SCHEMA public FROM anbaric_app");
37
+ await pool.query("REVOKE ALL ON SCHEMA anbaric_system FROM anbaric_app");
271
38
  };
272
39
 
273
40
  export { ensureSchema }
package/src/index.ts CHANGED
@@ -46,6 +46,7 @@ export * from "./docs/DocModel";
46
46
  export * from "./docs/GatewayDocModel";
47
47
  export * from "./docs/DocGenerator";
48
48
  export * from "./data-store/PostgresJsonStore";
49
+ export * from "./data-store/Migrations";
49
50
  export * from "./data-store/Schema";
50
51
  export * from "./data-store/SecretsManagerSecretStore";
51
52
  export * from "./hosting/HostingServer";