Browse Source

Add IAuditableEntity

Patrick Barron 3 years ago
parent
commit
15baf04bd2

+ 7 - 4
Jellyfin.Data/Entities/Security/Device.cs

@@ -2,13 +2,14 @@ using System;
 using System.ComponentModel.DataAnnotations;
 using System.ComponentModel.DataAnnotations.Schema;
 using System.Globalization;
+using Jellyfin.Data.Interfaces;
 
 namespace Jellyfin.Data.Entities.Security
 {
     /// <summary>
     /// An entity representing a device.
     /// </summary>
-    public class Device
+    public class Device : IAuditableEntity
     {
         /// <summary>
         /// Initializes a new instance of the <see cref="Device"/> class.
@@ -28,6 +29,7 @@ namespace Jellyfin.Data.Entities.Security
 
             AccessToken = Guid.NewGuid().ToString("N", CultureInfo.InvariantCulture);
             DateCreated = DateTime.UtcNow;
+            DateModified = DateCreated;
             DateLastActivity = DateCreated;
 
             // Non-nullable for EF Core, as this is a required relationship.
@@ -83,11 +85,12 @@ namespace Jellyfin.Data.Entities.Security
         /// </summary>
         public bool IsActive { get; set; }
 
-        /// <summary>
-        /// Gets or sets the date this device was created.
-        /// </summary>
+        /// <inheritdoc />
         public DateTime DateCreated { get; set; }
 
+        /// <inheritdoc />
+        public DateTime DateModified { get; set; }
+
         /// <summary>
         /// Gets or sets the date of last activity.
         /// </summary>

+ 20 - 0
Jellyfin.Data/Interfaces/IAuditableEntity.cs

@@ -0,0 +1,20 @@
+using System;
+
+namespace Jellyfin.Data.Interfaces
+{
+    /// <summary>
+    /// An interface representing an entity that has creation/modification dates.
+    /// </summary>
+    public interface IAuditableEntity
+    {
+        /// <summary>
+        /// Gets the date this entity was created.
+        /// </summary>
+        public DateTime DateCreated { get; }
+
+        /// <summary>
+        /// Gets or sets the date this entity was modified.
+        /// </summary>
+        public DateTime DateModified { get; set; }
+    }
+}

+ 0 - 128
Jellyfin.Server.Implementations/Migrations/20210602224232_AddDevices.cs

@@ -1,128 +0,0 @@
-#pragma warning disable CS1591
-#pragma warning disable SA1601
-
-using System;
-using Microsoft.EntityFrameworkCore.Migrations;
-
-namespace Jellyfin.Server.Implementations.Migrations
-{
-    public partial class AddDevices : Migration
-    {
-        protected override void Up(MigrationBuilder migrationBuilder)
-        {
-            migrationBuilder.CreateTable(
-                name: "ApiKeys",
-                schema: "jellyfin",
-                columns: table => new
-                {
-                    Id = table.Column<int>(type: "INTEGER", nullable: false)
-                        .Annotation("Sqlite:Autoincrement", true),
-                    DateCreated = table.Column<DateTime>(type: "TEXT", nullable: false),
-                    DateLastActivity = table.Column<DateTime>(type: "TEXT", nullable: false),
-                    Name = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
-                    AccessToken = table.Column<Guid>(type: "TEXT", nullable: false)
-                },
-                constraints: table =>
-                {
-                    table.PrimaryKey("PK_ApiKeys", x => x.Id);
-                });
-
-            migrationBuilder.CreateTable(
-                name: "DeviceOptions",
-                schema: "jellyfin",
-                columns: table => new
-                {
-                    Id = table.Column<int>(type: "INTEGER", nullable: false)
-                        .Annotation("Sqlite:Autoincrement", true),
-                    DeviceId = table.Column<string>(type: "TEXT", nullable: false),
-                    CustomName = table.Column<string>(type: "TEXT", nullable: true)
-                },
-                constraints: table =>
-                {
-                    table.PrimaryKey("PK_DeviceOptions", x => x.Id);
-                });
-
-            migrationBuilder.CreateTable(
-                name: "Devices",
-                schema: "jellyfin",
-                columns: table => new
-                {
-                    Id = table.Column<int>(type: "INTEGER", nullable: false)
-                        .Annotation("Sqlite:Autoincrement", true),
-                    UserId = table.Column<Guid>(type: "TEXT", nullable: false),
-                    AccessToken = table.Column<string>(type: "TEXT", nullable: false),
-                    AppName = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
-                    AppVersion = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
-                    DeviceName = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
-                    DeviceId = table.Column<string>(type: "TEXT", maxLength: 256, nullable: false),
-                    IsActive = table.Column<bool>(type: "INTEGER", nullable: false),
-                    DateCreated = table.Column<DateTime>(type: "TEXT", nullable: false),
-                    DateLastActivity = table.Column<DateTime>(type: "TEXT", nullable: false)
-                },
-                constraints: table =>
-                {
-                    table.PrimaryKey("PK_Devices", x => x.Id);
-                    table.ForeignKey(
-                        name: "FK_Devices_Users_UserId",
-                        column: x => x.UserId,
-                        principalSchema: "jellyfin",
-                        principalTable: "Users",
-                        principalColumn: "Id",
-                        onDelete: ReferentialAction.Cascade);
-                });
-
-            migrationBuilder.CreateIndex(
-                name: "IX_ApiKeys_AccessToken",
-                schema: "jellyfin",
-                table: "ApiKeys",
-                column: "AccessToken",
-                unique: true);
-
-            migrationBuilder.CreateIndex(
-                name: "IX_DeviceOptions_DeviceId",
-                schema: "jellyfin",
-                table: "DeviceOptions",
-                column: "DeviceId",
-                unique: true);
-
-            migrationBuilder.CreateIndex(
-                name: "IX_Devices_AccessToken_DateLastActivity",
-                schema: "jellyfin",
-                table: "Devices",
-                columns: new[] { "AccessToken", "DateLastActivity" });
-
-            migrationBuilder.CreateIndex(
-                name: "IX_Devices_DeviceId",
-                schema: "jellyfin",
-                table: "Devices",
-                column: "DeviceId");
-
-            migrationBuilder.CreateIndex(
-                name: "IX_Devices_DeviceId_DateLastActivity",
-                schema: "jellyfin",
-                table: "Devices",
-                columns: new[] { "DeviceId", "DateLastActivity" });
-
-            migrationBuilder.CreateIndex(
-                name: "IX_Devices_UserId_DeviceId",
-                schema: "jellyfin",
-                table: "Devices",
-                columns: new[] { "UserId", "DeviceId" });
-        }
-
-        protected override void Down(MigrationBuilder migrationBuilder)
-        {
-            migrationBuilder.DropTable(
-                name: "ApiKeys",
-                schema: "jellyfin");
-
-            migrationBuilder.DropTable(
-                name: "DeviceOptions",
-                schema: "jellyfin");
-
-            migrationBuilder.DropTable(
-                name: "Devices",
-                schema: "jellyfin");
-        }
-    }
-}

+ 7 - 3
Jellyfin.Server.Implementations/Migrations/20210602224232_AddDevices.Designer.cs → Jellyfin.Server.Implementations/Migrations/20210814002109_AddDevices.Designer.cs

@@ -11,7 +11,7 @@ using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
 namespace Jellyfin.Server.Implementations.Migrations
 {
     [DbContext(typeof(JellyfinDb))]
-    [Migration("20210602224232_AddDevices")]
+    [Migration("20210814002109_AddDevices")]
     partial class AddDevices
     {
         protected override void BuildTargetModel(ModelBuilder modelBuilder)
@@ -19,7 +19,7 @@ namespace Jellyfin.Server.Implementations.Migrations
 #pragma warning disable 612, 618
             modelBuilder
                 .HasDefaultSchema("jellyfin")
-                .HasAnnotation("ProductVersion", "5.0.6");
+                .HasAnnotation("ProductVersion", "5.0.7");
 
             modelBuilder.Entity("Jellyfin.Data.Entities.AccessSchedule", b =>
                 {
@@ -342,7 +342,8 @@ namespace Jellyfin.Server.Implementations.Migrations
                         .ValueGeneratedOnAdd()
                         .HasColumnType("INTEGER");
 
-                    b.Property<Guid>("AccessToken")
+                    b.Property<string>("AccessToken")
+                        .IsRequired()
                         .HasColumnType("TEXT");
 
                     b.Property<DateTime>("DateCreated")
@@ -390,6 +391,9 @@ namespace Jellyfin.Server.Implementations.Migrations
                     b.Property<DateTime>("DateLastActivity")
                         .HasColumnType("TEXT");
 
+                    b.Property<DateTime>("DateModified")
+                        .HasColumnType("TEXT");
+
                     b.Property<string>("DeviceId")
                         .IsRequired()
                         .HasMaxLength(256)

+ 348 - 0
Jellyfin.Server.Implementations/Migrations/20210814002109_AddDevices.cs

@@ -0,0 +1,348 @@
+#pragma warning disable CS1591, SA1601
+
+using System;
+using Microsoft.EntityFrameworkCore.Migrations;
+
+namespace Jellyfin.Server.Implementations.Migrations
+{
+    public partial class AddDevices : Migration
+    {
+        protected override void Up(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.DropForeignKey(
+                name: "FK_ImageInfos_Users_UserId",
+                schema: "jellyfin",
+                table: "ImageInfos");
+
+            migrationBuilder.DropForeignKey(
+                name: "FK_Permissions_Users_Permission_Permissions_Guid",
+                schema: "jellyfin",
+                table: "Permissions");
+
+            migrationBuilder.DropForeignKey(
+                name: "FK_Preferences_Users_Preference_Preferences_Guid",
+                schema: "jellyfin",
+                table: "Preferences");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Preferences_Preference_Preferences_Guid",
+                schema: "jellyfin",
+                table: "Preferences");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Permissions_Permission_Permissions_Guid",
+                schema: "jellyfin",
+                table: "Permissions");
+
+            migrationBuilder.DropIndex(
+                name: "IX_DisplayPreferences_UserId",
+                schema: "jellyfin",
+                table: "DisplayPreferences");
+
+            migrationBuilder.DropIndex(
+                name: "IX_CustomItemDisplayPreferences_UserId",
+                schema: "jellyfin",
+                table: "CustomItemDisplayPreferences");
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Username",
+                schema: "jellyfin",
+                table: "Users",
+                type: "TEXT",
+                maxLength: 255,
+                nullable: false,
+                collation: "NOCASE",
+                oldClrType: typeof(string),
+                oldType: "TEXT",
+                oldMaxLength: 255);
+
+            migrationBuilder.AddColumn<Guid>(
+                name: "UserId",
+                schema: "jellyfin",
+                table: "Preferences",
+                type: "TEXT",
+                nullable: true);
+
+            migrationBuilder.AddColumn<Guid>(
+                name: "UserId",
+                schema: "jellyfin",
+                table: "Permissions",
+                type: "TEXT",
+                nullable: true);
+
+            migrationBuilder.CreateTable(
+                name: "ApiKeys",
+                schema: "jellyfin",
+                columns: table => new
+                {
+                    Id = table.Column<int>(type: "INTEGER", nullable: false)
+                        .Annotation("Sqlite:Autoincrement", true),
+                    DateCreated = table.Column<DateTime>(type: "TEXT", nullable: false),
+                    DateLastActivity = table.Column<DateTime>(type: "TEXT", nullable: false),
+                    Name = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
+                    AccessToken = table.Column<string>(type: "TEXT", nullable: false)
+                },
+                constraints: table =>
+                {
+                    table.PrimaryKey("PK_ApiKeys", x => x.Id);
+                });
+
+            migrationBuilder.CreateTable(
+                name: "DeviceOptions",
+                schema: "jellyfin",
+                columns: table => new
+                {
+                    Id = table.Column<int>(type: "INTEGER", nullable: false)
+                        .Annotation("Sqlite:Autoincrement", true),
+                    DeviceId = table.Column<string>(type: "TEXT", nullable: false),
+                    CustomName = table.Column<string>(type: "TEXT", nullable: true)
+                },
+                constraints: table =>
+                {
+                    table.PrimaryKey("PK_DeviceOptions", x => x.Id);
+                });
+
+            migrationBuilder.CreateTable(
+                name: "Devices",
+                schema: "jellyfin",
+                columns: table => new
+                {
+                    Id = table.Column<int>(type: "INTEGER", nullable: false)
+                        .Annotation("Sqlite:Autoincrement", true),
+                    UserId = table.Column<Guid>(type: "TEXT", nullable: false),
+                    AccessToken = table.Column<string>(type: "TEXT", nullable: false),
+                    AppName = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
+                    AppVersion = table.Column<string>(type: "TEXT", maxLength: 32, nullable: false),
+                    DeviceName = table.Column<string>(type: "TEXT", maxLength: 64, nullable: false),
+                    DeviceId = table.Column<string>(type: "TEXT", maxLength: 256, nullable: false),
+                    IsActive = table.Column<bool>(type: "INTEGER", nullable: false),
+                    DateCreated = table.Column<DateTime>(type: "TEXT", nullable: false),
+                    DateModified = table.Column<DateTime>(type: "TEXT", nullable: false),
+                    DateLastActivity = table.Column<DateTime>(type: "TEXT", nullable: false)
+                },
+                constraints: table =>
+                {
+                    table.PrimaryKey("PK_Devices", x => x.Id);
+                    table.ForeignKey(
+                        name: "FK_Devices_Users_UserId",
+                        column: x => x.UserId,
+                        principalSchema: "jellyfin",
+                        principalTable: "Users",
+                        principalColumn: "Id",
+                        onDelete: ReferentialAction.Cascade);
+                });
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Users_Username",
+                schema: "jellyfin",
+                table: "Users",
+                column: "Username",
+                unique: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Preferences_UserId_Kind",
+                schema: "jellyfin",
+                table: "Preferences",
+                columns: new[] { "UserId", "Kind" },
+                unique: true,
+                filter: "[UserId] IS NOT NULL");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Permissions_UserId_Kind",
+                schema: "jellyfin",
+                table: "Permissions",
+                columns: new[] { "UserId", "Kind" },
+                unique: true,
+                filter: "[UserId] IS NOT NULL");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_ApiKeys_AccessToken",
+                schema: "jellyfin",
+                table: "ApiKeys",
+                column: "AccessToken",
+                unique: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_DeviceOptions_DeviceId",
+                schema: "jellyfin",
+                table: "DeviceOptions",
+                column: "DeviceId",
+                unique: true);
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Devices_AccessToken_DateLastActivity",
+                schema: "jellyfin",
+                table: "Devices",
+                columns: new[] { "AccessToken", "DateLastActivity" });
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Devices_DeviceId",
+                schema: "jellyfin",
+                table: "Devices",
+                column: "DeviceId");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Devices_DeviceId_DateLastActivity",
+                schema: "jellyfin",
+                table: "Devices",
+                columns: new[] { "DeviceId", "DateLastActivity" });
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Devices_UserId_DeviceId",
+                schema: "jellyfin",
+                table: "Devices",
+                columns: new[] { "UserId", "DeviceId" });
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_ImageInfos_Users_UserId",
+                schema: "jellyfin",
+                table: "ImageInfos",
+                column: "UserId",
+                principalSchema: "jellyfin",
+                principalTable: "Users",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Cascade);
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_Permissions_Users_UserId",
+                schema: "jellyfin",
+                table: "Permissions",
+                column: "UserId",
+                principalSchema: "jellyfin",
+                principalTable: "Users",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Cascade);
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_Preferences_Users_UserId",
+                schema: "jellyfin",
+                table: "Preferences",
+                column: "UserId",
+                principalSchema: "jellyfin",
+                principalTable: "Users",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Cascade);
+        }
+
+        protected override void Down(MigrationBuilder migrationBuilder)
+        {
+            migrationBuilder.DropForeignKey(
+                name: "FK_ImageInfos_Users_UserId",
+                schema: "jellyfin",
+                table: "ImageInfos");
+
+            migrationBuilder.DropForeignKey(
+                name: "FK_Permissions_Users_UserId",
+                schema: "jellyfin",
+                table: "Permissions");
+
+            migrationBuilder.DropForeignKey(
+                name: "FK_Preferences_Users_UserId",
+                schema: "jellyfin",
+                table: "Preferences");
+
+            migrationBuilder.DropTable(
+                name: "ApiKeys",
+                schema: "jellyfin");
+
+            migrationBuilder.DropTable(
+                name: "DeviceOptions",
+                schema: "jellyfin");
+
+            migrationBuilder.DropTable(
+                name: "Devices",
+                schema: "jellyfin");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Users_Username",
+                schema: "jellyfin",
+                table: "Users");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Preferences_UserId_Kind",
+                schema: "jellyfin",
+                table: "Preferences");
+
+            migrationBuilder.DropIndex(
+                name: "IX_Permissions_UserId_Kind",
+                schema: "jellyfin",
+                table: "Permissions");
+
+            migrationBuilder.DropColumn(
+                name: "UserId",
+                schema: "jellyfin",
+                table: "Preferences");
+
+            migrationBuilder.DropColumn(
+                name: "UserId",
+                schema: "jellyfin",
+                table: "Permissions");
+
+            migrationBuilder.AlterColumn<string>(
+                name: "Username",
+                schema: "jellyfin",
+                table: "Users",
+                type: "TEXT",
+                maxLength: 255,
+                nullable: false,
+                oldClrType: typeof(string),
+                oldType: "TEXT",
+                oldMaxLength: 255,
+                oldCollation: "NOCASE");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Preferences_Preference_Preferences_Guid",
+                schema: "jellyfin",
+                table: "Preferences",
+                column: "Preference_Preferences_Guid");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_Permissions_Permission_Permissions_Guid",
+                schema: "jellyfin",
+                table: "Permissions",
+                column: "Permission_Permissions_Guid");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_DisplayPreferences_UserId",
+                schema: "jellyfin",
+                table: "DisplayPreferences",
+                column: "UserId");
+
+            migrationBuilder.CreateIndex(
+                name: "IX_CustomItemDisplayPreferences_UserId",
+                schema: "jellyfin",
+                table: "CustomItemDisplayPreferences",
+                column: "UserId");
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_ImageInfos_Users_UserId",
+                schema: "jellyfin",
+                table: "ImageInfos",
+                column: "UserId",
+                principalSchema: "jellyfin",
+                principalTable: "Users",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Restrict);
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_Permissions_Users_Permission_Permissions_Guid",
+                schema: "jellyfin",
+                table: "Permissions",
+                column: "Permission_Permissions_Guid",
+                principalSchema: "jellyfin",
+                principalTable: "Users",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Restrict);
+
+            migrationBuilder.AddForeignKey(
+                name: "FK_Preferences_Users_Preference_Preferences_Guid",
+                schema: "jellyfin",
+                table: "Preferences",
+                column: "Preference_Preferences_Guid",
+                principalSchema: "jellyfin",
+                principalTable: "Users",
+                principalColumn: "Id",
+                onDelete: ReferentialAction.Restrict);
+        }
+    }
+}

+ 6 - 2
Jellyfin.Server.Implementations/Migrations/JellyfinDbModelSnapshot.cs

@@ -15,7 +15,7 @@ namespace Jellyfin.Server.Implementations.Migrations
 #pragma warning disable 612, 618
             modelBuilder
                 .HasDefaultSchema("jellyfin")
-                .HasAnnotation("ProductVersion", "5.0.6");
+                .HasAnnotation("ProductVersion", "5.0.7");
 
             modelBuilder.Entity("Jellyfin.Data.Entities.AccessSchedule", b =>
                 {
@@ -338,7 +338,8 @@ namespace Jellyfin.Server.Implementations.Migrations
                         .ValueGeneratedOnAdd()
                         .HasColumnType("INTEGER");
 
-                    b.Property<Guid>("AccessToken")
+                    b.Property<string>("AccessToken")
+                        .IsRequired()
                         .HasColumnType("TEXT");
 
                     b.Property<DateTime>("DateCreated")
@@ -386,6 +387,9 @@ namespace Jellyfin.Server.Implementations.Migrations
                     b.Property<DateTime>("DateLastActivity")
                         .HasColumnType("TEXT");
 
+                    b.Property<DateTime>("DateModified")
+                        .HasColumnType("TEXT");
+
                     b.Property<string>("DeviceId")
                         .IsRequired()
                         .HasMaxLength(256)