-- M11.4b, ADR-0119: allow one leased, no-tick profile-4 v1-to-v2 link. -- Ordinary publication continues to preserve its exact version bundle. ALTER FUNCTION evolution.parse_checkpoint_manifest(bytea) RENAME TO parse_checkpoint_manifest_v25; CREATE FUNCTION evolution.manifest_u64le(n numeric) RETURNS bytea LANGUAGE plpgsql IMMUTABLE STRICT SET search_path = pg_catalog, pg_temp AS $$ DECLARE result bytea := decode(repeat('00', 8), 'hex'); BEGIN IF n < 0 OR n > 18446744073709551615 OR trunc(n) <> n THEN RAISE EXCEPTION 'u64 out of range' USING ERRCODE = '22023'; END IF; FOR i IN 0 .. 7 LOOP result := set_byte(result, i, mod(n, 256)::integer); n := trunc(n / 256); END LOOP; RETURN result; END $$; REVOKE ALL ON FUNCTION evolution.manifest_u64le(numeric) FROM PUBLIC; CREATE FUNCTION evolution.parse_checkpoint_manifest(m bytea) RETURNS evolution.checkpoint_manifest LANGUAGE plpgsql IMMUTABLE SET search_path = pg_catalog, pg_temp AS $$ DECLARE r evolution.checkpoint_manifest; normalized bytea; zero32 constant bytea := decode(repeat('00', 32), 'hex'); v1_bundle constant bytea := '\x010000000100000001000000010000000100000002000000010000000100000000000000'; v2_bundle constant bytea := '\x020000000200000001000000010000000100000002000000010000000100000001000000'; begin_tick numeric; end_tick numeric; BEGIN IF m IS NOT NULL AND octet_length(m) = 360 AND evolution.manifest_uint(m, 12, 4) = 4 AND substring(m FROM 161 FOR 36) = v2_bundle AND evolution.manifest_uint(m, 56, 8) > 0 AND evolution.manifest_uint(m, 112, 8) = evolution.manifest_uint(m, 120, 8) AND evolution.manifest_uint(m, 112, 8) > 0 AND evolution.manifest_uint(m, 280, 8) = 0 AND substring(m FROM 289 FOR 32) = zero32 AND evolution.manifest_uint(m, 320, 4) = 0 AND substring(m FROM 329 FOR 32) = zero32 THEN begin_tick := evolution.manifest_uint(m, 112, 8); end_tick := evolution.manifest_uint(m, 120, 8); normalized := overlay(m PLACING v1_bundle FROM 161 FOR 36); -- The v25 parser admits only advancing trace commits. Validate the same -- immutable fields through it using a synthetic one-tick trace shape, then -- restore the canonical no-tick evidence in the returned record. normalized := overlay(normalized PLACING evolution.manifest_u64le(begin_tick - 1) FROM 113 FOR 8); normalized := overlay(normalized PLACING evolution.manifest_u64le(120) FROM 281 FOR 8); normalized := overlay(normalized PLACING decode(repeat('01', 32), 'hex') FROM 289 FOR 32); r := evolution.parse_checkpoint_manifest_v25(normalized); r.versions := v2_bundle; r.begin_tick := begin_tick; r.end_tick := end_tick; r.trace_bytes := 0; r.trace_sha256 := zero32; r.decision_count := 0; r.decisions_sha256 := zero32; RETURN r; END IF; RETURN evolution.parse_checkpoint_manifest_v25(m); END $$; REVOKE ALL ON FUNCTION evolution.parse_checkpoint_manifest(bytea) FROM PUBLIC; CREATE TABLE evolution.cellular_profile_migrations ( target_commit uuid PRIMARY KEY REFERENCES evolution.region_commits(commit_id) DEFERRABLE INITIALLY DEFERRED, world_id evolution.u64 NOT NULL, region_id evolution.u64 NOT NULL, source_sequence evolution.u64 NOT NULL, source_commit uuid NOT NULL, source_manifest_sha256 bytea NOT NULL CHECK (octet_length(source_manifest_sha256) = 32), source_payload_sha256 bytea NOT NULL CHECK (octet_length(source_payload_sha256) = 32), source_versions bytea NOT NULL CHECK (octet_length(source_versions) = 36), target_sequence evolution.u64 NOT NULL, target_manifest_sha256 bytea NOT NULL CHECK (octet_length(target_manifest_sha256) = 32), target_payload_sha256 bytea NOT NULL CHECK (octet_length(target_payload_sha256) = 32), target_versions bytea NOT NULL CHECK (octet_length(target_versions) = 36), CHECK (target_sequence = source_sequence + 1), FOREIGN KEY (world_id, region_id) REFERENCES evolution.region_leases, CHECK (source_commit <> target_commit) ); REVOKE ALL ON evolution.cellular_profile_migrations FROM PUBLIC; CREATE TRIGGER cellular_profile_migrations_immutable BEFORE UPDATE OR DELETE ON evolution.cellular_profile_migrations FOR EACH ROW EXECUTE FUNCTION evolution.refuse_history_rewrite(); CREATE TRIGGER cellular_profile_migrations_no_truncate BEFORE TRUNCATE ON evolution.cellular_profile_migrations FOR EACH STATEMENT EXECUTE FUNCTION evolution.refuse_history_rewrite(); CREATE OR REPLACE FUNCTION evolution.refuse_unpublished_cellular() RETURNS trigger LANGUAGE plpgsql SECURITY INVOKER SET search_path = pg_catalog, pg_temp AS $$ BEGIN IF evolution.manifest_uint(NEW.manifest, 12, 4) = 4 AND NEW.commit_sequence > 0 AND NOT EXISTS (SELECT 1 FROM evolution.cellular_publications p WHERE p.commit_id = NEW.commit_id) AND NOT EXISTS (SELECT 1 FROM evolution.cellular_profile_migrations m WHERE m.target_commit = NEW.commit_id) THEN RAISE EXCEPTION 'cellular commits require their fenced publisher' USING ERRCODE = 'EVL06'; END IF; RETURN NEW; END $$; CREATE FUNCTION evolution.publish_cellular_profile_migration(p_manifest bytea) RETURNS TABLE(region_frame evolution.u64, head_sequence evolution.u64, head_commit uuid, head_digest text, head_tick evolution.u64) LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ DECLARE c evolution.checkpoint_manifest := evolution.parse_checkpoint_manifest(p_manifest); s evolution.checkpoint_manifest; h evolution.region_heads%ROWTYPE; p evolution.region_commits%ROWTYPE; old evolution.region_commits%ROWTYPE; frame evolution.u64; d bytea := sha256(p_manifest); v1_bundle constant bytea := '\x010000000100000001000000010000000100000002000000010000000100000000000000'; v2_bundle constant bytea := '\x020000000200000001000000010000000100000002000000010000000100000001000000'; BEGIN IF c.profile <> 4 OR c.commit_sequence = 0 OR c.begin_tick <> c.end_tick OR c.end_tick = 0 OR c.versions <> v2_bundle OR evolution.manifest_uint(p_manifest, 228, 4) <> 0 OR evolution.manifest_uint(p_manifest, 232, 4) <> 3 OR evolution.manifest_uint(p_manifest, 236, 4) <> 1 OR c.trace_bytes <> 0 OR c.trace_sha256 <> decode(repeat('00', 32), 'hex') OR c.decision_count <> 0 OR c.decisions_sha256 <> decode(repeat('00', 32), 'hex') THEN RAISE EXCEPTION 'invalid cellular profile migration' USING ERRCODE = '22023'; END IF; PERFORM evolution.check_region_lease(c.world_id, c.region_id, c.holder, c.epoch); SELECT * INTO old FROM evolution.region_commits r WHERE r.commit_id = c.commit_id; IF FOUND THEN IF old.manifest <> p_manifest THEN RAISE EXCEPTION 'cellular profile migration identity reused' USING ERRCODE = 'EVL07'; END IF; SELECT * INTO p FROM evolution.region_commits r WHERE r.world_id = old.world_id AND r.region_id = old.region_id AND r.commit_sequence = old.commit_sequence - 1; IF NOT FOUND OR NOT EXISTS ( SELECT 1 FROM evolution.cellular_profile_migrations m WHERE m.target_commit = c.commit_id AND m.world_id = c.world_id AND m.region_id = c.region_id AND m.source_sequence = p.commit_sequence AND m.source_commit = p.commit_id AND m.source_manifest_sha256 = p.commit_digest AND m.source_payload_sha256 = p.payload_sha256 AND m.source_versions = p.versions AND m.target_sequence = old.commit_sequence AND m.target_manifest_sha256 = d AND m.target_payload_sha256 = old.payload_sha256 AND m.target_versions = old.versions AND m.source_commit = c.predecessor_commit AND m.source_manifest_sha256 = c.predecessor_digest) THEN RAISE EXCEPTION 'cellular profile migration identity reused' USING ERRCODE = 'EVL07'; END IF; SELECT l.frame_id INTO frame FROM evolution.region_leases l WHERE l.world_id = c.world_id AND l.region_id = c.region_id; RETURN QUERY SELECT frame, old.commit_sequence, old.commit_id, encode(old.commit_digest, 'hex'), old.end_tick; RETURN; END IF; SELECT * INTO h FROM evolution.region_heads r WHERE r.world_id = c.world_id AND r.region_id = c.region_id FOR UPDATE; IF NOT FOUND OR h.commit_sequence = 18446744073709551615 OR c.commit_sequence <> h.commit_sequence + 1 OR c.predecessor_commit <> h.commit_id OR c.predecessor_digest <> h.digest OR c.begin_tick <> h.end_tick THEN RAISE EXCEPTION 'region head moved' USING ERRCODE = 'EVL03'; END IF; SELECT * INTO p FROM evolution.region_commits r WHERE r.world_id = c.world_id AND r.region_id = c.region_id AND r.commit_sequence = h.commit_sequence; IF NOT FOUND THEN RAISE EXCEPTION 'missing migration predecessor' USING ERRCODE = 'XX000'; END IF; s := evolution.parse_checkpoint_manifest(p.manifest); IF s.profile <> 4 OR s.versions <> v1_bundle OR p.versions <> v1_bundle OR c.frame_id <> s.frame_id OR c.tick_microseconds <> s.tick_microseconds OR s.commit_id <> c.predecessor_commit OR s.end_tick <> c.end_tick OR evolution.manifest_uint(p.manifest, 232, 4) <> 3 OR NOT EXISTS (SELECT 1 FROM evolution.retained_builds b WHERE b.build_identity = c.build_identity) THEN RAISE EXCEPTION 'unsupported cellular profile migration' USING ERRCODE = 'EVL06'; END IF; SELECT l.frame_id INTO frame FROM evolution.region_leases l WHERE l.world_id = c.world_id AND l.region_id = c.region_id; IF c.frame_id <> frame THEN RAISE EXCEPTION 'foreign frame' USING ERRCODE = '22023'; END IF; INSERT INTO evolution.cellular_profile_migrations(target_commit, world_id, region_id, source_sequence, source_commit, source_manifest_sha256, source_payload_sha256, source_versions, target_sequence, target_manifest_sha256, target_payload_sha256, target_versions) VALUES (c.commit_id, c.world_id, c.region_id, p.commit_sequence, p.commit_id, p.commit_digest, p.payload_sha256, p.versions, c.commit_sequence, d, c.payload_sha256, c.versions); INSERT INTO evolution.region_commits(world_id, region_id, commit_sequence, commit_id, commit_digest, manifest, begin_tick, end_tick, published_epoch, tick_microseconds, versions, build_identity, payload_bytes, payload_sha256) VALUES (c.world_id, c.region_id, c.commit_sequence, c.commit_id, d, p_manifest, c.begin_tick, c.end_tick, c.epoch, c.tick_microseconds, c.versions, c.build_identity, c.payload_bytes, c.payload_sha256); UPDATE evolution.region_heads r SET commit_sequence = c.commit_sequence, commit_id = c.commit_id, digest = d, end_tick = c.end_tick, published_epoch = c.epoch WHERE r.world_id = c.world_id AND r.region_id = c.region_id; RETURN QUERY SELECT frame, c.commit_sequence, c.commit_id, encode(d, 'hex'), c.end_tick; END $$; REVOKE ALL ON FUNCTION evolution.publish_cellular_profile_migration(bytea) FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.publish_cellular_profile_migration(bytea) TO :"runtime_role"; CREATE FUNCTION evolution.load_cellular_profile_migration(p_world evolution.u64, p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_commit uuid) RETURNS TABLE(source_sequence evolution.u64, source_commit uuid, source_manifest_sha256 bytea, source_payload_sha256 bytea, source_versions bytea, target_sequence evolution.u64, target_commit uuid, target_manifest_sha256 bytea, target_payload_sha256 bytea, target_versions bytea) LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ BEGIN PERFORM evolution.check_region_lease(p_world, p_region, p_holder, p_epoch); RETURN QUERY SELECT m.source_sequence, m.source_commit, m.source_manifest_sha256, m.source_payload_sha256, m.source_versions, m.target_sequence, m.target_commit, m.target_manifest_sha256, m.target_payload_sha256, m.target_versions FROM evolution.cellular_profile_migrations m WHERE m.world_id = p_world AND m.region_id = p_region AND m.target_commit = p_commit; END $$; REVOKE ALL ON FUNCTION evolution.load_cellular_profile_migration( evolution.u64,evolution.u64,uuid,evolution.u64,uuid) FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.load_cellular_profile_migration( evolution.u64,evolution.u64,uuid,evolution.u64,uuid) TO :"runtime_role"; INSERT INTO evolution.schema_migrations(version, sha256) VALUES (26, :'migration_sha_26');