Skip to content

Commit

Permalink
Implement migration downgrade with data loss
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-lerch committed Jan 25, 2024
1 parent 6b8d2e5 commit 2ab2124
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ protected override void Down(MigrationBuilder migrationBuilder)
name: "PermittedRecipientsId",
table: "DistributionLists");

// Delete all person filters because we cannot convert a recursive tree to a flat list
migrationBuilder.Sql("DELETE FROM `PersonFilters`");

migrationBuilder.AddColumn<long>(
name: "DistributionListId",
table: "PersonFilters",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ public class DistributionList
public required string Alias { get; set; }
public int Flags { get; set; }
public long? PermittedRecipientsId { get; set; }
public PersonFilter? PermittedRecipients { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,93 @@ public async Task TestUpgrade()
[Fact]
public async Task TestDowngrade()
{

PersonFilterTree.Status status = new()
{
Id = 1,
Name = "unknown",
};
PersonFilterTree.GroupStatus groupStatus = new()
{
Id = 1,
Name = "active",
};
PersonFilterTree.GroupType groupType = new()
{
Id = 1,
Name = "Kleingruppe",
};
PersonFilterTree.Group group = new()
{
Id = 1,
Name = "Hauskreis",
GroupTypeId = 1,
GroupStatusId = 1,
};
PersonFilterTree.Person person = new()
{
Id = 1,
StatusId = 1,
FirstName = "Max",
LastName = "Mustermann",
Email = "[email protected]",
};
PersonFilterTree.StatusFilter statusFilter = new()
{
Id = 1,
StatusId = 1,
};
PersonFilterTree.DistributionList distToStatus = new()
{
Id = 1,
Alias = "unknown",
PermittedRecipientsId = 1,
};
PersonFilterTree.DistributionList distToNobody = new()
{
Id = 2,
Alias = "nobody",
PermittedRecipientsId = null,
};
PersonFilterTree.GroupFilter groupFilter = new()
{
Id = 2,
ParentId = 4,
GroupId = 1,
};
PersonFilterTree.SinglePerson singlePerson = new()
{
Id = 3,
ParentId = 4,
PersonId = 1,
};
PersonFilterTree.LogicalOr groupOrPerson = new()
{
Id = 4,
};
PersonFilterTree.DistributionList distToGroupAndSinglePerson = new()
{
Id = 3,
Alias = "kleingruppen",
PermittedRecipientsId = 4,
};
IMigrator migrator = databaseContext.GetInfrastructure().GetRequiredService<IMigrator>();

// Create database schema of the migration to test
await migrator.MigrateAsync("PersonFilterTree");

// Add test data
after.Status.Add(status);
after.GroupStatuses.Add(groupStatus);
after.GroupTypes.Add(groupType);
after.Groups.Add(group);
after.People.Add(person);
after.PersonFilters.Add(statusFilter);
after.DistributionLists.Add(distToStatus);
after.DistributionLists.Add(distToNobody);
after.PersonFilters.Add(groupFilter);
after.PersonFilters.Add(singlePerson);
after.PersonFilters.Add(groupOrPerson);
after.DistributionLists.Add(distToGroupAndSinglePerson);
await after.SaveChangesAsync();

// Migrate to migration before the one to test and thereby revert it
Expand Down

0 comments on commit 2ab2124

Please sign in to comment.