Database migration style now determined by config

This commit is contained in:
Jordan Brown
2018-08-07 13:42:39 -04:00
parent 1d62bb7e21
commit 076b80595b
6 changed files with 48 additions and 22 deletions
-1
View File
@@ -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
@@ -24,5 +24,10 @@
/// The connection string for the database
/// </summary>
public string ConnectionString { get; set; }
/// <summary>
/// If the database should use direct table creation instead of automatic migrations. Should not be used in production!
/// </summary>
public bool NoMigrations { get; set; }
}
}
@@ -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
/// <inheritdoc />
public DbSet<Job> Jobs { get; set; }
/// <inheritdoc />
public DbSet<ReattachInformation> ReattachInformations { get; set; }
/// <inheritdoc />
public DbSet<WatchdogReattachInformation> WatchdogReattachInformations { get; set; }
/// <summary>
/// The <see cref="TestMerge"/>s in the <see cref="DatabaseContext{TParentContext}"/>
/// </summary>
public DbSet<TestMerge> TestMerges { get; set; }
/// <inheritdoc />
public DbSet<ReattachInformation> ReattachInformations { get; set; }
/// <summary>
/// The <see cref="RevInfoTestMerge"/>s om the <see cref="DatabaseContext{TParentContext}"/>
/// </summary>
public DbSet<RevInfoTestMerge> RevInfoTestMerges { get; set; }
/// <inheritdoc />
public DbSet<WatchdogReattachInformation> WatchdogReattachInformations { get; set; }
/// <summary>
/// The <see cref="ILogger"/> for the <see cref="DatabaseContext{TParentContext}"/>
/// </summary>
@@ -77,6 +75,7 @@ namespace Tgstation.Server.Host.Models
/// The <see cref="DatabaseConfiguration"/> for the <see cref="DatabaseContext{TParentContext}"/>
/// </summary>
readonly DatabaseConfiguration databaseConfiguration;
/// <summary>
/// The <see cref="IDatabaseSeeder"/> for the <see cref="DatabaseContext{TParentContext}"/>
/// </summary>
@@ -99,7 +98,7 @@ namespace Tgstation.Server.Host.Models
/// <inheritdoc />
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<User>();
@@ -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);
}
}
}
@@ -54,6 +54,9 @@
</ItemGroup>
<ItemGroup>
<None Update="appsettings.Development.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
@@ -0,0 +1,8 @@
{
"General": {
"DisableFileLogging": true
},
"Database": {
"NoMigrations": true
}
}
@@ -29,6 +29,7 @@
"UpdatePackageAssetName": "ServerUpdatePackage.zip"
},
"Database": {
"NoMigrations": false,
"DatabaseType": "SqlServer",
"ResetAdminPassword": false,
"ConnectionString": "Data Source=(local);Initial Catalog=TGS;Integrated Security=True"