internal DbUpgrade(string connectionString)
 {
     _upgradeEngine = DeployChanges.To
                      .SQLiteDatabase(connectionString)
                      .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
                      .Build();
 }
示例#2
0
        public static int Main(string[] args)
        {
            string connectionString = Configuration["ConnectionStrings:PrimarySchoolDB"];

            EnsureDatabase.For.SqlDatabase(connectionString);

            UpgradeEngine upgrader =
                DeployChanges.To
                .SqlDatabase(connectionString)
                .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
                .LogToConsole()
                .Build();

            DatabaseUpgradeResult result = upgrader.PerformUpgrade();

            if (!result.Successful)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(result.Error);
                Console.ResetColor();

                return(-1);
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Success!");
            Console.ResetColor();
            return(0);
        }
示例#3
0
        private static DatabaseUpgradeResult RunDatabaseUpdate(string connectionString, bool dropDatabase)
        {
            if (dropDatabase)
            {
                DropDatabase.For.PostgresqlDatabase(connectionString);
            }

            EnsureDatabase.For.PostgresqlDatabase(connectionString);

            UpgradeEngine upgradeEngine = DeployChanges
                                          .To
                                          .PostgresqlDatabase(connectionString)
                                          .WithScriptsFromFileSystem
                                          (
                SRC_PATH, new FileSystemScriptOptions
            {
                IncludeSubDirectories = true
            }
                                          )
                                          .WithTransactionPerScript()
                                          .WithVariablesDisabled()
                                          .Build();

            return(upgradeEngine.PerformUpgrade());
        }
示例#4
0
        private static bool FinishUpgrade(UpgradeEngine upgrader, ILog log)
        {
            var toExecute = upgrader.GetScriptsToExecute();

            log.WriteLine("Upgrading File...");
            log.WriteLine($"Applying {toExecute.Count} scripts.");
            var result = upgrader.PerformUpgrade();

            if (!result.Successful)
            {
                log.WriteLine("Upgrage failed!", LogLevel.Error);
                log.WriteLine(result.Error.ToString(), LogLevel.Error);
                return(false);
            }

            log.WriteLine("Upgrade successful!");

            foreach (SqlScript script in result.Scripts)
            {
                log.WriteLine($"Applied Upgrade Script {script.Name}.");
            }

            log.WriteLine();

            return(true);
        }
示例#5
0
        private static void GenerateReport(string destinationDir, UpgradeEngine upgrader)
        {
            var fullReportPath = Path.Combine(destinationDir, "UpgradeReport.html");

            Console.WriteLine($"Generating the report at {fullReportPath}");

            upgrader.GenerateUpgradeHtmlReport(fullReportPath);
        }
示例#6
0
 public static void RunDBMigration()
 {
     UpgradeEngine upgradeEngine = DeployChanges.To
                                   .PostgresqlDatabase(GetConnectionString())
                                   .WithScriptsFromFileSystem("sqlScriptDirectory")
                                   .LogToConsole()
                                   .Build();
 }
示例#7
0
 public virtual void AfterEach()
 {
     ScriptProvider = null;
     VersionTracker = null;
     ScriptExecutor = null;
     Log            = null;
     DbUpgrader     = null;
 }
示例#8
0
        /// <summary>
        /// Performs the upgrade.
        /// </summary>
        /// <param name="upgradeEngine">The upgrade engine.</param>
        /// <param name="sqlVerify">The SQL verify.</param>
        public static void PerformUpgrade(this UpgradeEngine upgradeEngine, ISqlVerify sqlVerify)
        {
            var result = upgradeEngine.PerformUpgrade();

            if (result.Successful)
            {
                sqlVerify.UpdateDb();
            }
        }
示例#9
0
        private void GivenAnUpToDateDatabase()
        {
            var journal = GetJournal();

            journal.StoreExecutedScript(scripts[0]);
            journal.StoreExecutedScript(scripts[1]);
            journal.StoreExecutedScript(scripts[2]);

            upgradeEngine = upgradeEngineBuilder.Build();
        }
示例#10
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Migrator"/> class.
 /// </summary>
 /// <param name="configuration">Application configuration for configuring services.</param>
 /// <param name="assemblies">The assemblies where there are migration scripts to run.</param>
 /// <param name="upgradeLog">The logger instance for DbUp.</param>
 public Migrator(IConfiguration configuration, IEnumerable <Assembly> assemblies, IUpgradeLog upgradeLog)
 {
     this.upgradeEngine = DeployChanges.To.PostgresqlDatabase(configuration.GetConnectionString("MandarinConnection"))
                          .WithScriptsEmbeddedInAssemblies(assemblies.ToArray())
                          .WithTransactionPerScript()
                          .WithScriptNameComparer(new MigrationIdComparer())
                          .WithVariable("FixedCommissionAmountJson", Migrator.GetFixedCommissionAmountJson(configuration))
                          .LogTo(upgradeLog)
                          .Build();
 }
示例#11
0
 public DatabaseManager(string connectionName)
 {
     _connectionString =
         System.Configuration.ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
     _upgradeEngine = DeployChanges.To
                      .SqlDatabase(_connectionString)
                      .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
                      .LogToConsole()
                      .Build();
 }
示例#12
0
        } // Run

        private static void GenerateScriptFile(string destinationDir, UpgradeEngine upgrader)
        {
            // Generate full script to apply
            var scriptPath = Path.Combine(destinationDir, "UpgradeScript.sql");

            Console.WriteLine($"Generating the script {scriptPath}");

            File.WriteAllLines(scriptPath,
                               upgrader.GetScriptsToExecute().Select(x => $"-- ### {x.Name}{Environment.NewLine}{x.Contents}"),
                               Encoding.UTF8);
        }
示例#13
0
        public static string DryRun(this UpgradeEngine upgradeEngine, IDictionary <string, string> variables)
        {
            var buffer           = new StringBuilder($"-- DbUp generated script at {DateTimeOffset.Now}\n");
            var variableReplacer = new VariableSubstitutionPreprocessor(variables);

            upgradeEngine.GetScriptsToExecute().ForEach(s =>
            {
                buffer.AppendLine($"-- Script name: {s.Name}");
                buffer.AppendLine(variableReplacer.Process(s.Contents));
            });
            return(buffer.ToString());
        }
示例#14
0
        /// <summary>
        /// Executes the engine.
        /// </summary>
        /// <param name="engine">
        /// The engine.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        private static bool ExecuteEngine(UpgradeEngine engine)
        {
            var result = engine.PerformUpgrade();

            if (result.Successful == false)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(result.Error);
                Console.ResetColor();
            }

            return(result.Successful);
        }
示例#15
0
        public static DatabaseUpgradeResult DeployTestSeedData(string connectionString)
        {
            EnsureDatabase.For.SqlDatabase(connectionString);
            UpgradeEngine upgrader = DeployChanges.To
                                     .SqlDatabase(connectionString)
                                     .WithTransactionPerScript()
                                     .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly(), (s) => { return(s.Contains("SeedData")); })
                                     .LogToConsole()
                                     .Build();

            DatabaseUpgradeResult result = upgrader.PerformUpgrade();

            return(result);
        }
示例#16
0
文件: Startup.cs 项目: czlatea/formly
        private void RunDbUpdate(string connectionString)
        {
            DropDatabase.For.SqlDatabase(connectionString);
            EnsureDatabase.For.SqlDatabase(connectionString);

            Assembly      dbMigrationAssembly = GetDbMigrationAssembly();
            UpgradeEngine upgradeEngine       = GetUpgradeEngine(dbMigrationAssembly);

            var result = upgradeEngine.PerformUpgrade();

            if (!result.Successful)
            {
                throw new Exception(result.ErrorScript.Contents, result.Error);
            }
        }
示例#17
0
        private static void SetupDatabase()
        {
            EnsureDatabase.For.SqlDatabase(DataContext.GetDatabaseConnectionString());
            UpgradeEngine dbUpgradeEngine = DeployChanges.To.SqlDatabase(DataContext.GetDatabaseConnectionString())
                                            .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
                                            .LogToConsole()
                                            .Build();

            DatabaseUpgradeResult dbUpgradeResult = dbUpgradeEngine.PerformUpgrade();

            if (!dbUpgradeResult.Successful)
            {
                throw new ApplicationException("Database setup failed", dbUpgradeResult.Error);
            }
        }
示例#18
0
        public static void UseMigration(this IApplicationBuilder app, string connectionString)
        {
            DropDatabase.For.SqlDatabase(connectionString);
            EnsureDatabase.For.SqlDatabase(connectionString);

            UpgradeEngine upgrader = DeployChanges.To
                                     .SqlDatabase(connectionString)
                                     .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
                                     .LogToConsole()
                                     .Build();

            DatabaseUpgradeResult result = upgrader.PerformUpgrade();

            ShowMessage(result);
        }
示例#19
0
        public DbUpDatabaseUpgrader(IConnectionStringProvider connectionStringProvider, ILog logger)
        {
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            if (connectionStringProvider == null)
            {
                throw new ArgumentNullException(nameof(connectionStringProvider));
            }

            this.connectionStringProvider = connectionStringProvider;
            this.upgradeEngine            = GetDbUpEngine();
            this.logger = logger;
        }
示例#20
0
        private static int InstallTests(string connectionString)
        {
            UpgradeEngine upgradeEngine = DeployChanges
                                          .To
                                          .SqlDatabase(connectionString)
                                          .WithScriptsFromFileSystem(TESTS_PATH, new FileSystemScriptOptions {
                IncludeSubDirectories = true
            })
                                          .JournalTo(new NullJournal())
                                          .Build();

            DatabaseUpgradeResult databaseUpgradeResult = upgradeEngine.PerformUpgrade();

            return(!databaseUpgradeResult.Successful ? ShowError(databaseUpgradeResult.Error) : ShowSuccess());
        }
示例#21
0
        private void DatabasePrepare(string connectionString, string prov)
        {
            try
            {
                UpgradeEngineBuilder builder = null;
                if (prov == "System.Data.SqlClient")
                {
                    EnsureDatabase.For.SqlDatabase(connectionString, this);
                    builder = DeployChanges.To.SqlDatabase(connectionString).WithTransactionPerScript();
                }
                if (prov == "SQLite")
                {
                    //EnsureDatabase.For gibt es nicht
                    builder = DeployChanges.To.SQLiteDatabase(connectionString)
                              .WithPreprocessor(new SQLiteConverter()).WithTransactionPerScript();
                }
                if (prov == "Npgsql")
                {
                    EnsureDatabase.For.PostgresqlDatabase(connectionString, this);
                    builder = DeployChanges.To.PostgresqlDatabase(connectionString)
                              .WithPreprocessor(new PostgresConverter()).WithTransactionPerScript();
                }


                UpgradeEngine upgrader =
                    builder.WithScriptsEmbeddedInAssembly(System.Reflection.Assembly.GetExecutingAssembly())
                    .LogTo(this)
                    .Build();

                var result = upgrader.PerformUpgrade();

                if (!result.Successful)
                {
                    AddLogText2(result.Error.Message);
                }
                else
                {
                    AddLogText2("Success!");
                }
            }
            catch (Exception ex)
            {
                AddLogText2("Error preparing database");
                AddLogText2(BauerLib.Registry.DbConnection);
                AddLogText2(ex.Message);
            }
        }
示例#22
0
        public static void UpdateDatabase(string connectionString)
        {
            UpgradeEngine runner = DeployChanges
                                   .To
                                   .PostgresqlDatabase(connectionString)
                                   .WithScriptsFromFileSystem("/src/DbScripts")
                                   .WithTransactionPerScript()
                                   .LogToConsole()
                                   .Build();

            DatabaseUpgradeResult result = runner.PerformUpgrade();

            if (!result.Successful)
            {
                throw result.Error;
            }
        }
示例#23
0
        private void Upgrade(UpgradeEngine upgradeEngine)
        {
            var scripts = upgradeEngine.GetScriptsToExecute()
                          .Select(s => s.Name)
                          .ToList();

            _log.Information(
                "Going to migrate the SQL database by executing these scripts: {0}",
                string.Join(", ", scripts));

            var result = upgradeEngine.PerformUpgrade();

            if (!result.Successful)
            {
                throw new SqlMigrationException(scripts, result.Error.Message, result.Error);
            }
        }
示例#24
0
        public static void DbSetup(string connectionString)
        {
            UpgradeEngine upgradeEngine = DeployChanges.To
                                          .SqlDatabase(connectionString)
                                          .WithScriptsEmbeddedInAssembly(typeof(AirportTable).Assembly)
                                          .LogToConsole()
                                          .Build();

            DatabaseUpgradeResult result = upgradeEngine.PerformUpgrade();

            if (!result.Successful)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(result.Error);
                Console.ResetColor();
            }
        }
示例#25
0
        public bool Migrate()
        {
            string connectionString = this.configuration.GetConnectionString(DB_CONNECTION);

            EnsureDatabase.For.SqlDatabase(connectionString);

            UpgradeEngine upgradeEngine =
                DeployChanges.To
                .SqlDatabase(connectionString)
                .WithScriptsEmbeddedInAssembly(Assembly.GetAssembly(GetType()))
                .LogToConsole()
                .Build();

            DatabaseUpgradeResult upgradeResult = upgradeEngine.PerformUpgrade();

            return(upgradeResult.Successful);
        }
示例#26
0
        private static int PerformUpgrade(UpgradeEngine engine)
        {
            var result = engine.PerformUpgrade();

            if (!result.Successful)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(result.Error);
                Console.ResetColor();

                return(-1);
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Success!");
            Console.ResetColor();
            return(0);
        }
        public async Task <bool> Upgrade()
        {
            upgradeEngine = DeployChanges.To
                            .SqlDatabase(ConnectionInfo.ConnectionString)
                            .WithScriptsEmbeddedInAssembly(this.GetType().Assembly)
                            .LogToConsole()
                            .Build();
            EnsureDatabase.For.SqlDatabase(ConnectionInfo.ConnectionString);
            var result = upgradeEngine.PerformUpgrade();

            await CheckAll();

            if (result.Successful == false)
            {
                throw result.Error;
            }
            return(result.Successful);
        }
示例#28
0
        static int Main(string[] args)
        {
            string connectionString = ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;

            UpgradeEngine engine =
                DeployChanges.To
                .SqlDatabase(connectionString)
                .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
                .LogToConsole()
                .Build();

            if (!engine.IsUpgradeRequired())
            {
                Console.WriteLine("No upgrade required, database is already up to date.");
                return(0);
            }

            return(PerformUpgrade(engine));
        }
        public void CheckDatabaseVersion()
        {
            upgradeEngine = DeployChanges.To
                            .SqlDatabase(ConnectionInfo.ConnectionString)
                            .WithScriptsEmbeddedInAssembly(this.GetType().Assembly)
                            .LogToConsole()
                            .Build();

            IsUpgradeRequired = upgradeEngine.IsUpgradeRequired();
            if (IsUpgradeRequired.HasValue && IsUpgradeRequired.Value == false)
            {
                Status = DatabaseHelperStatusEnum.CelsusDatabaseVersionOk;
            }
            if (IsUpgradeRequired.HasValue && IsUpgradeRequired.Value == true)
            {
                Status = DatabaseHelperStatusEnum.CelsusDatabaseVersionOld;
            }
            //var tr=upgradeEngine.GetScriptsToExecute();
        }
示例#30
0
        public virtual void BeforeEach()
        {
            ScriptProvider     = Substitute.For <IScriptProvider> ();
            VersionTracker     = Substitute.For <IJournal> ();
            ScriptExecutor     = Substitute.For <IScriptExecutor> ();
            ScriptPreprocessor = Substitute.For <IScriptPreprocessor>();

            Log = Substitute.For <IUpgradeLog> ();

            var config = new UpgradeConfiguration();

            config.ScriptPreprocessors.Add(ScriptPreprocessor);
            config.ScriptProviders.Add(ScriptProvider);
            config.ScriptExecutor = ScriptExecutor;
            config.Journal        = VersionTracker;
            config.Log            = Log;

            DbUpgrader = new UpgradeEngine(config);
        }