-- Canonical Party: analytical replication into DuckLake (runs on the lakehouse service).
--
-- pg_duckpipe follows the ODS write-ahead log and maintains a DuckLake copy of each table as
-- Parquet in the domain's `lake` bucket. The platform-level sync group `ods` already points
-- at the ODS (platform/compose.domain.yml); the product only registers its tables with it.
-- Trino reads the result as lake.canonical_party.
.
CREATE SCHEMA IF NOT EXISTS canonical_party;
SELECT duckpipe.add_table(
'canonical_party.party',
target_table => 'canonical_party.party',
sync_group => 'ods'
);
SELECT duckpipe.add_table(
'canonical_party.address',
target_table => 'canonical_party.address',
sync_group => 'ods'
);
SELECT duckpipe.add_table(
'canonical_party.person',
target_table => 'canonical_party.person',
sync_group => 'ods'
);
SELECT duckpipe.add_table(
'canonical_party.company',
target_table => 'canonical_party.company',
sync_group => 'ods'
);
SELECT duckpipe.add_table(
'canonical_party.party_role',
target_table => 'canonical_party.party_role',
sync_group => 'ods'
);
SELECT duckpipe.add_table(
'canonical_party.customer',
target_table => 'canonical_party.customer',
sync_group => 'ods'
);
SELECT duckpipe.add_table(
'canonical_party.contact_address',
target_table => 'canonical_party.contact_address',
sync_group => 'ods'
);
SELECT duckpipe.add_table(
'canonical_party.party_role_contact_address',
target_table => 'canonical_party.party_role_contact_address',
sync_group => 'ods'
);
SELECT duckpipe.add_table(
'canonical_party.party_association',
target_table => 'canonical_party.party_association',
sync_group => 'ods'
);
-- Readers (Trino) and writers (dbt, building experience products on top) need the tables.
-- pg_ducklake does not yet enforce SELECT on DuckLake tables, so these grants document intent
-- as much as they control it.
GRANT USAGE ON SCHEMA canonical_party TO ducklake_reader, ducklake_writer;
GRANT SELECT ON ALL TABLES IN SCHEMA canonical_party TO ducklake_reader, ducklake_writer;