-- Enforce uniqueness for managed units and protect concurrent inserts.
CREATE UNIQUE INDEX IF NOT EXISTS ux_managed_units_unit_number
  ON managed_units ((NULLIF(TRIM(unit_number), '')))
  WHERE NULLIF(TRIM(unit_number), '') IS NOT NULL;

CREATE UNIQUE INDEX IF NOT EXISTS ux_managed_units_description_ci
  ON managed_units (LOWER(TRIM(description)));
