public void Execute(Dictionary <string, string> input) { if (!input.Any(x => x.Key == PARAMKEYS_CREATE)) { return; } connectionString = DatabaseInstaller.GetSetting(input, DatabaseInstaller.PARAMKEYS_APPDB, string.Empty); Log.Information($"connectionstring censored: {CensorConnectionString(connectionString)}"); masterConnectionString = DatabaseInstaller.GetSetting(input, PARAMKEYS_MASTERDB, string.Empty); Log.Information($"masterconnectionstring censored: {CensorConnectionString(masterConnectionString)}"); newDatabaseName = DatabaseInstaller.GetSetting(input, PARAMKEYS_NEWNAME, string.Empty); Log.Information($"newDatabaseName: {newDatabaseName}"); if (DatabaseServer.TestConnectionString(connectionString)) { throw new Exception("The connection string references an existing database."); } if (string.IsNullOrEmpty(newDatabaseName)) { throw new Exception("A new database name was not specified."); } var builder = new Npgsql.NpgsqlConnectionStringBuilder(connectionString); if (builder.Database.ToLower() != newDatabaseName.ToLower()) { throw new Exception("A new database name does not match the specified connection string."); } CreateDatabase(); }
void IDatabaseAction.Execute(Dictionary <string, string> input) { if (input.Any(x => x.Key == PARAMKEYS_CREATE)) { masterConnectionString = DatabaseInstaller.GetSetting(input, PARAMKEYS_MASTERDB, string.Empty); if (!MasterDbAvailable()) { throw new Exception("Master Db Not Available"); } } else { connectionString = DatabaseInstaller.GetSetting(input, DatabaseInstaller.PARAMKEYS_APPDB, string.Empty); if (!DbAvailable()) { throw new Exception("Database Not Available"); } } }
static void Main(string[] args) { /* * Example Connection String * Server=localhost;Database=MyDatabase;UID=postgres;PWD=postgres; * * Example command line to update a database * --update=true --connectionstring="Server=localhost;Database=MyDatabase;UID=postgres;PWD=postgres;" * * NOTE: To run this installer from Visual Studio, add one of the lines above to * this project's properties sheet, Debug tab, Application Arguments * */ IConfiguration Configuration = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddJsonFile("appsettings.development.json", optional: true, reloadOnChange: true) .AddCommandLine(args) .AddEnvironmentVariables() .Build(); var allValues = Configuration.GetChildren().Select(x => new { x.Key, x.Value }).ToDictionary(x => x.Key.ToString(), x => x.Value?.ToString()); Log.Logger = new LoggerConfiguration() .ReadFrom.Configuration(Configuration) .CreateLogger(); Log.Information("Starting Install..."); try { var installer = new DatabaseInstaller(); installer.Install(allValues); } catch (Exception ex) { Log.Fatal(ex, "Install Exception"); } Log.Information("Install Complete"); }