-- v4.13_fleet_vehicle_customer.sql
-- Link fleet vehicles to a customer.

ALTER TABLE fleet_vehicles
    ADD COLUMN IF NOT EXISTS customer_id INTEGER;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1 FROM pg_constraint
        WHERE conname = 'fleet_vehicles_customer_id_fkey'
    ) THEN
        ALTER TABLE fleet_vehicles
            ADD CONSTRAINT fleet_vehicles_customer_id_fkey
            FOREIGN KEY (customer_id) REFERENCES customers(id);
    END IF;
END $$;

CREATE INDEX IF NOT EXISTS idx_fleet_vehicles_customer_id
    ON fleet_vehicles (customer_id);
