setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $database->exec(<<<'SQL' CREATE TABLE batch_items ( id INTEGER PRIMARY KEY AUTOINCREMENT, batch_id INTEGER NOT NULL, instance_id INTEGER NOT NULL, address_family INTEGER NOT NULL, source_public_ip_id INTEGER NOT NULL, target_public_ip_id INTEGER NOT NULL, child_job_id INTEGER DEFAULT NULL, state TEXT NOT NULL, UNIQUE (batch_id, instance_id), UNIQUE (child_job_id), UNIQUE (batch_id, target_public_ip_id), CHECK (source_public_ip_id <> target_public_ip_id), CHECK (address_family IN (4,6)), CHECK ( (state = 'preparing' AND child_job_id IS NULL) OR (state IN ('queued','succeeded','failed','unknown') AND child_job_id IS NOT NULL) ) ) SQL); $insert = $database->prepare( 'INSERT INTO batch_items ' . '(batch_id, instance_id, address_family, source_public_ip_id, ' . 'target_public_ip_id, child_job_id, state) ' . 'VALUES (?, ?, ?, ?, ?, ?, ?)' ); $rejects = static function (callable $callback, string $message): void { try { $callback(); } catch (PDOException) { return; } throw new RuntimeException($message); }; $insert->execute([1, 10, 4, 100, 101, null, 'preparing']); $insert->execute([1, 11, 6, 200, 201, 900, 'queued']); $insert->execute([2, 12, 4, 300, 301, null, 'preparing']); $rejects( static fn () => $insert->execute([1, 13, 4, 400, 401, 901, 'preparing']), 'Preparing batch item accepted an already-linked child.' ); $rejects( static fn () => $insert->execute([1, 14, 6, 500, 501, null, 'queued']), 'Queued batch item accepted a missing child.' ); $rejects( static fn () => $insert->execute([1, 15, 5, 600, 601, null, 'preparing']), 'Batch item accepted an unsupported address family.' ); $rejects( static fn () => $insert->execute([1, 16, 4, 700, 700, null, 'preparing']), 'Batch item accepted the same source and target address.' ); $rejects( static fn () => $insert->execute([2, 17, 4, 800, 801, 900, 'failed']), 'Batch item reused one child job across batches.' ); echo "Cloud public-IP pool replacement item schema behavior passed.\n";