ALTER TABLE discount_codes
  ADD COLUMN IF NOT EXISTS applicable_on_sales BOOLEAN NOT NULL DEFAULT FALSE;

UPDATE discount_codes
   SET applicable_on_sales = TRUE
 WHERE type != '%';

CREATE OR REPLACE FUNCTION discount_code_applicable_on_sales()
  RETURNS TRIGGER AS $$
    BEGIN
        IF NEW.type != '%' THEN
            NEW.applicable_on_sales := TRUE;
        END IF;
        RETURN NEW;
    END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS discount_code_applicable_on_sales ON discount_codes;

CREATE TRIGGER discount_code_applicable_on_sales
  BEFORE INSERT OR UPDATE ON discount_codes
  FOR EACH ROW
  EXECUTE FUNCTION discount_code_applicable_on_sales();
