-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migration and unit tests (upgrade side only)
- Loading branch information
1 parent
ac5623e
commit 6b8d2e5
Showing
29 changed files
with
1,327 additions
and
11 deletions.
There are no files selected for viewing
646 changes: 646 additions & 0 deletions
646
server/src/Korga.Server/Migrations/20240121221944_PersonFilterTree.Designer.cs
Large diffs are not rendered by default.
Oops, something went wrong.
166 changes: 166 additions & 0 deletions
166
server/src/Korga.Server/Migrations/20240121221944_PersonFilterTree.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
using Microsoft.EntityFrameworkCore.Migrations; | ||
|
||
#nullable disable | ||
|
||
namespace Korga.Server.Migrations | ||
{ | ||
/// <inheritdoc /> | ||
public partial class PersonFilterTree : Migration | ||
{ | ||
/// <inheritdoc /> | ||
protected override void Up(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.AddColumn<int>( | ||
name: "GroupTypeId", | ||
table: "PersonFilters", | ||
type: "int", | ||
nullable: true); | ||
|
||
migrationBuilder.AddColumn<long>( | ||
name: "ParentId", | ||
table: "PersonFilters", | ||
type: "bigint", | ||
nullable: true); | ||
|
||
migrationBuilder.AddColumn<long>( | ||
name: "PermittedRecipientsId", | ||
table: "DistributionLists", | ||
type: "bigint", | ||
nullable: true); | ||
|
||
migrationBuilder.CreateIndex( | ||
name: "IX_PersonFilters_GroupTypeId", | ||
table: "PersonFilters", | ||
column: "GroupTypeId"); | ||
|
||
migrationBuilder.CreateIndex( | ||
name: "IX_PersonFilters_ParentId", | ||
table: "PersonFilters", | ||
column: "ParentId"); | ||
|
||
migrationBuilder.CreateIndex( | ||
name: "IX_DistributionLists_PermittedRecipientsId", | ||
table: "DistributionLists", | ||
column: "PermittedRecipientsId"); | ||
|
||
migrationBuilder.AddForeignKey( | ||
name: "FK_DistributionLists_PersonFilters_PermittedRecipientsId", | ||
table: "DistributionLists", | ||
column: "PermittedRecipientsId", | ||
principalTable: "PersonFilters", | ||
principalColumn: "Id"); | ||
|
||
migrationBuilder.AddForeignKey( | ||
name: "FK_PersonFilters_GroupTypes_GroupTypeId", | ||
table: "PersonFilters", | ||
column: "GroupTypeId", | ||
principalTable: "GroupTypes", | ||
principalColumn: "Id"); | ||
|
||
migrationBuilder.AddForeignKey( | ||
name: "FK_PersonFilters_PersonFilters_ParentId", | ||
table: "PersonFilters", | ||
column: "ParentId", | ||
principalTable: "PersonFilters", | ||
principalColumn: "Id"); | ||
|
||
// Create LogicalOr filters for every distribution list that has a person filter | ||
migrationBuilder.Sql( | ||
@"INSERT INTO `PersonFilters` (`DistributionListId`, `Discriminator`) | ||
SELECT DISTINCT(`DistributionListId`), ""LogicalOr"" FROM `PersonFilters`"); | ||
|
||
// These UPDATE SELECT queries might not work with databases other than MySQL/MariaDB | ||
// Furthermore, the inner query must not filter rows because of optimization problems: | ||
// https://dev.mysql.com/doc/refman/8.0/en/update.html | ||
|
||
// Set ParentId for all original person filters | ||
migrationBuilder.Sql( | ||
@"UPDATE `PersonFilters` AS `original`, | ||
(SELECT `Id`, `DistributionListId`, `Discriminator` FROM `PersonFilters`) AS `parent` | ||
SET `original`.`ParentId` = `parent`.`Id` | ||
WHERE `parent`.`Discriminator` = ""LogicalOr"" | ||
AND `parent`.`DistributionListId` = `original`.`DistributionListId` | ||
AND `original`.`Discriminator` <> ""LogicalOr"""); | ||
|
||
// Set permitted recipients | ||
migrationBuilder.Sql( | ||
@"UPDATE `DistributionLists` AS `dist`, | ||
(SELECT `Id`, `DistributionListId`, `Discriminator` FROM `PersonFilters`) AS `filter` | ||
SET `dist`.`PermittedRecipientsId` = `filter`.`Id` | ||
WHERE `filter`.`Discriminator` = ""LogicalOr"" | ||
AND `filter`.`DistributionListId` = `dist`.`Id`"); | ||
|
||
migrationBuilder.DropForeignKey( | ||
name: "FK_PersonFilters_DistributionLists_DistributionListId", | ||
table: "PersonFilters"); | ||
|
||
migrationBuilder.DropIndex( | ||
name: "IX_PersonFilters_DistributionListId", | ||
table: "PersonFilters"); | ||
|
||
migrationBuilder.DropColumn( | ||
name: "DistributionListId", | ||
table: "PersonFilters"); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void Down(MigrationBuilder migrationBuilder) | ||
{ | ||
migrationBuilder.DropForeignKey( | ||
name: "FK_DistributionLists_PersonFilters_PermittedRecipientsId", | ||
table: "DistributionLists"); | ||
|
||
migrationBuilder.DropForeignKey( | ||
name: "FK_PersonFilters_GroupTypes_GroupTypeId", | ||
table: "PersonFilters"); | ||
|
||
migrationBuilder.DropForeignKey( | ||
name: "FK_PersonFilters_PersonFilters_ParentId", | ||
table: "PersonFilters"); | ||
|
||
migrationBuilder.DropIndex( | ||
name: "IX_PersonFilters_GroupTypeId", | ||
table: "PersonFilters"); | ||
|
||
migrationBuilder.DropIndex( | ||
name: "IX_PersonFilters_ParentId", | ||
table: "PersonFilters"); | ||
|
||
migrationBuilder.DropIndex( | ||
name: "IX_DistributionLists_PermittedRecipientsId", | ||
table: "DistributionLists"); | ||
|
||
migrationBuilder.DropColumn( | ||
name: "GroupTypeId", | ||
table: "PersonFilters"); | ||
|
||
migrationBuilder.DropColumn( | ||
name: "ParentId", | ||
table: "PersonFilters"); | ||
|
||
migrationBuilder.DropColumn( | ||
name: "PermittedRecipientsId", | ||
table: "DistributionLists"); | ||
|
||
migrationBuilder.AddColumn<long>( | ||
name: "DistributionListId", | ||
table: "PersonFilters", | ||
type: "bigint", | ||
nullable: false, | ||
defaultValue: 0L); | ||
|
||
migrationBuilder.CreateIndex( | ||
name: "IX_PersonFilters_DistributionListId", | ||
table: "PersonFilters", | ||
column: "DistributionListId"); | ||
|
||
migrationBuilder.AddForeignKey( | ||
name: "FK_PersonFilters_DistributionLists_DistributionListId", | ||
table: "PersonFilters", | ||
column: "DistributionListId", | ||
principalTable: "DistributionLists", | ||
principalColumn: "Id", | ||
onDelete: ReferentialAction.Cascade); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
server/tests/Korga.Server.Tests/Migrations/08_GroupMemberStatus/DatabaseContext.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
|
||
namespace Korga.Server.Tests.Migrations.GroupMemberStatus; | ||
|
||
public class DatabaseContext : DbContext | ||
{ | ||
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options) { } | ||
|
||
public DbSet<DistributionList> DistributionLists => Set<DistributionList>(); | ||
public DbSet<PersonFilter> PersonFilters => Set<PersonFilter>(); | ||
|
||
public DbSet<Person> People => Set<Person>(); | ||
public DbSet<Group> Groups => Set<Group>(); | ||
public DbSet<GroupRole> GroupRoles => Set<GroupRole>(); | ||
public DbSet<GroupType> GroupTypes => Set<GroupType>(); | ||
public DbSet<GroupStatus> GroupStatuses => Set<GroupStatus>(); | ||
public DbSet<Status> Status => Set<Status>(); | ||
|
||
protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
{ | ||
base.OnModelCreating(modelBuilder); | ||
|
||
modelBuilder.Entity<PersonFilter>(); | ||
modelBuilder.Entity<StatusFilter>(); | ||
modelBuilder.Entity<GroupFilter>(); | ||
modelBuilder.Entity<SinglePerson>(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
server/tests/Korga.Server.Tests/Migrations/08_GroupMemberStatus/DistributionList.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Korga.Server.Tests.Migrations.GroupMemberStatus; | ||
|
||
public class DistributionList | ||
{ | ||
public required long Id { get; set; } | ||
public required string Alias { get; set; } | ||
public int Flags { get; set; } | ||
} |
16 changes: 16 additions & 0 deletions
16
server/tests/Korga.Server.Tests/Migrations/08_GroupMemberStatus/Group.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System; | ||
|
||
namespace Korga.Server.Tests.Migrations.GroupMemberStatus; | ||
|
||
public class Group | ||
{ | ||
public int Id { get; set; } | ||
|
||
public GroupType? GroupType { get; set; } | ||
public int GroupTypeId { get; set; } | ||
public GroupStatus? GroupStatus { get; set; } | ||
public int GroupStatusId { get; set; } | ||
|
||
public required string Name { get; set; } | ||
public DateTime DeletionTime { get; set; } | ||
} |
10 changes: 10 additions & 0 deletions
10
server/tests/Korga.Server.Tests/Migrations/08_GroupMemberStatus/GroupFilter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace Korga.Server.Tests.Migrations.GroupMemberStatus; | ||
|
||
public class GroupFilter : PersonFilter | ||
{ | ||
public Group? Group { get; set; } | ||
public int GroupId { get; set; } | ||
|
||
public GroupRole? GroupRole { get; set; } | ||
public int? GroupRoleId { get; set; } | ||
} |
14 changes: 14 additions & 0 deletions
14
server/tests/Korga.Server.Tests/Migrations/08_GroupMemberStatus/GroupRole.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
using System; | ||
|
||
namespace Korga.Server.Tests.Migrations.GroupMemberStatus; | ||
|
||
public class GroupRole | ||
{ | ||
public int Id { get; set; } | ||
|
||
public GroupType? GroupType { get; set; } | ||
public int GroupTypeId { get; set; } | ||
|
||
public required string Name { get; set; } | ||
public DateTime DeletionTime { get; set; } | ||
} |
Oops, something went wrong.