diff --git a/src/StreamMaster.Infrastructure.EF.PGSQL/Migrations/Repository/20250214180110_AddUserGroups_M3UGroups_APIKeys_Devices_to_PGSQLRepositoryContext_to_PGSQLRepositoryContext.cs b/src/StreamMaster.Infrastructure.EF.PGSQL/Migrations/Repository/20250214180110_AddUserGroups_M3UGroups_APIKeys_Devices_to_PGSQLRepositoryContext_to_PGSQLRepositoryContext.cs index 838b1fe8b..074199095 100644 --- a/src/StreamMaster.Infrastructure.EF.PGSQL/Migrations/Repository/20250214180110_AddUserGroups_M3UGroups_APIKeys_Devices_to_PGSQLRepositoryContext_to_PGSQLRepositoryContext.cs +++ b/src/StreamMaster.Infrastructure.EF.PGSQL/Migrations/Repository/20250214180110_AddUserGroups_M3UGroups_APIKeys_Devices_to_PGSQLRepositoryContext_to_PGSQLRepositoryContext.cs @@ -7,84 +7,99 @@ namespace StreamMaster.Infrastructure.EF.PGSQL.Migrations.Repository { - /// public partial class AddUserGroups_M3UGroups_APIKeys_Devices_to_PGSQLRepositoryContext_to_PGSQLRepositoryContext : Migration { - /// protected override void Up(MigrationBuilder migrationBuilder) { - migrationBuilder.CreateTable( - name: "APIKeys", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - Key = table.Column(type: "text", nullable: false), - UserId = table.Column(type: "text", nullable: false), - DeviceName = table.Column(type: "text", nullable: false), - Scopes = table.Column>(type: "text[]", nullable: false), - Expiration = table.Column(type: "timestamp with time zone", nullable: true), - CreatedAt = table.Column(type: "timestamp with time zone", nullable: false), - LastUsedAt = table.Column(type: "timestamp with time zone", nullable: true), - IsActive = table.Column(type: "boolean", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_APIKeys", x => x.Id); - }); + // Create APIKeys table if it doesn't exist + migrationBuilder.Sql(@" + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT FROM information_schema.tables + WHERE table_name = 'APIKeys' + ) THEN + CREATE TABLE ""APIKeys"" ( + ""Id"" uuid NOT NULL, + ""Key"" text NOT NULL, + ""UserId"" text NOT NULL, + ""DeviceName"" text NOT NULL, + ""Scopes"" text[] NOT NULL, + ""Expiration"" timestamp with time zone, + ""CreatedAt"" timestamp with time zone NOT NULL, + ""LastUsedAt"" timestamp with time zone, + ""IsActive"" boolean NOT NULL, + CONSTRAINT ""PK_APIKeys"" PRIMARY KEY (""Id"") + ); + END IF; + END $$; + "); - migrationBuilder.CreateTable( - name: "Devices", - columns: table => new - { - Id = table.Column(type: "uuid", nullable: false), - ApiKeyId = table.Column(type: "text", nullable: false), - UserId = table.Column(type: "text", nullable: false), - DeviceType = table.Column(type: "text", nullable: false), - DeviceId = table.Column(type: "text", nullable: false), - UserAgent = table.Column(type: "text", nullable: false), - IPAddress = table.Column(type: "text", nullable: false), - LastActivity = table.Column(type: "timestamp with time zone", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_Devices", x => x.Id); - }); + // Create Devices table if it doesn't exist + migrationBuilder.Sql(@" + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT FROM information_schema.tables + WHERE table_name = 'Devices' + ) THEN + CREATE TABLE ""Devices"" ( + ""Id"" uuid NOT NULL, + ""ApiKeyId"" text NOT NULL, + ""UserId"" text NOT NULL, + ""DeviceType"" text NOT NULL, + ""DeviceId"" text NOT NULL, + ""UserAgent"" text NOT NULL, + ""IPAddress"" text NOT NULL, + ""LastActivity"" timestamp with time zone NOT NULL, + CONSTRAINT ""PK_Devices"" PRIMARY KEY (""Id"") + ); + END IF; + END $$; + "); - migrationBuilder.CreateTable( - name: "M3UGroups", - columns: table => new - { - Id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityAlwaysColumn), - Name = table.Column(type: "citext", nullable: false), - IsIncluded = table.Column(type: "boolean", nullable: false), - TotalCount = table.Column(type: "integer", nullable: false), - IsUser = table.Column(type: "boolean", nullable: false), - IsPPV = table.Column(type: "boolean", nullable: false), - IsVOD = table.Column(type: "boolean", nullable: false), - M3UFileId = table.Column(type: "integer", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_M3UGroups", x => x.Id); - }); + // Create M3UGroups table if it doesn't exist + migrationBuilder.Sql(@" + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT FROM information_schema.tables + WHERE table_name = 'M3UGroups' + ) THEN + CREATE TABLE ""M3UGroups"" ( + ""Id"" integer NOT NULL GENERATED ALWAYS AS IDENTITY, + ""Name"" citext NOT NULL, + ""IsIncluded"" boolean NOT NULL, + ""TotalCount"" integer NOT NULL, + ""IsUser"" boolean NOT NULL, + ""IsPPV"" boolean NOT NULL, + ""IsVOD"" boolean NOT NULL, + ""M3UFileId"" integer NOT NULL, + CONSTRAINT ""PK_M3UGroups"" PRIMARY KEY (""Id"") + ); + END IF; + END $$; + "); - migrationBuilder.CreateTable( - name: "UserGroups", - columns: table => new - { - Id = table.Column(type: "integer", nullable: false) - .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityAlwaysColumn), - TotalCount = table.Column(type: "integer", nullable: false), - Name = table.Column(type: "citext", nullable: false) - }, - constraints: table => - { - table.PrimaryKey("PK_UserGroups", x => x.Id); - }); + // Create UserGroups table if it doesn't exist + migrationBuilder.Sql(@" + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT FROM information_schema.tables + WHERE table_name = 'UserGroups' + ) THEN + CREATE TABLE ""UserGroups"" ( + ""Id"" integer NOT NULL GENERATED ALWAYS AS IDENTITY, + ""TotalCount"" integer NOT NULL, + ""Name"" citext NOT NULL, + CONSTRAINT ""PK_UserGroups"" PRIMARY KEY (""Id"") + ); + END IF; + END $$; + "); } - /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropTable( @@ -100,4 +115,4 @@ protected override void Down(MigrationBuilder migrationBuilder) name: "UserGroups"); } } -} +} \ No newline at end of file diff --git a/src/StreamMaster.Infrastructure.EF.PGSQL/Migrations/Repository/20250217190832_M3UFileId.cs b/src/StreamMaster.Infrastructure.EF.PGSQL/Migrations/Repository/20250217190832_M3UFileId.cs index c4d9abe07..d13b5be9a 100644 --- a/src/StreamMaster.Infrastructure.EF.PGSQL/Migrations/Repository/20250217190832_M3UFileId.cs +++ b/src/StreamMaster.Infrastructure.EF.PGSQL/Migrations/Repository/20250217190832_M3UFileId.cs @@ -4,34 +4,60 @@ namespace StreamMaster.Infrastructure.EF.PGSQL.Migrations.Repository { - /// public partial class M3UFileId : Migration { - /// protected override void Up(MigrationBuilder migrationBuilder) { - migrationBuilder.DropPrimaryKey( - name: "PK_SMChannelStreamLinks", - table: "SMChannelStreamLinks"); + // First check if M3UFileId exists + migrationBuilder.Sql(@" + DO $$ + BEGIN + IF EXISTS ( + SELECT FROM information_schema.columns + WHERE table_name = 'SMChannels' + AND column_name = 'M3UFileId' + ) THEN + ALTER TABLE ""SMChannels"" DROP COLUMN ""M3UFileId""; + END IF; + END $$; + "); - migrationBuilder.DropColumn( - name: "M3UFileId", - table: "SMChannels"); + // Drop the existing primary key + migrationBuilder.Sql(@" + DO $$ + BEGIN + IF EXISTS ( + SELECT FROM information_schema.table_constraints + WHERE constraint_name = 'PK_SMChannelStreamLinks' + ) THEN + ALTER TABLE ""SMChannelStreamLinks"" DROP CONSTRAINT ""PK_SMChannelStreamLinks""; + END IF; + END $$; + "); - migrationBuilder.AddColumn( - name: "SMStreamM3UFileId", - table: "SMChannelStreamLinks", - type: "integer", - nullable: false, - defaultValue: 0); + // Add new column if it doesn't exist + migrationBuilder.Sql(@" + DO $$ + BEGIN + IF NOT EXISTS ( + SELECT FROM information_schema.columns + WHERE table_name = 'SMChannelStreamLinks' + AND column_name = 'SMStreamM3UFileId' + ) THEN + ALTER TABLE ""SMChannelStreamLinks"" + ADD COLUMN ""SMStreamM3UFileId"" integer NOT NULL DEFAULT 0; + END IF; + END $$; + "); - migrationBuilder.AddPrimaryKey( - name: "PK_SMChannelStreamLinks", - table: "SMChannelStreamLinks", - columns: new[] { "SMChannelId", "SMStreamId", "SMStreamM3UFileId" }); + // Add new primary key + migrationBuilder.Sql(@" + ALTER TABLE ""SMChannelStreamLinks"" + ADD CONSTRAINT ""PK_SMChannelStreamLinks"" + PRIMARY KEY (""SMChannelId"", ""SMStreamId"", ""SMStreamM3UFileId""); + "); } - /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropPrimaryKey( @@ -55,4 +81,4 @@ protected override void Down(MigrationBuilder migrationBuilder) columns: new[] { "SMChannelId", "SMStreamId" }); } } -} +} \ No newline at end of file