Skip to content

Commit

Permalink
fix: Attempt to conditionally apply migration
Browse files Browse the repository at this point in the history
When coming from a "future" version of SM, these columns would already exist.
  • Loading branch information
carlreid committed Feb 19, 2025
1 parent 0853a38 commit 8c8a8ff
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,84 +7,99 @@

namespace StreamMaster.Infrastructure.EF.PGSQL.Migrations.Repository
{
/// <inheritdoc />
public partial class AddUserGroups_M3UGroups_APIKeys_Devices_to_PGSQLRepositoryContext_to_PGSQLRepositoryContext : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "APIKeys",
columns: table => new
{
Id = table.Column<Guid>(type: "uuid", nullable: false),
Key = table.Column<string>(type: "text", nullable: false),
UserId = table.Column<string>(type: "text", nullable: false),
DeviceName = table.Column<string>(type: "text", nullable: false),
Scopes = table.Column<List<string>>(type: "text[]", nullable: false),
Expiration = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false),
LastUsedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: true),
IsActive = table.Column<bool>(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<Guid>(type: "uuid", nullable: false),
ApiKeyId = table.Column<string>(type: "text", nullable: false),
UserId = table.Column<string>(type: "text", nullable: false),
DeviceType = table.Column<string>(type: "text", nullable: false),
DeviceId = table.Column<string>(type: "text", nullable: false),
UserAgent = table.Column<string>(type: "text", nullable: false),
IPAddress = table.Column<string>(type: "text", nullable: false),
LastActivity = table.Column<DateTime>(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<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityAlwaysColumn),
Name = table.Column<string>(type: "citext", nullable: false),
IsIncluded = table.Column<bool>(type: "boolean", nullable: false),
TotalCount = table.Column<int>(type: "integer", nullable: false),
IsUser = table.Column<bool>(type: "boolean", nullable: false),
IsPPV = table.Column<bool>(type: "boolean", nullable: false),
IsVOD = table.Column<bool>(type: "boolean", nullable: false),
M3UFileId = table.Column<int>(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<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityAlwaysColumn),
TotalCount = table.Column<int>(type: "integer", nullable: false),
Name = table.Column<string>(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 $$;
");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
Expand All @@ -100,4 +115,4 @@ protected override void Down(MigrationBuilder migrationBuilder)
name: "UserGroups");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,60 @@

namespace StreamMaster.Infrastructure.EF.PGSQL.Migrations.Repository
{
/// <inheritdoc />
public partial class M3UFileId : Migration
{
/// <inheritdoc />
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<int>(
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"");
");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
Expand All @@ -55,4 +81,4 @@ protected override void Down(MigrationBuilder migrationBuilder)
columns: new[] { "SMChannelId", "SMStreamId" });
}
}
}
}

0 comments on commit 8c8a8ff

Please sign in to comment.