﻿CREATE TABLE IF NOT EXISTS "__EFMigrationsHistory" (
    "MigrationId" character varying(150) NOT NULL,
    "ProductVersion" character varying(32) NOT NULL,
    CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId")
);

START TRANSACTION;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201120312_InitialCreate') THEN
    CREATE TABLE "ServiceInstances" (
        "Id" bigint GENERATED BY DEFAULT AS IDENTITY,
        "ClusterId" character varying(100) NOT NULL,
        "DestinationId" character varying(100) NOT NULL,
        "Address" character varying(200) NOT NULL,
        "Health" integer NOT NULL,
        "Weight" integer NOT NULL,
        "Status" integer NOT NULL,
        "CreatedBy" bigint,
        "CreatedTime" timestamp with time zone NOT NULL,
        "UpdatedBy" bigint,
        "UpdatedTime" timestamp with time zone,
        "IsDeleted" boolean NOT NULL,
        "Version" integer NOT NULL,
        CONSTRAINT "PK_ServiceInstances" PRIMARY KEY ("Id")
    );
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201120312_InitialCreate') THEN
    CREATE TABLE "Tenants" (
        "Id" bigint GENERATED BY DEFAULT AS IDENTITY,
        "TenantCode" character varying(50) NOT NULL,
        "TenantName" character varying(100) NOT NULL,
        "Status" integer NOT NULL,
        "CreatedBy" bigint,
        "CreatedTime" timestamp with time zone NOT NULL,
        "UpdatedBy" bigint,
        "UpdatedTime" timestamp with time zone,
        "IsDeleted" boolean NOT NULL,
        "Version" integer NOT NULL,
        CONSTRAINT "PK_Tenants" PRIMARY KEY ("Id"),
        CONSTRAINT "AK_Tenants_TenantCode" UNIQUE ("TenantCode")
    );
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201120312_InitialCreate') THEN
    CREATE TABLE "TenantRoutes" (
        "Id" bigint GENERATED BY DEFAULT AS IDENTITY,
        "TenantCode" character varying(50) NOT NULL,
        "ServiceName" character varying(100) NOT NULL,
        "ClusterId" character varying(100) NOT NULL,
        "PathPattern" character varying(200) NOT NULL,
        "Priority" integer NOT NULL,
        "Status" integer NOT NULL,
        "CreatedBy" bigint,
        "CreatedTime" timestamp with time zone NOT NULL,
        "UpdatedBy" bigint,
        "UpdatedTime" timestamp with time zone,
        "IsDeleted" boolean NOT NULL,
        "Version" integer NOT NULL,
        CONSTRAINT "PK_TenantRoutes" PRIMARY KEY ("Id"),
        CONSTRAINT "FK_TenantRoutes_Tenants_TenantCode" FOREIGN KEY ("TenantCode") REFERENCES "Tenants" ("TenantCode") ON DELETE RESTRICT
    );
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201120312_InitialCreate') THEN
    CREATE UNIQUE INDEX "IX_ServiceInstances_ClusterId_DestinationId" ON "ServiceInstances" ("ClusterId", "DestinationId");
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201120312_InitialCreate') THEN
    CREATE INDEX "IX_ServiceInstances_Health" ON "ServiceInstances" ("Health");
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201120312_InitialCreate') THEN
    CREATE INDEX "IX_TenantRoutes_ClusterId" ON "TenantRoutes" ("ClusterId");
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201120312_InitialCreate') THEN
    CREATE UNIQUE INDEX "IX_TenantRoutes_TenantCode_ServiceName" ON "TenantRoutes" ("TenantCode", "ServiceName");
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201120312_InitialCreate') THEN
    CREATE UNIQUE INDEX "IX_Tenants_TenantCode" ON "Tenants" ("TenantCode");
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201120312_InitialCreate') THEN
    INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
    VALUES ('20260201120312_InitialCreate', '9.0.0');
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201133826_AddIsGlobalToTenantRoute') THEN
    ALTER TABLE "TenantRoutes" DROP CONSTRAINT "FK_TenantRoutes_Tenants_TenantCode";
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201133826_AddIsGlobalToTenantRoute') THEN
    ALTER TABLE "Tenants" DROP CONSTRAINT "AK_Tenants_TenantCode";
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201133826_AddIsGlobalToTenantRoute') THEN
    DROP INDEX "IX_TenantRoutes_TenantCode_ServiceName";
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201133826_AddIsGlobalToTenantRoute') THEN
    ALTER TABLE "TenantRoutes" ADD "IsGlobal" boolean NOT NULL DEFAULT FALSE;
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201133826_AddIsGlobalToTenantRoute') THEN
    CREATE INDEX "IX_TenantRoutes_ServiceName" ON "TenantRoutes" ("ServiceName");
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201133826_AddIsGlobalToTenantRoute') THEN
    CREATE INDEX "IX_TenantRoutes_ServiceName_IsGlobal_Status" ON "TenantRoutes" ("ServiceName", "IsGlobal", "Status");
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201133826_AddIsGlobalToTenantRoute') THEN
    CREATE INDEX "IX_TenantRoutes_TenantCode" ON "TenantRoutes" ("TenantCode");
    END IF;
END $EF$;

DO $EF$
BEGIN
    IF NOT EXISTS(SELECT 1 FROM "__EFMigrationsHistory" WHERE "MigrationId" = '20260201133826_AddIsGlobalToTenantRoute') THEN
    INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
    VALUES ('20260201133826_AddIsGlobalToTenantRoute', '9.0.0');
    END IF;
END $EF$;
COMMIT;

