1 using Microsoft.EntityFrameworkCore;
2 using Microsoft.Extensions.Logging;
3 using Microsoft.Extensions.Options;
7 using System.Threading.Tasks;
13 using Z.EntityFramework.Plus;
38 readonly ILogger<DatabaseSeeder>
logger;
62 IOptions<GeneralConfiguration> generalConfigurationOptions,
63 IOptions<DatabaseConfiguration> databaseConfigurationOptions,
64 ILogger<DatabaseContext> databaseLogger,
65 ILogger<DatabaseSeeder> logger)
67 this.cryptographySuite = cryptographySuite ??
throw new ArgumentNullException(nameof(cryptographySuite));
68 this.platformIdentifier = platformIdentifier ??
throw new ArgumentNullException(nameof(platformIdentifier));
69 databaseConfiguration = databaseConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(databaseConfigurationOptions));
70 generalConfiguration = generalConfigurationOptions?.Value ??
throw new ArgumentNullException(nameof(generalConfigurationOptions));
71 this.databaseLogger = databaseLogger ??
throw new ArgumentNullException(nameof(databaseLogger));
72 this.logger = logger ??
throw new ArgumentNullException(nameof(logger));
84 CreatedAt = DateTimeOffset.Now,
86 Name = Api.Models.User.AdminName,
90 cryptographySuite.SetUserPassword(admin, Api.Models.User.DefaultAdminPassword,
true);
102 SeedAdminUser(databaseContext);
103 await databaseContext.
Save(cancellationToken).ConfigureAwait(
false);
114 var admin = await GetAdminUser(databaseContext, cancellationToken).ConfigureAwait(
false);
124 if (platformIdentifier.IsWindows)
127 var allInstances = await databaseContext
130 .ToListAsync(cancellationToken)
131 .ConfigureAwait(
false);
132 foreach (var instance
in allInstances)
133 instance.Path = instance.Path.Replace(
'\\',
'/');
136 var ids = await databaseContext
139 .Where(x => x.TopicRequestTimeout == 0)
141 .ToListAsync(cancellationToken)
142 .ConfigureAwait(
false);
144 var rowsUpdated = ids.Count;
145 foreach (var
id in ids)
152 databaseContext.DreamDaemonSettings.Attach(newDDSettings);
157 logger.LogInformation(
158 "Updated {0} instances to use database backed BYOND topic timeouts from configuration setting of {1}",
160 generalConfiguration.ByondTopicTimeout);
162 await databaseContext.Save(cancellationToken).ConfigureAwait(
false);
173 var admin = await GetAdminUser(databaseContext, cancellationToken).ConfigureAwait(
false);
176 admin.Enabled =
true;
177 cryptographySuite.SetUserPassword(admin, Api.Models.User.DefaultAdminPassword,
false);
180 await databaseContext.
Save(cancellationToken).ConfigureAwait(
false);
191 var admin = await databaseContext
195 .FirstOrDefaultAsync(cancellationToken)
196 .ConfigureAwait(
false);
197 if (admin ==
default)
198 SeedAdminUser(databaseContext);
206 if (databaseContext == null)
207 throw new ArgumentNullException(nameof(databaseContext));
209 if (databaseConfiguration.DropDatabase)
211 logger.LogCritical(
"DropDatabase configuration option set! Dropping any existing database...");
212 await databaseContext.
Drop(cancellationToken).ConfigureAwait(
false);
215 var wasEmpty = await databaseContext.
Migrate(databaseLogger, cancellationToken).ConfigureAwait(
false);
218 logger.LogInformation(
"Seeding database...");
219 await SeedDatabase(databaseContext, cancellationToken).ConfigureAwait(
false);
223 if (databaseConfiguration.ResetAdminPassword)
225 logger.LogWarning(
"Enabling and resetting admin password due to configuration!");
226 await ResetAdminPassword(databaseContext, cancellationToken).ConfigureAwait(
false);
229 await SanitizeDatabase(databaseContext, cancellationToken).ConfigureAwait(
false);
236 if (databaseContext == null)
237 throw new ArgumentNullException(nameof(databaseContext));
238 if (downgradeVersion == null)
239 throw new ArgumentNullException(nameof(downgradeVersion));
Task SchemaDowngradeForServerVersion(ILogger< DatabaseContext > logger, Version version, DatabaseType currentDatabaseType, CancellationToken cancellationToken)
Attempt to downgrade the schema to the migration used for a given server version ...
readonly GeneralConfiguration generalConfiguration
The GeneralConfiguration for the DatabaseSeeder.
Task Drop(CancellationToken cancellationToken)
Attempts to delete all tables and drop the database in use.
readonly ICryptographySuite cryptographySuite
The ICryptographySuite for the DatabaseSeeder
Use server authentication
DatabaseSeeder(ICryptographySuite cryptographySuite, IPlatformIdentifier platformIdentifier, IOptions< GeneralConfiguration > generalConfigurationOptions, IOptions< DatabaseConfiguration > databaseConfigurationOptions, ILogger< DatabaseContext > databaseLogger, ILogger< DatabaseSeeder > logger)
Construct a DatabaseSeeder
For initially setting up a database.
async Task SanitizeDatabase(IDatabaseContext databaseContext, CancellationToken cancellationToken)
Correct invalid database data caused by previous versions.
void Add(TModel model)
Add a given model to the the working set.
async Task SeedDatabase(IDatabaseContext databaseContext, CancellationToken cancellationToken)
Initially seed a given databaseContext
AdministrationRights
Rights for Models.Administration
uint TopicRequestTimeout
The timeout for sending and receiving BYOND topics in milliseconds.
Contains various cryptographic functions
async Task Initialize(IDatabaseContext databaseContext, CancellationToken cancellationToken)
Setup up a given databaseContext .
IDatabaseCollection< Instance > Instances
The Instances in the IDatabaseContext
async Task ResetAdminPassword(IDatabaseContext databaseContext, CancellationToken cancellationToken)
Changes the admin password in IDatabaseContext back to it's default and enables the account ...
InstanceManagerRights
Rights for managing Models.Instances
static string CanonicalizeName(string name)
Change a Api.Models.Internal.User.Name into a CanonicalName.
readonly ILogger< DatabaseContext > databaseLogger
The ILogger used for IDatabaseContexts.
readonly DatabaseConfiguration databaseConfiguration
The DatabaseConfiguration for the DatabaseSeeder.
readonly IPlatformIdentifier platformIdentifier
The IPlatformIdentifier for the DatabaseSeeder.
Configuration options for the Database.DatabaseContext
General configuration options
Task< bool > Migrate(ILogger< DatabaseContext > logger, CancellationToken cancellationToken)
Creates and migrates the IDatabaseContext
readonly ILogger< DatabaseSeeder > logger
The ILogger for the DatabaseSeeder.
Task Downgrade(IDatabaseContext databaseContext, Version downgradeVersion, CancellationToken cancellationToken)
Migrate a given databaseContext down.
Task Save(CancellationToken cancellationToken)
Saves changes made to the IDatabaseContext
async Task< User > GetAdminUser(IDatabaseContext databaseContext, CancellationToken cancellationToken)
Get or create the admin User.
IDatabaseCollection< DreamDaemonSettings > DreamDaemonSettings
The Models.DreamDaemonSettings in the IDatabaseContext
void SeedAdminUser(IDatabaseContext databaseContext)
Add a default admin User to a given databaseContext
IDatabaseCollection< User > Users
The Users in the IDatabaseContext