Skip to content

Commit

Permalink
add database migration to limit UID and Hash max lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
Stanley Dimant committed Jul 12, 2022
1 parent b8950ca commit e70f564
Show file tree
Hide file tree
Showing 6 changed files with 325 additions and 11 deletions.
2 changes: 0 additions & 2 deletions MareSynchronosServer/MareSynchronosServer/Hubs/FilesHub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ public async Task<List<UploadFileDto>> SendFiles(List<string> fileListHashes)
await DbContext.Files.AddAsync(new FileCache()
{
Hash = file,
LastAccessTime = DateTime.Now,
Uploaded = false,
Uploader = DbContext.Users.Single(u => u.UID == userId)
});
Expand Down Expand Up @@ -225,7 +224,6 @@ public async Task UploadFileStreamAsync(string hash, IAsyncEnumerable<byte[]> fi
await File.WriteAllBytesAsync(Path.Combine(BasePath, hash), uploadedFile.ToArray());
relatedFile = DbContext.Files.Single(f => f.Hash == hash);
relatedFile.Uploaded = true;
relatedFile.LastAccessTime = DateTime.Now;
await DbContext.SaveChangesAsync();
Logger.LogInformation("File " + hash + " added to DB");
}
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,141 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MareSynchronosServer.Migrations
{
public partial class SetMaxLengths : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "LastAccessTime",
table: "FileCaches");

migrationBuilder.DropForeignKey("FK_FileCaches_Users_UploaderUID", "FileCaches");
migrationBuilder.DropForeignKey("FK_ClientPairs_Users_UserUID", "ClientPairs");
migrationBuilder.DropForeignKey("FK_ClientPairs_Users_OtherUserUID", "ClientPairs");
migrationBuilder.DropPrimaryKey("PK_FileCaches", "FileCaches");
migrationBuilder.DropPrimaryKey("PK_Users", "Users");

migrationBuilder.AlterColumn<string>(
name: "UploaderUID",
table: "FileCaches",
type: "nvarchar(10)",
nullable: true,
oldClrType: typeof(string),
oldType: "nvarchar(450)",
oldNullable: true);

migrationBuilder.AlterColumn<string>(
name: "Hash",
table: "FileCaches",
type: "nvarchar(40)",
maxLength: 20,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(450)");

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

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

migrationBuilder.AlterColumn<string>(
name: "UID",
table: "Users",
type: "nvarchar(10)",
maxLength: 10,
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(450)");

migrationBuilder.AddPrimaryKey("PK_Users", "Users", "UID");
migrationBuilder.AddPrimaryKey("PK_FileCaches", "FileCaches", "Hash");
migrationBuilder.AddForeignKey("FK_FileCaches_Users_UploaderUID", "FileCaches", "UploaderUID", "Users");
migrationBuilder.AddForeignKey("FK_ClientPairs_Users_UserUID", "ClientPairs", "UserUID", "Users");
migrationBuilder.AddForeignKey("FK_ClientPairs_Users_OtherUserUID", "ClientPairs", "UserUID", "Users");
}

protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey("FK_FileCaches_Users_UploaderUID", "FileCaches");
migrationBuilder.DropForeignKey("FK_ClientPairs_Users_UserUID", "ClientPairs");
migrationBuilder.DropForeignKey("FK_ClientPairs_Users_OtherUserUID", "ClientPairs");
migrationBuilder.DropPrimaryKey("PK_FileCaches", "FileCaches");
migrationBuilder.DropPrimaryKey("PK_Users", "Users");

migrationBuilder.AlterColumn<string>(
name: "UID",
table: "Users",
type: "nvarchar(450)",
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(10)",
oldMaxLength: 10);

migrationBuilder.AlterColumn<string>(
name: "UploaderUID",
table: "FileCaches",
type: "nvarchar(450)",
nullable: true,
oldClrType: typeof(string),
oldType: "nvarchar(10)",
oldNullable: true);

migrationBuilder.AlterColumn<string>(
name: "Hash",
table: "FileCaches",
type: "nvarchar(450)",
nullable: false,
oldClrType: typeof(string),
oldType: "nvarchar(20)",
oldMaxLength: 20);

migrationBuilder.AddColumn<DateTime>(
name: "LastAccessTime",
table: "FileCaches",
type: "datetime2",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

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

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

migrationBuilder.AddPrimaryKey("PK_Users", "Users", "UID");
migrationBuilder.AddPrimaryKey("PK_FileCaches", "FileCaches", "Hash");
migrationBuilder.AddForeignKey("FK_FileCaches_Users_UploaderUID", "FileCaches", "UploaderUID", "Users");
migrationBuilder.AddForeignKey("FK_ClientPairs_Users_UserUID", "ClientPairs", "UserUID", "Users");
migrationBuilder.AddForeignKey("FK_ClientPairs_Users_OtherUserUID", "ClientPairs", "UserUID", "Users");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("bit");
b.Property<string>("OtherUserUID")
.HasColumnType("nvarchar(450)");
.HasColumnType("nvarchar(10)");
b.Property<byte[]>("Timestamp")
.IsConcurrencyToken()
.ValueGeneratedOnAddOrUpdate()
.HasColumnType("rowversion");
b.Property<string>("UserUID")
.HasColumnType("nvarchar(450)");
.HasColumnType("nvarchar(10)");
b.HasKey("Id");
Expand All @@ -77,10 +77,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)
modelBuilder.Entity("MareSynchronosServer.Models.FileCache", b =>
{
b.Property<string>("Hash")
.HasColumnType("nvarchar(450)");
b.Property<DateTime>("LastAccessTime")
.HasColumnType("datetime2");
.HasMaxLength(20)
.HasColumnType("nvarchar(40)");
b.Property<byte[]>("Timestamp")
.IsConcurrencyToken()
Expand All @@ -91,7 +89,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("bit");
b.Property<string>("UploaderUID")
.HasColumnType("nvarchar(450)");
.HasColumnType("nvarchar(10)");
b.HasKey("Hash");
Expand Down Expand Up @@ -121,7 +119,8 @@ protected override void BuildModel(ModelBuilder modelBuilder)
modelBuilder.Entity("MareSynchronosServer.Models.User", b =>
{
b.Property<string>("UID")
.HasColumnType("nvarchar(450)");
.HasMaxLength(10)
.HasColumnType("nvarchar(10)");
b.Property<string>("CharacterIdentification")
.HasColumnType("nvarchar(max)");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ namespace MareSynchronosServer.Models
public class FileCache
{
[Key]
[MaxLength(40)]
public string Hash { get; set; }
public User Uploader { get; set; }
public bool Uploaded { get; set; }
public DateTime LastAccessTime { get; set; }
[Timestamp]
public byte[] Timestamp { get; set; }
}
Expand Down
Loading

0 comments on commit e70f564

Please sign in to comment.