setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo->exec(<<<'SQL' CREATE TABLE claims ( id INTEGER PRIMARY KEY AUTOINCREMENT, reservation_id INTEGER NOT NULL, address_family INTEGER NOT NULL, claim_generation INTEGER NOT NULL, active_claim INTEGER DEFAULT 1, operation_id TEXT NOT NULL, state TEXT NOT NULL, released_at INTEGER DEFAULT NULL, release_reason TEXT NOT NULL DEFAULT '', UNIQUE (reservation_id, address_family, active_claim), UNIQUE (reservation_id, claim_generation, address_family), CHECK (address_family IN (4,6)), CHECK (claim_generation >= 1), CHECK ( (state = 'released' AND active_claim IS NULL) OR ( state = 'committed' AND active_claim IS NOT NULL AND active_claim = 1 ) OR ( state IN ('reserved','uncertain') AND (active_claim IS NULL OR active_claim = 1) ) ), CHECK ( (state = 'released' AND released_at IS NOT NULL AND release_reason <> '') OR (state <> 'released' AND released_at IS NULL AND release_reason = '') ) ); CREATE TABLE addresses ( id INTEGER PRIMARY KEY AUTOINCREMENT, state TEXT NOT NULL, reservation_id INTEGER DEFAULT NULL, reservation_ip_claim_id INTEGER DEFAULT NULL, instance_id INTEGER DEFAULT NULL, operation_id TEXT DEFAULT NULL, CHECK ( state IN ('available','disabled','reserved','assigned','changing','uncertain') ), CHECK ( ( state IN ('available','disabled') AND reservation_id IS NULL AND reservation_ip_claim_id IS NULL AND instance_id IS NULL AND operation_id IS NULL ) OR ( state = 'reserved' AND reservation_id IS NOT NULL AND reservation_ip_claim_id IS NOT NULL AND instance_id IS NULL AND operation_id IS NOT NULL ) OR ( state IN ('changing','uncertain') AND reservation_id IS NOT NULL AND reservation_ip_claim_id IS NOT NULL AND operation_id IS NOT NULL ) OR ( state = 'assigned' AND reservation_id IS NOT NULL AND reservation_ip_claim_id IS NOT NULL AND instance_id IS NOT NULL AND operation_id IS NOT NULL ) ) ); SQL); $assert = static function (bool $condition, string $message): void { if (!$condition) { throw new RuntimeException($message); } }; $rejects = static function (callable $callback, string $message): void { try { $callback(); } catch (PDOException) { return; } throw new RuntimeException($message); }; $claim = $pdo->prepare( 'INSERT INTO claims ' . '(reservation_id,address_family,claim_generation,active_claim,' . 'operation_id,state,released_at,release_reason) ' . 'VALUES (?,?,?,?,?,?,?,?)' ); $claim->execute([10, 4, 1, 1, str_repeat('a', 32), 'committed', null, '']); $claim->execute([10, 6, 3, 1, str_repeat('b', 32), 'committed', null, '']); $assert((int) $pdo->query( 'SELECT COUNT(*) FROM claims WHERE reservation_id=10 AND active_claim=1' )->fetchColumn() === 2, 'Different per-family generation and operation identities were rejected.'); $rejects(static fn () => $claim->execute([ 10, 4, 2, 1, str_repeat('c', 32), 'reserved', null, '', ]), 'A second active claim for one family was accepted.'); $claim->execute([ 10, 4, 2, null, str_repeat('c', 32), 'reserved', null, '', ]); $claim->execute([ 11, 6, 1, null, str_repeat('e', 32), 'uncertain', null, '', ]); $claim->execute([ 10, 4, 3, null, str_repeat('d', 32), 'released', 100, 'replaced', ]); $rejects(static fn () => $claim->execute([ 12, 4, 1, null, str_repeat('f', 32), 'committed', null, '', ]), 'A committed claim was persisted without the active authority fence.'); $rejects(static fn () => $claim->execute([ 13, 4, 1, 1, str_repeat('0', 32), 'released', 100, 'released', ]), 'A released claim retained the active fence.'); $rejects(static fn () => $claim->execute([ 14, 4, 1, null, str_repeat('1', 32), 'released', null, '', ]), 'A released claim omitted terminal evidence through SQL NULL semantics.'); $address = $pdo->prepare( 'INSERT INTO addresses ' . '(state,reservation_id,reservation_ip_claim_id,instance_id,operation_id) ' . 'VALUES (?,?,?,?,?)' ); $address->execute(['available', null, null, null, null]); $address->execute(['reserved', 10, 1, null, str_repeat('a', 32)]); $address->execute(['changing', 10, 3, 88, str_repeat('c', 32)]); $address->execute(['uncertain', 10, 3, null, str_repeat('c', 32)]); $address->execute(['assigned', 10, 1, 88, str_repeat('a', 32)]); $rejects(static fn () => $address->execute([ 'available', null, 1, null, null, ]), 'An available address retained a claim fence.'); $rejects(static fn () => $address->execute([ 'reserved', 10, null, null, str_repeat('a', 32), ]), 'A reserved address omitted its exact claim.'); $rejects(static fn () => $address->execute([ 'changing', null, null, 88, str_repeat('c', 32), ]), 'A legacy scalar-only changing target bypassed the claim ledger.'); $rejects(static fn () => $address->execute([ 'uncertain', null, null, 88, str_repeat('c', 32), ]), 'A legacy scalar-only uncertain target bypassed the claim ledger.'); $rejects(static fn () => $address->execute([ 'assigned', 10, 1, null, str_repeat('a', 32), ]), 'An assigned address omitted its instance owner.'); echo "Active public-network claim schema behavior tests passed.\n";