Skip to content

Commit 107e16c

Browse files
committed
feat: add view tables to metaschema-schema
Add view, view_grant, view_rule, and view_table tables to support the views system infrastructure. These tables enable: - view: Main view definition with schema, name, view_type, and data - view_table: Junction table linking views to their referenced tables - view_grant: View-level permissions - view_rule: View rules for triggers/actions Upstream from constructive-db PR #421.
1 parent 24510af commit 107e16c

13 files changed

Lines changed: 226 additions & 0 deletions

File tree

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
-- Deploy schemas/metaschema_public/tables/view/table to pg
2+
3+
-- requires: schemas/metaschema_public/schema
4+
-- requires: schemas/metaschema_public/tables/schema/table
5+
-- requires: schemas/metaschema_public/tables/table/table
6+
-- requires: schemas/metaschema_public/tables/database/table
7+
-- requires: schemas/metaschema_public/types/object_category
8+
9+
BEGIN;
10+
11+
CREATE TABLE metaschema_public.view (
12+
id uuid PRIMARY KEY DEFAULT uuid_generate_v4 (),
13+
database_id uuid NOT NULL DEFAULT uuid_nil(),
14+
15+
schema_id uuid NOT NULL,
16+
name text NOT NULL,
17+
18+
-- Primary/source table for the view (nullable for ViewComposite)
19+
-- For ViewTableProjection, ViewFilteredTable, ViewAggregated: the source table
20+
-- For ViewJoinedTables: the primary (left-most) table
21+
-- For ViewComposite: NULL (no table reference)
22+
table_id uuid,
23+
24+
-- View query definition using View* node types
25+
view_type text NOT NULL,
26+
data jsonb DEFAULT '{}',
27+
28+
-- Optional filter using Authz* node types (baked into view WHERE clause)
29+
filter_type text,
30+
filter_data jsonb DEFAULT '{}',
31+
32+
-- View options
33+
security_invoker boolean DEFAULT true,
34+
is_read_only boolean DEFAULT true,
35+
36+
smart_tags jsonb,
37+
38+
category metaschema_public.object_category NOT NULL DEFAULT 'app',
39+
module text NULL,
40+
scope int NULL,
41+
42+
tags citext[] NOT NULL DEFAULT '{}',
43+
44+
CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE,
45+
CONSTRAINT schema_fkey FOREIGN KEY (schema_id) REFERENCES metaschema_public.schema (id) ON DELETE CASCADE,
46+
CONSTRAINT table_fkey FOREIGN KEY (table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
47+
48+
UNIQUE (schema_id, name)
49+
);
50+
51+
COMMENT ON CONSTRAINT schema_fkey ON metaschema_public.view IS E'@omit manyToMany';
52+
COMMENT ON CONSTRAINT db_fkey ON metaschema_public.view IS E'@omit manyToMany';
53+
COMMENT ON CONSTRAINT table_fkey ON metaschema_public.view IS E'@omit manyToMany';
54+
55+
CREATE INDEX view_schema_id_idx ON metaschema_public.view ( schema_id );
56+
CREATE INDEX view_database_id_idx ON metaschema_public.view ( database_id );
57+
CREATE INDEX view_table_id_idx ON metaschema_public.view ( table_id );
58+
59+
COMMIT;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
-- Deploy schemas/metaschema_public/tables/view_grant/table to pg
2+
3+
-- requires: schemas/metaschema_public/schema
4+
-- requires: schemas/metaschema_public/tables/view/table
5+
-- requires: schemas/metaschema_public/tables/database/table
6+
7+
BEGIN;
8+
9+
CREATE TABLE metaschema_public.view_grant (
10+
id uuid PRIMARY KEY DEFAULT uuid_generate_v4 (),
11+
database_id uuid NOT NULL DEFAULT uuid_nil(),
12+
13+
view_id uuid NOT NULL,
14+
role_name text NOT NULL,
15+
privilege text NOT NULL,
16+
17+
with_grant_option boolean DEFAULT false,
18+
19+
CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE,
20+
CONSTRAINT view_fkey FOREIGN KEY (view_id) REFERENCES metaschema_public.view (id) ON DELETE CASCADE,
21+
22+
UNIQUE (view_id, role_name, privilege)
23+
);
24+
25+
COMMENT ON CONSTRAINT view_fkey ON metaschema_public.view_grant IS E'@omit manyToMany';
26+
COMMENT ON CONSTRAINT db_fkey ON metaschema_public.view_grant IS E'@omit manyToMany';
27+
28+
CREATE INDEX view_grant_view_id_idx ON metaschema_public.view_grant ( view_id );
29+
CREATE INDEX view_grant_database_id_idx ON metaschema_public.view_grant ( database_id );
30+
31+
COMMIT;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
-- Deploy schemas/metaschema_public/tables/view_rule/table to pg
2+
3+
-- requires: schemas/metaschema_public/schema
4+
-- requires: schemas/metaschema_public/tables/view/table
5+
-- requires: schemas/metaschema_public/tables/database/table
6+
7+
BEGIN;
8+
9+
CREATE TABLE metaschema_public.view_rule (
10+
id uuid PRIMARY KEY DEFAULT uuid_generate_v4 (),
11+
database_id uuid NOT NULL DEFAULT uuid_nil(),
12+
13+
view_id uuid NOT NULL,
14+
name text NOT NULL,
15+
event text NOT NULL,
16+
action text NOT NULL DEFAULT 'NOTHING',
17+
18+
CONSTRAINT db_fkey FOREIGN KEY (database_id) REFERENCES metaschema_public.database (id) ON DELETE CASCADE,
19+
CONSTRAINT view_fkey FOREIGN KEY (view_id) REFERENCES metaschema_public.view (id) ON DELETE CASCADE,
20+
21+
UNIQUE (view_id, name)
22+
);
23+
24+
COMMENT ON TABLE metaschema_public.view_rule IS 'DO INSTEAD rules for views (e.g., read-only enforcement)';
25+
COMMENT ON COLUMN metaschema_public.view_rule.event IS 'INSERT, UPDATE, or DELETE';
26+
COMMENT ON COLUMN metaschema_public.view_rule.action IS 'NOTHING (for read-only) or custom action';
27+
28+
COMMENT ON CONSTRAINT view_fkey ON metaschema_public.view_rule IS E'@omit manyToMany';
29+
COMMENT ON CONSTRAINT db_fkey ON metaschema_public.view_rule IS E'@omit manyToMany';
30+
31+
CREATE INDEX view_rule_view_id_idx ON metaschema_public.view_rule ( view_id );
32+
CREATE INDEX view_rule_database_id_idx ON metaschema_public.view_rule ( database_id );
33+
34+
COMMIT;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
-- Deploy schemas/metaschema_public/tables/view_table/table to pg
2+
3+
-- requires: schemas/metaschema_public/schema
4+
-- requires: schemas/metaschema_public/tables/view/table
5+
-- requires: schemas/metaschema_public/tables/table/table
6+
7+
BEGIN;
8+
9+
-- Junction table linking views to their joined tables (for ViewJoinedTables)
10+
-- This provides referential integrity for views that reference multiple tables.
11+
-- The primary table is stored in view.table_id; this table stores additional joined tables.
12+
CREATE TABLE metaschema_public.view_table (
13+
id uuid PRIMARY KEY DEFAULT uuid_generate_v4 (),
14+
view_id uuid NOT NULL,
15+
table_id uuid NOT NULL,
16+
17+
-- Order of joins (0 = first join, 1 = second join, etc.)
18+
join_order int NOT NULL DEFAULT 0,
19+
20+
CONSTRAINT view_fkey FOREIGN KEY (view_id) REFERENCES metaschema_public.view (id) ON DELETE CASCADE,
21+
CONSTRAINT table_fkey FOREIGN KEY (table_id) REFERENCES metaschema_public.table (id) ON DELETE CASCADE,
22+
23+
UNIQUE (view_id, table_id)
24+
);
25+
26+
COMMENT ON TABLE metaschema_public.view_table IS 'Junction table linking views to their joined tables for referential integrity';
27+
COMMENT ON CONSTRAINT view_fkey ON metaschema_public.view_table IS E'@omit manyToMany';
28+
COMMENT ON CONSTRAINT table_fkey ON metaschema_public.view_table IS E'@omit manyToMany';
29+
30+
CREATE INDEX view_table_view_id_idx ON metaschema_public.view_table ( view_id );
31+
CREATE INDEX view_table_table_id_idx ON metaschema_public.view_table ( table_id );
32+
33+
COMMIT;

packages/metaschema-schema/pgpm.plan

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ schemas/metaschema_public/tables/table/indexes/databases_table_unique_name_idx [
2828
schemas/metaschema_public/tables/trigger_function/table [schemas/metaschema_public/schema schemas/metaschema_public/tables/database/table] 2017-08-11T08:11:51Z skitch <skitch@5b0c196eeb62> # add schemas/metaschema_public/tables/trigger_function/table
2929
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 <skitch@5b0c196eeb62> # add schemas/metaschema_public/tables/trigger/table
3030
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 <skitch@5b0c196eeb62> # add schemas/metaschema_public/tables/unique_constraint/table
31+
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 <devin@cognition.ai> # add schemas/metaschema_public/tables/view/table
32+
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 <devin@cognition.ai> # add schemas/metaschema_public/tables/view_table/table
33+
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 <devin@cognition.ai> # add schemas/metaschema_public/tables/view_grant/table
34+
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 <devin@cognition.ai> # add schemas/metaschema_public/tables/view_rule/table
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Revert schemas/metaschema_public/tables/view/table from pg
2+
3+
BEGIN;
4+
5+
DROP TABLE IF EXISTS metaschema_public.view;
6+
7+
COMMIT;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Revert schemas/metaschema_public/tables/view_grant/table from pg
2+
3+
BEGIN;
4+
5+
DROP TABLE IF EXISTS metaschema_public.view_grant;
6+
7+
COMMIT;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Revert schemas/metaschema_public/tables/view_rule/table from pg
2+
3+
BEGIN;
4+
5+
DROP TABLE IF EXISTS metaschema_public.view_rule;
6+
7+
COMMIT;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Revert schemas/metaschema_public/tables/view_table/table from pg
2+
3+
BEGIN;
4+
5+
DROP TABLE IF EXISTS metaschema_public.view_table;
6+
7+
COMMIT;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Verify schemas/metaschema_public/tables/view/table on pg
2+
3+
BEGIN;
4+
5+
SELECT id, database_id, schema_id, name, view_type, data, filter_type, filter_data,
6+
security_invoker, is_read_only, smart_tags, category, module, scope, tags
7+
FROM metaschema_public.view
8+
WHERE FALSE;
9+
10+
ROLLBACK;

0 commit comments

Comments
 (0)