diff --git a/Foxnouns.Backend/Database/DatabaseContext.cs b/Foxnouns.Backend/Database/DatabaseContext.cs index e6dc524..dae8b28 100644 --- a/Foxnouns.Backend/Database/DatabaseContext.cs +++ b/Foxnouns.Backend/Database/DatabaseContext.cs @@ -71,6 +71,22 @@ public class DatabaseContext(DbContextOptions options) : DbContext(options) modelBuilder.Entity().HasIndex(m => new { m.UserId, m.Name }).IsUnique(); modelBuilder.Entity().HasIndex(m => m.Sid).IsUnique(); modelBuilder.Entity().HasIndex(k => k.Key).IsUnique(); + modelBuilder + .Entity() + .HasIndex(m => new + { + m.AuthType, + m.RemoteId, + m.FediverseApplicationId, + }) + .HasFilter("fediverse_application_id IS NOT NULL") + .IsUnique(); + + modelBuilder + .Entity() + .HasIndex(m => new { m.AuthType, m.RemoteId }) + .HasFilter("fediverse_application_id IS NULL") + .IsUnique(); modelBuilder.Entity().Property(u => u.Sid).HasDefaultValueSql("find_free_user_sid()"); modelBuilder.Entity().Property(u => u.Fields).HasColumnType("jsonb"); diff --git a/Foxnouns.Backend/Database/Migrations/20241128202508_AddAuthMethodUniqueIndex.cs b/Foxnouns.Backend/Database/Migrations/20241128202508_AddAuthMethodUniqueIndex.cs new file mode 100644 index 0000000..f6a00b5 --- /dev/null +++ b/Foxnouns.Backend/Database/Migrations/20241128202508_AddAuthMethodUniqueIndex.cs @@ -0,0 +1,47 @@ +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Foxnouns.Backend.Database.Migrations +{ + /// + [DbContext(typeof(DatabaseContext))] + [Migration("20241128202508_AddAuthMethodUniqueIndex")] + public partial class AddAuthMethodUniqueIndex : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateIndex( + name: "ix_auth_methods_auth_type_remote_id", + table: "auth_methods", + columns: new[] { "auth_type", "remote_id" }, + unique: true, + filter: "fediverse_application_id IS NULL" + ); + + migrationBuilder.CreateIndex( + name: "ix_auth_methods_auth_type_remote_id_fediverse_application_id", + table: "auth_methods", + columns: new[] { "auth_type", "remote_id", "fediverse_application_id" }, + unique: true, + filter: "fediverse_application_id IS NOT NULL" + ); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropIndex( + name: "ix_auth_methods_auth_type_remote_id", + table: "auth_methods" + ); + + migrationBuilder.DropIndex( + name: "ix_auth_methods_auth_type_remote_id_fediverse_application_id", + table: "auth_methods" + ); + } + } +} diff --git a/Foxnouns.Backend/Database/Migrations/DatabaseContextModelSnapshot.cs b/Foxnouns.Backend/Database/Migrations/DatabaseContextModelSnapshot.cs index d012fe0..f9f1609 100644 --- a/Foxnouns.Backend/Database/Migrations/DatabaseContextModelSnapshot.cs +++ b/Foxnouns.Backend/Database/Migrations/DatabaseContextModelSnapshot.cs @@ -98,6 +98,16 @@ namespace Foxnouns.Backend.Database.Migrations b.HasIndex("UserId") .HasDatabaseName("ix_auth_methods_user_id"); + b.HasIndex("AuthType", "RemoteId") + .IsUnique() + .HasDatabaseName("ix_auth_methods_auth_type_remote_id") + .HasFilter("fediverse_application_id IS NULL"); + + b.HasIndex("AuthType", "RemoteId", "FediverseApplicationId") + .IsUnique() + .HasDatabaseName("ix_auth_methods_auth_type_remote_id_fediverse_application_id") + .HasFilter("fediverse_application_id IS NOT NULL"); + b.ToTable("auth_methods", (string)null); });