-- M11.2d contract closure, ADR-0100. Existing durable request rows become -- queryable outcomes for conflicts, and reservations cannot remain pending at -- an old owner after account control moves. ALTER TABLE evolution.life_reservations DROP CONSTRAINT life_reservations_rejection_check; ALTER TABLE evolution.life_reservations ADD CONSTRAINT life_reservations_rejection_check CHECK (rejection BETWEEN 0 AND 4); -- 4 is an immutable refusal: another life is already reserved or published. CREATE FUNCTION evolution.cancel_moved_primitive_reservations() RETURNS trigger LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ BEGIN IF OLD.region_id IS NOT NULL AND (OLD.region_id IS DISTINCT FROM NEW.region_id OR OLD.region_epoch IS DISTINCT FROM NEW.region_epoch) THEN UPDATE evolution.life_reservations r SET rejection = 3 WHERE r.world_id = OLD.world_id AND r.account_id = OLD.account_id AND r.region_id = OLD.region_id AND r.rejection = 0 AND NOT EXISTS (SELECT 1 FROM evolution.primitive_life_bindings b WHERE b.world_id = r.world_id AND b.account_id = r.account_id AND b.request_id = r.request_id); END IF; RETURN NEW; END $$; CREATE TRIGGER cancel_moved_primitive_reservations AFTER UPDATE OF region_id, region_epoch ON evolution.account_controls FOR EACH ROW EXECUTE FUNCTION evolution.cancel_moved_primitive_reservations(); -- Exact retry is checked before conflict detection. A conflicting request gets -- its own durable refusal row and allocated identity (a permitted gap), so its -- result survives reconnect and remains distinct from the older request. CREATE OR REPLACE FUNCTION evolution.reserve_primitive_life(p_world evolution.u64, p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_account uuid, p_generation evolution.u64, p_request evolution.u64, p_rules integer) RETURNS TABLE (life_identity evolution.u64, life_region evolution.u64, rejection smallint) LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ DECLARE r evolution.life_reservations%ROWTYPE; allocated numeric; BEGIN IF p_request IS NULL OR p_request = 0 OR p_rules IS DISTINCT FROM 1 THEN RAISE EXCEPTION 'invalid life request' USING ERRCODE = '22023'; END IF; PERFORM evolution.check_life_control(p_world, p_region, p_holder, p_epoch, p_account, p_generation); SELECT * INTO r FROM evolution.life_reservations x WHERE x.world_id = p_world AND x.account_id = p_account AND x.request_id = p_request; IF FOUND THEN RETURN QUERY SELECT r.identity, r.region_id, r.rejection; RETURN; END IF; SELECT a.next_identity INTO allocated FROM evolution.life_allocators a WHERE a.world_id = p_world FOR UPDATE; IF NOT FOUND THEN RAISE EXCEPTION 'life allocator not provisioned' USING ERRCODE = 'EVL04'; END IF; IF allocated = 18446744073709551615 THEN RAISE EXCEPTION 'life identities exhausted' USING ERRCODE = 'EVL05'; END IF; PERFORM evolution.check_life_control(p_world, p_region, p_holder, p_epoch, p_account, p_generation); IF EXISTS (SELECT 1 FROM evolution.life_reservations x WHERE x.world_id = p_world AND x.account_id = p_account AND x.rejection = 0) THEN UPDATE evolution.life_allocators SET next_identity = allocated + 1 WHERE world_id = p_world; INSERT INTO evolution.life_reservations(world_id, account_id, request_id, identity, region_id, rules, rejection) VALUES (p_world, p_account, p_request, allocated, p_region, p_rules, 4); RETURN QUERY SELECT allocated::evolution.u64, p_region, 4::smallint; RETURN; END IF; UPDATE evolution.life_allocators SET next_identity = allocated + 1 WHERE world_id = p_world; INSERT INTO evolution.life_reservations(world_id, account_id, request_id, identity, region_id, rules) VALUES (p_world, p_account, p_request, allocated, p_region, p_rules); RETURN QUERY SELECT allocated::evolution.u64, p_region, 0::smallint; END $$; -- The caller must hold current account control in a live region session. The -- row is world/account/request scoped, not limited to the reservation's region. CREATE FUNCTION evolution.query_primitive_life(p_world evolution.u64, p_region evolution.u64, p_holder uuid, p_epoch evolution.u64, p_account uuid, p_generation evolution.u64, p_request evolution.u64) RETURNS TABLE (life_identity evolution.u64, life_region evolution.u64, rejection smallint, born evolution.u64, commit_id uuid, commit_sequence evolution.u64) LANGUAGE plpgsql SECURITY DEFINER SET search_path = pg_catalog, pg_temp AS $$ BEGIN IF p_request IS NULL OR p_request = 0 THEN RAISE EXCEPTION 'invalid life request' USING ERRCODE = '22023'; END IF; PERFORM evolution.check_life_control(p_world, p_region, p_holder, p_epoch, p_account, p_generation); RETURN QUERY SELECT r.identity, r.region_id, r.rejection, b.born, b.commit_id, c.commit_sequence FROM evolution.life_reservations r LEFT JOIN evolution.primitive_life_bindings b USING(world_id,account_id,request_id) LEFT JOIN evolution.region_commits c ON c.commit_id=b.commit_id WHERE r.world_id=p_world AND r.account_id=p_account AND r.request_id=p_request; END $$; REVOKE ALL ON FUNCTION evolution.cancel_moved_primitive_reservations(), evolution.query_primitive_life(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,evolution.u64) FROM PUBLIC; GRANT EXECUTE ON FUNCTION evolution.query_primitive_life(evolution.u64,evolution.u64,uuid,evolution.u64,uuid,evolution.u64,evolution.u64) TO :"runtime_role"; INSERT INTO evolution.schema_migrations(version,sha256) VALUES (17, :'migration_sha_17');