package store import ( "context" "database/sql" "errors" "pageswiki/internal/db" ) // User-space tables are deliberately not adopted/skipped dynamically here. // Their future port must append migrations installing their generation guards. var publicationNavigationTables = []string{ "locality_auto_publication", "locality_publication_policies", "locality_access_policies", "locality_access_entries", "city_owner_bindings", "principals", "domain_memberships", "group_members", "groups", "group_permissions", "auth_roles", "auth_role_bindings", "auth_capability_grants", "campuses", "paths", "revisions", "domains", "config", "zones", "spots", "street_spots", "spot_pages", "spot_relations", "street_inbox_pages", "city_page_overrides", "domain_topology_versions", "city_governance_epochs", "locality_publication_entries", "locality_publication_manifests", } func (s *Store) ensurePostgreSQLPublication(ctx context.Context) error { if err := s.ensurePostgreSQLLocalityFoundation(ctx); err != nil { return err } statements := []string{ `CREATE TABLE navigation_generation(id INTEGER PRIMARY KEY CHECK(id=1),version BIGINT NOT NULL)`, `INSERT INTO navigation_generation VALUES(1,1)`, `CREATE FUNCTION pages_navigation_changed() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN UPDATE navigation_generation SET version=version+1 WHERE id=1; RETURN NULL; END $$`, `CREATE FUNCTION pages_publication_config_guard() RETURNS trigger LANGUAGE plpgsql AS $$ DECLARE d text; check_mode boolean:=false; BEGIN PERFORM pg_advisory_xact_lock(1885431653,1853058931); IF TG_OP='INSERT' THEN d:=NEW.domain_uuid; check_mode:=NEW.key='authorization_mode' AND NEW.value!='enforcing'; ELSE d:=OLD.domain_uuid; IF OLD.key='authorization_mode' THEN IF TG_OP='DELETE' THEN check_mode:=true; ELSE check_mode:=NEW.key!=OLD.key OR NEW.domain_uuid!=OLD.domain_uuid OR NEW.value!='enforcing'; END IF; END IF; END IF; IF check_mode AND (EXISTS(SELECT 1 FROM locality_publication_manifests WHERE domain_uuid=d) OR EXISTS(SELECT 1 FROM locality_auto_publication WHERE domain_uuid=d AND mode='automatic')) THEN RAISE EXCEPTION USING ERRCODE='23514', MESSAGE='publication_requires_enforcing'; END IF; IF TG_OP='DELETE' THEN RETURN OLD; END IF; RETURN NEW; END $$`, `CREATE TRIGGER publication_config_guard BEFORE INSERT OR UPDATE OR DELETE ON config FOR EACH ROW EXECUTE FUNCTION pages_publication_config_guard()`, `CREATE FUNCTION pages_publication_revision_changed() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF TG_OP='INSERT' THEN DELETE FROM locality_publication_entries WHERE object_uuid=NEW.object_uuid AND lang=COALESCE(NULLIF(NEW.lang,''),'und'); ELSE DELETE FROM locality_publication_entries WHERE revision_uuid=OLD.uuid; END IF; RETURN NULL; END $$`, `CREATE TRIGGER publication_revision_changed AFTER INSERT OR UPDATE OR DELETE ON revisions FOR EACH ROW EXECUTE FUNCTION pages_publication_revision_changed()`, `CREATE FUNCTION pages_publication_path_changed() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF TG_OP!='INSERT' THEN DELETE FROM locality_publication_entries WHERE object_uuid=OLD.object_uuid; END IF; IF TG_OP!='DELETE' THEN DELETE FROM locality_publication_entries WHERE object_uuid=NEW.object_uuid; END IF; RETURN NULL; END $$`, `CREATE TRIGGER publication_path_changed AFTER INSERT OR UPDATE OR DELETE ON paths FOR EACH ROW EXECUTE FUNCTION pages_publication_path_changed()`, `CREATE FUNCTION pages_publication_city_changed() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF TG_OP!='INSERT' THEN DELETE FROM locality_publication_manifests WHERE domain_uuid=OLD.domain_uuid; DELETE FROM locality_publication_entries WHERE domain_uuid=OLD.domain_uuid; END IF; IF TG_OP!='DELETE' THEN DELETE FROM locality_publication_manifests WHERE domain_uuid=NEW.domain_uuid; DELETE FROM locality_publication_entries WHERE domain_uuid=NEW.domain_uuid; END IF; RETURN NULL; END $$`, `CREATE TRIGGER publication_zones_changed AFTER INSERT OR UPDATE OR DELETE ON zones FOR EACH ROW EXECUTE FUNCTION pages_publication_city_changed()`, `CREATE TRIGGER publication_spots_changed AFTER INSERT OR UPDATE OR DELETE ON spots FOR EACH ROW EXECUTE FUNCTION pages_publication_city_changed()`, `CREATE FUNCTION pages_publication_street_spots_changed() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF TG_OP!='INSERT' THEN DELETE FROM locality_publication_manifests WHERE domain_uuid=(SELECT domain_uuid FROM spots WHERE uuid=OLD.spot_uuid); DELETE FROM locality_publication_entries WHERE domain_uuid=(SELECT domain_uuid FROM spots WHERE uuid=OLD.spot_uuid); END IF; IF TG_OP!='DELETE' THEN DELETE FROM locality_publication_manifests WHERE domain_uuid=(SELECT domain_uuid FROM spots WHERE uuid=NEW.spot_uuid); DELETE FROM locality_publication_entries WHERE domain_uuid=(SELECT domain_uuid FROM spots WHERE uuid=NEW.spot_uuid); END IF; RETURN NULL; END $$`, `CREATE TRIGGER publication_street_spots_changed AFTER INSERT OR UPDATE OR DELETE ON street_spots FOR EACH ROW EXECUTE FUNCTION pages_publication_street_spots_changed()`, } assertions := []string{`SELECT id,version FROM navigation_generation WHERE 1=0`, `SELECT 1 WHERE NOT EXISTS(SELECT 1 FROM navigation_generation WHERE id=1 AND version>0)`} for _, target := range publicationGuardTargets() { assertions = append(assertions, `SELECT 1 WHERE NOT EXISTS(SELECT 1 FROM pg_trigger WHERE tgrelid='`+target.table+`'::regclass AND tgname='`+target.name+`' AND tgenabled='O' AND NOT tgisinternal)`) } for _, table := range publicationNavigationTables { // Statement triggers bound overhead for bulk writes; generations are // invalidation tokens, not counts of changed rows. statements = append(statements, `CREATE TRIGGER navigation_`+table+` AFTER INSERT OR UPDATE OR DELETE ON `+table+` FOR EACH STATEMENT EXECUTE FUNCTION pages_navigation_changed()`) } return db.ApplyPostgreSQLMigrations(ctx, s.DB, "store-publication", []db.PostgreSQLMigration{{Version: 1, Name: "publication fencing and navigation generation", Statements: statements, Assertions: assertions}}) } type publicationGuardTarget struct{ table, name string } func publicationGuardTargets() []publicationGuardTarget { list := []publicationGuardTarget{{"config", "publication_config_guard"}, {"paths", "publication_path_changed"}, {"revisions", "publication_revision_changed"}, {"zones", "publication_zones_changed"}, {"spots", "publication_spots_changed"}, {"street_spots", "publication_street_spots_changed"}} for _, table := range publicationNavigationTables { list = append(list, publicationGuardTarget{table, "navigation_" + table}) } return list } func (s *Store) installPublicationGuards(ctx context.Context, tx *sql.Tx) error { if s.sqlBackend == storeSQLite { return installPublicationGuards(ctx, tx) } for _, target := range publicationGuardTargets() { var enabled bool if err := tx.QueryRowContext(ctx, `SELECT EXISTS(SELECT 1 FROM pg_trigger WHERE tgrelid=to_regclass($1) AND tgname=$2 AND tgenabled='O' AND NOT tgisinternal)`, target.table, target.name).Scan(&enabled); err != nil { return err } if !enabled { return errors.New("publication protection unavailable") } } return nil }