From 076b80595baab60bb3665ce4fccb30b369399d3c Mon Sep 17 00:00:00 2001 From: Jordan Brown Date: Tue, 7 Aug 2018 13:42:39 -0400 Subject: [PATCH] Database migration style now determined by config --- .gitignore | 1 - .../Configuration/DatabaseConfiguration.cs | 5 ++ .../Models/DatabaseContext.cs | 52 +++++++++++-------- .../Tgstation.Server.Host.csproj | 3 ++ .../appsettings.Development.json | 8 +++ src/Tgstation.Server.Host/appsettings.json | 1 + 6 files changed, 48 insertions(+), 22 deletions(-) create mode 100644 src/Tgstation.Server.Host/appsettings.Development.json diff --git a/.gitignore b/.gitignore index 5074f2b86e..a8f3e2c779 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,6 @@ artifacts/ *DS_Store *.sln.ide /TestResults -/src/Tgstation.Server.Host/appsettings.Development.json /tests/DMAPI/travistester.lk /tests/DMAPI/travistester.int /tests/DMAPI/travistester.dmb diff --git a/src/Tgstation.Server.Host/Configuration/DatabaseConfiguration.cs b/src/Tgstation.Server.Host/Configuration/DatabaseConfiguration.cs index a75d463a48..07f8690601 100644 --- a/src/Tgstation.Server.Host/Configuration/DatabaseConfiguration.cs +++ b/src/Tgstation.Server.Host/Configuration/DatabaseConfiguration.cs @@ -24,5 +24,10 @@ /// The connection string for the database /// public string ConnectionString { get; set; } + + /// + /// If the database should use direct table creation instead of automatic migrations. Should not be used in production! + /// + public bool NoMigrations { get; set; } } } diff --git a/src/Tgstation.Server.Host/Models/DatabaseContext.cs b/src/Tgstation.Server.Host/Models/DatabaseContext.cs index 9165ab4f15..67d44688a1 100644 --- a/src/Tgstation.Server.Host/Models/DatabaseContext.cs +++ b/src/Tgstation.Server.Host/Models/DatabaseContext.cs @@ -2,9 +2,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using System; -#if !DEBUG using System.Linq; -#endif using System.Threading; using System.Threading.Tasks; using Tgstation.Server.Host.Configuration; @@ -47,22 +45,22 @@ namespace Tgstation.Server.Host.Models /// public DbSet Jobs { get; set; } + /// + public DbSet ReattachInformations { get; set; } + + /// + public DbSet WatchdogReattachInformations { get; set; } + /// /// The s in the /// public DbSet TestMerges { get; set; } - /// - public DbSet ReattachInformations { get; set; } - /// /// The s om the /// public DbSet RevInfoTestMerges { get; set; } - /// - public DbSet WatchdogReattachInformations { get; set; } - /// /// The for the /// @@ -77,6 +75,7 @@ namespace Tgstation.Server.Host.Models /// The for the /// readonly DatabaseConfiguration databaseConfiguration; + /// /// The for the /// @@ -99,7 +98,7 @@ namespace Tgstation.Server.Host.Models /// protected override void OnModelCreating(ModelBuilder modelBuilder) { - Logger.LogDebug("Building entity framework context..."); + Logger.LogTrace("Building entity framework context..."); base.OnModelCreating(modelBuilder); var userModel = modelBuilder.Entity(); @@ -138,24 +137,35 @@ namespace Tgstation.Server.Host.Models public async Task Initialize(CancellationToken cancellationToken) { Logger.LogInformation("Migrating database..."); -#if DEBUG - Logger.LogWarning("Running in debug mode. Using all or nothing strategy!"); - await Database.EnsureCreatedAsync().ConfigureAwait(false); - var wasEmpty = (await Users.CountAsync().ConfigureAwait(false)) == 0; -#else - var migrations = await Database.GetAppliedMigrationsAsync().ConfigureAwait(false); - var wasEmpty = !migrations.Any(); - await Database.MigrateAsync(cancellationToken).ConfigureAwait(false); -#endif + + var wasEmpty = false; + if (!databaseConfiguration.NoMigrations) + { + Logger.LogWarning("Running in debug mode. Using all or nothing migration strategy!"); + await Database.EnsureCreatedAsync(cancellationToken).ConfigureAwait(false); + } + else + { + var migrations = await Database.GetAppliedMigrationsAsync(cancellationToken).ConfigureAwait(false); + wasEmpty = !migrations.Any(); + await Database.MigrateAsync(cancellationToken).ConfigureAwait(false); + } + + wasEmpty |= (await Users.CountAsync(cancellationToken).ConfigureAwait(false)) == 0; + if (wasEmpty) { Logger.LogInformation("Seeding database..."); await databaseSeeder.SeedDatabase(this, cancellationToken).ConfigureAwait(false); } - else if (databaseConfiguration.ResetAdminPassword) + else { - Logger.LogWarning("Enabling and resetting admin password due to configuration!"); - await databaseSeeder.ResetAdminPassword(this, cancellationToken).ConfigureAwait(false); + Logger.LogDebug("No migrations applied!"); + if (databaseConfiguration.ResetAdminPassword) + { + Logger.LogWarning("Enabling and resetting admin password due to configuration!"); + await databaseSeeder.ResetAdminPassword(this, cancellationToken).ConfigureAwait(false); + } } } diff --git a/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj b/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj index 575d65fa34..a68eac4eb5 100644 --- a/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj +++ b/src/Tgstation.Server.Host/Tgstation.Server.Host.csproj @@ -54,6 +54,9 @@ + + PreserveNewest + PreserveNewest diff --git a/src/Tgstation.Server.Host/appsettings.Development.json b/src/Tgstation.Server.Host/appsettings.Development.json new file mode 100644 index 0000000000..d40e70e835 --- /dev/null +++ b/src/Tgstation.Server.Host/appsettings.Development.json @@ -0,0 +1,8 @@ +{ + "General": { + "DisableFileLogging": true + }, + "Database": { + "NoMigrations": true + } +} diff --git a/src/Tgstation.Server.Host/appsettings.json b/src/Tgstation.Server.Host/appsettings.json index f97d9bf4e7..1c91417297 100644 --- a/src/Tgstation.Server.Host/appsettings.json +++ b/src/Tgstation.Server.Host/appsettings.json @@ -29,6 +29,7 @@ "UpdatePackageAssetName": "ServerUpdatePackage.zip" }, "Database": { + "NoMigrations": false, "DatabaseType": "SqlServer", "ResetAdminPassword": false, "ConnectionString": "Data Source=(local);Initial Catalog=TGS;Integrated Security=True"