-- Create new tables
CREATE TABLE "component_set_component_version_excluded" (
    "component_set_web_path" VARCHAR(128) NOT NULL,
    "component_name" VARCHAR(128) NOT NULL,
    "component_version" VARCHAR(128) NOT NULL,

    CONSTRAINT "component_set_component_version_excluded_pkey" PRIMARY KEY ("component_set_web_path", "component_name", "component_version")
);

CREATE TABLE "component_set_component_version_editable" (
    "component_set_web_path" VARCHAR(128) NOT NULL,
    "component_name" VARCHAR(128) NOT NULL,
    "component_version" VARCHAR(128) NOT NULL,

    CONSTRAINT "component_set_component_version_editable_pkey" PRIMARY KEY ("component_set_web_path", "component_name", "component_version")
);

-- Add foreign keys
ALTER TABLE "component_set_component_version_excluded" ADD CONSTRAINT "component_set_web_path_fkey" FOREIGN KEY ("component_set_web_path") REFERENCES "component_set"("web_path") ON DELETE CASCADE ON UPDATE CASCADE;
ALTER TABLE "component_set_component_version_editable" ADD CONSTRAINT "component_set_web_path_fkey" FOREIGN KEY ("component_set_web_path") REFERENCES "component_set"("web_path") ON DELETE CASCADE ON UPDATE CASCADE;


-- Add enum column to component_set_component_version table
CREATE TYPE "component_set_component_version_status" AS ENUM('active', 'excluded');
ALTER TABLE "component_set_component_version" ADD COLUMN "status" "component_set_component_version_status" NOT NULL DEFAULT 'active';

-- Migrate the existing "is_visible" data to the new "status" column
UPDATE "component_set_component_version" 
SET "status" = (CASE 
    WHEN "is_visible" = 'true' 
      THEN 'active'::component_set_component_version_status
    WHEN "is_visible" = 'false'
      THEN 'excluded'::component_set_component_version_status
    END
);
    
-- Delete now un-needed "is_visible" column
ALTER TABLE "component_set_component_version" DROP COLUMN "is_visible";