Exemple #1
0
        public static void BaselineDatabase(string host, string database, string username, string password, string scriptRoot)
        {
            using (var connection = DatabaseConnector.Connect(host, database, username, password, false))
            {
                var scripts = DatabaseStatus.GetPendingScripts(connection, scriptRoot);

                Log.Information("Found {Count} script files without change log records", scripts.Length);

                if (scripts.Length != 0)
                {
                    var commandText = new StringBuilder();
                    commandText.AppendLine("START TRANSACTION ISOLATION LEVEL REPEATABLE READ;");
                    commandText.AppendLine(AppliedChangeScriptLog.ChangesTableCreateScript);

                    foreach (var script in scripts)
                    {
                        Log.Information("Recording {FullPath} as {ScriptFile}", script.FullPath, script.RelativeName);
                        commandText.AppendLine(AppliedChangeScriptLog.CreateApplyLogScriptFor(script));
                    }

                    commandText.AppendLine("COMMIT TRANSACTION;");

                    Log.Information("Writing change log");
                    using (var command = new NpgsqlCommand(commandText.ToString(), connection))
                    {
                        command.ExecuteNonQuery();
                    }
                }

                Log.Information("Done");
            }
        }
Exemple #2
0
        public static void ApplyChangeScripts(string host, string database, string username, string password,
                                              bool createIfMissing, string scriptRoot, IReadOnlyDictionary <string, string> variables)
        {
            using (var connection = DatabaseConnector.Connect(host, database, username, password, createIfMissing))
            {
                var scripts = DatabaseStatus.GetPendingScripts(connection, scriptRoot);

                Log.Information("Found {Count} new script files to apply", scripts.Length);

                if (scripts.Length != 0)
                {
                    Log.Information("Ensuring the change log table exists");
                    using (var command = new NpgsqlCommand(AppliedChangeScriptLog.ChangesTableCreateScript, connection))
                        command.ExecuteNonQuery();
                }

                foreach (var script in scripts)
                {
                    Log.Information("Applying {FullPath} as {ScriptFile}", script.FullPath, script.RelativeName);
                    ApplyChangeScript(connection, script, variables);
                }

                Log.Information("Done");
            }
        }
Exemple #3
0
        protected override int Run()
        {
            _loggingFeature.Configure();

            try
            {
                using (var connection = DatabaseConnector.Connect(_databaseFeature.Host, _databaseFeature.Database,
                                                                  _usernamePasswordFeature.Username, _usernamePasswordFeature.Password, false))
                {
                    foreach (var pending in DatabaseStatus.GetPendingScripts(connection, _scriptRootFeature.ScriptRoot))
                    {
                        Console.WriteLine($"{pending.RelativeName}");
                    }
                }

                return(0);
            }
            catch (Exception ex)
            {
                Log.Fatal(ex, "Could not determine pending change scripts");
                return(-1);
            }
        }