private void ResetDatabase() { if (_manager.Exists(DB)) { _manager.ClearDatabase(DB); } else { _manager.CreateDatabase(DB, EfHelper.GetSchema(), null); } }
public Task Run() { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("SEED DATABASE\n"); Console.ResetColor(); Console.WriteLine( $"Input: {_database}\n" + $"Count: {_count}\n" + $"Entities: {_entities ?? "all"}"); // create database if not exists string connection = string.Format(CultureInfo.InvariantCulture, _config.GetConnectionString("Default"), _database); MySqlDbManager manager = new MySqlDbManager(connection); if (!manager.Exists(_database)) { Console.WriteLine("Creating database..."); Serilog.Log.Information($"Creating database {_database}..."); manager.CreateDatabase(_database, EfHelper.GetSchema(), null); Console.WriteLine("Database created."); Serilog.Log.Information("Database created."); } Console.Write("Seeding..."); IBiblioRepository repository = new EfBiblioRepository(connection, "mysql"); BiblioSeeder seeder = new BiblioSeeder(repository); seeder.Seed(_count, _entities); Console.WriteLine(" completed"); return(Task.CompletedTask); }
private static async Task CreateBiblioAsync(IServiceProvider serviceProvider) { await Policy.Handle <DbException>() .WaitAndRetry(new[] { TimeSpan.FromSeconds(10), TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(60) }, (exception, timeSpan, _) => { // in case of DbException we must retry ILogger logger = serviceProvider .GetService <ILoggerFactory>() .CreateLogger(typeof(BiblioHostSeedExtensions)); string message = "Unable to connect to DB" + $" (sleep {timeSpan}): {exception.Message}"; Console.WriteLine(message); logger.LogError(exception, message); }).Execute(async() => { IConfiguration config = serviceProvider.GetService <IConfiguration>(); ILogger logger = serviceProvider .GetService <ILoggerFactory>() .CreateLogger(typeof(BiblioHostSeedExtensions)); // delay if requested, to allow DB start int delay = config.GetValue <int>("Seed:BiblioDelay"); if (delay > 0) { Console.WriteLine($"Waiting for {delay} seconds..."); Thread.Sleep(delay * 1000); } else { Console.WriteLine("No delay for seeding"); } // if the DB does not exist, create and seed it string dbName = config.GetValue <string>("DatabaseNames:Biblio"); string cs = string.Format( CultureInfo.InvariantCulture, config.GetConnectionString("Biblio"), dbName); Console.WriteLine($"Checking for database {dbName}..."); Serilog.Log.Information($"Checking for database {dbName}..."); MySqlDbManager manager = new MySqlDbManager(cs); if (!manager.Exists(dbName)) { // create Console.WriteLine("Creating database..."); Serilog.Log.Information($"Creating database {dbName}..."); manager.CreateDatabase(dbName, EfHelper.GetSchema(), null); Console.WriteLine("Database created."); Serilog.Log.Information("Database created."); // seed Console.WriteLine("Seeding database..."); Serilog.Log.Information("Seeding database..."); await SeedBiblioAsync(serviceProvider); Console.WriteLine("Seeding completed."); Serilog.Log.Information("Seeding completed."); } }); }