Skip to content

Commit

Permalink
fix: make usernames unique
Browse files Browse the repository at this point in the history
  • Loading branch information
buehler committed Jan 25, 2024
1 parent aaa8d72 commit 71fd599
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 4 deletions.
11 changes: 8 additions & 3 deletions MumbleApi/Database/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,26 +42,31 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
.WithOne(p => p.Creator)
.OnDelete(DeleteBehavior.Cascade);

modelBuilder
.Entity<User>()
.HasIndex(u => u.Username)
.IsUnique();

modelBuilder
.Entity<User>()
.ToTable(t =>
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)"));
"(avatar_url is null and avatar_media_type is null) or (avatar_url is not null and avatar_media_type is not null)"));

modelBuilder
.Entity<Post>()
.ToTable(t =>
t.HasCheckConstraint(
"chk_media_data",
@"(media_url is null and media_type is null) or (media_url is not null and media_type is not null)"));
"(media_url is null and media_type is null) or (media_url is not null and media_type is not null)"));

modelBuilder
.Entity<Post>()
.ToTable(t =>
t.HasCheckConstraint(
"chk_post_content",
@"(media_url is not null and media_type is not null) or text is not null"));
"(media_url is not null and media_type is not null) or text is not null"));

modelBuilder
.Entity<Post>()
Expand Down
238 changes: 238 additions & 0 deletions MumbleApi/Migrations/20240125091156_MakeUsernameUnique.Designer.cs

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

28 changes: 28 additions & 0 deletions MumbleApi/Migrations/20240125091156_MakeUsernameUnique.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace MumbleApi.Migrations
{
/// <inheritdoc />
public partial class MakeUsernameUnique : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateIndex(
name: "ix_users_username",
table: "users",
column: "username",
unique: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropIndex(
name: "ix_users_username",
table: "users");
}
}
}
6 changes: 5 additions & 1 deletion MumbleApi/Migrations/DataContextModelSnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "7.0.5")
.HasAnnotation("ProductVersion", "8.0.1")
.HasAnnotation("Relational:MaxIdentifierLength", 63);

NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
Expand Down Expand Up @@ -140,6 +140,10 @@ protected override void BuildModel(ModelBuilder modelBuilder)
b.HasKey("Id")
.HasName("pk_users");
b.HasIndex("Username")
.IsUnique()
.HasDatabaseName("ix_users_username");
b.ToTable("users", null, t =>
{
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)");
Expand Down

0 comments on commit 71fd599

Please sign in to comment.