【问题标题】:Force DBUP to rerun new scripts during development强制 DBUP 在开发期间重新运行新脚本
【发布时间】:2019-06-23 21:12:56
【问题描述】:

我们使用 DBUP 来处理数据库迁移。每个版本,我们希望使用命令行开关运行 dbup 控制台应用程序,以便在开发期间我们可以在处理脚本时重新运行我们的脚本,但是我们不希望它重新运行所有以前的发布已经出现在数据库中的脚本。如何实现?

【问题讨论】:

    标签: c# sql continuous-integration continuous-deployment dbup


    【解决方案1】:

    我们向 DbUp 控制台应用程序添加了一个“-debug”命令行开关。如果存在,我们在与数据库交谈时切换使用哪个 Journal 类。

    DbUp 中的 Journal 类 (https://dbup.readthedocs.io/en/latest/more-info/journaling/) 是与数据库交互以检查和记录哪些脚本已经运行(默认存储在 Schema Versions 表中)的类。对于 Dev,我们强制它使用它的只读版本,它可以检查哪些脚本已经存在(以防止您每次重新运行 everything)但防止记录新记录,所以下次它会再次尝试重新运行您的新脚本。

    只读日记是这样的;

    public class ReadOnlyJournal : IJournal
    {
    
        private readonly IJournal _innerJournal;
    
        public ReadOnlyJournal(IJournal innerJournal)
        {
            _innerJournal = innerJournal;
        }
    
        public void EnsureTableExistsAndIsLatestVersion(Func<IDbCommand> dbCommandFactory)
        {
            _innerJournal.EnsureTableExistsAndIsLatestVersion(dbCommandFactory);
        }
    
        public string[] GetExecutedScripts()
        {
            return _innerJournal.GetExecutedScripts().ToArray();
        }
    
        public void StoreExecutedScript(SqlScript script, Func<IDbCommand> dbCommandFactory)
        {
            // don't store anything
        }
    }
    

    然后是一个扩展方法,允许更容易地指定这个新期刊的使用;

    public static class DbUpHelper
    {
        public static UpgradeEngineBuilder WithReadOnlyJournal(this UpgradeEngineBuilder builder, string schema, string table)
        {
            builder.Configure(c => c.Journal = new ReadOnlyJournal(new SqlTableJournal(() => c.ConnectionManager, () => c.Log, schema, table)));
            return builder;
        }
    }
    

    最后是对 DbUp 控制台应用程序的更改;

    var upgrader = debug 
                ? DeployChanges.To
                    .SqlDatabase(connectionString)
                    .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
                    .WithReadOnlyJournal("dbo", "SchemaVersions")
                    .LogToConsole()
                    .Build()
                : DeployChanges.To
                    .SqlDatabase(connectionString)
                    .WithScriptsEmbeddedInAssembly(Assembly.GetExecutingAssembly())
                    .LogToConsole()
                    .Build();
    
    var result = upgrader.PerformUpgrade();
    
            if (!result.Successful)
            ....
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-16
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-05
      • 2011-04-04
      相关资源
      最近更新 更多