【问题标题】:Orchard CMS- Get the current Data Migration Record version numberOrchard CMS-获取当前数据迁移记录版本号
【发布时间】:2013-02-22 03:12:55
【问题描述】:

给定 Migrations 类的名称作为字符串,我如何获取存储在 Orchard_Framework_DataMigrationRecord 中的当前版本号?

我可以在 IExtensionManager 中看到 Version,但这似乎只是 module.txt 中定义的模块版本。

【问题讨论】:

  • 你需要这个做什么?
  • 好问题 - 我正在用几个助手扩展命令行工具;其中之一是用于生成类、方法和文件以创建新小部件的命令,类似于 codegen 模块。我需要知道模块在哪个迁移版本上,这样我才能创建 UpdateFromX() 方法(其中 X 是当前版本 + 1)

标签: orchardcms


【解决方案1】:

好的,我自己解决了这个问题-

我知道 Orchard 在触发迁移方法时一定已经在执行与我需要的代码类似的代码,因此我创建了一个新的迁移文件,并在 Create() 方法上放置了一个断点。当断点命中时,我通过调用堆栈查找 Orchard.Data.Migration 中的 DataMigrationManager。我需要的一切都在那里,如果其他人有类似的要求,我建议他们以该课程为起点。

这几乎是直接从那个类中提升出来的:

        string moduleName="Your.Module.Name";
        var migrations = GetDataMigrations(moduleName);

        // apply update methods to each migration class for the module
        var current = 0;
        foreach (var migration in migrations)
        {
            // copy the objet for the Linq query
            var tempMigration = migration;

            // get current version for this migration
            var dataMigrationRecord = GetDataMigrationRecord(tempMigration);

            if (dataMigrationRecord != null)
            {
                current = dataMigrationRecord.Version.Value;
            }

            // do we need to call Create() ?
            if (current == 0)
            {
                // try to resolve a Create method

                var createMethod = GetCreateMethod(migration);
                if (createMethod != null)
                {
                    //create method has been written, but not executed!
                    current = (int)createMethod.Invoke(migration, new object[0]);
                }
            }
        }
        Context.Output.WriteLine("Version: {0}", current);

您可能需要的几种方法:

    private DataMigrationRecord GetDataMigrationRecord(IDataMigration tempMigration)
    {
        return _dataMigrationRepository.Table
            .Where(dm => dm.DataMigrationClass == tempMigration.GetType().FullName)
            .FirstOrDefault();
    }

    private static MethodInfo GetCreateMethod(IDataMigration dataMigration)
    {
        var methodInfo = dataMigration.GetType().GetMethod("Create", BindingFlags.Public | BindingFlags.Instance);
        if (methodInfo != null && methodInfo.ReturnType == typeof(int))
        {
            return methodInfo;
        }

        return null;
    }

不要忘记注入您可能需要的任何依赖项。

【讨论】:

  • 哦还有别的东西——我的印象是每个模块都有一个迁移编号。我现在认为这是不正确的——一个模块可以有多个迁移类,版本号是指每个类
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 2017-03-17
  • 2014-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多