Skip to content

Commit

Permalink
add migration to make client pair UIDs PK
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanley Dimant committed Jul 13, 2022
1 parent e70f564 commit 4a83a7d
Show file tree
Hide file tree
Showing 5 changed files with 328 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<User>().ToTable("Users");
modelBuilder.Entity<FileCache>().ToTable("FileCaches");
modelBuilder.Entity<ClientPair>().ToTable("ClientPairs");
modelBuilder.Entity<ClientPair>().HasKey(u => new { u.UserUID, u.OtherUserUID });
modelBuilder.Entity<ClientPair>().HasIndex(c => c.UserUID);
modelBuilder.Entity<ClientPair>().HasIndex(c => c.OtherUserUID);
modelBuilder.Entity<ForbiddenUploadEntry>().ToTable("ForbiddenUploadEntries");
modelBuilder.Entity<Banned>().ToTable("BannedUsers");
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MareSynchronosServer.Migrations
{
public partial class ClientPairKey : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_ClientPairs_Users_OtherUserUID",
table: "ClientPairs");

migrationBuilder.DropForeignKey(
name: "FK_ClientPairs_Users_UserUID",
table: "ClientPairs");

migrationBuilder.DropPrimaryKey(
name: "PK_ClientPairs",
table: "ClientPairs");

migrationBuilder.DropColumn(
name: "Id",
table: "ClientPairs");


migrationBuilder.AlterColumn<string>(
name: "UserUID",
table: "ClientPairs",
type: "nvarchar(10)",
maxLength: 10,
nullable: false,
defaultValue: "",
oldClrType: typeof(string),
oldType: "nvarchar(10)",
oldNullable: true);

migrationBuilder.AlterColumn<string>(
name: "OtherUserUID",
table: "ClientPairs",
type: "nvarchar(10)",
maxLength: 10,
nullable: false,
defaultValue: "",
oldClrType: typeof(string),
oldType: "nvarchar(10)",
oldNullable: true);

migrationBuilder.AddPrimaryKey(
name: "PK_ClientPairs",
table: "ClientPairs",
columns: new[] { "UserUID", "OtherUserUID" });

migrationBuilder.AddForeignKey(
name: "FK_ClientPairs_Users_OtherUserUID",
table: "ClientPairs",
column: "OtherUserUID",
principalTable: "Users",
principalColumn: "UID",
onDelete: ReferentialAction.NoAction);

migrationBuilder.AddForeignKey(
name: "FK_ClientPairs_Users_UserUID",
table: "ClientPairs",
column: "UserUID",
principalTable: "Users",
principalColumn: "UID",
onDelete: ReferentialAction.NoAction);
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_ClientPairs_Users_OtherUserUID",
table: "ClientPairs");

migrationBuilder.DropForeignKey(
name: "FK_ClientPairs_Users_UserUID",
table: "ClientPairs");

migrationBuilder.DropPrimaryKey(
name: "PK_ClientPairs",
table: "ClientPairs");

migrationBuilder.AlterColumn<string>(
name: "OtherUserUID",
table: "ClientPairs",
type: "nvarchar(10)",
nullable: true,
oldClrType: typeof(string),
oldType: "nvarchar(10)",
oldMaxLength: 10);

migrationBuilder.AlterColumn<string>(
name: "UserUID",
table: "ClientPairs",
type: "nvarchar(10)",
nullable: true,
oldClrType: typeof(string),
oldType: "nvarchar(10)",
oldMaxLength: 10);

migrationBuilder.AddColumn<int>(
name: "Id",
table: "ClientPairs",
type: "int",
nullable: false,
defaultValue: 0)
.Annotation("SqlServer:Identity", "1, 1");

migrationBuilder.AddPrimaryKey(
name: "PK_ClientPairs",
table: "ClientPairs",
column: "Id");

migrationBuilder.AddForeignKey(
name: "FK_ClientPairs_Users_OtherUserUID",
table: "ClientPairs",
column: "OtherUserUID",
principalTable: "Users",
principalColumn: "UID");

migrationBuilder.AddForeignKey(
name: "FK_ClientPairs_Users_UserUID",
table: "ClientPairs",
column: "UserUID",
principalTable: "Users",
principalColumn: "UID");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,30 +42,26 @@ protected override void BuildModel(ModelBuilder modelBuilder)

modelBuilder.Entity("MareSynchronosServer.Models.ClientPair", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<string>("UserUID")
.HasMaxLength(10)
.HasColumnType("nvarchar(10)");
SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"), 1L, 1);
b.Property<string>("OtherUserUID")
.HasMaxLength(10)
.HasColumnType("nvarchar(10)");
b.Property<bool>("AllowReceivingMessages")
.HasColumnType("bit");
b.Property<bool>("IsPaused")
.HasColumnType("bit");
b.Property<string>("OtherUserUID")
.HasColumnType("nvarchar(10)");
b.Property<byte[]>("Timestamp")
.IsConcurrencyToken()
.ValueGeneratedOnAddOrUpdate()
.HasColumnType("rowversion");
b.Property<string>("UserUID")
.HasColumnType("nvarchar(10)");
b.HasKey("Id");
b.HasKey("UserUID", "OtherUserUID");
b.HasIndex("OtherUserUID");
Expand All @@ -77,7 +73,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
modelBuilder.Entity("MareSynchronosServer.Models.FileCache", b =>
{
b.Property<string>("Hash")
.HasMaxLength(20)
.HasMaxLength(40)
.HasColumnType("nvarchar(40)");
b.Property<byte[]>("Timestamp")
Expand Down Expand Up @@ -148,11 +144,15 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
b.HasOne("MareSynchronosServer.Models.User", "OtherUser")
.WithMany()
.HasForeignKey("OtherUserUID");
.HasForeignKey("OtherUserUID")
.OnDelete(DeleteBehavior.NoAction)
.IsRequired();
b.HasOne("MareSynchronosServer.Models.User", "User")
.WithMany()
.HasForeignKey("UserUID");
.HasForeignKey("UserUID")
.OnDelete(DeleteBehavior.NoAction)
.IsRequired();
b.Navigation("OtherUser");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ namespace MareSynchronosServer.Models
{
public class ClientPair
{
public int Id { get; set; }
[MaxLength(10)]
public string UserUID { get; set; }
public User User { get; set; }
[MaxLength(10)]
public string OtherUserUID { get; set; }
public User OtherUser { get; set; }
public bool IsPaused { get; set; }
public bool AllowReceivingMessages { get; set; } = false;
Expand Down

0 comments on commit 4a83a7d

Please sign in to comment.