Files
semprini-data-product-templ…/canonical_data_product/ods/020_tables.sql
T
paulandClaude Opus 5 62a3714562 Add the canonical data product template
Moves the data product template from semprini-blog-dataproducts, as adapted to
the semprini-data domain platform, into canonical_data_product/. Its worked
example passed 9 of 9 checks on the domain platform on 2026-09-17.

Adds agent-canonical-product, the blog's agent-product adapted to generate a
product into a domain repository from a declaration in the domain's model
repository, and README/CLAUDE.md describing the template catalogue, the
product contract with the domain platform, and team ownership. Source-aligned
and experience templates are planned.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-17 22:55:11 +12:00

70 lines
4.1 KiB
SQL

-- {{product.name}}: operational tables (PostgreSQL 17).
--
-- One block per entity in the product's `entities` list, parents before children, association
-- tables last. Names are snake_case of the MD-DDL names. Every table has:
-- <table>_id uuid the MD-DDL primary identifier; producers may supply it
-- created_at / updated_at platform columns, always last
-- Enum attributes are text with a CHECK listing the MD-DDL enum values verbatim.
-- One-to-many relationships become a foreign key on the owned side; many-to-many
-- relationships become association tables. Inheritance (`extends:`) is class-table: the
-- subtype's primary key is its foreign key to the parent, and an abstract parent carries a
-- <table>_type discriminator naming the subtype.
-- >>> repeat per core entity table (no `extends:`)
CREATE TABLE {{product.schema}}.{{table}} (
{{table}}_id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
{{table}}_type text NOT NULL CHECK ({{table}}_type IN ({{subtype_names}})), -- abstract parents only
{{column}} {{pg_type}}, -- {{attribute.name}}
{{enum_column}} text CHECK ({{enum_column}} IN ({{enum_values}})), -- enum:{{enum.name}}
{{parent_table}}_id uuid NOT NULL REFERENCES {{product.schema}}.{{parent_table}} ({{parent_table}}_id), -- one-to-many, owned side
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
COMMENT ON TABLE {{product.schema}}.{{table}} IS '{{entity.description}}';
COMMENT ON COLUMN {{product.schema}}.{{table}}.{{column}} IS '{{attribute.description}}';
CREATE INDEX {{table}}_{{parent_table}}_id_idx ON {{product.schema}}.{{table}} ({{parent_table}}_id);
CREATE TRIGGER {{table}}_set_updated_at
BEFORE UPDATE ON {{product.schema}}.{{table}}
FOR EACH ROW EXECUTE FUNCTION {{product.schema}}.set_updated_at();
-- <<< end repeat
-- >>> repeat per subtype table (`extends: {{parent}}`)
CREATE TABLE {{product.schema}}.{{child_table}} (
{{child_table}}_id uuid PRIMARY KEY REFERENCES {{product.schema}}.{{parent_table}} ({{parent_table}}_id) ON DELETE CASCADE,
{{column}} {{pg_type}}, -- {{attribute.name}}
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
COMMENT ON TABLE {{product.schema}}.{{child_table}} IS '{{entity.description}}';
CREATE TRIGGER {{child_table}}_set_updated_at
BEFORE UPDATE ON {{product.schema}}.{{child_table}}
FOR EACH ROW EXECUTE FUNCTION {{product.schema}}.set_updated_at();
-- <<< end repeat
-- >>> repeat per many-to-many relationship
-- Self-referential relationships are named <table>_association and use
-- source_<table>_id / target_<table>_id in place of the two foreign keys below.
CREATE TABLE {{product.schema}}.{{source_table}}_{{target_table}} (
{{source_table}}_{{target_table}}_id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
{{source_table}}_id uuid NOT NULL REFERENCES {{product.schema}}.{{source_table}} ({{source_table}}_id) ON DELETE CASCADE,
{{target_table}}_id uuid NOT NULL REFERENCES {{product.schema}}.{{target_table}} ({{target_table}}_id) ON DELETE CASCADE,
{{relationship_attribute}} {{pg_type}}, -- from relationship_attributes
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
COMMENT ON TABLE {{product.schema}}.{{source_table}}_{{target_table}} IS '{{relationship.description}}';
CREATE INDEX {{source_table}}_{{target_table}}_source_idx ON {{product.schema}}.{{source_table}}_{{target_table}} ({{source_table}}_id);
CREATE INDEX {{source_table}}_{{target_table}}_target_idx ON {{product.schema}}.{{source_table}}_{{target_table}} ({{target_table}}_id);
CREATE TRIGGER {{source_table}}_{{target_table}}_set_updated_at
BEFORE UPDATE ON {{product.schema}}.{{source_table}}_{{target_table}}
FOR EACH ROW EXECUTE FUNCTION {{product.schema}}.set_updated_at();
-- <<< end repeat