﻿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;
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")
);

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")
);

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
);

CREATE UNIQUE INDEX "IX_ServiceInstances_ClusterId_DestinationId" ON "ServiceInstances" ("ClusterId", "DestinationId");

CREATE INDEX "IX_ServiceInstances_Health" ON "ServiceInstances" ("Health");

CREATE INDEX "IX_TenantRoutes_ClusterId" ON "TenantRoutes" ("ClusterId");

CREATE UNIQUE INDEX "IX_TenantRoutes_TenantCode_ServiceName" ON "TenantRoutes" ("TenantCode", "ServiceName");

CREATE UNIQUE INDEX "IX_Tenants_TenantCode" ON "Tenants" ("TenantCode");

INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20260201120312_InitialCreate', '10.0.2');

COMMIT;

START TRANSACTION;
ALTER TABLE "TenantRoutes" DROP CONSTRAINT "FK_TenantRoutes_Tenants_TenantCode";

ALTER TABLE "Tenants" DROP CONSTRAINT "AK_Tenants_TenantCode";

DROP INDEX "IX_TenantRoutes_TenantCode_ServiceName";

ALTER TABLE "TenantRoutes" ADD "IsGlobal" boolean NOT NULL DEFAULT FALSE;

CREATE INDEX "IX_TenantRoutes_ServiceName" ON "TenantRoutes" ("ServiceName");

CREATE INDEX "IX_TenantRoutes_ServiceName_IsGlobal_Status" ON "TenantRoutes" ("ServiceName", "IsGlobal", "Status");

CREATE INDEX "IX_TenantRoutes_TenantCode" ON "TenantRoutes" ("TenantCode");

INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20260201133826_AddIsGlobalToTenantRoute', '10.0.2');

COMMIT;

START TRANSACTION;
CREATE TABLE "PendingServiceDiscoveries" (
    "Id" bigint GENERATED BY DEFAULT AS IDENTITY,
    "K8sServiceName" character varying(255) NOT NULL,
    "K8sNamespace" character varying(255) NOT NULL,
    "K8sClusterIP" character varying(50),
    "DiscoveredPorts" character varying(500) NOT NULL,
    "Labels" character varying(2000) NOT NULL,
    "PodCount" integer NOT NULL,
    "Status" integer NOT NULL,
    "AssignedClusterId" character varying(100),
    "AssignedBy" character varying(100),
    "AssignedAt" timestamp with time zone,
    "DiscoveredAt" timestamp with time zone NOT NULL,
    "IsDeleted" boolean NOT NULL,
    "Version" integer NOT NULL,
    CONSTRAINT "PK_PendingServiceDiscoveries" PRIMARY KEY ("Id")
);

CREATE INDEX "IX_PendingServiceDiscoveries_DiscoveredAt" ON "PendingServiceDiscoveries" ("DiscoveredAt");

CREATE UNIQUE INDEX "IX_PendingServiceDiscoveries_K8sServiceName_K8sNamespace_IsDel~" ON "PendingServiceDiscoveries" ("K8sServiceName", "K8sNamespace", "IsDeleted");

CREATE INDEX "IX_PendingServiceDiscoveries_Status" ON "PendingServiceDiscoveries" ("Status");

INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20260222134342_AddPendingServiceDiscovery', '10.0.2');

COMMIT;

