public static bool Initialize(IConfiguration configuration) { var connections = new Dictionary <string, ConnectionData>(); var passwords = GetPasswordDict(configuration); Console.WriteLine(); var children = configuration.GetSection("ConnectionStrings").GetChildren().ToList(); var count = children.Count; int current = 1; foreach (var section in children) { Console.Write("Initializing connections "); Console.ForegroundColor = ConsoleColor.Yellow; Console.Write($"{current++} of {count}"); Console.Write(": "); Console.ForegroundColor = ConsoleColor.Cyan; Console.Write($"{section.Key}"); Console.ResetColor(); var(connection, connectionString) = TryCreateConnection(section, passwords); if (connection == null) { continue; } var migrations = new MigrationRunner(connection, Program.Settings); try { connection.Open(); Console.ResetColor(); Console.Write(" ... "); Console.ForegroundColor = ConsoleColor.Green; int availableVersion; int?schemaVersion; (schemaVersion, availableVersion) = migrations.GetSchemaVersions(); var serverVersion = connection.ServerVersion; if (Convert.ToDecimal(serverVersion) < Convert.ToDecimal(Program.Settings.MinimalPgVersion)) { throw new Exception($"PostgreSQL connection name '{section.Key}' is version {serverVersion} that is not supported. Lowest supported version is {Program.Settings.MinimalPgVersion}"); } Console.Write("success, server version {0}", serverVersion); if (schemaVersion == availableVersion) { Console.WriteLine(", schema version {0}", schemaVersion); } else { Console.WriteLine(); Console.ForegroundColor = ConsoleColor.Red; if (schemaVersion == null) { Console.WriteLine( "No migration applied for connection {0}, latest available is {1}. Upgrading migrations now.", section.Key, availableVersion); Console.WriteLine(); RunMigrations(configuration, runner => runner.Up(null), section.Key); } else { if (schemaVersion > availableVersion) { Console.WriteLine("Connection {0}. appears to have to many migrations.", section.Key); Console.WriteLine( "Current schema version is {0}, latest available is {1}. Downgrading migrations now.", schemaVersion, availableVersion); Console.WriteLine(); RunMigrations(configuration, runner => runner.Down(null), section.Key); } else { Console.WriteLine("Some migrations appear to be missing for connection {0}.", section.Key); Console.WriteLine( "Current schema version is {0}, latest available is {1}. Downgrading migrations now.", schemaVersion, availableVersion); Console.WriteLine(); RunMigrations(configuration, runner => runner.Up(null), section.Key); } schemaVersion = availableVersion; } } Console.ResetColor(); connections.Add(section.Key, new ConnectionData { Connection = connection, ConnectionString = connectionString, Name = section.Key, SchemaVersion = schemaVersion, ServerVersion = serverVersion }); } catch (Exception e) { Console.ResetColor(); Console.Write(" ... "); Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("failed: {0}", e.Message); Console.ResetColor(); } finally { connection.Close(); } } Console.WriteLine(); if (connections.Keys.Count == 0) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine("Error: no available connections, exiting..."); Console.ResetColor(); return(false); } Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine("Total {0} connection(s) available: {1}", connections.Keys.Count, string.Join(", ", connections.Keys)); Console.WriteLine(); Console.ResetColor(); _connections = connections.ToImmutableDictionary(); return(true); }