-- +goose Up
-- +goose StatementBegin

-- Multi-member support for bridge/bond. `parent` (single TEXT) was
-- enough for VLANs but bridges and bonds aggregate N kernel ifaces.
-- We store the member-list as a JSONB array of iface-names so the
-- runtime renderer can emit the correct systemd-networkd .network
-- files (Bridge=br0 on each member etc.).
ALTER TABLE network_interfaces
    ADD COLUMN IF NOT EXISTS members JSONB NOT NULL DEFAULT '[]'::jsonb;

-- Bridge and bond MUST have ≥ 1 member; vlan/wireguard/ethernet
-- ignore the field. NOT VALID skips the back-fill check on the
-- existing rows — they may have been declared in an earlier release
-- before the field existed and we don't want to break the upgrade.
-- Inserts and UPDATEs from now on are validated normally.
ALTER TABLE network_interfaces
    DROP CONSTRAINT IF EXISTS network_interfaces_members_check;
ALTER TABLE network_interfaces
    ADD  CONSTRAINT network_interfaces_members_check CHECK (
        type NOT IN ('bridge', 'bond')
        OR jsonb_array_length(members) >= 1
    ) NOT VALID;

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
ALTER TABLE network_interfaces DROP CONSTRAINT IF EXISTS network_interfaces_members_check;
ALTER TABLE network_interfaces DROP COLUMN IF EXISTS members;
-- +goose StatementEnd
