-- Location names are meaningful within a customer and hierarchy, not globally.
-- Example: every building may have an "1 Sal".

BEGIN;

ALTER TABLE locations_locations
    DROP CONSTRAINT IF EXISTS locations_locations_name_key;

CREATE UNIQUE INDEX IF NOT EXISTS idx_locations_name_scope_unique
    ON locations_locations (
        COALESCE(parent_location_id, 0),
        COALESCE(customer_id, 0),
        lower(name)
    )
    WHERE deleted_at IS NULL;

COMMIT;
