-- Read-only active handoff evidence, not another ownership store (ADR-0156). CREATE FUNCTION evolution.load_world_cellular_migrations_v1(w evolution.u64) RETURNS TABLE(trace bytea,source_manifest bytea,destination_manifest bytea, ghost_sha256 bytea,ghost_bytes evolution.u64,coherent boolean) LANGUAGE plpgsql STABLE SECURITY DEFINER SET search_path=pg_catalog,pg_temp AS $$ BEGIN IF w IS NULL OR w=0 THEN RAISE EXCEPTION 'nonzero world required' USING ERRCODE='22023'; END IF; RETURN QUERY WITH ids AS ( SELECT x.request_id FROM evolution.cellular_migration_pending x WHERE x.world_id=w UNION SELECT x.request_id FROM evolution.cellular_migration_destination_pending x WHERE x.world_id=w ) SELECT p.trace,s.manifest,d.manifest,g.ghost_sha256,g.ghost_bytes, coalesce(p.world_id=w AND s.world_id=w AND d.world_id=w AND s.region_id=p.source_region AND d.region_id=p.destination_region AND substring(p.trace FROM 49 FOR 16)=pg_catalog.uuid_send(p.request_id) AND EXISTS(SELECT 1 FROM evolution.cellular_migration_pending x WHERE x.world_id=w AND x.region_id=p.source_region AND x.request_id=p.request_id) AND (CASE WHEN g.request_id IS NULL THEN NOT EXISTS(SELECT 1 FROM evolution.cellular_migration_destination_pending x WHERE x.request_id=p.request_id) ELSE EXISTS(SELECT 1 FROM evolution.cellular_migration_destination_pending x WHERE x.world_id=w AND x.region_id=p.destination_region AND x.request_id=p.request_id) END) AND NOT EXISTS(SELECT 1 FROM evolution.cellular_migration_aborts x WHERE x.request_id=p.request_id) AND NOT EXISTS(SELECT 1 FROM evolution.cellular_migration_commits x WHERE x.request_id=p.request_id),false) FROM ids LEFT JOIN evolution.cellular_migration_preparations p USING(request_id) LEFT JOIN evolution.region_commits s ON s.commit_id=p.source_commit LEFT JOIN evolution.region_commits d ON d.commit_id=p.destination_commit LEFT JOIN evolution.cellular_migration_stages g USING(request_id) ORDER BY ids.request_id LIMIT 65; END $$; REVOKE ALL ON FUNCTION evolution.load_world_cellular_migrations_v1(evolution.u64) FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.load_world_cellular_migrations_v1(evolution.u64) TO :"runtime_role"; INSERT INTO evolution.schema_migrations(version,sha256) VALUES(52,:'migration_sha_52');