diff --git a/packages/metaschema-modules/__tests__/__snapshots__/modules.test.ts.snap b/packages/metaschema-modules/__tests__/__snapshots__/modules.test.ts.snap index 4c485783..710201fe 100644 --- a/packages/metaschema-modules/__tests__/__snapshots__/modules.test.ts.snap +++ b/packages/metaschema-modules/__tests__/__snapshots__/modules.test.ts.snap @@ -89,13 +89,13 @@ exports[`db_meta_modules should verify emails_module table structure 1`] = ` exports[`db_meta_modules should verify module table structures have database_id foreign keys 1`] = ` { - "constraintCount": 67416, + "constraintCount": 75264, } `; exports[`db_meta_modules should verify module tables have proper foreign key relationships 1`] = ` { - "constraintCount": 94299, + "constraintCount": 104916, "foreignTables": [ "apis", "database", diff --git a/packages/metaschema-schema/deploy/schemas/metaschema_public/tables/view/table.sql b/packages/metaschema-schema/deploy/schemas/metaschema_public/tables/view/table.sql new file mode 100644 index 00000000..7058ea01 --- /dev/null +++ b/packages/metaschema-schema/deploy/schemas/metaschema_public/tables/view/table.sql @@ -0,0 +1,59 @@ +-- Deploy schemas/metaschema_public/tables/view/table to pg + +-- requires: schemas/metaschema_public/schema +-- requires: schemas/metaschema_public/tables/schema/table +-- requires: schemas/metaschema_public/tables/table/table +-- requires: schemas/metaschema_public/tables/database/table +-- requires: schemas/metaschema_public/types/object_category + +BEGIN; + +CREATE TABLE metaschema_public.view ( + id uuid PRIMARY KEY DEFAULT uuid_generate_v4 (), + database_id uuid NOT NULL DEFAULT uuid_nil(), + + schema_id uuid NOT NULL, + name text NOT NULL, + + -- Primary/source table for the view (nullable for ViewComposite) + -- For ViewTableProjection, ViewFilteredTable, ViewAggregated: the source table + -- For ViewJoinedTables: the primary (left-most) table + -- For ViewComposite: NULL (no table reference) + table_id uuid, + + -- View query definition using View* node types + view_type text NOT NULL, + data jsonb DEFAULT '{}', + + -- Optional filter using Authz* node types (baked into view WHERE clause) + filter_type text, + filter_data jsonb DEFAULT '{}', + + -- View options + security_invoker boolean DEFAULT true, + is_read_only boolean DEFAULT true, + + smart_tags jsonb, + + category metaschema_public.object_category NOT NULL DEFAULT 'app', + module text NULL, + scope int NULL, + + tags citext[] NOT NULL DEFAULT '{}', + + CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE, + CONSTRAINT schema_fkey FOREIGN KEY (schema_id) REFERENCES metaschema_public.schema (id) ON DELETE CASCADE, + CONSTRAINT table_fkey FOREIGN KEY (table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE, + + UNIQUE (schema_id, name) +); + +COMMENT ON CONSTRAINT schema_fkey ON metaschema_public.view IS E'@omit manyToMany'; +COMMENT ON CONSTRAINT db_fkey ON metaschema_public.view IS E'@omit manyToMany'; +COMMENT ON CONSTRAINT table_fkey ON metaschema_public.view IS E'@omit manyToMany'; + +CREATE INDEX view_schema_id_idx ON metaschema_public.view ( schema_id ); +CREATE INDEX view_database_id_idx ON metaschema_public.view ( database_id ); +CREATE INDEX view_table_id_idx ON metaschema_public.view ( table_id ); + +COMMIT; diff --git a/packages/metaschema-schema/deploy/schemas/metaschema_public/tables/view_grant/table.sql b/packages/metaschema-schema/deploy/schemas/metaschema_public/tables/view_grant/table.sql new file mode 100644 index 00000000..32f29b3a --- /dev/null +++ b/packages/metaschema-schema/deploy/schemas/metaschema_public/tables/view_grant/table.sql @@ -0,0 +1,31 @@ +-- Deploy schemas/metaschema_public/tables/view_grant/table to pg + +-- requires: schemas/metaschema_public/schema +-- requires: schemas/metaschema_public/tables/view/table +-- requires: schemas/metaschema_public/tables/database/table + +BEGIN; + +CREATE TABLE metaschema_public.view_grant ( + id uuid PRIMARY KEY DEFAULT uuid_generate_v4 (), + database_id uuid NOT NULL DEFAULT uuid_nil(), + + view_id uuid NOT NULL, + role_name text NOT NULL, + privilege text NOT NULL, + + with_grant_option boolean DEFAULT false, + + CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE, + CONSTRAINT view_fkey FOREIGN KEY (view_id) REFERENCES metaschema_public.view (id) ON DELETE CASCADE, + + UNIQUE (view_id, role_name, privilege) +); + +COMMENT ON CONSTRAINT view_fkey ON metaschema_public.view_grant IS E'@omit manyToMany'; +COMMENT ON CONSTRAINT db_fkey ON metaschema_public.view_grant IS E'@omit manyToMany'; + +CREATE INDEX view_grant_view_id_idx ON metaschema_public.view_grant ( view_id ); +CREATE INDEX view_grant_database_id_idx ON metaschema_public.view_grant ( database_id ); + +COMMIT; diff --git a/packages/metaschema-schema/deploy/schemas/metaschema_public/tables/view_rule/table.sql b/packages/metaschema-schema/deploy/schemas/metaschema_public/tables/view_rule/table.sql new file mode 100644 index 00000000..fc52303a --- /dev/null +++ b/packages/metaschema-schema/deploy/schemas/metaschema_public/tables/view_rule/table.sql @@ -0,0 +1,34 @@ +-- Deploy schemas/metaschema_public/tables/view_rule/table to pg + +-- requires: schemas/metaschema_public/schema +-- requires: schemas/metaschema_public/tables/view/table +-- requires: schemas/metaschema_public/tables/database/table + +BEGIN; + +CREATE TABLE metaschema_public.view_rule ( + id uuid PRIMARY KEY DEFAULT uuid_generate_v4 (), + database_id uuid NOT NULL DEFAULT uuid_nil(), + + view_id uuid NOT NULL, + name text NOT NULL, + event text NOT NULL, + action text NOT NULL DEFAULT 'NOTHING', + + CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE, + CONSTRAINT view_fkey FOREIGN KEY (view_id) REFERENCES metaschema_public.view (id) ON DELETE CASCADE, + + UNIQUE (view_id, name) +); + +COMMENT ON TABLE metaschema_public.view_rule IS 'DO INSTEAD rules for views (e.g., read-only enforcement)'; +COMMENT ON COLUMN metaschema_public.view_rule.event IS 'INSERT, UPDATE, or DELETE'; +COMMENT ON COLUMN metaschema_public.view_rule.action IS 'NOTHING (for read-only) or custom action'; + +COMMENT ON CONSTRAINT view_fkey ON metaschema_public.view_rule IS E'@omit manyToMany'; +COMMENT ON CONSTRAINT db_fkey ON metaschema_public.view_rule IS E'@omit manyToMany'; + +CREATE INDEX view_rule_view_id_idx ON metaschema_public.view_rule ( view_id ); +CREATE INDEX view_rule_database_id_idx ON metaschema_public.view_rule ( database_id ); + +COMMIT; diff --git a/packages/metaschema-schema/deploy/schemas/metaschema_public/tables/view_table/table.sql b/packages/metaschema-schema/deploy/schemas/metaschema_public/tables/view_table/table.sql new file mode 100644 index 00000000..e01b87ae --- /dev/null +++ b/packages/metaschema-schema/deploy/schemas/metaschema_public/tables/view_table/table.sql @@ -0,0 +1,33 @@ +-- Deploy schemas/metaschema_public/tables/view_table/table to pg + +-- requires: schemas/metaschema_public/schema +-- requires: schemas/metaschema_public/tables/view/table +-- requires: schemas/metaschema_public/tables/table/table + +BEGIN; + +-- Junction table linking views to their joined tables (for ViewJoinedTables) +-- This provides referential integrity for views that reference multiple tables. +-- The primary table is stored in view.table_id; this table stores additional joined tables. +CREATE TABLE metaschema_public.view_table ( + id uuid PRIMARY KEY DEFAULT uuid_generate_v4 (), + view_id uuid NOT NULL, + table_id uuid NOT NULL, + + -- Order of joins (0 = first join, 1 = second join, etc.) + join_order int NOT NULL DEFAULT 0, + + CONSTRAINT view_fkey FOREIGN KEY (view_id) REFERENCES metaschema_public.view (id) ON DELETE CASCADE, + CONSTRAINT table_fkey FOREIGN KEY (table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE, + + UNIQUE (view_id, table_id) +); + +COMMENT ON TABLE metaschema_public.view_table IS 'Junction table linking views to their joined tables for referential integrity'; +COMMENT ON CONSTRAINT view_fkey ON metaschema_public.view_table IS E'@omit manyToMany'; +COMMENT ON CONSTRAINT table_fkey ON metaschema_public.view_table IS E'@omit manyToMany'; + +CREATE INDEX view_table_view_id_idx ON metaschema_public.view_table ( view_id ); +CREATE INDEX view_table_table_id_idx ON metaschema_public.view_table ( table_id ); + +COMMIT; diff --git a/packages/metaschema-schema/pgpm.plan b/packages/metaschema-schema/pgpm.plan index e0a761a1..9f24f668 100644 --- a/packages/metaschema-schema/pgpm.plan +++ b/packages/metaschema-schema/pgpm.plan @@ -28,3 +28,7 @@ schemas/metaschema_public/tables/table/indexes/databases_table_unique_name_idx [ schemas/metaschema_public/tables/trigger_function/table [schemas/metaschema_public/schema schemas/metaschema_public/tables/database/table] 2017-08-11T08:11:51Z skitch # add schemas/metaschema_public/tables/trigger_function/table schemas/metaschema_public/tables/trigger/table [schemas/metaschema_public/schema schemas/metaschema_public/tables/table/table schemas/metaschema_public/types/object_category] 2017-08-11T08:11:51Z skitch # add schemas/metaschema_public/tables/trigger/table schemas/metaschema_public/tables/unique_constraint/table [schemas/metaschema_public/schema schemas/metaschema_public/tables/database/table schemas/metaschema_public/tables/table/table schemas/metaschema_public/types/object_category] 2017-08-11T08:11:51Z skitch # add schemas/metaschema_public/tables/unique_constraint/table +schemas/metaschema_public/tables/view/table [schemas/metaschema_public/schema schemas/metaschema_public/tables/schema/table schemas/metaschema_public/tables/table/table schemas/metaschema_public/tables/database/table schemas/metaschema_public/types/object_category] 2026-01-23T00:00:00Z devin # add schemas/metaschema_public/tables/view/table +schemas/metaschema_public/tables/view_table/table [schemas/metaschema_public/schema schemas/metaschema_public/tables/view/table schemas/metaschema_public/tables/table/table] 2026-01-23T00:00:00Z devin # add schemas/metaschema_public/tables/view_table/table +schemas/metaschema_public/tables/view_grant/table [schemas/metaschema_public/schema schemas/metaschema_public/tables/view/table schemas/metaschema_public/tables/database/table] 2026-01-23T00:00:00Z devin # add schemas/metaschema_public/tables/view_grant/table +schemas/metaschema_public/tables/view_rule/table [schemas/metaschema_public/schema schemas/metaschema_public/tables/view/table schemas/metaschema_public/tables/database/table] 2026-01-23T00:00:00Z devin # add schemas/metaschema_public/tables/view_rule/table diff --git a/packages/metaschema-schema/revert/schemas/metaschema_public/tables/view/table.sql b/packages/metaschema-schema/revert/schemas/metaschema_public/tables/view/table.sql new file mode 100644 index 00000000..422f467b --- /dev/null +++ b/packages/metaschema-schema/revert/schemas/metaschema_public/tables/view/table.sql @@ -0,0 +1,7 @@ +-- Revert schemas/metaschema_public/tables/view/table from pg + +BEGIN; + +DROP TABLE IF EXISTS metaschema_public.view; + +COMMIT; diff --git a/packages/metaschema-schema/revert/schemas/metaschema_public/tables/view_grant/table.sql b/packages/metaschema-schema/revert/schemas/metaschema_public/tables/view_grant/table.sql new file mode 100644 index 00000000..52fdd9fb --- /dev/null +++ b/packages/metaschema-schema/revert/schemas/metaschema_public/tables/view_grant/table.sql @@ -0,0 +1,7 @@ +-- Revert schemas/metaschema_public/tables/view_grant/table from pg + +BEGIN; + +DROP TABLE IF EXISTS metaschema_public.view_grant; + +COMMIT; diff --git a/packages/metaschema-schema/revert/schemas/metaschema_public/tables/view_rule/table.sql b/packages/metaschema-schema/revert/schemas/metaschema_public/tables/view_rule/table.sql new file mode 100644 index 00000000..76c3265d --- /dev/null +++ b/packages/metaschema-schema/revert/schemas/metaschema_public/tables/view_rule/table.sql @@ -0,0 +1,7 @@ +-- Revert schemas/metaschema_public/tables/view_rule/table from pg + +BEGIN; + +DROP TABLE IF EXISTS metaschema_public.view_rule; + +COMMIT; diff --git a/packages/metaschema-schema/revert/schemas/metaschema_public/tables/view_table/table.sql b/packages/metaschema-schema/revert/schemas/metaschema_public/tables/view_table/table.sql new file mode 100644 index 00000000..08df9345 --- /dev/null +++ b/packages/metaschema-schema/revert/schemas/metaschema_public/tables/view_table/table.sql @@ -0,0 +1,7 @@ +-- Revert schemas/metaschema_public/tables/view_table/table from pg + +BEGIN; + +DROP TABLE IF EXISTS metaschema_public.view_table; + +COMMIT; diff --git a/packages/metaschema-schema/verify/schemas/metaschema_public/tables/view/table.sql b/packages/metaschema-schema/verify/schemas/metaschema_public/tables/view/table.sql new file mode 100644 index 00000000..b35e8ab2 --- /dev/null +++ b/packages/metaschema-schema/verify/schemas/metaschema_public/tables/view/table.sql @@ -0,0 +1,10 @@ +-- Verify schemas/metaschema_public/tables/view/table on pg + +BEGIN; + +SELECT id, database_id, schema_id, name, view_type, data, filter_type, filter_data, + security_invoker, is_read_only, smart_tags, category, module, scope, tags +FROM metaschema_public.view +WHERE FALSE; + +ROLLBACK; diff --git a/packages/metaschema-schema/verify/schemas/metaschema_public/tables/view_grant/table.sql b/packages/metaschema-schema/verify/schemas/metaschema_public/tables/view_grant/table.sql new file mode 100644 index 00000000..d3084fc2 --- /dev/null +++ b/packages/metaschema-schema/verify/schemas/metaschema_public/tables/view_grant/table.sql @@ -0,0 +1,9 @@ +-- Verify schemas/metaschema_public/tables/view_grant/table on pg + +BEGIN; + +SELECT id, database_id, view_id, role_name, privilege, with_grant_option +FROM metaschema_public.view_grant +WHERE FALSE; + +ROLLBACK; diff --git a/packages/metaschema-schema/verify/schemas/metaschema_public/tables/view_rule/table.sql b/packages/metaschema-schema/verify/schemas/metaschema_public/tables/view_rule/table.sql new file mode 100644 index 00000000..f280ea01 --- /dev/null +++ b/packages/metaschema-schema/verify/schemas/metaschema_public/tables/view_rule/table.sql @@ -0,0 +1,9 @@ +-- Verify schemas/metaschema_public/tables/view_rule/table on pg + +BEGIN; + +SELECT id, database_id, view_id, name, event, action +FROM metaschema_public.view_rule +WHERE FALSE; + +ROLLBACK; diff --git a/packages/metaschema-schema/verify/schemas/metaschema_public/tables/view_table/table.sql b/packages/metaschema-schema/verify/schemas/metaschema_public/tables/view_table/table.sql new file mode 100644 index 00000000..7270a4bf --- /dev/null +++ b/packages/metaschema-schema/verify/schemas/metaschema_public/tables/view_table/table.sql @@ -0,0 +1,9 @@ +-- Verify schemas/metaschema_public/tables/view_table/table on pg + +BEGIN; + +SELECT id, view_id, table_id, join_order +FROM metaschema_public.view_table +WHERE FALSE; + +ROLLBACK;