Files
MilkyShots/Lactose/Migrations/20260714154509_AuthRedesign_VisibilityAccessLevel.cs
REDCODE 1f2bf02d02 fix(migration): default Visibility to Protected instead of Public, preserve old IsPubliclyShared mapping
- Changed column default from 0 (Public) to 1 (Protected) for Assets, People, Albums
- Reordered migration to add Visibility column before dropping IsPubliclyShared
- Assets that were IsPubliclyShared = true are restored to Public (0) via SQL
- Private assets and all existing people/albums default to Protected (1)
2026-07-14 20:18:13 +02:00

230 lines
8.2 KiB
C#

using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Lactose.Migrations
{
/// <inheritdoc />
public partial class AuthRedesign_VisibilityAccessLevel : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Albums_Users_UserOwnerId",
table: "Albums");
migrationBuilder.DropForeignKey(
name: "FK_Assets_Users_OwnerId",
table: "Assets");
// Remap old EAccessLevel values: old Admin (2) → new Admin (3) first, then old Curator (1) → new Curator (2)
migrationBuilder.Sql("UPDATE \"Users\" SET \"AccessLevel\" = 3 WHERE \"AccessLevel\" = 2");
migrationBuilder.Sql("UPDATE \"Users\" SET \"AccessLevel\" = 2 WHERE \"AccessLevel\" = 1");
migrationBuilder.DropTable(
name: "AssetUser");
migrationBuilder.DropIndex(
name: "IX_Assets_VisibleForSearch",
table: "Assets");
migrationBuilder.DropIndex(
name: "IX_Albums_UserOwnerId",
table: "Albums");
// Add Visibility columns with default Protected (1) before dropping IsPubliclyShared,
// so we can map old values via SQL
migrationBuilder.AddColumn<int>(
name: "Visibility",
table: "People",
type: "integer",
nullable: false,
defaultValue: 1);
migrationBuilder.AddColumn<int>(
name: "Visibility",
table: "Assets",
type: "integer",
nullable: false,
defaultValue: 1);
migrationBuilder.AddColumn<int>(
name: "Visibility",
table: "Albums",
type: "integer",
nullable: false,
defaultValue: 1);
// Map old IsPubliclyShared = true assets back to Public (0)
// Assets that were private default to Protected (1) from the column default
migrationBuilder.Sql("UPDATE \"Assets\" SET \"Visibility\" = 0 WHERE \"IsPubliclyShared\" = true");
migrationBuilder.DropColumn(
name: "IsPubliclyShared",
table: "Assets");
migrationBuilder.DropColumn(
name: "UserOwnerId",
table: "Albums");
migrationBuilder.RenameColumn(
name: "OwnerId",
table: "Assets",
newName: "UploadedBy");
migrationBuilder.RenameIndex(
name: "IX_Assets_OwnerId",
table: "Assets",
newName: "IX_Assets_UploadedBy");
migrationBuilder.CreateTable(
name: "PersonMaintainers",
columns: table => new
{
PersonId = table.Column<Guid>(type: "uuid", nullable: false),
UserId = table.Column<Guid>(type: "uuid", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_PersonMaintainers", x => new { x.PersonId, x.UserId });
table.ForeignKey(
name: "FK_PersonMaintainers_People_PersonId",
column: x => x.PersonId,
principalTable: "People",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_PersonMaintainers_Users_UserId",
column: x => x.UserId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Assets_VisibleForSearch",
table: "Assets",
columns: new[] { "Visibility", "UploadedBy" },
filter: "\"DeletedAt\" IS NULL");
migrationBuilder.CreateIndex(
name: "IX_PersonMaintainers_UserId",
table: "PersonMaintainers",
column: "UserId");
migrationBuilder.AddForeignKey(
name: "FK_Assets_Users_UploadedBy",
table: "Assets",
column: "UploadedBy",
principalTable: "Users",
principalColumn: "Id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Assets_Users_UploadedBy",
table: "Assets");
migrationBuilder.DropTable(
name: "PersonMaintainers");
migrationBuilder.DropIndex(
name: "IX_Assets_VisibleForSearch",
table: "Assets");
migrationBuilder.DropColumn(
name: "Visibility",
table: "People");
migrationBuilder.DropColumn(
name: "Visibility",
table: "Assets");
migrationBuilder.DropColumn(
name: "Visibility",
table: "Albums");
migrationBuilder.RenameColumn(
name: "UploadedBy",
table: "Assets",
newName: "OwnerId");
migrationBuilder.RenameIndex(
name: "IX_Assets_UploadedBy",
table: "Assets",
newName: "IX_Assets_OwnerId");
migrationBuilder.AddColumn<bool>(
name: "IsPubliclyShared",
table: "Assets",
type: "boolean",
nullable: false,
defaultValue: false);
migrationBuilder.AddColumn<Guid>(
name: "UserOwnerId",
table: "Albums",
type: "uuid",
nullable: true);
migrationBuilder.CreateTable(
name: "AssetUser",
columns: table => new
{
SharedAssetsId = table.Column<Guid>(type: "uuid", nullable: false),
SharedWithId = table.Column<Guid>(type: "uuid", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_AssetUser", x => new { x.SharedAssetsId, x.SharedWithId });
table.ForeignKey(
name: "FK_AssetUser_Assets_SharedAssetsId",
column: x => x.SharedAssetsId,
principalTable: "Assets",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_AssetUser_Users_SharedWithId",
column: x => x.SharedWithId,
principalTable: "Users",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "IX_Assets_VisibleForSearch",
table: "Assets",
columns: new[] { "IsPubliclyShared", "OwnerId" },
filter: "\"DeletedAt\" IS NULL");
migrationBuilder.CreateIndex(
name: "IX_Albums_UserOwnerId",
table: "Albums",
column: "UserOwnerId");
migrationBuilder.CreateIndex(
name: "IX_AssetUser_SharedWithId",
table: "AssetUser",
column: "SharedWithId");
migrationBuilder.AddForeignKey(
name: "FK_Albums_Users_UserOwnerId",
table: "Albums",
column: "UserOwnerId",
principalTable: "Users",
principalColumn: "Id");
migrationBuilder.AddForeignKey(
name: "FK_Assets_Users_OwnerId",
table: "Assets",
column: "OwnerId",
principalTable: "Users",
principalColumn: "Id");
}
}
}