From 5b77980cd74fd0b5a5e3b416c29f7f4ab92b6824 Mon Sep 17 00:00:00 2001 From: SW Date: Thu, 1 Oct 2026 14:20:40 +0200 Subject: [PATCH] fix: allow re-allocation of released subnets by removing global cidr unique constraint --- .../20241001140000_allow_released_cidr.sql | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 migrations/20241001140000_allow_released_cidr.sql diff --git a/migrations/20241001140000_allow_released_cidr.sql b/migrations/20241001140000_allow_released_cidr.sql new file mode 100644 index 0000000..ce65dcc --- /dev/null +++ b/migrations/20241001140000_allow_released_cidr.sql @@ -0,0 +1,34 @@ +-- Remove the global UNIQUE constraint on allocations.cidr so that a released +-- subnet can be re-allocated later. Enforce uniqueness only among active and +-- reserved allocations within the same pool. + +CREATE TABLE IF NOT EXISTS allocations_new ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + pool_id INTEGER NOT NULL REFERENCES pools(id), + cidr TEXT NOT NULL, + prefix_length INTEGER NOT NULL, + address_count INTEGER NOT NULL, + usable_host_count INTEGER NOT NULL, + note TEXT NOT NULL, + status TEXT NOT NULL CHECK(status IN ('active', 'reserved', 'released')), + created_at TEXT NOT NULL, + created_by TEXT NOT NULL DEFAULT 'local', + released_at TEXT NULL, + released_by TEXT NULL, + release_note TEXT NULL +); + +INSERT INTO allocations_new SELECT * FROM allocations; + +DROP TABLE allocations; + +ALTER TABLE allocations_new RENAME TO allocations; + +CREATE INDEX IF NOT EXISTS idx_allocations_pool_id ON allocations(pool_id); +CREATE INDEX IF NOT EXISTS idx_allocations_status ON allocations(status); +CREATE INDEX IF NOT EXISTS idx_allocations_pool_status ON allocations(pool_id, status); +CREATE INDEX IF NOT EXISTS idx_allocations_created_at ON allocations(created_at DESC); + +CREATE UNIQUE INDEX IF NOT EXISTS idx_allocations_active_unique + ON allocations(pool_id, cidr) + WHERE status IN ('active', 'reserved');