Skip to content

Commit 71fd599

Browse files
committed
fix: make usernames unique
1 parent aaa8d72 commit 71fd599

File tree

4 files changed

+279
-4
lines changed

4 files changed

+279
-4
lines changed

MumbleApi/Database/DataContext.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,26 +42,31 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
4242
.WithOne(p => p.Creator)
4343
.OnDelete(DeleteBehavior.Cascade);
4444

45+
modelBuilder
46+
.Entity<User>()
47+
.HasIndex(u => u.Username)
48+
.IsUnique();
49+
4550
modelBuilder
4651
.Entity<User>()
4752
.ToTable(t =>
4853
t.HasCheckConstraint(
4954
"chk_avatar_type",
50-
@"(avatar_url is null and avatar_media_type is null) or (avatar_url is not null and avatar_media_type is not null)"));
55+
"(avatar_url is null and avatar_media_type is null) or (avatar_url is not null and avatar_media_type is not null)"));
5156

5257
modelBuilder
5358
.Entity<Post>()
5459
.ToTable(t =>
5560
t.HasCheckConstraint(
5661
"chk_media_data",
57-
@"(media_url is null and media_type is null) or (media_url is not null and media_type is not null)"));
62+
"(media_url is null and media_type is null) or (media_url is not null and media_type is not null)"));
5863

5964
modelBuilder
6065
.Entity<Post>()
6166
.ToTable(t =>
6267
t.HasCheckConstraint(
6368
"chk_post_content",
64-
@"(media_url is not null and media_type is not null) or text is not null"));
69+
"(media_url is not null and media_type is not null) or text is not null"));
6570

6671
modelBuilder
6772
.Entity<Post>()

MumbleApi/Migrations/20240125091156_MakeUsernameUnique.Designer.cs

Lines changed: 238 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.EntityFrameworkCore.Migrations;
2+
3+
#nullable disable
4+
5+
namespace MumbleApi.Migrations
6+
{
7+
/// <inheritdoc />
8+
public partial class MakeUsernameUnique : Migration
9+
{
10+
/// <inheritdoc />
11+
protected override void Up(MigrationBuilder migrationBuilder)
12+
{
13+
migrationBuilder.CreateIndex(
14+
name: "ix_users_username",
15+
table: "users",
16+
column: "username",
17+
unique: true);
18+
}
19+
20+
/// <inheritdoc />
21+
protected override void Down(MigrationBuilder migrationBuilder)
22+
{
23+
migrationBuilder.DropIndex(
24+
name: "ix_users_username",
25+
table: "users");
26+
}
27+
}
28+
}

MumbleApi/Migrations/DataContextModelSnapshot.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
1717
{
1818
#pragma warning disable 612, 618
1919
modelBuilder
20-
.HasAnnotation("ProductVersion", "7.0.5")
20+
.HasAnnotation("ProductVersion", "8.0.1")
2121
.HasAnnotation("Relational:MaxIdentifierLength", 63);
2222

2323
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
@@ -140,6 +140,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
140140
b.HasKey("Id")
141141
.HasName("pk_users");
142142

143+
b.HasIndex("Username")
144+
.IsUnique()
145+
.HasDatabaseName("ix_users_username");
146+
143147
b.ToTable("users", null, t =>
144148
{
145149
t.HasCheckConstraint("chk_avatar_type", "(avatar_url is null and avatar_media_type is null) or (avatar_url is not null and avatar_media_type is not null)");

0 commit comments

Comments
 (0)