Fix Sqlite migration generation

This commit is contained in:
Jordan Brown
2020-05-10 21:23:51 -04:00
parent e8b2cdff78
commit a6bbb13b2f
3 changed files with 21 additions and 13 deletions
@@ -34,6 +34,11 @@ namespace Tgstation.Server.Host.Configuration
/// </summary>
public bool DropDatabase { get; set; }
/// <summary>
/// Used to indicate that the database is being loaded to generate migrations. Should not be used in production!
/// </summary>
public bool DesignTime { get; set; }
/// <summary>
/// The <see cref="string"/> form of the <see cref="global::System.Version"/> of a target MySQL/MariaDB server
/// </summary>
@@ -34,7 +34,9 @@ namespace Tgstation.Server.Host.Database.Design
builder.AddJsonFile(RootJson);
builder.AddJsonFile(DevJson);
var configuration = builder.Build();
return Options.Create(configuration.GetSection(DatabaseConfiguration.Section).Get<DatabaseConfiguration>());
var dbConfig = configuration.GetSection(DatabaseConfiguration.Section).Get<DatabaseConfiguration>();
dbConfig.DesignTime = true;
return Options.Create(dbConfig);
}
}
}
@@ -43,18 +43,19 @@ namespace Tgstation.Server.Host.Database
// use the DateTimeOffsetToBinaryConverter
// Based on: https://github.com/aspnet/EntityFrameworkCore/issues/10784#issuecomment-415769754
// This only supports millisecond precision, but should be sufficient for most use cases.
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var properties = entityType
.ClrType
.GetProperties()
.Where(p => p.PropertyType == typeof(DateTimeOffset) || p.PropertyType == typeof(DateTimeOffset?));
foreach (var property in properties)
modelBuilder
.Entity(entityType.Name)
.Property(property.Name)
.HasConversion(new DateTimeOffsetToBinaryConverter());
}
if (!DatabaseConfiguration.DesignTime)
foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
var properties = entityType
.ClrType
.GetProperties()
.Where(p => p.PropertyType == typeof(DateTimeOffset) || p.PropertyType == typeof(DateTimeOffset?));
foreach (var property in properties)
modelBuilder
.Entity(entityType.Name)
.Property(property.Name)
.HasConversion(new DateTimeOffsetToBinaryConverter());
}
}
/// <inheritdoc />