DO $$
BEGIN
    IF EXISTS (
        SELECT 1
        FROM information_schema.columns
        WHERE table_schema = 'sonex'
          AND table_name = 'products'
          AND column_name = 'article_status'
    ) AND NOT EXISTS (
        SELECT 1
        FROM information_schema.columns
        WHERE table_schema = 'sonex'
          AND table_name = 'products'
          AND column_name = 'website_status'
    ) THEN
        EXECUTE 'ALTER TABLE sonex.products RENAME COLUMN article_status TO website_status';
    END IF;
END $$;

ALTER TABLE IF EXISTS sonex.products
    ADD COLUMN IF NOT EXISTS website_status text COLLATE pg_catalog."default" DEFAULT ''::text;

UPDATE sonex.products
SET website_status = ''
WHERE website_status IS NULL;

ALTER TABLE IF EXISTS sonex.products
    ALTER COLUMN website_status SET DEFAULT ''::text,
    ALTER COLUMN website_status SET NOT NULL;

CREATE INDEX IF NOT EXISTS ix_products_website_status
    ON sonex.products USING btree
    (website_status COLLATE pg_catalog."default" ASC NULLS LAST)
    TABLESPACE pg_default;
