@pgpm/metaschema-modules 0.21.2 → 0.23.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.
Files changed (48) hide show
  1. package/README.md +7 -8
  2. package/deploy/schemas/metaschema_modules_public/tables/billing_module/table.sql +47 -0
  3. package/deploy/schemas/metaschema_modules_public/tables/billing_provider_module/table.sql +61 -0
  4. package/deploy/schemas/metaschema_modules_public/tables/devices_module/table.sql +36 -0
  5. package/deploy/schemas/metaschema_modules_public/tables/entity_type_provision/table.sql +332 -0
  6. package/deploy/schemas/metaschema_modules_public/tables/identity_providers_module/table.sql +33 -0
  7. package/deploy/schemas/metaschema_modules_public/tables/invites_module/table.sql +8 -0
  8. package/deploy/schemas/metaschema_modules_public/tables/limits_module/table.sql +38 -2
  9. package/deploy/schemas/metaschema_modules_public/tables/memberships_module/table.sql +7 -0
  10. package/deploy/schemas/metaschema_modules_public/tables/notifications_module/table.sql +66 -0
  11. package/deploy/schemas/metaschema_modules_public/tables/permissions_module/table.sql +23 -1
  12. package/deploy/schemas/metaschema_modules_public/tables/plans_module/table.sql +46 -0
  13. package/deploy/schemas/metaschema_modules_public/tables/profiles_module/table.sql +5 -0
  14. package/deploy/schemas/metaschema_modules_public/tables/realtime_module/table.sql +48 -0
  15. package/deploy/schemas/metaschema_modules_public/tables/relation_provision/table.sql +10 -45
  16. package/deploy/schemas/metaschema_modules_public/tables/secure_table_provision/table.sql +8 -39
  17. package/deploy/schemas/metaschema_modules_public/tables/session_secrets_module/table.sql +31 -0
  18. package/deploy/schemas/metaschema_modules_public/tables/storage_module/table.sql +42 -4
  19. package/deploy/schemas/metaschema_modules_public/tables/user_auth_module/table.sql +2 -2
  20. package/deploy/schemas/metaschema_modules_public/tables/webauthn_auth_module/table.sql +63 -0
  21. package/deploy/schemas/metaschema_modules_public/tables/webauthn_credentials_module/table.sql +32 -0
  22. package/package.json +5 -5
  23. package/pgpm.plan +12 -1
  24. package/revert/schemas/metaschema_modules_public/tables/billing_module/table.sql +3 -0
  25. package/revert/schemas/metaschema_modules_public/tables/billing_provider_module/table.sql +3 -0
  26. package/revert/schemas/metaschema_modules_public/tables/devices_module/table.sql +7 -0
  27. package/revert/schemas/metaschema_modules_public/tables/entity_type_provision/table.sql +7 -0
  28. package/revert/schemas/metaschema_modules_public/tables/identity_providers_module/table.sql +7 -0
  29. package/revert/schemas/metaschema_modules_public/tables/notifications_module/table.sql +7 -0
  30. package/revert/schemas/metaschema_modules_public/tables/plans_module/table.sql +7 -0
  31. package/revert/schemas/metaschema_modules_public/tables/realtime_module/table.sql +7 -0
  32. package/revert/schemas/metaschema_modules_public/tables/session_secrets_module/table.sql +7 -0
  33. package/revert/schemas/metaschema_modules_public/tables/webauthn_auth_module/table.sql +7 -0
  34. package/revert/schemas/metaschema_modules_public/tables/webauthn_credentials_module/table.sql +7 -0
  35. package/sql/metaschema-modules--0.15.5.sql +1269 -469
  36. package/verify/schemas/metaschema_modules_public/tables/billing_module/table.sql +23 -0
  37. package/verify/schemas/metaschema_modules_public/tables/billing_provider_module/table.sql +29 -0
  38. package/verify/schemas/metaschema_modules_public/tables/devices_module/table.sql +7 -0
  39. package/verify/schemas/metaschema_modules_public/tables/entity_type_provision/table.sql +7 -0
  40. package/verify/schemas/metaschema_modules_public/tables/identity_providers_module/table.sql +7 -0
  41. package/verify/schemas/metaschema_modules_public/tables/notifications_module/table.sql +7 -0
  42. package/verify/schemas/metaschema_modules_public/tables/plans_module/table.sql +13 -0
  43. package/verify/schemas/metaschema_modules_public/tables/realtime_module/table.sql +7 -0
  44. package/verify/schemas/metaschema_modules_public/tables/relation_provision/table.sql +2 -8
  45. package/verify/schemas/metaschema_modules_public/tables/secure_table_provision/table.sql +2 -8
  46. package/verify/schemas/metaschema_modules_public/tables/session_secrets_module/table.sql +7 -0
  47. package/verify/schemas/metaschema_modules_public/tables/webauthn_auth_module/table.sql +7 -0
  48. package/verify/schemas/metaschema_modules_public/tables/webauthn_credentials_module/table.sql +7 -0
@@ -24,7 +24,7 @@ ALTER DEFAULT PRIVILEGES IN SCHEMA metaschema_modules_public
24
24
  GRANT ALL ON FUNCTIONS TO administrator;
25
25
 
26
26
  CREATE TABLE metaschema_modules_public.connected_accounts_module (
27
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
27
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
28
28
  database_id uuid NOT NULL,
29
29
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
30
30
  private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -53,20 +53,10 @@ CREATE TABLE metaschema_modules_public.connected_accounts_module (
53
53
  ON DELETE CASCADE
54
54
  );
55
55
 
56
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.connected_accounts_module IS '@omit manyToMany';
57
-
58
- COMMENT ON CONSTRAINT private_schema_fkey ON metaschema_modules_public.connected_accounts_module IS '@omit manyToMany';
59
-
60
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.connected_accounts_module IS '@omit manyToMany';
61
-
62
- COMMENT ON CONSTRAINT owner_table_fkey ON metaschema_modules_public.connected_accounts_module IS '@omit manyToMany';
63
-
64
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.connected_accounts_module IS '@omit manyToMany';
65
-
66
56
  CREATE INDEX connected_accounts_module_database_id_idx ON metaschema_modules_public.connected_accounts_module (database_id);
67
57
 
68
58
  CREATE TABLE metaschema_modules_public.crypto_addresses_module (
69
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
59
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
70
60
  database_id uuid NOT NULL,
71
61
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
72
62
  private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -96,20 +86,10 @@ CREATE TABLE metaschema_modules_public.crypto_addresses_module (
96
86
  ON DELETE CASCADE
97
87
  );
98
88
 
99
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.crypto_addresses_module IS '@omit manyToMany';
100
-
101
- COMMENT ON CONSTRAINT private_schema_fkey ON metaschema_modules_public.crypto_addresses_module IS '@omit manyToMany';
102
-
103
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.crypto_addresses_module IS '@omit manyToMany';
104
-
105
- COMMENT ON CONSTRAINT owner_table_fkey ON metaschema_modules_public.crypto_addresses_module IS '@omit manyToMany';
106
-
107
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.crypto_addresses_module IS '@omit manyToMany';
108
-
109
89
  CREATE INDEX crypto_addresses_module_database_id_idx ON metaschema_modules_public.crypto_addresses_module (database_id);
110
90
 
111
91
  CREATE TABLE metaschema_modules_public.crypto_auth_module (
112
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
92
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
113
93
  database_id uuid NOT NULL,
114
94
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
115
95
  users_table_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -149,22 +129,10 @@ CREATE TABLE metaschema_modules_public.crypto_auth_module (
149
129
  ON DELETE CASCADE
150
130
  );
151
131
 
152
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.crypto_auth_module IS '@omit manyToMany';
153
-
154
- COMMENT ON CONSTRAINT secrets_table_fkey ON metaschema_modules_public.crypto_auth_module IS '@omit manyToMany';
155
-
156
- COMMENT ON CONSTRAINT users_table_fkey ON metaschema_modules_public.crypto_auth_module IS '@omit manyToMany';
157
-
158
- COMMENT ON CONSTRAINT sessions_table_fkey ON metaschema_modules_public.crypto_auth_module IS '@omit manyToMany';
159
-
160
- COMMENT ON CONSTRAINT session_credentials_table_fkey ON metaschema_modules_public.crypto_auth_module IS '@omit manyToMany';
161
-
162
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.crypto_auth_module IS '@omit manyToMany';
163
-
164
132
  CREATE INDEX crypto_auth_module_database_id_idx ON metaschema_modules_public.crypto_auth_module (database_id);
165
133
 
166
134
  CREATE TABLE metaschema_modules_public.default_ids_module (
167
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
135
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
168
136
  database_id uuid NOT NULL,
169
137
  CONSTRAINT db_fkey
170
138
  FOREIGN KEY(database_id)
@@ -172,12 +140,10 @@ CREATE TABLE metaschema_modules_public.default_ids_module (
172
140
  ON DELETE CASCADE
173
141
  );
174
142
 
175
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.default_ids_module IS '@omit manyToMany';
176
-
177
143
  CREATE INDEX default_ids_module_database_id_idx ON metaschema_modules_public.default_ids_module (database_id);
178
144
 
179
145
  CREATE TABLE metaschema_modules_public.denormalized_table_field (
180
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
146
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
181
147
  database_id uuid NOT NULL,
182
148
  table_id uuid NOT NULL,
183
149
  field_id uuid NOT NULL,
@@ -211,20 +177,10 @@ CREATE TABLE metaschema_modules_public.denormalized_table_field (
211
177
  ON DELETE CASCADE
212
178
  );
213
179
 
214
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.denormalized_table_field IS '@omit manyToMany';
215
-
216
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.denormalized_table_field IS '@omit manyToMany';
217
-
218
- COMMENT ON CONSTRAINT ref_table_fkey ON metaschema_modules_public.denormalized_table_field IS '@omit manyToMany';
219
-
220
- COMMENT ON CONSTRAINT field_fkey ON metaschema_modules_public.denormalized_table_field IS '@omit manyToMany';
221
-
222
- COMMENT ON CONSTRAINT ref_field_fkey ON metaschema_modules_public.denormalized_table_field IS '@omit manyToMany';
223
-
224
180
  CREATE INDEX denormalized_table_field_database_id_idx ON metaschema_modules_public.denormalized_table_field (database_id);
225
181
 
226
182
  CREATE TABLE metaschema_modules_public.emails_module (
227
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
183
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
228
184
  database_id uuid NOT NULL,
229
185
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
230
186
  private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -253,20 +209,10 @@ CREATE TABLE metaschema_modules_public.emails_module (
253
209
  ON DELETE CASCADE
254
210
  );
255
211
 
256
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.emails_module IS '@omit manyToMany';
257
-
258
- COMMENT ON CONSTRAINT private_schema_fkey ON metaschema_modules_public.emails_module IS '@omit manyToMany';
259
-
260
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.emails_module IS '@omit manyToMany';
261
-
262
- COMMENT ON CONSTRAINT owner_table_fkey ON metaschema_modules_public.emails_module IS '@omit manyToMany';
263
-
264
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.emails_module IS '@omit manyToMany';
265
-
266
212
  CREATE INDEX emails_module_database_id_idx ON metaschema_modules_public.emails_module (database_id);
267
213
 
268
214
  CREATE TABLE metaschema_modules_public.encrypted_secrets_module (
269
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
215
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
270
216
  database_id uuid NOT NULL,
271
217
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
272
218
  table_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -285,190 +231,10 @@ CREATE TABLE metaschema_modules_public.encrypted_secrets_module (
285
231
  ON DELETE CASCADE
286
232
  );
287
233
 
288
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.encrypted_secrets_module IS '@omit manyToMany';
289
-
290
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.encrypted_secrets_module IS '@omit manyToMany';
291
-
292
234
  CREATE INDEX encrypted_secrets_module_database_id_idx ON metaschema_modules_public.encrypted_secrets_module (database_id);
293
235
 
294
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.encrypted_secrets_module IS '@omit manyToMany';
295
-
296
- CREATE TABLE metaschema_modules_public.field_module (
297
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
298
- database_id uuid NOT NULL,
299
- private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
300
- table_id uuid NOT NULL DEFAULT uuid_nil(),
301
- field_id uuid NOT NULL DEFAULT uuid_nil(),
302
- node_type text NOT NULL,
303
- data jsonb NOT NULL DEFAULT '{}',
304
- triggers text[],
305
- functions text[],
306
- CONSTRAINT db_fkey
307
- FOREIGN KEY(database_id)
308
- REFERENCES metaschema_public.database (id)
309
- ON DELETE CASCADE,
310
- CONSTRAINT table_fkey
311
- FOREIGN KEY(table_id)
312
- REFERENCES metaschema_public."table" (id)
313
- ON DELETE CASCADE,
314
- CONSTRAINT field_fkey
315
- FOREIGN KEY(field_id)
316
- REFERENCES metaschema_public.field (id)
317
- ON DELETE CASCADE,
318
- CONSTRAINT private_schema_fkey
319
- FOREIGN KEY(private_schema_id)
320
- REFERENCES metaschema_public.schema (id)
321
- ON DELETE CASCADE
322
- );
323
-
324
- COMMENT ON CONSTRAINT private_schema_fkey ON metaschema_modules_public.field_module IS '@omit manyToMany';
325
-
326
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.field_module IS '@omit manyToMany';
327
-
328
- COMMENT ON CONSTRAINT field_fkey ON metaschema_modules_public.field_module IS '@omit manyToMany';
329
-
330
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.field_module IS '@omit manyToMany';
331
-
332
- CREATE INDEX field_module_database_id_idx ON metaschema_modules_public.field_module (database_id);
333
-
334
- CREATE INDEX field_module_node_type_idx ON metaschema_modules_public.field_module (node_type);
335
-
336
- CREATE TABLE metaschema_modules_public.table_module (
337
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
338
- database_id uuid NOT NULL,
339
- schema_id uuid NOT NULL DEFAULT uuid_nil(),
340
- table_id uuid NOT NULL DEFAULT uuid_nil(),
341
- table_name text DEFAULT NULL,
342
- node_type text NOT NULL,
343
- use_rls boolean NOT NULL DEFAULT true,
344
- data jsonb NOT NULL DEFAULT '{}',
345
- fields uuid[],
346
- CONSTRAINT db_fkey
347
- FOREIGN KEY(database_id)
348
- REFERENCES metaschema_public.database (id)
349
- ON DELETE CASCADE,
350
- CONSTRAINT table_fkey
351
- FOREIGN KEY(table_id)
352
- REFERENCES metaschema_public."table" (id)
353
- ON DELETE CASCADE,
354
- CONSTRAINT schema_fkey
355
- FOREIGN KEY(schema_id)
356
- REFERENCES metaschema_public.schema (id)
357
- ON DELETE CASCADE
358
- );
359
-
360
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.table_module IS '@omit manyToMany';
361
-
362
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.table_module IS '@omit manyToMany';
363
-
364
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.table_module IS '@omit manyToMany';
365
-
366
- CREATE INDEX table_module_database_id_idx ON metaschema_modules_public.table_module (database_id);
367
-
368
- CREATE INDEX table_module_table_id_idx ON metaschema_modules_public.table_module (table_id);
369
-
370
- CREATE INDEX table_module_node_type_idx ON metaschema_modules_public.table_module (node_type);
371
-
372
- CREATE TABLE metaschema_modules_public.secure_table_provision (
373
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
374
- database_id uuid NOT NULL,
375
- schema_id uuid NOT NULL DEFAULT uuid_nil(),
376
- table_id uuid NOT NULL DEFAULT uuid_nil(),
377
- table_name text DEFAULT NULL,
378
- node_type text DEFAULT NULL,
379
- use_rls boolean NOT NULL DEFAULT true,
380
- node_data jsonb NOT NULL DEFAULT '{}',
381
- grant_roles text[] NOT NULL DEFAULT ARRAY['authenticated'],
382
- grant_privileges jsonb NOT NULL DEFAULT '[]',
383
- policy_type text DEFAULT NULL,
384
- policy_privileges text[] DEFAULT NULL,
385
- policy_role text DEFAULT NULL,
386
- policy_permissive boolean NOT NULL DEFAULT true,
387
- policy_name text DEFAULT NULL,
388
- policy_data jsonb NOT NULL DEFAULT '{}',
389
- out_fields uuid[] DEFAULT NULL,
390
- CONSTRAINT db_fkey
391
- FOREIGN KEY(database_id)
392
- REFERENCES metaschema_public.database (id)
393
- ON DELETE CASCADE,
394
- CONSTRAINT table_fkey
395
- FOREIGN KEY(table_id)
396
- REFERENCES metaschema_public."table" (id)
397
- ON DELETE CASCADE,
398
- CONSTRAINT schema_fkey
399
- FOREIGN KEY(schema_id)
400
- REFERENCES metaschema_public.schema (id)
401
- ON DELETE CASCADE
402
- );
403
-
404
- COMMENT ON TABLE metaschema_modules_public.secure_table_provision IS 'Provisions security, fields, grants, and policies onto a table. Each row can independently: (1) create fields via node_type, (2) grant privileges via grant_privileges, (3) create RLS policies via policy_type. Multiple rows can target the same table to compose different concerns. All three concerns are optional and independent.';
405
-
406
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.id IS 'Unique identifier for this provision row.';
407
-
408
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.database_id IS 'The database this provision belongs to. Required.';
409
-
410
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.schema_id IS 'Target schema for the table. Defaults to uuid_nil(); the trigger resolves this to the app_public schema if not explicitly provided.';
411
-
412
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.table_id IS 'Target table to provision. Defaults to uuid_nil(); the trigger creates or resolves the table via table_name if not explicitly provided.';
413
-
414
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.table_name IS 'Name of the target table. Used to create or look up the table when table_id is not provided. If omitted, it is backfilled from the resolved table.';
415
-
416
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.node_type IS 'Which generator to invoke for field creation. One of: DataId, DataDirectOwner, DataEntityMembership, DataOwnershipInEntity, DataTimestamps, DataPeoplestamps, DataPublishable, DataSoftDelete. NULL means no field creation — the row only provisions grants and/or policies.';
417
-
418
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.use_rls IS 'If true and Row Level Security is not yet enabled on the target table, enable it. Automatically set to true by the trigger when policy_type is provided. Defaults to true.';
419
-
420
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.node_data IS 'Configuration passed to the generator function for field creation (only used when node_type is set). Known keys include: field_name (text, default ''id'') for DataId, owner_field_name (text, default ''owner_id'') for DataDirectOwner/DataOwnershipInEntity, entity_field_name (text, default ''entity_id'') for DataEntityMembership/DataOwnershipInEntity, include_id (boolean, default true) for most node_types, include_user_fk (boolean, default true) to add FK to users table. Defaults to ''{}''.';
421
-
422
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.grant_roles IS 'Database roles to grant privileges to. Supports multiple roles, e.g. ARRAY[''authenticated'', ''admin'']. Each role receives all privileges defined in grant_privileges. Defaults to ARRAY[''authenticated''].';
423
-
424
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.grant_privileges IS 'Array of [privilege, columns] tuples defining table grants. Examples: [["select","*"],["insert","*"]] for full access, or [["update",["name","bio"]]] for column-level grants. "*" means all columns; an array means column-level grant. Defaults to ''[]'' (no grants — callers must explicitly specify privileges). The trigger validates this is a proper jsonb array.';
425
-
426
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.policy_type IS 'Policy generator type, e.g. ''AuthzEntityMembership'', ''AuthzMembership'', ''AuthzAllowAll''. NULL means no policy is created. When set, the trigger automatically enables RLS on the target table.';
427
-
428
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.policy_privileges IS 'Privileges the policy applies to, e.g. ARRAY[''select'',''update'']. NULL means privileges are derived from the grant_privileges verbs.';
429
-
430
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.policy_role IS 'Role the policy targets. NULL means it falls back to the first role in grant_roles.';
431
-
432
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.policy_permissive IS 'Whether the policy is PERMISSIVE (true) or RESTRICTIVE (false). Defaults to true.';
433
-
434
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.policy_name IS 'Custom suffix for the generated policy name. When NULL and policy_type is set, the trigger auto-derives a suffix from policy_type by stripping the Authz prefix and underscoring the remainder (e.g. AuthzDirectOwner becomes direct_owner, producing policy names like auth_sel_direct_owner). When explicitly set, the value is passed through as-is to metaschema.create_policy name parameter. This ensures multiple policies on the same table do not collide (e.g. AuthzDirectOwner + AuthzPublishable each get unique names).';
435
-
436
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.policy_data IS 'Opaque configuration passed through to metaschema.create_policy(). Structure varies by policy_type and is not interpreted by this trigger. Defaults to ''{}''.';
437
-
438
- COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.out_fields IS 'Output column populated by the trigger after field creation. Contains the UUIDs of the metaschema fields created on the target table by this provision row''s generator. NULL when node_type is NULL or before the trigger runs. Callers should not set this directly.';
439
-
440
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.secure_table_provision IS '@omit manyToMany';
441
-
442
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.secure_table_provision IS '@omit manyToMany';
443
-
444
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.secure_table_provision IS '@omit manyToMany';
445
-
446
- CREATE INDEX secure_table_provision_database_id_idx ON metaschema_modules_public.secure_table_provision (database_id);
447
-
448
- CREATE INDEX secure_table_provision_table_id_idx ON metaschema_modules_public.secure_table_provision (table_id);
449
-
450
- CREATE INDEX secure_table_provision_node_type_idx ON metaschema_modules_public.secure_table_provision (node_type);
451
-
452
- CREATE FUNCTION metaschema_modules_private.tg_set_secure_table_provision_deterministic_id() RETURNS trigger AS $EOFCODE$
453
- BEGIN
454
- IF current_setting('metaschema.deterministic_ids', true) = 'true' THEN
455
- NEW.id := metaschema_private.deterministic_id(NEW.table_id, NEW.node_type);
456
- END IF;
457
- RETURN NEW;
458
- END;
459
- $EOFCODE$ LANGUAGE plpgsql;
460
-
461
- CREATE TRIGGER zzz_set_deterministic_id
462
- BEFORE INSERT
463
- ON metaschema_modules_public.secure_table_provision
464
- FOR EACH ROW
465
- EXECUTE PROCEDURE metaschema_modules_private.tg_set_secure_table_provision_deterministic_id();
466
-
467
- ALTER TABLE metaschema_modules_public.secure_table_provision
468
- ENABLE ALWAYS TRIGGER zzz_set_deterministic_id;
469
-
470
236
  CREATE TABLE metaschema_modules_public.invites_module (
471
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
237
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
472
238
  database_id uuid NOT NULL,
473
239
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
474
240
  private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -516,24 +282,12 @@ CREATE TABLE metaschema_modules_public.invites_module (
516
282
  ON DELETE CASCADE
517
283
  );
518
284
 
519
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.invites_module IS '@omit manyToMany';
520
-
521
- COMMENT ON CONSTRAINT emails_table_fkey ON metaschema_modules_public.invites_module IS '@omit manyToMany';
522
-
523
- COMMENT ON CONSTRAINT users_table_fkey ON metaschema_modules_public.invites_module IS '@omit manyToMany';
524
-
525
- COMMENT ON CONSTRAINT invites_table_fkey ON metaschema_modules_public.invites_module IS '@omit manyToMany';
526
-
527
- COMMENT ON CONSTRAINT claimed_invites_table_fkey ON metaschema_modules_public.invites_module IS '@omit manyToMany';
528
-
529
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.invites_module IS '@omit manyToMany';
530
-
531
- COMMENT ON CONSTRAINT pschema_fkey ON metaschema_modules_public.invites_module IS '@omit manyToMany';
532
-
533
285
  CREATE INDEX invites_module_database_id_idx ON metaschema_modules_public.invites_module (database_id);
534
286
 
287
+ CREATE UNIQUE INDEX invites_module_unique_scope ON metaschema_modules_public.invites_module (database_id, membership_type);
288
+
535
289
  CREATE TABLE metaschema_modules_public.levels_module (
536
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
290
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
537
291
  database_id uuid NOT NULL,
538
292
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
539
293
  private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -597,24 +351,10 @@ CREATE TABLE metaschema_modules_public.levels_module (
597
351
  ON DELETE CASCADE
598
352
  );
599
353
 
600
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.levels_module IS '@omit manyToMany';
601
-
602
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.levels_module IS '@omit manyToMany';
603
-
604
- COMMENT ON CONSTRAINT steps_table_fkey ON metaschema_modules_public.levels_module IS '@omit manyToMany';
605
-
606
- COMMENT ON CONSTRAINT achievements_table_fkey ON metaschema_modules_public.levels_module IS '@omit manyToMany';
607
-
608
- COMMENT ON CONSTRAINT levels_table_fkey ON metaschema_modules_public.levels_module IS '@omit manyToMany';
609
-
610
- COMMENT ON CONSTRAINT level_requirements_table_fkey ON metaschema_modules_public.levels_module IS '@omit manyToMany';
611
-
612
- COMMENT ON CONSTRAINT actor_table_fkey ON metaschema_modules_public.levels_module IS '@omit manyToMany';
613
-
614
354
  CREATE INDEX user_status_module_database_id_idx ON metaschema_modules_public.levels_module (database_id);
615
355
 
616
356
  CREATE TABLE metaschema_modules_public.limits_module (
617
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
357
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
618
358
  database_id uuid NOT NULL,
619
359
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
620
360
  private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -628,6 +368,16 @@ CREATE TABLE metaschema_modules_public.limits_module (
628
368
  limit_decrement_trigger text NOT NULL DEFAULT '',
629
369
  limit_update_trigger text NOT NULL DEFAULT '',
630
370
  limit_check_function text NOT NULL DEFAULT '',
371
+ limit_credits_table_id uuid NULL,
372
+ events_table_id uuid NULL,
373
+ credit_codes_table_id uuid NULL,
374
+ credit_code_items_table_id uuid NULL,
375
+ credit_redemptions_table_id uuid NULL,
376
+ aggregate_table_id uuid NULL,
377
+ limit_caps_table_id uuid NULL,
378
+ limit_caps_defaults_table_id uuid NULL,
379
+ cap_check_trigger text NOT NULL DEFAULT '',
380
+ resolve_cap_function text NOT NULL DEFAULT '',
631
381
  prefix text NULL,
632
382
  membership_type int NOT NULL,
633
383
  entity_table_id uuid NULL,
@@ -659,25 +409,45 @@ CREATE TABLE metaschema_modules_public.limits_module (
659
409
  CONSTRAINT actor_table_fkey
660
410
  FOREIGN KEY(actor_table_id)
661
411
  REFERENCES metaschema_public."table" (id)
412
+ ON DELETE CASCADE,
413
+ CONSTRAINT aggregate_table_fkey
414
+ FOREIGN KEY(aggregate_table_id)
415
+ REFERENCES metaschema_public."table" (id)
416
+ ON DELETE CASCADE,
417
+ CONSTRAINT limit_credits_table_fkey
418
+ FOREIGN KEY(limit_credits_table_id)
419
+ REFERENCES metaschema_public."table" (id)
420
+ ON DELETE CASCADE,
421
+ CONSTRAINT events_table_fkey
422
+ FOREIGN KEY(events_table_id)
423
+ REFERENCES metaschema_public."table" (id)
424
+ ON DELETE CASCADE,
425
+ CONSTRAINT credit_codes_table_fkey
426
+ FOREIGN KEY(credit_codes_table_id)
427
+ REFERENCES metaschema_public."table" (id)
428
+ ON DELETE CASCADE,
429
+ CONSTRAINT credit_code_items_table_fkey
430
+ FOREIGN KEY(credit_code_items_table_id)
431
+ REFERENCES metaschema_public."table" (id)
432
+ ON DELETE CASCADE,
433
+ CONSTRAINT credit_redemptions_table_fkey
434
+ FOREIGN KEY(credit_redemptions_table_id)
435
+ REFERENCES metaschema_public."table" (id)
436
+ ON DELETE CASCADE,
437
+ CONSTRAINT limit_caps_table_fkey
438
+ FOREIGN KEY(limit_caps_table_id)
439
+ REFERENCES metaschema_public."table" (id)
440
+ ON DELETE CASCADE,
441
+ CONSTRAINT limit_caps_defaults_table_fkey
442
+ FOREIGN KEY(limit_caps_defaults_table_id)
443
+ REFERENCES metaschema_public."table" (id)
662
444
  ON DELETE CASCADE
663
445
  );
664
446
 
665
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.limits_module IS '@omit manyToMany';
666
-
667
- COMMENT ON CONSTRAINT private_schema_fkey ON metaschema_modules_public.limits_module IS '@omit manyToMany';
668
-
669
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.limits_module IS '@omit manyToMany';
670
-
671
447
  CREATE INDEX limits_module_database_id_idx ON metaschema_modules_public.limits_module (database_id);
672
448
 
673
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.limits_module IS '@omit manyToMany';
674
-
675
- COMMENT ON CONSTRAINT default_table_fkey ON metaschema_modules_public.limits_module IS '@omit manyToMany';
676
-
677
- COMMENT ON CONSTRAINT actor_table_fkey ON metaschema_modules_public.limits_module IS '@omit manyToMany';
678
-
679
449
  CREATE TABLE metaschema_modules_public.membership_types_module (
680
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
450
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
681
451
  database_id uuid NOT NULL,
682
452
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
683
453
  table_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -696,16 +466,10 @@ CREATE TABLE metaschema_modules_public.membership_types_module (
696
466
  ON DELETE CASCADE
697
467
  );
698
468
 
699
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.membership_types_module IS '@omit manyToMany';
700
-
701
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.membership_types_module IS '@omit manyToMany';
702
-
703
469
  CREATE INDEX membership_types_module_database_id_idx ON metaschema_modules_public.membership_types_module (database_id);
704
470
 
705
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.membership_types_module IS '@omit manyToMany';
706
-
707
471
  CREATE TABLE metaschema_modules_public.memberships_module (
708
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
472
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
709
473
  database_id uuid NOT NULL,
710
474
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
711
475
  private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -715,6 +479,8 @@ CREATE TABLE metaschema_modules_public.memberships_module (
715
479
  members_table_name text NOT NULL DEFAULT '',
716
480
  membership_defaults_table_id uuid NOT NULL DEFAULT uuid_nil(),
717
481
  membership_defaults_table_name text NOT NULL DEFAULT '',
482
+ membership_settings_table_id uuid NULL,
483
+ membership_settings_table_name text NOT NULL DEFAULT '',
718
484
  grants_table_id uuid NOT NULL DEFAULT uuid_nil(),
719
485
  grants_table_name text NOT NULL DEFAULT '',
720
486
  actor_table_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -736,6 +502,7 @@ CREATE TABLE metaschema_modules_public.memberships_module (
736
502
  entity_ids_by_mask text NULL,
737
503
  entity_ids_by_perm text NULL,
738
504
  entity_ids_function text NULL,
505
+ member_profiles_table_id uuid NULL,
739
506
  CONSTRAINT db_fkey
740
507
  FOREIGN KEY(database_id)
741
508
  REFERENCES metaschema_public.database (id)
@@ -756,6 +523,10 @@ CREATE TABLE metaschema_modules_public.memberships_module (
756
523
  FOREIGN KEY(membership_defaults_table_id)
757
524
  REFERENCES metaschema_public."table" (id)
758
525
  ON DELETE CASCADE,
526
+ CONSTRAINT membership_settings_table_fkey
527
+ FOREIGN KEY(membership_settings_table_id)
528
+ REFERENCES metaschema_public."table" (id)
529
+ ON DELETE CASCADE,
759
530
  CONSTRAINT members_table_fkey
760
531
  FOREIGN KEY(members_table_id)
761
532
  REFERENCES metaschema_public."table" (id)
@@ -798,40 +569,10 @@ CREATE TABLE metaschema_modules_public.memberships_module (
798
569
  ON DELETE CASCADE
799
570
  );
800
571
 
801
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
802
-
803
- COMMENT ON CONSTRAINT private_schema_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
804
-
805
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
806
-
807
572
  CREATE INDEX memberships_module_database_id_idx ON metaschema_modules_public.memberships_module (database_id);
808
573
 
809
- COMMENT ON CONSTRAINT entity_table_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
810
-
811
- COMMENT ON CONSTRAINT entity_table_owner_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
812
-
813
- COMMENT ON CONSTRAINT memberships_table_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
814
-
815
- COMMENT ON CONSTRAINT members_table_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
816
-
817
- COMMENT ON CONSTRAINT membership_defaults_table_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
818
-
819
- COMMENT ON CONSTRAINT grants_table_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
820
-
821
- COMMENT ON CONSTRAINT sprt_table_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
822
-
823
- COMMENT ON CONSTRAINT actor_table_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
824
-
825
- COMMENT ON CONSTRAINT limits_table_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
826
-
827
- COMMENT ON CONSTRAINT default_limits_table_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
828
-
829
- COMMENT ON CONSTRAINT permissions_table_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
830
-
831
- COMMENT ON CONSTRAINT default_permissions_table_fkey ON metaschema_modules_public.memberships_module IS '@omit manyToMany';
832
-
833
574
  CREATE TABLE metaschema_modules_public.permissions_module (
834
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
575
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
835
576
  database_id uuid NOT NULL,
836
577
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
837
578
  private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -839,7 +580,7 @@ CREATE TABLE metaschema_modules_public.permissions_module (
839
580
  table_name text NOT NULL DEFAULT '',
840
581
  default_table_id uuid NOT NULL DEFAULT uuid_nil(),
841
582
  default_table_name text NOT NULL DEFAULT '',
842
- bitlen int NOT NULL DEFAULT 24,
583
+ bitlen int NOT NULL DEFAULT 64,
843
584
  membership_type int NOT NULL,
844
585
  entity_table_id uuid NULL,
845
586
  actor_table_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -878,22 +619,10 @@ CREATE TABLE metaschema_modules_public.permissions_module (
878
619
  ON DELETE CASCADE
879
620
  );
880
621
 
881
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.permissions_module IS '@omit manyToMany';
882
-
883
- COMMENT ON CONSTRAINT private_schema_fkey ON metaschema_modules_public.permissions_module IS '@omit manyToMany';
884
-
885
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.permissions_module IS '@omit manyToMany';
886
-
887
622
  CREATE INDEX permissions_module_database_id_idx ON metaschema_modules_public.permissions_module (database_id);
888
623
 
889
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.permissions_module IS '@omit manyToMany';
890
-
891
- COMMENT ON CONSTRAINT default_table_fkey ON metaschema_modules_public.permissions_module IS '@omit manyToMany';
892
-
893
- COMMENT ON CONSTRAINT actor_table_fkey ON metaschema_modules_public.permissions_module IS '@omit manyToMany';
894
-
895
624
  CREATE TABLE metaschema_modules_public.phone_numbers_module (
896
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
625
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
897
626
  database_id uuid NOT NULL,
898
627
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
899
628
  private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -922,20 +651,10 @@ CREATE TABLE metaschema_modules_public.phone_numbers_module (
922
651
  ON DELETE CASCADE
923
652
  );
924
653
 
925
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.phone_numbers_module IS '@omit manyToMany';
926
-
927
- COMMENT ON CONSTRAINT private_schema_fkey ON metaschema_modules_public.phone_numbers_module IS '@omit manyToMany';
928
-
929
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.phone_numbers_module IS '@omit manyToMany';
930
-
931
- COMMENT ON CONSTRAINT owner_table_fkey ON metaschema_modules_public.phone_numbers_module IS '@omit manyToMany';
932
-
933
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.phone_numbers_module IS '@omit manyToMany';
934
-
935
654
  CREATE INDEX phone_numbers_module_database_id_idx ON metaschema_modules_public.phone_numbers_module (database_id);
936
655
 
937
656
  CREATE TABLE metaschema_modules_public.profiles_module (
938
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
657
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
939
658
  database_id uuid NOT NULL,
940
659
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
941
660
  private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -947,6 +666,8 @@ CREATE TABLE metaschema_modules_public.profiles_module (
947
666
  profile_grants_table_name text NOT NULL DEFAULT '',
948
667
  profile_definition_grants_table_id uuid NOT NULL DEFAULT uuid_nil(),
949
668
  profile_definition_grants_table_name text NOT NULL DEFAULT '',
669
+ profile_templates_table_id uuid NOT NULL DEFAULT uuid_nil(),
670
+ profile_templates_table_name text NOT NULL DEFAULT '',
950
671
  membership_type int NOT NULL,
951
672
  entity_table_id uuid NULL,
952
673
  actor_table_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -981,6 +702,10 @@ CREATE TABLE metaschema_modules_public.profiles_module (
981
702
  FOREIGN KEY(profile_definition_grants_table_id)
982
703
  REFERENCES metaschema_public."table" (id)
983
704
  ON DELETE CASCADE,
705
+ CONSTRAINT profile_templates_table_fkey
706
+ FOREIGN KEY(profile_templates_table_id)
707
+ REFERENCES metaschema_public."table" (id)
708
+ ON DELETE CASCADE,
984
709
  CONSTRAINT entity_table_fkey
985
710
  FOREIGN KEY(entity_table_id)
986
711
  REFERENCES metaschema_public."table" (id)
@@ -1001,34 +726,11 @@ CREATE TABLE metaschema_modules_public.profiles_module (
1001
726
  UNIQUE (database_id, membership_type)
1002
727
  );
1003
728
 
1004
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.profiles_module IS '@omit manyToMany';
1005
-
1006
- COMMENT ON CONSTRAINT private_schema_fkey ON metaschema_modules_public.profiles_module IS '@omit manyToMany';
1007
-
1008
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.profiles_module IS '@omit manyToMany';
1009
-
1010
729
  CREATE INDEX profiles_module_database_id_idx ON metaschema_modules_public.profiles_module (database_id);
1011
730
 
1012
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.profiles_module IS '@omit manyToMany';
1013
-
1014
- COMMENT ON CONSTRAINT profile_permissions_table_fkey ON metaschema_modules_public.profiles_module IS '@omit manyToMany';
1015
-
1016
- COMMENT ON CONSTRAINT profile_grants_table_fkey ON metaschema_modules_public.profiles_module IS '@omit manyToMany';
1017
-
1018
- COMMENT ON CONSTRAINT profile_definition_grants_table_fkey ON metaschema_modules_public.profiles_module IS '@omit manyToMany';
1019
-
1020
- COMMENT ON CONSTRAINT entity_table_fkey ON metaschema_modules_public.profiles_module IS '@omit manyToMany';
1021
-
1022
- COMMENT ON CONSTRAINT actor_table_fkey ON metaschema_modules_public.profiles_module IS '@omit manyToMany';
1023
-
1024
- COMMENT ON CONSTRAINT permissions_table_fkey ON metaschema_modules_public.profiles_module IS '@omit manyToMany';
1025
-
1026
- COMMENT ON CONSTRAINT memberships_table_fkey ON metaschema_modules_public.profiles_module IS '@omit manyToMany';
1027
-
1028
731
  CREATE TABLE metaschema_modules_public.rls_module (
1029
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
732
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1030
733
  database_id uuid NOT NULL,
1031
- api_id uuid NOT NULL DEFAULT uuid_nil(),
1032
734
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
1033
735
  private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
1034
736
  session_credentials_table_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -1042,10 +744,6 @@ CREATE TABLE metaschema_modules_public.rls_module (
1042
744
  FOREIGN KEY(database_id)
1043
745
  REFERENCES metaschema_public.database (id)
1044
746
  ON DELETE CASCADE,
1045
- CONSTRAINT api_fkey
1046
- FOREIGN KEY(api_id)
1047
- REFERENCES services_public.apis (id)
1048
- ON DELETE CASCADE,
1049
747
  CONSTRAINT session_credentials_table_fkey
1050
748
  FOREIGN KEY(session_credentials_table_id)
1051
749
  REFERENCES metaschema_public."table" (id)
@@ -1066,16 +764,10 @@ CREATE TABLE metaschema_modules_public.rls_module (
1066
764
  FOREIGN KEY(private_schema_id)
1067
765
  REFERENCES metaschema_public.schema (id)
1068
766
  ON DELETE CASCADE,
1069
- CONSTRAINT api_id_uniq
1070
- UNIQUE (api_id)
767
+ CONSTRAINT database_id_uniq
768
+ UNIQUE (database_id)
1071
769
  );
1072
770
 
1073
- COMMENT ON CONSTRAINT api_fkey ON metaschema_modules_public.rls_module IS '@omit manyToMany';
1074
-
1075
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.rls_module IS '@omit manyToMany';
1076
-
1077
- COMMENT ON CONSTRAINT pschema_fkey ON metaschema_modules_public.rls_module IS '@omit manyToMany';
1078
-
1079
771
  COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.rls_module IS '@omit';
1080
772
 
1081
773
  COMMENT ON CONSTRAINT session_credentials_table_fkey ON metaschema_modules_public.rls_module IS '@omit';
@@ -1087,7 +779,7 @@ COMMENT ON CONSTRAINT users_table_fkey ON metaschema_modules_public.rls_module I
1087
779
  CREATE INDEX rls_module_database_id_idx ON metaschema_modules_public.rls_module (database_id);
1088
780
 
1089
781
  CREATE TABLE metaschema_modules_public.secrets_module (
1090
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
782
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1091
783
  database_id uuid NOT NULL,
1092
784
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
1093
785
  table_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -1106,16 +798,10 @@ CREATE TABLE metaschema_modules_public.secrets_module (
1106
798
  ON DELETE CASCADE
1107
799
  );
1108
800
 
1109
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.secrets_module IS '@omit manyToMany';
1110
-
1111
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.secrets_module IS '@omit manyToMany';
1112
-
1113
801
  CREATE INDEX secrets_module_database_id_idx ON metaschema_modules_public.secrets_module (database_id);
1114
802
 
1115
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.secrets_module IS '@omit manyToMany';
1116
-
1117
803
  CREATE TABLE metaschema_modules_public.sessions_module (
1118
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
804
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1119
805
  database_id uuid NOT NULL,
1120
806
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
1121
807
  sessions_table_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -1125,7 +811,7 @@ CREATE TABLE metaschema_modules_public.sessions_module (
1125
811
  sessions_default_expiration interval NOT NULL DEFAULT '30 days'::interval,
1126
812
  sessions_table text NOT NULL DEFAULT 'sessions',
1127
813
  session_credentials_table text NOT NULL DEFAULT 'session_credentials',
1128
- auth_settings_table text NOT NULL DEFAULT 'app_auth_settings',
814
+ auth_settings_table text NOT NULL DEFAULT 'app_settings_auth',
1129
815
  CONSTRAINT db_fkey
1130
816
  FOREIGN KEY(database_id)
1131
817
  REFERENCES metaschema_public.database (id)
@@ -1152,25 +838,16 @@ CREATE TABLE metaschema_modules_public.sessions_module (
1152
838
  ON DELETE CASCADE
1153
839
  );
1154
840
 
1155
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.sessions_module IS '@omit manyToMany';
1156
-
1157
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.sessions_module IS '@omit manyToMany';
1158
-
1159
841
  CREATE INDEX sessions_module_database_id_idx ON metaschema_modules_public.sessions_module (database_id);
1160
842
 
1161
- COMMENT ON CONSTRAINT sessions_table_fkey ON metaschema_modules_public.sessions_module IS '@fieldName sessionsTableBySessionsTableId
1162
- @omit manyToMany';
1163
-
1164
- COMMENT ON CONSTRAINT session_credentials_table_fkey ON metaschema_modules_public.sessions_module IS '@fieldName sessionCredentialsTableBySessionCredentialsTableId
1165
- @omit manyToMany';
843
+ COMMENT ON CONSTRAINT sessions_table_fkey ON metaschema_modules_public.sessions_module IS '@fieldName sessionsTableBySessionsTableId';
1166
844
 
1167
- COMMENT ON CONSTRAINT auth_settings_table_fkey ON metaschema_modules_public.sessions_module IS '@fieldName authSettingsTableByAuthSettingsTableId
1168
- @omit manyToMany';
845
+ COMMENT ON CONSTRAINT session_credentials_table_fkey ON metaschema_modules_public.sessions_module IS '@fieldName sessionCredentialsTableBySessionCredentialsTableId';
1169
846
 
1170
- COMMENT ON CONSTRAINT users_table_fkey ON metaschema_modules_public.sessions_module IS '@omit manyToMany';
847
+ COMMENT ON CONSTRAINT auth_settings_table_fkey ON metaschema_modules_public.sessions_module IS '@fieldName authSettingsTableByAuthSettingsTableId';
1171
848
 
1172
849
  CREATE TABLE metaschema_modules_public.user_auth_module (
1173
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
850
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1174
851
  database_id uuid NOT NULL,
1175
852
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
1176
853
  emails_table_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -1193,8 +870,8 @@ CREATE TABLE metaschema_modules_public.user_auth_module (
1193
870
  check_password_function text NOT NULL DEFAULT 'check_password',
1194
871
  send_account_deletion_email_function text NOT NULL DEFAULT 'send_account_deletion_email',
1195
872
  delete_account_function text NOT NULL DEFAULT 'confirm_delete_account',
1196
- sign_in_one_time_token_function text NOT NULL DEFAULT 'sign_in_one_time_token',
1197
- one_time_token_function text NOT NULL DEFAULT 'one_time_token',
873
+ sign_in_cross_origin_function text NOT NULL DEFAULT 'sign_in_cross_origin',
874
+ request_cross_origin_token_function text NOT NULL DEFAULT 'request_cross_origin_token',
1198
875
  extend_token_expires text NOT NULL DEFAULT 'extend_token_expires',
1199
876
  CONSTRAINT db_fkey
1200
877
  FOREIGN KEY(database_id)
@@ -1230,10 +907,6 @@ CREATE TABLE metaschema_modules_public.user_auth_module (
1230
907
  ON DELETE CASCADE
1231
908
  );
1232
909
 
1233
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.user_auth_module IS '@omit manyToMany';
1234
-
1235
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.user_auth_module IS '@omit manyToMany';
1236
-
1237
910
  CREATE INDEX user_auth_module_database_id_idx ON metaschema_modules_public.user_auth_module (database_id);
1238
911
 
1239
912
  COMMENT ON CONSTRAINT email_table_fkey ON metaschema_modules_public.user_auth_module IS '@omit';
@@ -1249,7 +922,7 @@ COMMENT ON CONSTRAINT sessions_table_fkey ON metaschema_modules_public.user_auth
1249
922
  COMMENT ON CONSTRAINT session_credentials_table_fkey ON metaschema_modules_public.user_auth_module IS '@omit';
1250
923
 
1251
924
  CREATE TABLE metaschema_modules_public.users_module (
1252
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
925
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1253
926
  database_id uuid NOT NULL,
1254
927
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
1255
928
  table_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -1274,40 +947,10 @@ CREATE TABLE metaschema_modules_public.users_module (
1274
947
  ON DELETE CASCADE
1275
948
  );
1276
949
 
1277
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.users_module IS '@omit manyToMany';
1278
-
1279
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.users_module IS '@omit manyToMany';
1280
-
1281
950
  CREATE INDEX users_module_database_id_idx ON metaschema_modules_public.users_module (database_id);
1282
951
 
1283
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.users_module IS '@omit manyToMany';
1284
-
1285
- COMMENT ON CONSTRAINT type_table_fkey ON metaschema_modules_public.users_module IS '@omit manyToMany';
1286
-
1287
- CREATE TABLE metaschema_modules_public.uuid_module (
1288
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
1289
- database_id uuid NOT NULL,
1290
- schema_id uuid NOT NULL DEFAULT uuid_nil(),
1291
- uuid_function text NOT NULL DEFAULT 'uuid_generate_v4',
1292
- uuid_seed text NOT NULL,
1293
- CONSTRAINT schema_fkey
1294
- FOREIGN KEY(schema_id)
1295
- REFERENCES metaschema_public.schema (id)
1296
- ON DELETE CASCADE,
1297
- CONSTRAINT db_fkey
1298
- FOREIGN KEY(database_id)
1299
- REFERENCES metaschema_public.database (id)
1300
- ON DELETE CASCADE
1301
- );
1302
-
1303
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.uuid_module IS '@omit manyToMany';
1304
-
1305
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.uuid_module IS '@omit manyToMany';
1306
-
1307
- CREATE INDEX uuid_module_database_id_idx ON metaschema_modules_public.uuid_module (database_id);
1308
-
1309
952
  CREATE TABLE metaschema_modules_public.hierarchy_module (
1310
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
953
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1311
954
  database_id uuid NOT NULL,
1312
955
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
1313
956
  private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
@@ -1363,34 +1006,795 @@ CREATE TABLE metaschema_modules_public.hierarchy_module (
1363
1006
  UNIQUE (database_id)
1364
1007
  );
1365
1008
 
1366
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.hierarchy_module IS '@omit manyToMany';
1009
+ CREATE INDEX hierarchy_module_database_id_idx ON metaschema_modules_public.hierarchy_module (database_id);
1010
+
1011
+ CREATE TABLE metaschema_modules_public.secure_table_provision (
1012
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1013
+ database_id uuid NOT NULL,
1014
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
1015
+ table_id uuid NOT NULL DEFAULT uuid_nil(),
1016
+ table_name text DEFAULT NULL,
1017
+ nodes jsonb NOT NULL DEFAULT '[]',
1018
+ use_rls boolean NOT NULL DEFAULT true,
1019
+ fields jsonb[] NOT NULL DEFAULT '{}',
1020
+ grants jsonb NOT NULL DEFAULT '[]',
1021
+ policies jsonb NOT NULL DEFAULT '[]',
1022
+ out_fields uuid[] DEFAULT NULL,
1023
+ CONSTRAINT db_fkey
1024
+ FOREIGN KEY(database_id)
1025
+ REFERENCES metaschema_public.database (id)
1026
+ ON DELETE CASCADE,
1027
+ CONSTRAINT table_fkey
1028
+ FOREIGN KEY(table_id)
1029
+ REFERENCES metaschema_public."table" (id)
1030
+ ON DELETE CASCADE,
1031
+ CONSTRAINT schema_fkey
1032
+ FOREIGN KEY(schema_id)
1033
+ REFERENCES metaschema_public.schema (id)
1034
+ ON DELETE CASCADE
1035
+ );
1036
+
1037
+ COMMENT ON TABLE metaschema_modules_public.secure_table_provision IS 'Provisions security, fields, grants, and policies onto a table. Each row can independently: (1) create fields via nodes[] array (supporting multiple Data* modules per row), (2) grant privileges via grants[] array (supporting per-role privilege targeting), (3) create RLS policies via policies[] array (supporting multiple Authz* policies per row). Multiple rows can target the same table to compose different concerns. All three concerns are optional and independent.';
1367
1038
 
1368
- COMMENT ON CONSTRAINT private_schema_fkey ON metaschema_modules_public.hierarchy_module IS '@omit manyToMany';
1039
+ COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.id IS 'Unique identifier for this provision row.';
1369
1040
 
1370
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.hierarchy_module IS '@omit manyToMany';
1041
+ COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.database_id IS 'The database this provision belongs to. Required.';
1371
1042
 
1372
- CREATE INDEX hierarchy_module_database_id_idx ON metaschema_modules_public.hierarchy_module (database_id);
1043
+ COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.schema_id IS 'Target schema for the table. Defaults to uuid_nil(); the trigger resolves this to the app_public schema if not explicitly provided.';
1044
+
1045
+ COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.table_id IS 'Target table to provision. Defaults to uuid_nil(); the trigger creates or resolves the table via table_name if not explicitly provided.';
1373
1046
 
1374
- COMMENT ON CONSTRAINT chart_edges_table_fkey ON metaschema_modules_public.hierarchy_module IS '@omit manyToMany';
1047
+ COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.table_name IS 'Name of the target table. Used to create or look up the table when table_id is not provided. If omitted, it is backfilled from the resolved table.';
1375
1048
 
1376
- COMMENT ON CONSTRAINT hierarchy_sprt_table_fkey ON metaschema_modules_public.hierarchy_module IS '@omit manyToMany';
1049
+ COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.nodes IS 'Array of node objects to apply to the table. Each element is a jsonb object with a required "$type" key (one of: DataId, DataDirectOwner, DataEntityMembership, DataOwnershipInEntity, DataTimestamps, DataPeoplestamps, DataPublishable, DataSoftDelete, DataEmbedding, DataFullTextSearch, DataSlug, etc.) and an optional "data" key containing generator-specific configuration. Supports multiple nodes per row, matching the blueprint definition format. Example: [{"$type": "DataId"}, {"$type": "DataTimestamps"}, {"$type": "DataDirectOwner", "data": {"owner_field_name": "author_id"}}]. Defaults to ''[]'' (no node processing).';
1377
1050
 
1378
- COMMENT ON CONSTRAINT chart_edge_grants_table_fkey ON metaschema_modules_public.hierarchy_module IS '@omit manyToMany';
1051
+ COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.use_rls IS 'If true and Row Level Security is not yet enabled on the target table, enable it. Automatically set to true by the trigger when policies[] is non-empty. Defaults to true.';
1379
1052
 
1380
- COMMENT ON CONSTRAINT entity_table_fkey ON metaschema_modules_public.hierarchy_module IS '@omit manyToMany';
1053
+ COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.fields IS 'PostgreSQL array of jsonb field definition objects to create on the target table. Each object has keys: "name" (text, required), "type" (text, required), "default" (text, optional), "is_required" (boolean, optional, defaults to false), "min" (float, optional), "max" (float, optional), "regexp" (text, optional), "index" (boolean, optional, defaults to false — creates a btree index on the field). min/max generate CHECK constraints: for text/citext they constrain character_length, for integer/float types they constrain the value. regexp generates a CHECK (col ~ pattern) constraint for text/citext. Fields are created via metaschema.create_field() after any node_type generator runs, and their IDs are appended to out_fields. Example: ARRAY[''{"name":"username","type":"citext","max":256,"regexp":"^[a-z0-9_]+$"}''::jsonb, ''{"name":"score","type":"integer","min":0,"max":100}''::jsonb]. Defaults to ''{}'' (no additional fields).';
1381
1054
 
1382
- COMMENT ON CONSTRAINT users_table_fkey ON metaschema_modules_public.hierarchy_module IS '@omit manyToMany';
1055
+ COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.grants IS 'Array of grant objects defining table privileges. Each element is a jsonb object with keys: "roles" (text[], required — database roles to grant to, e.g. ["authenticated","admin"]), "privileges" (jsonb[], required — array of [privilege, columns] tuples, e.g. [["select","*"],["insert","*"]]). "*" means all columns; an array means column-level grant. Supports per-role privilege targeting: different grant entries can target different roles with different privileges. Example: [{"roles":["authenticated"],"privileges":[["select","*"]]},{"roles":["admin"],"privileges":[["insert","*"],["update","*"],["delete","*"]]}]. Defaults to ''[]'' (no grants). When policies[] omit explicit privileges/policy_role, they fall back to the verbs and first role from grants[].';
1383
1056
 
1384
- CREATE TABLE metaschema_modules_public.table_template_module (
1385
- id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
1057
+ COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.policies IS 'Array of policy objects to create on the target table. Each element is a jsonb object with keys: "$type" (text, required — the Authz* policy generator type, e.g. AuthzEntityMembership, AuthzMembership, AuthzDirectOwner, AuthzPublishable, AuthzAllowAll), "data" (jsonb, optional — opaque configuration passed to metaschema.create_policy(), structure varies by type), "privileges" (text[], optional — privileges the policy applies to, e.g. ["select","insert"]; if omitted, derived from grants[] privilege verbs), "policy_role" (text, optional — role the policy targets; if omitted, falls back to first role in first grants[] entry, or ''authenticated'' if no grants), "permissive" (boolean, optional — PERMISSIVE or RESTRICTIVE; defaults to true), "policy_name" (text, optional — custom suffix for the generated policy name; if omitted, auto-derived from $type by stripping Authz prefix). Supports multiple policies per row. Example: [{"$type": "AuthzEntityMembership", "data": {"entity_field": "owner_id", "membership_type": 3}, "privileges": ["select", "insert"]}, {"$type": "AuthzDirectOwner", "data": {"entity_field": "actor_id"}, "privileges": ["update", "delete"]}]. Defaults to ''[]'' (no policies created). When non-empty, the trigger automatically enables RLS.';
1058
+
1059
+ COMMENT ON COLUMN metaschema_modules_public.secure_table_provision.out_fields IS 'Output column populated by the trigger after field creation. Contains the UUIDs of the metaschema fields created on the target table by this provision row''s nodes. NULL when nodes is empty or before the trigger runs. Callers should not set this directly.';
1060
+
1061
+ CREATE INDEX secure_table_provision_database_id_idx ON metaschema_modules_public.secure_table_provision (database_id);
1062
+
1063
+ CREATE INDEX secure_table_provision_table_id_idx ON metaschema_modules_public.secure_table_provision (table_id);
1064
+
1065
+ CREATE TABLE metaschema_modules_public.relation_provision (
1066
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1067
+ database_id uuid NOT NULL,
1068
+ relation_type text NOT NULL CHECK (relation_type IN ('RelationBelongsTo', 'RelationHasOne', 'RelationHasMany', 'RelationManyToMany')),
1069
+ source_table_id uuid NOT NULL,
1070
+ target_table_id uuid NOT NULL,
1071
+ field_name text DEFAULT NULL,
1072
+ delete_action text DEFAULT NULL,
1073
+ is_required boolean NOT NULL DEFAULT true,
1074
+ api_required boolean NOT NULL DEFAULT false,
1075
+ junction_table_id uuid NOT NULL DEFAULT uuid_nil(),
1076
+ junction_table_name text DEFAULT NULL,
1077
+ junction_schema_id uuid DEFAULT NULL,
1078
+ source_field_name text DEFAULT NULL,
1079
+ target_field_name text DEFAULT NULL,
1080
+ use_composite_key boolean NOT NULL DEFAULT false,
1081
+ create_index boolean NOT NULL DEFAULT true,
1082
+ expose_in_api boolean NOT NULL DEFAULT true,
1083
+ nodes jsonb NOT NULL DEFAULT '[]',
1084
+ grants jsonb NOT NULL DEFAULT '[]',
1085
+ policies jsonb NOT NULL DEFAULT '[]',
1086
+ out_field_id uuid DEFAULT NULL,
1087
+ out_junction_table_id uuid DEFAULT NULL,
1088
+ out_source_field_id uuid DEFAULT NULL,
1089
+ out_target_field_id uuid DEFAULT NULL,
1090
+ CONSTRAINT db_fkey
1091
+ FOREIGN KEY(database_id)
1092
+ REFERENCES metaschema_public.database (id)
1093
+ ON DELETE CASCADE,
1094
+ CONSTRAINT source_table_fkey
1095
+ FOREIGN KEY(source_table_id)
1096
+ REFERENCES metaschema_public."table" (id)
1097
+ ON DELETE CASCADE,
1098
+ CONSTRAINT target_table_fkey
1099
+ FOREIGN KEY(target_table_id)
1100
+ REFERENCES metaschema_public."table" (id)
1101
+ ON DELETE CASCADE
1102
+ );
1103
+
1104
+ COMMENT ON TABLE metaschema_modules_public.relation_provision IS 'Provisions relational structure between tables. Supports four relation types:
1105
+ - RelationBelongsTo: adds a FK field on the source table referencing the target table (child perspective: "tasks belongs to projects" -> tasks.project_id).
1106
+ - RelationHasMany: adds a FK field on the target table referencing the source table (parent perspective: "projects has many tasks" -> tasks.project_id). Inverse of BelongsTo.
1107
+ - RelationHasOne: adds a FK field with a unique constraint on the source table referencing the target table. Also supports shared-primary-key patterns where the FK field IS the primary key (set field_name to the existing PK field name).
1108
+ - RelationManyToMany: creates a junction table with FK fields to both source and target tables, delegating table creation and security to secure_table_provision.
1109
+ This is a one-and-done structural provisioner. To layer additional security onto junction tables after creation, use secure_table_provision directly.
1110
+ All operations are graceful: existing fields, FK constraints, and unique constraints are reused if found.
1111
+ The trigger never injects values the caller did not provide. All security config is forwarded to secure_table_provision as-is.';
1112
+
1113
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.id IS 'Unique identifier for this relation provision row.';
1114
+
1115
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.database_id IS 'The database this relation belongs to. Required. Must match the database of both source_table_id and target_table_id.';
1116
+
1117
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.relation_type IS 'The type of relation to create. Uses SuperCase naming:
1118
+ - RelationBelongsTo: creates a FK field on source_table referencing target_table (e.g., tasks belongs to projects -> tasks.project_id). Field name auto-derived from target table.
1119
+ - RelationHasMany: creates a FK field on target_table referencing source_table (e.g., projects has many tasks -> tasks.project_id). Field name auto-derived from source table. Inverse of BelongsTo — same FK, different perspective.
1120
+ - RelationHasOne: creates a FK field + unique constraint on source_table referencing target_table (e.g., user_settings has one user -> user_settings.user_id with UNIQUE). Also supports shared-primary-key patterns (e.g., user_profiles.id = users.id) by setting field_name to the existing PK field.
1121
+ - RelationManyToMany: creates a junction table with FK fields to both tables (e.g., projects and tags -> project_tags table).
1122
+ Each relation type uses a different subset of columns on this table. Required.';
1123
+
1124
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.source_table_id IS 'The source table in the relation. Required.
1125
+ - RelationBelongsTo: the table that receives the FK field (e.g., tasks in "tasks belongs to projects").
1126
+ - RelationHasMany: the parent table being referenced (e.g., projects in "projects has many tasks"). The FK field is created on the target table.
1127
+ - RelationHasOne: the table that receives the FK field + unique constraint (e.g., user_settings in "user_settings has one user").
1128
+ - RelationManyToMany: one of the two tables being joined (e.g., projects in "projects and tags"). The junction table will have a FK field referencing this table.';
1129
+
1130
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.target_table_id IS 'The target table in the relation. Required.
1131
+ - RelationBelongsTo: the table being referenced by the FK (e.g., projects in "tasks belongs to projects").
1132
+ - RelationHasMany: the table that receives the FK field (e.g., tasks in "projects has many tasks").
1133
+ - RelationHasOne: the table being referenced by the FK (e.g., users in "user_settings has one user").
1134
+ - RelationManyToMany: the other table being joined (e.g., tags in "projects and tags"). The junction table will have a FK field referencing this table.';
1135
+
1136
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.field_name IS 'FK field name for RelationBelongsTo, RelationHasOne, and RelationHasMany.
1137
+ - RelationBelongsTo/RelationHasOne: if NULL, auto-derived from the target table name (e.g., target "projects" derives "project_id").
1138
+ - RelationHasMany: if NULL, auto-derived from the source table name (e.g., source "projects" derives "project_id").
1139
+ For RelationHasOne shared-primary-key patterns, set field_name to the existing PK field (e.g., "id") so the FK reuses it.
1140
+ Ignored for RelationManyToMany — use source_field_name/target_field_name instead.';
1141
+
1142
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.delete_action IS 'FK delete action for RelationBelongsTo, RelationHasOne, and RelationHasMany. One of: c (CASCADE), r (RESTRICT), n (SET NULL), d (SET DEFAULT), a (NO ACTION). Required — the trigger raises an error if not provided. The caller must explicitly choose the cascade behavior; there is no default. Ignored for RelationManyToMany (junction FK fields always use CASCADE).';
1143
+
1144
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.is_required IS 'Whether the FK field is NOT NULL. Defaults to true.
1145
+ - RelationBelongsTo: set to false for optional associations (e.g., tasks.assignee_id that can be NULL).
1146
+ - RelationHasMany: set to false if the child can exist without a parent.
1147
+ - RelationHasOne: typically true.
1148
+ Ignored for RelationManyToMany (junction FK fields are always required).';
1149
+
1150
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.api_required IS 'Whether the FK field should be required at the API level even though it is nullable at the database level. Defaults to false.
1151
+ When true and is_required is false, the field is created as nullable (allowing SET NULL cascade) but a @requiredInput smart tag is added so PostGraphile treats it as non-null in create/update input types.
1152
+ When is_required is true, api_required is ignored (the field is already required at both levels).
1153
+ Ignored for RelationManyToMany (junction FK fields are always required).';
1154
+
1155
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.junction_table_id IS 'For RelationManyToMany: an existing junction table to use. Defaults to uuid_nil().
1156
+ - When uuid_nil(): the trigger creates a new junction table via secure_table_provision using junction_table_name.
1157
+ - When set to a valid table UUID: the trigger skips table creation and only adds FK fields, composite key (if use_composite_key is true), and security to the existing table.
1158
+ Ignored for RelationBelongsTo/RelationHasOne.';
1159
+
1160
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.junction_table_name IS 'For RelationManyToMany: name of the junction table to create or look up. If NULL, auto-derived from source and target table names using inflection_db (e.g., "projects" + "tags" derives "project_tags"). Only used when junction_table_id is uuid_nil(). Ignored for RelationBelongsTo/RelationHasOne.';
1161
+
1162
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.junction_schema_id IS 'For RelationManyToMany: schema for the junction table. If NULL, defaults to the source table''s schema. Ignored for RelationBelongsTo/RelationHasOne.';
1163
+
1164
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.source_field_name IS 'For RelationManyToMany: FK field name on the junction table referencing the source table. If NULL, auto-derived from the source table name using inflection_db.get_foreign_key_field_name() (e.g., source table "projects" derives "project_id"). Ignored for RelationBelongsTo/RelationHasOne.';
1165
+
1166
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.target_field_name IS 'For RelationManyToMany: FK field name on the junction table referencing the target table. If NULL, auto-derived from the target table name using inflection_db.get_foreign_key_field_name() (e.g., target table "tags" derives "tag_id"). Ignored for RelationBelongsTo/RelationHasOne.';
1167
+
1168
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.use_composite_key IS 'For RelationManyToMany: whether to create a composite primary key from the two FK fields (source + target) on the junction table. Defaults to false.
1169
+ - When true: the trigger calls metaschema.pk() with ARRAY[source_field_id, target_field_id] to create a composite PK. No separate id column is created. This enforces uniqueness of the pair and is suitable for simple junction tables.
1170
+ - When false: no primary key is created by the trigger. The caller should provide node_type=''DataId'' to create a UUID primary key, or handle the PK strategy via a separate secure_table_provision row.
1171
+ use_composite_key and node_type=''DataId'' are mutually exclusive — using both would create two conflicting PKs.
1172
+ Ignored for RelationBelongsTo/RelationHasOne.';
1173
+
1174
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.create_index IS 'Whether to create a btree index on FK fields created by this relation. Defaults to true.
1175
+ PostgreSQL does not automatically index foreign key columns (only the referenced PK side is indexed).
1176
+ Without indexes on FK columns, JOINs, CASCADE deletes, and RLS policy lookups perform sequential scans.
1177
+ - RelationBelongsTo: creates an index on the FK field on the source table.
1178
+ - RelationHasMany: creates an index on the FK field on the target table.
1179
+ - RelationHasOne: skipped — the unique constraint already creates an implicit index.
1180
+ - RelationManyToMany: creates indexes on both FK fields on the junction table.
1181
+ Set to false only for very small tables or write-heavy tables where index maintenance cost outweighs read performance.';
1182
+
1183
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.expose_in_api IS 'For RelationManyToMany: whether to expose the M:N shortcut fields in the GraphQL API. Defaults to true.
1184
+ When true, sets @behavior +manyToMany on the junction table smart_tags so PostGraphile generates
1185
+ clean M:N connection fields (e.g., event.contacts instead of event.contactEventsByEventId).
1186
+ When false (or toggled off via UPDATE), the behavior tag is removed and the M:N fields disappear from GraphQL.
1187
+ Toggling is supported: UPDATE expose_in_api to true/false and the smart tag is added/removed automatically.
1188
+ Ignored for RelationBelongsTo/RelationHasOne/RelationHasMany.';
1189
+
1190
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.nodes IS 'For RelationManyToMany: array of node objects to apply to the junction table. Each element is a jsonb object with a required "$type" key and an optional "data" key. Forwarded to provision_table as-is. The trigger does not interpret or validate this value.
1191
+ Examples: [{"$type": "DataId"}, {"$type": "DataTimestamps"}, {"$type": "DataDirectOwner", "data": {"owner_field_name": "author_id"}}].
1192
+ Defaults to ''[]'' (no node processing beyond the FK fields and composite key if use_composite_key is true).
1193
+ Ignored for RelationBelongsTo/RelationHasOne/RelationHasMany.';
1194
+
1195
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.grants IS 'For RelationManyToMany: array of grant objects for the junction table. Forwarded to provision_table as-is. Each element is a jsonb object with keys: "roles" (text[], required), "privileges" (jsonb[], required — array of [privilege, columns] tuples). Example: [{"roles":["authenticated"],"privileges":[["select","*"],["insert","*"],["delete","*"]]}]. Defaults to ''[]'' (no grants). Ignored for RelationBelongsTo/RelationHasOne.';
1196
+
1197
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.policies IS 'For RelationManyToMany: array of policy objects for the junction table. Forwarded to provision_table as-is. Each element is a jsonb object with keys: "$type" (text, required — the Authz* policy generator type), "data" (jsonb, optional — opaque config), "privileges" (text[], optional — e.g. ["select","insert"]; if omitted, derived from grants[] privilege verbs), "policy_role" (text, optional — falls back to first role in first grants[] entry, or ''authenticated''), "permissive" (boolean, optional, defaults to true), "policy_name" (text, optional). Supports multiple policies per row.
1198
+ Example: [{"$type": "AuthzEntityMembership", "data": {"entity_field": "entity_id", "membership_type": 2}, "privileges": ["select", "insert", "delete"]}].
1199
+ Defaults to ''[]'' (no policies — the junction table will have RLS enabled but no policies unless added separately).
1200
+ Ignored for RelationBelongsTo/RelationHasOne/RelationHasMany.';
1201
+
1202
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.out_field_id IS 'Output column for RelationBelongsTo/RelationHasOne/RelationHasMany: the UUID of the FK field created (or found). For BelongsTo/HasOne this is on the source table; for HasMany this is on the target table. Populated by the trigger. NULL for RelationManyToMany. Callers should not set this directly.';
1203
+
1204
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.out_junction_table_id IS 'Output column for RelationManyToMany: the UUID of the junction table created (or found). Populated by the trigger. NULL for RelationBelongsTo/RelationHasOne. Callers should not set this directly.';
1205
+
1206
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.out_source_field_id IS 'Output column for RelationManyToMany: the UUID of the FK field on the junction table referencing the source table. Populated by the trigger. NULL for RelationBelongsTo/RelationHasOne. Callers should not set this directly.';
1207
+
1208
+ COMMENT ON COLUMN metaschema_modules_public.relation_provision.out_target_field_id IS 'Output column for RelationManyToMany: the UUID of the FK field on the junction table referencing the target table. Populated by the trigger. NULL for RelationBelongsTo/RelationHasOne. Callers should not set this directly.';
1209
+
1210
+ CREATE INDEX relation_provision_database_id_idx ON metaschema_modules_public.relation_provision (database_id);
1211
+
1212
+ CREATE INDEX relation_provision_relation_type_idx ON metaschema_modules_public.relation_provision (relation_type);
1213
+
1214
+ CREATE INDEX relation_provision_source_table_id_idx ON metaschema_modules_public.relation_provision (source_table_id);
1215
+
1216
+ CREATE INDEX relation_provision_target_table_id_idx ON metaschema_modules_public.relation_provision (target_table_id);
1217
+
1218
+ CREATE TABLE metaschema_modules_public.blueprint_template (
1219
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1220
+ name text NOT NULL,
1221
+ version text NOT NULL DEFAULT '1.0.0',
1222
+ display_name text NOT NULL,
1223
+ description text,
1224
+ owner_id uuid NOT NULL,
1225
+ visibility text NOT NULL DEFAULT 'private' CHECK (visibility IN ('private', 'public')),
1226
+ categories text[] NOT NULL DEFAULT '{}',
1227
+ tags text[] NOT NULL DEFAULT '{}',
1228
+ definition jsonb NOT NULL,
1229
+ definition_schema_version text NOT NULL DEFAULT '1',
1230
+ source text NOT NULL DEFAULT 'user' CHECK (source IN ('user', 'system', 'agent')),
1231
+ complexity text DEFAULT NULL CHECK (
1232
+ complexity IS NULL
1233
+ OR complexity IN ('simple', 'moderate', 'complex')
1234
+ ),
1235
+ copy_count int NOT NULL DEFAULT 0,
1236
+ fork_count int NOT NULL DEFAULT 0,
1237
+ forked_from_id uuid DEFAULT NULL,
1238
+ definition_hash uuid,
1239
+ table_hashes jsonb,
1240
+ created_at timestamptz NOT NULL DEFAULT now(),
1241
+ updated_at timestamptz NOT NULL DEFAULT now(),
1242
+ CONSTRAINT blueprint_template_unique_owner_name_version
1243
+ UNIQUE (owner_id, name, version),
1244
+ CONSTRAINT blueprint_template_forked_from_fkey
1245
+ FOREIGN KEY(forked_from_id)
1246
+ REFERENCES metaschema_modules_public.blueprint_template (id)
1247
+ );
1248
+
1249
+ COMMENT ON TABLE metaschema_modules_public.blueprint_template IS 'A shareable, versioned schema recipe for the blueprint marketplace. Templates define arrays of secure_table_provision + relation_provision inputs that together describe a complete domain schema (e.g. e-commerce, telemedicine, habit tracker). Templates are never executed directly — they are copied into a blueprint first via copy_template_to_blueprint(). Can be private (owner-only) or public (marketplace-visible).';
1250
+
1251
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.id IS 'Unique identifier for this template.';
1252
+
1253
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.name IS 'Machine-readable name for the template (e.g. e_commerce_basic). Must be unique per owner + version.';
1254
+
1255
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.version IS 'Semantic version string. Defaults to 1.0.0.';
1256
+
1257
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.display_name IS 'Human-readable display name for the template (e.g. E-Commerce Basic).';
1258
+
1259
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.description IS 'Optional description of what the template provisions.';
1260
+
1261
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.owner_id IS 'The user who created or published this template.';
1262
+
1263
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.visibility IS 'Access control for the template. private: only the owner can see and copy. public: anyone can browse and copy from the marketplace. Defaults to private.';
1264
+
1265
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.categories IS 'Domain categories for marketplace browsing (e.g. e-commerce, healthcare, social). Defaults to empty array.';
1266
+
1267
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.tags IS 'Freeform tags for search and discovery (e.g. products, orders, payments). Defaults to empty array.';
1268
+
1269
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.definition IS 'The blueprint definition as a JSONB document. Contains tables[] (each with nodes[] for data behaviors via string shorthand or {"$type": "...", "data": {...}} objects, fields[], grants[], and policies[] using {"$type": "...", "data": {...}}), and relations[] (using $type for relation_type with junction config in data). This is the core payload that gets copied into a blueprint for execution.';
1270
+
1271
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.definition_schema_version IS 'Version of the definition format schema. Used for forward-compatible parsing. Defaults to 1.';
1272
+
1273
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.source IS 'Provenance of the template. user: manually created by a human. system: official curated template from the Constructive team. agent: AI-generated. Defaults to user.';
1274
+
1275
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.complexity IS 'Complexity indicator for marketplace filtering. simple: 3-5 tables. moderate: 6-12 tables. complex: 13+ tables. NULL if not categorized.';
1276
+
1277
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.copy_count IS 'Denormalized count of how many blueprints have been created from this template via copy_template_to_blueprint(). Incremented automatically. Defaults to 0.';
1278
+
1279
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.fork_count IS 'Denormalized count of how many derivative templates have been forked from this template. Defaults to 0.';
1280
+
1281
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.forked_from_id IS 'If this template was forked from another template, the ID of the parent. NULL for original templates.';
1282
+
1283
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.created_at IS 'Timestamp when this template was created.';
1284
+
1285
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.definition_hash IS 'UUIDv5 Merkle root hash of the definition. Computed automatically via trigger from the ordered table_hashes. Used for content-addressable deduplication, provenance tracking, and cross-blueprint structural comparison. NULL columns are backend-computed — clients should never set this directly.';
1286
+
1287
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.table_hashes IS 'JSONB map of table ref names to their individual UUIDv5 content hashes (e.g. {"products": "uuid", "categories": "uuid"}). Each table hash is computed from the canonical jsonb::text of the table entry. Enables structural comparison at the table level across different blueprints. Backend-computed via trigger.';
1288
+
1289
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_template.updated_at IS 'Timestamp when this template was last modified.';
1290
+
1291
+ CREATE INDEX blueprint_template_owner_id_idx ON metaschema_modules_public.blueprint_template (owner_id);
1292
+
1293
+ CREATE INDEX blueprint_template_visibility_idx ON metaschema_modules_public.blueprint_template (visibility);
1294
+
1295
+ CREATE INDEX blueprint_template_forked_from_id_idx ON metaschema_modules_public.blueprint_template (forked_from_id);
1296
+
1297
+ CREATE INDEX blueprint_template_categories_idx ON metaschema_modules_public.blueprint_template USING gin (categories);
1298
+
1299
+ CREATE INDEX blueprint_template_tags_idx ON metaschema_modules_public.blueprint_template USING gin (tags);
1300
+
1301
+ CREATE INDEX blueprint_template_definition_hash_idx ON metaschema_modules_public.blueprint_template (definition_hash);
1302
+
1303
+ CREATE TABLE metaschema_modules_public.blueprint (
1304
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1305
+ owner_id uuid NOT NULL,
1306
+ database_id uuid NOT NULL,
1307
+ name text NOT NULL,
1308
+ display_name text NOT NULL,
1309
+ description text,
1310
+ definition jsonb NOT NULL,
1311
+ template_id uuid DEFAULT NULL,
1312
+ definition_hash uuid,
1313
+ table_hashes jsonb,
1314
+ created_at timestamptz NOT NULL DEFAULT now(),
1315
+ updated_at timestamptz NOT NULL DEFAULT now(),
1316
+ CONSTRAINT blueprint_unique_database_name
1317
+ UNIQUE (database_id, name),
1318
+ CONSTRAINT blueprint_db_fkey
1319
+ FOREIGN KEY(database_id)
1320
+ REFERENCES metaschema_public.database (id)
1321
+ ON DELETE CASCADE,
1322
+ CONSTRAINT blueprint_template_fkey
1323
+ FOREIGN KEY(template_id)
1324
+ REFERENCES metaschema_modules_public.blueprint_template (id)
1325
+ );
1326
+
1327
+ COMMENT ON TABLE metaschema_modules_public.blueprint IS 'An owned, editable blueprint scoped to a specific database. Created by copying from a blueprint_template via copy_template_to_blueprint() or built from scratch. The owner can customize the definition at any time. Execute it with construct_blueprint() which creates a separate blueprint_construction record to track the build.';
1328
+
1329
+ COMMENT ON COLUMN metaschema_modules_public.blueprint.id IS 'Unique identifier for this blueprint.';
1330
+
1331
+ COMMENT ON COLUMN metaschema_modules_public.blueprint.owner_id IS 'The user who owns this blueprint.';
1332
+
1333
+ COMMENT ON COLUMN metaschema_modules_public.blueprint.database_id IS 'The database this blueprint is scoped to. Tables created by construct_blueprint() are provisioned in this database.';
1334
+
1335
+ COMMENT ON COLUMN metaschema_modules_public.blueprint.name IS 'Machine-readable name for the blueprint. Must be unique per database.';
1336
+
1337
+ COMMENT ON COLUMN metaschema_modules_public.blueprint.display_name IS 'Human-readable display name for the blueprint.';
1338
+
1339
+ COMMENT ON COLUMN metaschema_modules_public.blueprint.description IS 'Optional description of the blueprint.';
1340
+
1341
+ COMMENT ON COLUMN metaschema_modules_public.blueprint.definition IS 'The blueprint definition as a JSONB document. Contains tables[] (each with table_name, optional schema_name, nodes[] for data behaviors, fields[], grants[], and policies[] using $type), relations[] (using $type with source_table/target_table and optional source_schema/target_schema), indexes[] (using table_name + column), and full_text_searches[] (using table_name + field + sources[]). Everything is name-based — no UUIDs in the definition.';
1342
+
1343
+ COMMENT ON COLUMN metaschema_modules_public.blueprint.template_id IS 'If this blueprint was created by copying a template, the ID of the source template. NULL if built from scratch.';
1344
+
1345
+ COMMENT ON COLUMN metaschema_modules_public.blueprint.created_at IS 'Timestamp when this blueprint was created.';
1346
+
1347
+ COMMENT ON COLUMN metaschema_modules_public.blueprint.definition_hash IS 'UUIDv5 Merkle root hash of the definition. Computed automatically via trigger from the ordered table_hashes. Used for content-addressable deduplication and provenance tracking. Backend-computed — clients should never set this directly.';
1348
+
1349
+ COMMENT ON COLUMN metaschema_modules_public.blueprint.table_hashes IS 'JSONB map of table names to their individual UUIDv5 content hashes. Each table hash is computed from the canonical jsonb::text of the table entry. Enables structural comparison at the table level across blueprints and templates. Backend-computed via trigger.';
1350
+
1351
+ COMMENT ON COLUMN metaschema_modules_public.blueprint.updated_at IS 'Timestamp when this blueprint was last modified.';
1352
+
1353
+ CREATE INDEX blueprint_owner_id_idx ON metaschema_modules_public.blueprint (owner_id);
1354
+
1355
+ CREATE INDEX blueprint_database_id_idx ON metaschema_modules_public.blueprint (database_id);
1356
+
1357
+ CREATE INDEX blueprint_template_id_idx ON metaschema_modules_public.blueprint (template_id);
1358
+
1359
+ CREATE INDEX blueprint_definition_hash_idx ON metaschema_modules_public.blueprint (definition_hash);
1360
+
1361
+ CREATE TABLE metaschema_modules_public.blueprint_construction (
1362
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1363
+ blueprint_id uuid NOT NULL,
1364
+ database_id uuid NOT NULL,
1365
+ schema_id uuid,
1366
+ status text NOT NULL DEFAULT 'pending' CHECK (status IN ('pending', 'constructing', 'constructed', 'failed')),
1367
+ error_details text,
1368
+ table_map jsonb NOT NULL DEFAULT '{}',
1369
+ constructed_definition jsonb,
1370
+ constructed_at timestamptz,
1371
+ created_at timestamptz NOT NULL DEFAULT now(),
1372
+ updated_at timestamptz NOT NULL DEFAULT now(),
1373
+ CONSTRAINT blueprint_construction_blueprint_fkey
1374
+ FOREIGN KEY(blueprint_id)
1375
+ REFERENCES metaschema_modules_public.blueprint (id)
1376
+ ON DELETE CASCADE,
1377
+ CONSTRAINT blueprint_construction_db_fkey
1378
+ FOREIGN KEY(database_id)
1379
+ REFERENCES metaschema_public.database (id)
1380
+ ON DELETE CASCADE
1381
+ );
1382
+
1383
+ COMMENT ON TABLE metaschema_modules_public.blueprint_construction IS 'Tracks individual construction attempts of a blueprint. Each time construct_blueprint() is called, a new record is created here. This separates the editable blueprint definition from its build history, allowing blueprints to be re-executed, constructed into multiple databases, and maintain an audit trail of all construction attempts.';
1384
+
1385
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_construction.id IS 'Unique identifier for this construction attempt.';
1386
+
1387
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_construction.blueprint_id IS 'The blueprint that was constructed.';
1388
+
1389
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_construction.database_id IS 'The database the blueprint was constructed into.';
1390
+
1391
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_construction.schema_id IS 'The default schema used for tables that did not specify an explicit schema_name. NULL if not yet resolved.';
1392
+
1393
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_construction.status IS 'Execution state of this construction attempt. pending: created but not yet started. constructing: currently executing. constructed: successfully completed. failed: execution failed (see error_details).';
1394
+
1395
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_construction.error_details IS 'Error message from a failed construction attempt. NULL unless status is failed.';
1396
+
1397
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_construction.table_map IS 'Mapping of table names to created table UUIDs, populated after successful construction. Format: {"products": "uuid", "categories": "uuid", ...}. Defaults to empty object.';
1398
+
1399
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_construction.constructed_definition IS 'Immutable snapshot of the definition at construct-time. Preserved so the exact definition that was executed is recorded even if the user later modifies the blueprint definition.';
1400
+
1401
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_construction.constructed_at IS 'Timestamp when construction successfully completed. NULL until constructed.';
1402
+
1403
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_construction.created_at IS 'Timestamp when this construction attempt was created.';
1404
+
1405
+ COMMENT ON COLUMN metaschema_modules_public.blueprint_construction.updated_at IS 'Timestamp when this construction attempt was last modified.';
1406
+
1407
+ CREATE INDEX blueprint_construction_blueprint_id_idx ON metaschema_modules_public.blueprint_construction (blueprint_id);
1408
+
1409
+ CREATE INDEX blueprint_construction_database_id_idx ON metaschema_modules_public.blueprint_construction (database_id);
1410
+
1411
+ CREATE INDEX blueprint_construction_status_idx ON metaschema_modules_public.blueprint_construction (status);
1412
+
1413
+ CREATE TABLE metaschema_modules_public.storage_module (
1414
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1415
+ database_id uuid NOT NULL,
1416
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
1417
+ private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
1418
+ buckets_table_id uuid NOT NULL DEFAULT uuid_nil(),
1419
+ files_table_id uuid NOT NULL DEFAULT uuid_nil(),
1420
+ buckets_table_name text NOT NULL DEFAULT 'app_buckets',
1421
+ files_table_name text NOT NULL DEFAULT 'app_files',
1422
+ membership_type int DEFAULT NULL,
1423
+ policies jsonb NULL,
1424
+ skip_default_policy_tables text[] NOT NULL DEFAULT '{}',
1425
+ entity_table_id uuid NULL,
1426
+ endpoint text NULL,
1427
+ public_url_prefix text NULL,
1428
+ provider text NULL,
1429
+ allowed_origins text[] NULL,
1430
+ restrict_reads boolean NOT NULL DEFAULT false,
1431
+ has_path_shares boolean NOT NULL DEFAULT false,
1432
+ path_shares_table_id uuid NULL DEFAULT NULL,
1433
+ upload_url_expiry_seconds int NULL,
1434
+ download_url_expiry_seconds int NULL,
1435
+ default_max_file_size bigint NULL,
1436
+ max_filename_length int NULL,
1437
+ cache_ttl_seconds int NULL,
1438
+ max_bulk_files int NULL,
1439
+ max_bulk_total_size bigint NULL,
1440
+ has_versioning boolean NOT NULL DEFAULT false,
1441
+ has_content_hash boolean NOT NULL DEFAULT false,
1442
+ has_custom_keys boolean NOT NULL DEFAULT false,
1443
+ has_audit_log boolean NOT NULL DEFAULT false,
1444
+ file_events_table_id uuid NULL DEFAULT NULL,
1445
+ CONSTRAINT db_fkey
1446
+ FOREIGN KEY(database_id)
1447
+ REFERENCES metaschema_public.database (id)
1448
+ ON DELETE CASCADE,
1449
+ CONSTRAINT schema_fkey
1450
+ FOREIGN KEY(schema_id)
1451
+ REFERENCES metaschema_public.schema (id)
1452
+ ON DELETE CASCADE,
1453
+ CONSTRAINT private_schema_fkey
1454
+ FOREIGN KEY(private_schema_id)
1455
+ REFERENCES metaschema_public.schema (id)
1456
+ ON DELETE CASCADE,
1457
+ CONSTRAINT buckets_table_fkey
1458
+ FOREIGN KEY(buckets_table_id)
1459
+ REFERENCES metaschema_public."table" (id)
1460
+ ON DELETE CASCADE,
1461
+ CONSTRAINT files_table_fkey
1462
+ FOREIGN KEY(files_table_id)
1463
+ REFERENCES metaschema_public."table" (id)
1464
+ ON DELETE CASCADE,
1465
+ CONSTRAINT entity_table_fkey
1466
+ FOREIGN KEY(entity_table_id)
1467
+ REFERENCES metaschema_public."table" (id)
1468
+ ON DELETE CASCADE,
1469
+ CONSTRAINT path_shares_table_fkey
1470
+ FOREIGN KEY(path_shares_table_id)
1471
+ REFERENCES metaschema_public."table" (id)
1472
+ ON DELETE CASCADE,
1473
+ CONSTRAINT file_events_table_fkey
1474
+ FOREIGN KEY(file_events_table_id)
1475
+ REFERENCES metaschema_public."table" (id)
1476
+ ON DELETE CASCADE
1477
+ );
1478
+
1479
+ CREATE INDEX storage_module_database_id_idx ON metaschema_modules_public.storage_module (database_id);
1480
+
1481
+ CREATE UNIQUE INDEX storage_module_unique_scope ON metaschema_modules_public.storage_module (database_id, (COALESCE(membership_type, -1)));
1482
+
1483
+ CREATE TABLE metaschema_modules_public.entity_type_provision (
1484
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1485
+ database_id uuid NOT NULL,
1486
+ name text NOT NULL,
1487
+ prefix text NOT NULL,
1488
+ description text NOT NULL DEFAULT '',
1489
+ parent_entity text NOT NULL DEFAULT 'org',
1490
+ table_name text DEFAULT NULL,
1491
+ is_visible boolean NOT NULL DEFAULT true,
1492
+ has_limits boolean NOT NULL DEFAULT false,
1493
+ has_profiles boolean NOT NULL DEFAULT false,
1494
+ has_levels boolean NOT NULL DEFAULT false,
1495
+ has_storage boolean NOT NULL DEFAULT false,
1496
+ has_invites boolean NOT NULL DEFAULT false,
1497
+ storage_config jsonb DEFAULT NULL,
1498
+ skip_entity_policies boolean NOT NULL DEFAULT false,
1499
+ table_provision jsonb DEFAULT NULL,
1500
+ out_membership_type int DEFAULT NULL,
1501
+ out_entity_table_id uuid DEFAULT NULL,
1502
+ out_entity_table_name text DEFAULT NULL,
1503
+ out_installed_modules text[] DEFAULT NULL,
1504
+ out_storage_module_id uuid DEFAULT NULL,
1505
+ out_buckets_table_id uuid DEFAULT NULL,
1506
+ out_files_table_id uuid DEFAULT NULL,
1507
+ out_path_shares_table_id uuid DEFAULT NULL,
1508
+ out_invites_module_id uuid DEFAULT NULL,
1509
+ CONSTRAINT entity_type_provision_unique_prefix
1510
+ UNIQUE (database_id, prefix),
1511
+ CONSTRAINT entity_type_provision_db_fkey
1512
+ FOREIGN KEY(database_id)
1513
+ REFERENCES metaschema_public.database (id)
1514
+ ON DELETE CASCADE
1515
+ );
1516
+
1517
+ COMMENT ON TABLE metaschema_modules_public.entity_type_provision IS 'Provisions a new membership entity type. Each INSERT creates an entity table, registers a membership type,
1518
+ and installs the required modules (permissions, memberships, limits) plus optional modules (profiles, levels, invites).
1519
+ Uses provision_membership_table() internally. Graceful: duplicate (database_id, prefix) pairs are silently skipped
1520
+ via the unique constraint (use INSERT ... ON CONFLICT DO NOTHING).
1521
+ Policy behavior: by default the five entity-table RLS policies are applied (gated by is_visible).
1522
+ Set table_provision to a single jsonb object (using the same shape as provision_table() /
1523
+ blueprint tables[] entries) to replace the defaults with your own; set skip_entity_policies=true
1524
+ as an escape hatch to apply zero policies.';
1525
+
1526
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.id IS 'Unique identifier for this provision row.';
1527
+
1528
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.database_id IS 'The database to provision this entity type in. Required.';
1529
+
1530
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.name IS 'Human-readable name for this entity type, e.g. ''Data Room'', ''Team Channel''. Required.
1531
+ Stored in the entity_types registry table.';
1532
+
1533
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.prefix IS 'SQL prefix used for table and module naming, e.g. ''data_room'', ''team_channel''. Required.
1534
+ Drives entity table name (prefix || ''s'' by default), module labels (permissions_module:prefix),
1535
+ and membership table names (prefix_memberships, prefix_members, etc.).
1536
+ Must be unique per database — the (database_id, prefix) constraint ensures graceful ON CONFLICT DO NOTHING.';
1537
+
1538
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.description IS 'Description of this entity type. Stored in the entity_types registry table. Defaults to empty string.';
1539
+
1540
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.parent_entity IS 'Prefix of the parent entity type. The trigger resolves this to a membership_type integer
1541
+ by looking up memberships_module WHERE prefix = parent_entity.
1542
+ Defaults to ''org'' (the organization-level type). For nested types, set to the parent''s prefix
1543
+ (e.g. ''data_room'' for a team_channel nested under data_room).
1544
+ The parent type must already be provisioned before this INSERT.';
1545
+
1546
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.table_name IS 'Override the entity table name. When NULL (default), the table name is derived as prefix || ''s''
1547
+ (e.g. prefix ''data_room'' produces table ''data_rooms'').
1548
+ Set this when the pluralization rule doesn''t apply (e.g. prefix ''staff'' should produce ''staff'' not ''staffs'').';
1549
+
1550
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.is_visible IS 'Whether members of the parent entity can see child entities. Defaults to true.
1551
+ When true: a SELECT policy allows parent members to list child entities (e.g. org members can see all data rooms).
1552
+ When false: only direct members of the entity itself can see it (private entity mode).
1553
+ Controls whether the parent_member SELECT policy is created on the entity table.
1554
+ Only meaningful on the defaults path — ignored (no-op) when table_provision is non-NULL or
1555
+ skip_entity_policies=true, since no default policies are being applied in those cases.';
1556
+
1557
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.has_limits IS 'Whether to apply limits_module security for this type. Defaults to false.
1558
+ The limits_module table structure is always created (memberships_module requires it),
1559
+ but when false, no RLS policies are applied to the limits tables.
1560
+ Set to true if this entity type needs configurable resource limits per membership.';
1561
+
1562
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.has_profiles IS 'Whether to provision profiles_module for this type. Defaults to false.
1563
+ Profiles provide named permission roles (e.g. ''Editor'', ''Viewer'') with pre-configured permission bitmasks.
1564
+ When true, creates profile tables and applies profiles security.';
1565
+
1566
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.has_levels IS 'Whether to provision levels_module for this type. Defaults to false.
1567
+ Levels provide gamification/achievement tracking for members.
1568
+ When true, creates level steps, achievements, and level tables with security.';
1569
+
1570
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.has_storage IS 'Whether to provision storage_module for this type. Defaults to false.
1571
+ When true, creates {prefix}_buckets and {prefix}_files tables
1572
+ with entity-scoped RLS (AuthzEntityMembership) using the entity''s membership_type.
1573
+ Storage tables get owner_id FK to the entity table, so files are owned by the entity.';
1574
+
1575
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.has_invites IS 'Whether to provision invites_module for this type. Defaults to false.
1576
+ When true, the trigger inserts a row into invites_module which in turn
1577
+ (via insert_invites_module BEFORE INSERT) creates {prefix}_invites and
1578
+ {prefix}_claimed_invites tables plus the submit_{prefix}_invite_code() function.
1579
+ Symmetric counterpart of has_storage. Re-provisioning is idempotent: the
1580
+ UNIQUE (database_id, membership_type) constraint on invites_module combined with
1581
+ ON CONFLICT DO NOTHING in the fan-out makes repeated INSERTs safe.';
1582
+
1583
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.skip_entity_policies IS 'Escape hatch: when true, apply zero RLS policies to the entity table. Defaults to false.
1584
+ Use this only when you want the entity table provisioned with zero policies (e.g. because you
1585
+ plan to insert secure_table_provision rows yourself later). In most cases, prefer leaving this
1586
+ false and either accepting the five defaults (table_provision=NULL) or overriding them via
1587
+ table_provision.
1588
+ Defaults (applied when table_provision IS NULL and skip_entity_policies=false):
1589
+ - SELECT (parent_member): parent entity members can see child entities (only when is_visible=true)
1590
+ - SELECT (self_member): direct members of the entity can see it
1591
+ - INSERT: create_entity permission on the parent entity
1592
+ - UPDATE: admin_entity permission on the entity itself
1593
+ - DELETE: owner of the entity can delete it';
1594
+
1595
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.table_provision IS 'Single jsonb object describing the full security setup to apply to the entity table.
1596
+ Uses the same vocabulary as metaschema_modules_public.provision_table() and blueprint tables[]
1597
+ entries, so an entity table is configured the same way an ordinary blueprint table is.
1598
+ Defaults to NULL; when non-NULL, the five default policies are implicitly replaced by
1599
+ table_provision.policies[] (is_visible becomes a no-op on this path).
1600
+ Recognized keys (all optional):
1601
+ - use_rls (boolean, default true)
1602
+ - nodes (jsonb array of {"$type","data"} Data* module entries)
1603
+ - fields (jsonb array of field objects: name,type,is_required,default,min,max,regexp,index)
1604
+ - grants (jsonb array of grant objects; each with roles[] and privileges[])
1605
+ - policies (jsonb array of policy objects; each with $type, privileges, data, name, role, permissive)
1606
+ The trigger forwards all setup (nodes/fields/grants/policies) as a single secure_table_provision row
1607
+ against the newly created entity table.
1608
+ Example — override with two SELECT policies:
1609
+ table_provision := jsonb_build_object(
1610
+ ''policies'', jsonb_build_array(
1611
+ jsonb_build_object(
1612
+ ''$type'', ''AuthzEntityMembership'',
1613
+ ''privileges'', jsonb_build_array(''select''),
1614
+ ''data'', jsonb_build_object(''entity_field'', ''id'', ''membership_type'', 3),
1615
+ ''name'', ''self_member''
1616
+ ),
1617
+ jsonb_build_object(
1618
+ ''$type'', ''AuthzDirectOwner'',
1619
+ ''privileges'', jsonb_build_array(''select'', ''update''),
1620
+ ''data'', jsonb_build_object(''owner_field'', ''owner_id'')
1621
+ )
1622
+ )
1623
+ )';
1624
+
1625
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_membership_type IS 'Output: the auto-assigned integer membership type ID. Populated by the trigger after successful provisioning.
1626
+ This is the ID used in entity_types, memberships_module, and all module tables.';
1627
+
1628
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_entity_table_id IS 'Output: the UUID of the created entity table. Populated by the trigger.
1629
+ Use this to reference the entity table in subsequent relation_provision or secure_table_provision rows.';
1630
+
1631
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_entity_table_name IS 'Output: the name of the created entity table (e.g. ''data_rooms''). Populated by the trigger.';
1632
+
1633
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_installed_modules IS 'Output: array of installed module labels (e.g. ARRAY[''permissions_module:data_room'', ''memberships_module:data_room'', ''invites_module:data_room'']).
1634
+ Populated by the trigger. Useful for verifying which modules were provisioned.';
1635
+
1636
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.storage_config IS 'Optional jsonb object for storage module configuration and initial bucket seeding.
1637
+ Only used when has_storage = true; ignored otherwise. NULL = use defaults.
1638
+ Recognized keys (all optional):
1639
+ - upload_url_expiry_seconds (integer) presigned PUT URL expiry override
1640
+ - download_url_expiry_seconds (integer) presigned GET URL expiry override
1641
+ - default_max_file_size (bigint) global max file size in bytes for this scope
1642
+ - allowed_origins (text[]) default CORS origins for all buckets in this scope
1643
+ - buckets (jsonb[]) array of initial bucket definitions to seed
1644
+ Each bucket in the buckets array recognizes:
1645
+ - name (text, required) bucket name e.g. ''documents''
1646
+ - description (text) human-readable description
1647
+ - is_public (boolean) whether files are publicly readable (default false)
1648
+ - allowed_mime_types (text[]) whitelist of MIME types (null = any)
1649
+ - max_file_size (bigint) max file size in bytes (null = use scope default)
1650
+ - allowed_origins (text[]) per-bucket CORS override
1651
+ - provisions (jsonb object) optional: customize storage tables
1652
+ with additional nodes, fields, grants, and policies.
1653
+ Keyed by table role: "files", "buckets".
1654
+ Each value uses the same shape as table_provision:
1655
+ { nodes, fields, grants, use_rls, policies }. Fanned out
1656
+ to secure_table_provision targeting the corresponding table.
1657
+ When a key includes policies[], those REPLACE the default
1658
+ storage policies for that table; tables without a key still
1659
+ get defaults. Missing "data" on policy entries is auto-populated
1660
+ with storage-specific defaults (same as table_provision).
1661
+ Example: add SearchBm25 for full-text search on files:
1662
+ {"provisions": {"files": {"nodes": [{"$type":
1663
+ "SearchBm25", "data": {"source_fields": ["description"]}}]}}}
1664
+ Example:
1665
+ storage_config := ''{"buckets": [{"name": "documents", "is_public": false, "allowed_mime_types": ["application/pdf"]}], "provisions": {"files": {"nodes": [{"$type": "SearchBm25", "data": {"source_fields": ["description"]}}]}}}''::jsonb';
1666
+
1667
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_storage_module_id IS 'Output: the UUID of the storage_module row created for this entity type. Populated by the trigger when has_storage=true.';
1668
+
1669
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_buckets_table_id IS 'Output: the UUID of the generated buckets table (e.g. data_room_buckets). Populated by the trigger when has_storage=true.';
1670
+
1671
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_files_table_id IS 'Output: the UUID of the generated files table (e.g. data_room_files). Populated by the trigger when has_storage=true.';
1672
+
1673
+ COMMENT ON COLUMN metaschema_modules_public.entity_type_provision.out_invites_module_id IS 'Output: the UUID of the invites_module row created for this entity type. Populated by the trigger when has_invites=true.
1674
+ NULL when has_invites=false, or when re-provisioning hits ON CONFLICT DO NOTHING
1675
+ (i.e. the invites_module row was created in a previous run).';
1676
+
1677
+ CREATE TABLE metaschema_modules_public.rate_limits_module (
1678
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1679
+ database_id uuid NOT NULL,
1680
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
1681
+ rate_limit_settings_table_id uuid NOT NULL DEFAULT uuid_nil(),
1682
+ ip_rate_limits_table_id uuid NOT NULL DEFAULT uuid_nil(),
1683
+ rate_limits_table_id uuid NOT NULL DEFAULT uuid_nil(),
1684
+ rate_limit_settings_table text NOT NULL DEFAULT 'app_settings_rate_limit',
1685
+ ip_rate_limits_table text NOT NULL DEFAULT 'auth_ip_rate_limits',
1686
+ rate_limits_table text NOT NULL DEFAULT 'auth_rate_limits',
1687
+ CONSTRAINT db_fkey
1688
+ FOREIGN KEY(database_id)
1689
+ REFERENCES metaschema_public.database (id)
1690
+ ON DELETE CASCADE,
1691
+ CONSTRAINT schema_fkey
1692
+ FOREIGN KEY(schema_id)
1693
+ REFERENCES metaschema_public.schema (id)
1694
+ ON DELETE CASCADE,
1695
+ CONSTRAINT rate_limit_settings_table_fkey
1696
+ FOREIGN KEY(rate_limit_settings_table_id)
1697
+ REFERENCES metaschema_public."table" (id)
1698
+ ON DELETE CASCADE,
1699
+ CONSTRAINT ip_rate_limits_table_fkey
1700
+ FOREIGN KEY(ip_rate_limits_table_id)
1701
+ REFERENCES metaschema_public."table" (id)
1702
+ ON DELETE CASCADE,
1703
+ CONSTRAINT rate_limits_table_fkey
1704
+ FOREIGN KEY(rate_limits_table_id)
1705
+ REFERENCES metaschema_public."table" (id)
1706
+ ON DELETE CASCADE,
1707
+ CONSTRAINT rate_limits_module_database_id_uniq
1708
+ UNIQUE (database_id)
1709
+ );
1710
+
1711
+ CREATE INDEX rate_limits_module_database_id_idx ON metaschema_modules_public.rate_limits_module (database_id);
1712
+
1713
+ COMMENT ON CONSTRAINT rate_limit_settings_table_fkey ON metaschema_modules_public.rate_limits_module IS '@fieldName rateLimitSettingsTableByRateLimitSettingsTableId';
1714
+
1715
+ COMMENT ON CONSTRAINT ip_rate_limits_table_fkey ON metaschema_modules_public.rate_limits_module IS '@fieldName ipRateLimitsTableByIpRateLimitsTableId';
1716
+
1717
+ COMMENT ON CONSTRAINT rate_limits_table_fkey ON metaschema_modules_public.rate_limits_module IS '@fieldName rateLimitsTableByRateLimitsTableId';
1718
+
1719
+ CREATE TABLE metaschema_modules_public.devices_module (
1720
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1721
+ database_id uuid NOT NULL,
1722
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
1723
+ user_devices_table_id uuid NOT NULL DEFAULT uuid_nil(),
1724
+ device_settings_table_id uuid NOT NULL DEFAULT uuid_nil(),
1725
+ user_devices_table text NOT NULL DEFAULT 'auth_user_devices',
1726
+ device_settings_table text NOT NULL DEFAULT 'app_settings_device',
1727
+ CONSTRAINT db_fkey
1728
+ FOREIGN KEY(database_id)
1729
+ REFERENCES metaschema_public.database (id)
1730
+ ON DELETE CASCADE,
1731
+ CONSTRAINT schema_fkey
1732
+ FOREIGN KEY(schema_id)
1733
+ REFERENCES metaschema_public.schema (id)
1734
+ ON DELETE CASCADE,
1735
+ CONSTRAINT user_devices_table_fkey
1736
+ FOREIGN KEY(user_devices_table_id)
1737
+ REFERENCES metaschema_public."table" (id)
1738
+ ON DELETE CASCADE,
1739
+ CONSTRAINT device_settings_table_fkey
1740
+ FOREIGN KEY(device_settings_table_id)
1741
+ REFERENCES metaschema_public."table" (id)
1742
+ ON DELETE CASCADE,
1743
+ CONSTRAINT devices_module_database_id_uniq
1744
+ UNIQUE (database_id)
1745
+ );
1746
+
1747
+ CREATE INDEX devices_module_database_id_idx ON metaschema_modules_public.devices_module (database_id);
1748
+
1749
+ COMMENT ON CONSTRAINT user_devices_table_fkey ON metaschema_modules_public.devices_module IS '@fieldName userDevicesTableByUserDevicesTableId';
1750
+
1751
+ COMMENT ON CONSTRAINT device_settings_table_fkey ON metaschema_modules_public.devices_module IS '@fieldName deviceSettingsTableByDeviceSettingsTableId';
1752
+
1753
+ CREATE TABLE metaschema_modules_public.session_secrets_module (
1754
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1755
+ database_id uuid NOT NULL,
1756
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
1757
+ table_id uuid NOT NULL DEFAULT uuid_nil(),
1758
+ table_name text NOT NULL DEFAULT 'session_secrets',
1759
+ sessions_table_id uuid NOT NULL DEFAULT uuid_nil(),
1760
+ CONSTRAINT db_fkey
1761
+ FOREIGN KEY(database_id)
1762
+ REFERENCES metaschema_public.database (id)
1763
+ ON DELETE CASCADE,
1764
+ CONSTRAINT schema_fkey
1765
+ FOREIGN KEY(schema_id)
1766
+ REFERENCES metaschema_public.schema (id)
1767
+ ON DELETE CASCADE,
1768
+ CONSTRAINT table_fkey
1769
+ FOREIGN KEY(table_id)
1770
+ REFERENCES metaschema_public."table" (id)
1771
+ ON DELETE CASCADE,
1772
+ CONSTRAINT sessions_table_fkey
1773
+ FOREIGN KEY(sessions_table_id)
1774
+ REFERENCES metaschema_public."table" (id)
1775
+ ON DELETE CASCADE
1776
+ );
1777
+
1778
+ CREATE INDEX session_secrets_module_database_id_idx ON metaschema_modules_public.session_secrets_module (database_id);
1779
+
1780
+ CREATE INDEX session_secrets_module_schema_id_idx ON metaschema_modules_public.session_secrets_module (schema_id);
1781
+
1782
+ CREATE INDEX session_secrets_module_table_id_idx ON metaschema_modules_public.session_secrets_module (table_id);
1783
+
1784
+ CREATE INDEX session_secrets_module_sessions_table_id_idx ON metaschema_modules_public.session_secrets_module (sessions_table_id);
1785
+
1786
+ COMMENT ON TABLE metaschema_modules_public.session_secrets_module IS 'Config row for the session_secrets_module, which provisions a DB-private, session-scoped ephemeral key-value store for challenges, nonces, and one-time tokens that must never be readable by end users.';
1787
+
1788
+ COMMENT ON COLUMN metaschema_modules_public.session_secrets_module.sessions_table_id IS 'Resolved reference to sessions_module.sessions_table, used to FK session_secrets.session_id with ON DELETE CASCADE.';
1789
+
1790
+ CREATE TABLE metaschema_modules_public.webauthn_credentials_module (
1791
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1386
1792
  database_id uuid NOT NULL,
1387
1793
  schema_id uuid NOT NULL DEFAULT uuid_nil(),
1388
1794
  private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
1389
1795
  table_id uuid NOT NULL DEFAULT uuid_nil(),
1390
1796
  owner_table_id uuid NOT NULL DEFAULT uuid_nil(),
1391
- table_name text NOT NULL,
1392
- node_type text NOT NULL,
1393
- data jsonb NOT NULL DEFAULT '{}',
1797
+ table_name text NOT NULL DEFAULT 'webauthn_credentials',
1394
1798
  CONSTRAINT db_fkey
1395
1799
  FOREIGN KEY(database_id)
1396
1800
  REFERENCES metaschema_public.database (id)
@@ -1413,24 +1817,420 @@ CREATE TABLE metaschema_modules_public.table_template_module (
1413
1817
  ON DELETE CASCADE
1414
1818
  );
1415
1819
 
1416
- COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.table_template_module IS '@omit manyToMany';
1820
+ CREATE INDEX webauthn_credentials_module_database_id_idx ON metaschema_modules_public.webauthn_credentials_module (database_id);
1821
+
1822
+ COMMENT ON TABLE metaschema_modules_public.webauthn_credentials_module IS 'Config row for the webauthn_credentials_module, which provisions the per-user WebAuthn/passkey credentials table (public key, counter, transports, device type, backup state) mirroring crypto_addresses_module. The sibling webauthn_auth_module holds RP config and the registration/sign-in challenge state.';
1823
+
1824
+ COMMENT ON COLUMN metaschema_modules_public.webauthn_credentials_module.private_schema_id IS 'Private schema that hosts SECURITY DEFINER helpers which write to webauthn_credentials (registration / counter-bump / delete).';
1825
+
1826
+ CREATE TABLE metaschema_modules_public.webauthn_auth_module (
1827
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1828
+ database_id uuid NOT NULL,
1829
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
1830
+ users_table_id uuid NOT NULL DEFAULT uuid_nil(),
1831
+ credentials_table_id uuid NOT NULL DEFAULT uuid_nil(),
1832
+ sessions_table_id uuid NOT NULL DEFAULT uuid_nil(),
1833
+ session_credentials_table_id uuid NOT NULL DEFAULT uuid_nil(),
1834
+ session_secrets_table_id uuid NOT NULL DEFAULT uuid_nil(),
1835
+ auth_settings_table_id uuid NOT NULL DEFAULT uuid_nil(),
1836
+ rp_id text NOT NULL DEFAULT '',
1837
+ rp_name text NOT NULL DEFAULT '',
1838
+ origin_allowlist text[] NOT NULL DEFAULT '{}',
1839
+ attestation_type text NOT NULL DEFAULT 'none' CHECK (attestation_type IN ('none', 'indirect', 'direct', 'enterprise')),
1840
+ require_user_verification boolean NOT NULL DEFAULT false,
1841
+ resident_key text NOT NULL DEFAULT 'required' CHECK (resident_key IN ('discouraged', 'preferred', 'required')),
1842
+ challenge_expiry interval NOT NULL DEFAULT '5 minutes',
1843
+ CONSTRAINT db_fkey
1844
+ FOREIGN KEY(database_id)
1845
+ REFERENCES metaschema_public.database (id)
1846
+ ON DELETE CASCADE,
1847
+ CONSTRAINT schema_fkey
1848
+ FOREIGN KEY(schema_id)
1849
+ REFERENCES metaschema_public.schema (id)
1850
+ ON DELETE CASCADE,
1851
+ CONSTRAINT users_table_fkey
1852
+ FOREIGN KEY(users_table_id)
1853
+ REFERENCES metaschema_public."table" (id)
1854
+ ON DELETE CASCADE,
1855
+ CONSTRAINT credentials_table_fkey
1856
+ FOREIGN KEY(credentials_table_id)
1857
+ REFERENCES metaschema_public."table" (id)
1858
+ ON DELETE CASCADE,
1859
+ CONSTRAINT sessions_table_fkey
1860
+ FOREIGN KEY(sessions_table_id)
1861
+ REFERENCES metaschema_public."table" (id)
1862
+ ON DELETE CASCADE,
1863
+ CONSTRAINT session_credentials_table_fkey
1864
+ FOREIGN KEY(session_credentials_table_id)
1865
+ REFERENCES metaschema_public."table" (id)
1866
+ ON DELETE CASCADE,
1867
+ CONSTRAINT session_secrets_table_fkey
1868
+ FOREIGN KEY(session_secrets_table_id)
1869
+ REFERENCES metaschema_public."table" (id)
1870
+ ON DELETE CASCADE,
1871
+ CONSTRAINT auth_settings_table_fkey
1872
+ FOREIGN KEY(auth_settings_table_id)
1873
+ REFERENCES metaschema_public."table" (id)
1874
+ ON DELETE CASCADE
1875
+ );
1876
+
1877
+ CREATE INDEX webauthn_auth_module_database_id_idx ON metaschema_modules_public.webauthn_auth_module (database_id);
1878
+
1879
+ CREATE TABLE metaschema_modules_public.identity_providers_module (
1880
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
1881
+ database_id uuid NOT NULL,
1882
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
1883
+ private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
1884
+ table_id uuid NOT NULL DEFAULT uuid_nil(),
1885
+ table_name text NOT NULL DEFAULT 'identity_providers',
1886
+ CONSTRAINT db_fkey
1887
+ FOREIGN KEY(database_id)
1888
+ REFERENCES metaschema_public.database (id)
1889
+ ON DELETE CASCADE,
1890
+ CONSTRAINT table_fkey
1891
+ FOREIGN KEY(table_id)
1892
+ REFERENCES metaschema_public."table" (id)
1893
+ ON DELETE CASCADE,
1894
+ CONSTRAINT schema_fkey
1895
+ FOREIGN KEY(schema_id)
1896
+ REFERENCES metaschema_public.schema (id)
1897
+ ON DELETE CASCADE,
1898
+ CONSTRAINT private_schema_fkey
1899
+ FOREIGN KEY(private_schema_id)
1900
+ REFERENCES metaschema_public.schema (id)
1901
+ ON DELETE CASCADE
1902
+ );
1903
+
1904
+ CREATE INDEX identity_providers_module_database_id_idx ON metaschema_modules_public.identity_providers_module (database_id);
1905
+
1906
+ CREATE INDEX identity_providers_module_schema_id_idx ON metaschema_modules_public.identity_providers_module (schema_id);
1907
+
1908
+ CREATE INDEX identity_providers_module_private_schema_id_idx ON metaschema_modules_public.identity_providers_module (private_schema_id);
1909
+
1910
+ CREATE INDEX identity_providers_module_table_id_idx ON metaschema_modules_public.identity_providers_module (table_id);
1911
+
1912
+ COMMENT ON TABLE metaschema_modules_public.identity_providers_module IS 'Config row for the identity_providers_module, which provisions a per-database identity_providers config table holding OAuth2 / OIDC (and future SAML) provider definitions: protocol kind, endpoint URLs, encrypted client secret, scopes, audience validation, PKCE, and email-handling flags. Built-in providers (google, github, apple, ...) are seeded as is_built_in=true rows; custom providers use slugs of the form custom:<slug>.';
1913
+
1914
+ COMMENT ON COLUMN metaschema_modules_public.identity_providers_module.private_schema_id IS 'Private schema that hosts SECURITY DEFINER admin helpers which write to identity_providers (create / update / enable / disable / rotate-secret / delete) and the per-app quota check.';
1915
+
1916
+ CREATE TABLE metaschema_modules_public.notifications_module (
1917
+ id uuid PRIMARY KEY DEFAULT uuid_generate_v4(),
1918
+ database_id uuid NOT NULL,
1919
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
1920
+ private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
1921
+ notifications_table_id uuid NOT NULL DEFAULT uuid_nil(),
1922
+ read_state_table_id uuid NOT NULL DEFAULT uuid_nil(),
1923
+ preferences_table_id uuid,
1924
+ channels_table_id uuid,
1925
+ delivery_log_table_id uuid,
1926
+ owner_table_id uuid NOT NULL DEFAULT uuid_nil(),
1927
+ user_settings_table_id uuid,
1928
+ organization_settings_table_id uuid,
1929
+ has_channels boolean NOT NULL DEFAULT true,
1930
+ has_preferences boolean NOT NULL DEFAULT true,
1931
+ has_settings_extension boolean NOT NULL DEFAULT false,
1932
+ has_digest_metadata boolean NOT NULL DEFAULT false,
1933
+ has_subscriptions boolean NOT NULL DEFAULT false,
1934
+ CONSTRAINT db_fkey
1935
+ FOREIGN KEY(database_id)
1936
+ REFERENCES metaschema_public.database (id)
1937
+ ON DELETE CASCADE,
1938
+ CONSTRAINT notifications_table_fkey
1939
+ FOREIGN KEY(notifications_table_id)
1940
+ REFERENCES metaschema_public."table" (id)
1941
+ ON DELETE CASCADE,
1942
+ CONSTRAINT read_state_table_fkey
1943
+ FOREIGN KEY(read_state_table_id)
1944
+ REFERENCES metaschema_public."table" (id)
1945
+ ON DELETE CASCADE,
1946
+ CONSTRAINT preferences_table_fkey
1947
+ FOREIGN KEY(preferences_table_id)
1948
+ REFERENCES metaschema_public."table" (id)
1949
+ ON DELETE SET NULL,
1950
+ CONSTRAINT channels_table_fkey
1951
+ FOREIGN KEY(channels_table_id)
1952
+ REFERENCES metaschema_public."table" (id)
1953
+ ON DELETE SET NULL,
1954
+ CONSTRAINT delivery_log_table_fkey
1955
+ FOREIGN KEY(delivery_log_table_id)
1956
+ REFERENCES metaschema_public."table" (id)
1957
+ ON DELETE SET NULL,
1958
+ CONSTRAINT owner_table_fkey
1959
+ FOREIGN KEY(owner_table_id)
1960
+ REFERENCES metaschema_public."table" (id)
1961
+ ON DELETE CASCADE,
1962
+ CONSTRAINT user_settings_table_fkey
1963
+ FOREIGN KEY(user_settings_table_id)
1964
+ REFERENCES metaschema_public."table" (id)
1965
+ ON DELETE SET NULL,
1966
+ CONSTRAINT organization_settings_table_fkey
1967
+ FOREIGN KEY(organization_settings_table_id)
1968
+ REFERENCES metaschema_public."table" (id)
1969
+ ON DELETE SET NULL,
1970
+ CONSTRAINT schema_fkey
1971
+ FOREIGN KEY(schema_id)
1972
+ REFERENCES metaschema_public.schema (id)
1973
+ ON DELETE CASCADE,
1974
+ CONSTRAINT private_schema_fkey
1975
+ FOREIGN KEY(private_schema_id)
1976
+ REFERENCES metaschema_public.schema (id)
1977
+ ON DELETE CASCADE
1978
+ );
1979
+
1980
+ COMMENT ON CONSTRAINT schema_fkey ON metaschema_modules_public.notifications_module IS '@omit manyToMany';
1981
+
1982
+ COMMENT ON CONSTRAINT private_schema_fkey ON metaschema_modules_public.notifications_module IS '@omit manyToMany';
1983
+
1984
+ COMMENT ON CONSTRAINT notifications_table_fkey ON metaschema_modules_public.notifications_module IS '@fieldName notificationsTableByNotificationsTableId
1985
+ @omit manyToMany';
1986
+
1987
+ COMMENT ON CONSTRAINT read_state_table_fkey ON metaschema_modules_public.notifications_module IS '@fieldName readStateTableByReadStateTableId
1988
+ @omit manyToMany';
1989
+
1990
+ COMMENT ON CONSTRAINT preferences_table_fkey ON metaschema_modules_public.notifications_module IS '@fieldName preferencesTableByPreferencesTableId
1991
+ @omit manyToMany';
1992
+
1993
+ COMMENT ON CONSTRAINT channels_table_fkey ON metaschema_modules_public.notifications_module IS '@fieldName channelsTableByChannelsTableId
1994
+ @omit manyToMany';
1995
+
1996
+ COMMENT ON CONSTRAINT delivery_log_table_fkey ON metaschema_modules_public.notifications_module IS '@fieldName deliveryLogTableByDeliveryLogTableId
1997
+ @omit manyToMany';
1998
+
1999
+ COMMENT ON CONSTRAINT owner_table_fkey ON metaschema_modules_public.notifications_module IS '@omit manyToMany';
2000
+
2001
+ COMMENT ON CONSTRAINT user_settings_table_fkey ON metaschema_modules_public.notifications_module IS '@fieldName userSettingsTableByUserSettingsTableId
2002
+ @omit manyToMany';
2003
+
2004
+ COMMENT ON CONSTRAINT organization_settings_table_fkey ON metaschema_modules_public.notifications_module IS '@fieldName organizationSettingsTableByOrganizationSettingsTableId
2005
+ @omit manyToMany';
2006
+
2007
+ COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.notifications_module IS '@omit manyToMany';
2008
+
2009
+ CREATE INDEX notifications_module_database_id_idx ON metaschema_modules_public.notifications_module (database_id);
2010
+
2011
+ CREATE TABLE metaschema_modules_public.plans_module (
2012
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
2013
+ database_id uuid NOT NULL,
2014
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
2015
+ private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
2016
+ plans_table_id uuid NOT NULL DEFAULT uuid_nil(),
2017
+ plans_table_name text NOT NULL DEFAULT '',
2018
+ plan_limits_table_id uuid NOT NULL DEFAULT uuid_nil(),
2019
+ plan_limits_table_name text NOT NULL DEFAULT '',
2020
+ plan_pricing_table_id uuid NULL,
2021
+ plan_overrides_table_id uuid NULL,
2022
+ apply_plan_function text NOT NULL DEFAULT '',
2023
+ apply_plan_aggregate_function text NOT NULL DEFAULT '',
2024
+ prefix text NULL,
2025
+ CONSTRAINT db_fkey
2026
+ FOREIGN KEY(database_id)
2027
+ REFERENCES metaschema_public.database (id)
2028
+ ON DELETE CASCADE,
2029
+ CONSTRAINT schema_fkey
2030
+ FOREIGN KEY(schema_id)
2031
+ REFERENCES metaschema_public.schema (id)
2032
+ ON DELETE CASCADE,
2033
+ CONSTRAINT private_schema_fkey
2034
+ FOREIGN KEY(private_schema_id)
2035
+ REFERENCES metaschema_public.schema (id)
2036
+ ON DELETE CASCADE,
2037
+ CONSTRAINT plans_table_fkey
2038
+ FOREIGN KEY(plans_table_id)
2039
+ REFERENCES metaschema_public."table" (id)
2040
+ ON DELETE CASCADE,
2041
+ CONSTRAINT plan_limits_table_fkey
2042
+ FOREIGN KEY(plan_limits_table_id)
2043
+ REFERENCES metaschema_public."table" (id)
2044
+ ON DELETE CASCADE,
2045
+ CONSTRAINT plan_pricing_table_fkey
2046
+ FOREIGN KEY(plan_pricing_table_id)
2047
+ REFERENCES metaschema_public."table" (id)
2048
+ ON DELETE CASCADE,
2049
+ CONSTRAINT plan_overrides_table_fkey
2050
+ FOREIGN KEY(plan_overrides_table_id)
2051
+ REFERENCES metaschema_public."table" (id)
2052
+ ON DELETE CASCADE,
2053
+ CONSTRAINT plans_module_database_id_unique
2054
+ UNIQUE (database_id)
2055
+ );
2056
+
2057
+ CREATE INDEX plans_module_database_id_idx ON metaschema_modules_public.plans_module (database_id);
1417
2058
 
1418
- COMMENT ON CONSTRAINT private_schema_fkey ON metaschema_modules_public.table_template_module IS '@omit manyToMany';
2059
+ CREATE TABLE metaschema_modules_public.billing_module (
2060
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
2061
+ database_id uuid NOT NULL,
2062
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
2063
+ private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
2064
+ meters_table_id uuid NOT NULL DEFAULT uuid_nil(),
2065
+ meters_table_name text NOT NULL DEFAULT '',
2066
+ plan_subscriptions_table_id uuid NOT NULL DEFAULT uuid_nil(),
2067
+ plan_subscriptions_table_name text NOT NULL DEFAULT '',
2068
+ ledger_table_id uuid NOT NULL DEFAULT uuid_nil(),
2069
+ ledger_table_name text NOT NULL DEFAULT '',
2070
+ balances_table_id uuid NOT NULL DEFAULT uuid_nil(),
2071
+ balances_table_name text NOT NULL DEFAULT '',
2072
+ record_usage_function text NOT NULL DEFAULT '',
2073
+ prefix text NULL,
2074
+ CONSTRAINT db_fkey
2075
+ FOREIGN KEY(database_id)
2076
+ REFERENCES metaschema_public.database (id)
2077
+ ON DELETE CASCADE,
2078
+ CONSTRAINT schema_fkey
2079
+ FOREIGN KEY(schema_id)
2080
+ REFERENCES metaschema_public.schema (id)
2081
+ ON DELETE CASCADE,
2082
+ CONSTRAINT private_schema_fkey
2083
+ FOREIGN KEY(private_schema_id)
2084
+ REFERENCES metaschema_public.schema (id)
2085
+ ON DELETE CASCADE,
2086
+ CONSTRAINT meters_table_fkey
2087
+ FOREIGN KEY(meters_table_id)
2088
+ REFERENCES metaschema_public."table" (id)
2089
+ ON DELETE CASCADE,
2090
+ CONSTRAINT plan_subscriptions_table_fkey
2091
+ FOREIGN KEY(plan_subscriptions_table_id)
2092
+ REFERENCES metaschema_public."table" (id)
2093
+ ON DELETE CASCADE,
2094
+ CONSTRAINT ledger_table_fkey
2095
+ FOREIGN KEY(ledger_table_id)
2096
+ REFERENCES metaschema_public."table" (id)
2097
+ ON DELETE CASCADE,
2098
+ CONSTRAINT balances_table_fkey
2099
+ FOREIGN KEY(balances_table_id)
2100
+ REFERENCES metaschema_public."table" (id)
2101
+ ON DELETE CASCADE,
2102
+ CONSTRAINT billing_module_database_id_unique
2103
+ UNIQUE (database_id)
2104
+ );
2105
+
2106
+ CREATE INDEX billing_module_database_id_idx ON metaschema_modules_public.billing_module (database_id);
1419
2107
 
1420
- COMMENT ON CONSTRAINT table_fkey ON metaschema_modules_public.table_template_module IS '@omit manyToMany';
2108
+ CREATE TABLE metaschema_modules_public.billing_provider_module (
2109
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
2110
+ database_id uuid NOT NULL,
2111
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
2112
+ private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
2113
+ provider text NOT NULL DEFAULT 'stripe',
2114
+ products_table_id uuid NULL,
2115
+ prices_table_id uuid NULL,
2116
+ subscriptions_table_id uuid NULL,
2117
+ billing_customers_table_id uuid NOT NULL DEFAULT uuid_nil(),
2118
+ billing_customers_table_name text NOT NULL DEFAULT '',
2119
+ billing_products_table_id uuid NOT NULL DEFAULT uuid_nil(),
2120
+ billing_products_table_name text NOT NULL DEFAULT '',
2121
+ billing_prices_table_id uuid NOT NULL DEFAULT uuid_nil(),
2122
+ billing_prices_table_name text NOT NULL DEFAULT '',
2123
+ billing_subscriptions_table_id uuid NOT NULL DEFAULT uuid_nil(),
2124
+ billing_subscriptions_table_name text NOT NULL DEFAULT '',
2125
+ billing_webhook_events_table_id uuid NOT NULL DEFAULT uuid_nil(),
2126
+ billing_webhook_events_table_name text NOT NULL DEFAULT '',
2127
+ process_billing_event_function text NOT NULL DEFAULT '',
2128
+ prefix text NULL,
2129
+ CONSTRAINT db_fkey
2130
+ FOREIGN KEY(database_id)
2131
+ REFERENCES metaschema_public.database (id)
2132
+ ON DELETE CASCADE,
2133
+ CONSTRAINT schema_fkey
2134
+ FOREIGN KEY(schema_id)
2135
+ REFERENCES metaschema_public.schema (id)
2136
+ ON DELETE CASCADE,
2137
+ CONSTRAINT private_schema_fkey
2138
+ FOREIGN KEY(private_schema_id)
2139
+ REFERENCES metaschema_public.schema (id)
2140
+ ON DELETE CASCADE,
2141
+ CONSTRAINT billing_customers_table_fkey
2142
+ FOREIGN KEY(billing_customers_table_id)
2143
+ REFERENCES metaschema_public."table" (id)
2144
+ ON DELETE CASCADE,
2145
+ CONSTRAINT billing_products_table_fkey
2146
+ FOREIGN KEY(billing_products_table_id)
2147
+ REFERENCES metaschema_public."table" (id)
2148
+ ON DELETE CASCADE,
2149
+ CONSTRAINT billing_prices_table_fkey
2150
+ FOREIGN KEY(billing_prices_table_id)
2151
+ REFERENCES metaschema_public."table" (id)
2152
+ ON DELETE CASCADE,
2153
+ CONSTRAINT billing_subscriptions_table_fkey
2154
+ FOREIGN KEY(billing_subscriptions_table_id)
2155
+ REFERENCES metaschema_public."table" (id)
2156
+ ON DELETE CASCADE,
2157
+ CONSTRAINT billing_webhook_events_table_fkey
2158
+ FOREIGN KEY(billing_webhook_events_table_id)
2159
+ REFERENCES metaschema_public."table" (id)
2160
+ ON DELETE CASCADE,
2161
+ CONSTRAINT products_table_fkey
2162
+ FOREIGN KEY(products_table_id)
2163
+ REFERENCES metaschema_public."table" (id)
2164
+ ON DELETE SET NULL,
2165
+ CONSTRAINT prices_table_fkey
2166
+ FOREIGN KEY(prices_table_id)
2167
+ REFERENCES metaschema_public."table" (id)
2168
+ ON DELETE SET NULL,
2169
+ CONSTRAINT subscriptions_table_fkey
2170
+ FOREIGN KEY(subscriptions_table_id)
2171
+ REFERENCES metaschema_public."table" (id)
2172
+ ON DELETE SET NULL,
2173
+ CONSTRAINT billing_provider_module_database_id_unique
2174
+ UNIQUE (database_id)
2175
+ );
2176
+
2177
+ CREATE INDEX billing_provider_module_database_id_idx ON metaschema_modules_public.billing_provider_module (database_id);
2178
+
2179
+ CREATE TABLE metaschema_modules_public.realtime_module (
2180
+ id uuid PRIMARY KEY DEFAULT uuidv7(),
2181
+ database_id uuid NOT NULL,
2182
+ schema_id uuid NOT NULL DEFAULT uuid_nil(),
2183
+ private_schema_id uuid NOT NULL DEFAULT uuid_nil(),
2184
+ subscriptions_schema_id uuid NOT NULL DEFAULT uuid_nil(),
2185
+ change_log_table_id uuid NOT NULL DEFAULT uuid_nil(),
2186
+ listener_node_table_id uuid NOT NULL DEFAULT uuid_nil(),
2187
+ source_registry_table_id uuid NOT NULL DEFAULT uuid_nil(),
2188
+ retention_hours int NOT NULL DEFAULT 168,
2189
+ lookahead_hours int NOT NULL DEFAULT 24,
2190
+ partition_interval text NOT NULL DEFAULT 'hourly',
2191
+ notify_channel text NULL,
2192
+ CONSTRAINT db_fkey
2193
+ FOREIGN KEY(database_id)
2194
+ REFERENCES metaschema_public.database (id)
2195
+ ON DELETE CASCADE,
2196
+ CONSTRAINT schema_fkey
2197
+ FOREIGN KEY(schema_id)
2198
+ REFERENCES metaschema_public.schema (id)
2199
+ ON DELETE CASCADE,
2200
+ CONSTRAINT private_schema_fkey
2201
+ FOREIGN KEY(private_schema_id)
2202
+ REFERENCES metaschema_public.schema (id)
2203
+ ON DELETE CASCADE,
2204
+ CONSTRAINT subscriptions_schema_fkey
2205
+ FOREIGN KEY(subscriptions_schema_id)
2206
+ REFERENCES metaschema_public.schema (id)
2207
+ ON DELETE CASCADE,
2208
+ CONSTRAINT change_log_table_fkey
2209
+ FOREIGN KEY(change_log_table_id)
2210
+ REFERENCES metaschema_public."table" (id)
2211
+ ON DELETE CASCADE,
2212
+ CONSTRAINT listener_node_table_fkey
2213
+ FOREIGN KEY(listener_node_table_id)
2214
+ REFERENCES metaschema_public."table" (id)
2215
+ ON DELETE CASCADE,
2216
+ CONSTRAINT source_registry_table_fkey
2217
+ FOREIGN KEY(source_registry_table_id)
2218
+ REFERENCES metaschema_public."table" (id)
2219
+ ON DELETE CASCADE
2220
+ );
1421
2221
 
1422
- COMMENT ON CONSTRAINT owner_table_fkey ON metaschema_modules_public.table_template_module IS '@omit manyToMany';
2222
+ CREATE INDEX realtime_module_database_id_idx ON metaschema_modules_public.realtime_module (database_id);
1423
2223
 
1424
- COMMENT ON CONSTRAINT db_fkey ON metaschema_modules_public.table_template_module IS '@omit manyToMany';
2224
+ CREATE UNIQUE INDEX realtime_module_unique_per_db ON metaschema_modules_public.realtime_module (database_id);
1425
2225
 
1426
- CREATE INDEX table_template_module_database_id_idx ON metaschema_modules_public.table_template_module (database_id);
2226
+ CREATE INDEX realtime_module_schema_id_idx ON metaschema_modules_public.realtime_module (schema_id);
1427
2227
 
1428
- CREATE INDEX table_template_module_schema_id_idx ON metaschema_modules_public.table_template_module (schema_id);
2228
+ CREATE INDEX realtime_module_private_schema_id_idx ON metaschema_modules_public.realtime_module (private_schema_id);
1429
2229
 
1430
- CREATE INDEX table_template_module_private_schema_id_idx ON metaschema_modules_public.table_template_module (private_schema_id);
2230
+ CREATE INDEX realtime_module_subscriptions_schema_id_idx ON metaschema_modules_public.realtime_module (subscriptions_schema_id);
1431
2231
 
1432
- CREATE INDEX table_template_module_table_id_idx ON metaschema_modules_public.table_template_module (table_id);
2232
+ CREATE INDEX realtime_module_change_log_table_id_idx ON metaschema_modules_public.realtime_module (change_log_table_id);
1433
2233
 
1434
- CREATE INDEX table_template_module_owner_table_id_idx ON metaschema_modules_public.table_template_module (owner_table_id);
2234
+ CREATE INDEX realtime_module_listener_node_table_id_idx ON metaschema_modules_public.realtime_module (listener_node_table_id);
1435
2235
 
1436
- CREATE INDEX table_template_module_node_type_idx ON metaschema_modules_public.table_template_module (node_type);
2236
+ CREATE INDEX realtime_module_source_registry_table_id_idx ON metaschema_modules_public.realtime_module (source_registry_table_id);