Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
4 changes: 4 additions & 0 deletions packages/metaschema-schema/pgpm.plan
Original file line number Diff line number Diff line change
Expand Up @@ -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 <skitch@5b0c196eeb62> # 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 <skitch@5b0c196eeb62> # 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 <skitch@5b0c196eeb62> # 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 <devin@cognition.ai> # 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 <devin@cognition.ai> # 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 <devin@cognition.ai> # 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 <devin@cognition.ai> # add schemas/metaschema_public/tables/view_rule/table
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert schemas/metaschema_public/tables/view/table from pg

BEGIN;

DROP TABLE IF EXISTS metaschema_public.view;

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert schemas/metaschema_public/tables/view_grant/table from pg

BEGIN;

DROP TABLE IF EXISTS metaschema_public.view_grant;

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert schemas/metaschema_public/tables/view_rule/table from pg

BEGIN;

DROP TABLE IF EXISTS metaschema_public.view_rule;

COMMIT;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Revert schemas/metaschema_public/tables/view_table/table from pg

BEGIN;

DROP TABLE IF EXISTS metaschema_public.view_table;

COMMIT;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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;