3

如何强制 Orchard 重新运行 Migrations.Create 方法来重新创建模块的数据?我见过的所有教程都使用 Migrations.UpdateFrom 方法让 Orchard 识别模块数据更改,但这意味着我在开发期间每次进行更改时都必须创建一个新方法。这些方法对实际版本更新有意义,但不适用于初始开发。

我试过禁用和重新启用模块,没有骰子。我也尝试过卸载并重新安装模块,但运行卸载命令会从我的计算机中永久删除模块,绕过回收站。

4

2 回答 2

3

我对你的问题有点困惑,但我会尽力回答

迁移更新数据库和数据库中的数据。如果您想更改在仪表板中看到的视图,则无需重新运行迁移。

但是,如果您想更改模型和数据,则需要运行更多迁移或重置数据库并运行创建迁移。

当我重建一个我正在处理的模块时,它将运行任何未完成的迁移,而不必在我的本地版本上禁用/启用该模块,

也就是说如果当前的migration版本是1,并且有一个UpdateFrom1方法,在migration中,这个会在项目构建的时候运行。

您可以在下表 Orchard_Framework_DataMigrationRecord 中查看当前版本号

如果要强制重新运行迁移,可以重置数据库中 table 的值。或者,您可以备份数据库,并在运行整个迁移套件之前进行恢复。

在开发过程中,我使用短迁移,每次需要添加数据时都创建一个方法,我尽量保持这些小迁移,以便在开发过程中轻松识别任何问题。

然后在完成模块之前,将迁移合并为 2 或 3 个逻辑代码块

这是自定义类型的迁移

public int Create()
        {
            // Define the project type 
            ContentDefinitionManager.AlterTypeDefinition("Project", cfg => cfg
                .WithSetting("Stereotype", "Content")
                .CommomPart()
                .AutoroutePart("our-work")
                .BodyPart()
                .WithPart("TitlePart")
                .WithPart("PublishLaterPart")
                .WithPart("MenuPart", builder => builder
                    .WithSetting("MenuPart.OnMenu", "true")
                    .WithSetting("MenuPart.CurrentMenu", "Project Menu"))
                .WithPart("Project")
                .Creatable()
                .Draftable());

            return 1;

        }

        public int UpdateFrom1()
        {
            // Define project part - having a part with the same name will create fields in the project type
            ContentDefinitionManager.AlterPartDefinition("Project", builder => builder
               .MediaPickerField("MainImage")
               .MediaPickerField("MediumImage")
               .MediaPickerField("SmallImage")
               .MediaPickerField("Logo")
               .TextField("ShortDescription", Flavour.TextArea)
               .TextField("Features", Flavour.Markdown)
               .TextField("ClientTitle", Flavour.DefaultFlavour)
               .TextField("ClientName", Flavour.DefaultFlavour)
               .TextField("ClientQuote", Flavour.Textarea)
               .BooleanField("MainProjectOnHomePage", false)
               .Attachable());

            return 2;
        }

我添加了一些扩展方法以使其不那么冗长。

他们来了

 public static class MigrationExtentionHelpers
    {
        // part definitions 

        public static ContentPartDefinitionBuilder MediaPickerField(this ContentPartDefinitionBuilder builder, 
                                                                    string name, bool required = true, string hint = "")
        {
            var displayName = SplitCamel(name);

            // default implementation of Media picker field - create overloads for more options
            return  builder.WithField(name, fieldBuilder => fieldBuilder
                        .OfType("MediaPickerField")
                        .WithDisplayName(displayName)
                        .WithSetting("MediaPickerFieldSettings.Required", required.ToString(CultureInfo.InvariantCulture))
                        .WithSetting("MediaPickerFieldSettings.AllowedExtensions", "jpg png gif")
                        .WithSetting("MediaPickerFieldSettings.Hint", hint));

        }

        public static ContentPartDefinitionBuilder TextField(this ContentPartDefinitionBuilder builder, 
                                                             string name, Flavour flavor, bool required = true, string hint = "")
        {
            var strFlavor = SplitCamel(flavor.ToString());

            // default implementation of Media picker field - create overloads for more options
            return builder.WithField(name, fieldBuilder => fieldBuilder
                        .OfType("TextField")
                        .WithSetting("TextFieldSettings.Required", required.ToString(CultureInfo.InvariantCulture))
                        .WithSetting("TextFieldSettings.Flavor", strFlavor)
                        .WithSetting("TextFieldSettings.Hint", hint));

        }

        public static ContentPartDefinitionBuilder BooleanField(this ContentPartDefinitionBuilder builder,
                                                                string name, bool defalut, string hint = "")
        {
            // default implementation of Media picker field - create overloads for more options
            return builder.WithField(name, fieldBuilder => fieldBuilder
                        .OfType("BooleanField")
                        .WithSetting("BooleanFieldSettings.Hint", hint)
                        .WithSetting("BooleanFieldSettings.DefaultValue", defalut.ToString(CultureInfo.InvariantCulture)));

        }

        // type definitions 

        public static ContentTypeDefinitionBuilder AutoroutePart(this ContentTypeDefinitionBuilder builder, string pathPrefix = "")
        {
            var pattern = string.Format("[{{Name:'{0}/Title', Pattern: '{0}/{{Content.Slug}}', Description: 'my-page'}}]", pathPrefix);

            return builder.WithPart("AutoroutePart", partBuilder => partBuilder
                        .WithSetting("AutorouteSettings.PatternDefinitions", pattern)); 
        }


        public static ContentTypeDefinitionBuilder BodyPart(this ContentTypeDefinitionBuilder builder, 
            Flavour defaultFlavour = Flavour.Markdown)
        {
            return builder.WithPart("BodyPart", partBuilder => partBuilder
                        .WithSetting("BodyTypePartSettings.Flavor", defaultFlavour.ToString()));            
        }

        public static ContentTypeDefinitionBuilder CommomPart(this ContentTypeDefinitionBuilder builder)
        {
            return builder.WithPart("CommonPart")
                        .WithSetting("OwnerEditorSettings.ShowOwnerEditor", false.ToString(CultureInfo.InvariantCulture).ToLower());

        }

        private static string SplitCamel(string enumString)
        {

            StringBuilder sb = new StringBuilder();

            char last = char.MinValue;
            foreach (char c in enumString)
            {
                if (char.IsLower(last) && char.IsUpper(c))
                {
                    sb.Append(' ');
                    sb.Append(c.ToString(CultureInfo.InvariantCulture).ToLower());
                }
                else
                {
                    sb.Append(c);
                }
                last = c;
            }
            return sb.ToString();

        }
    }
于 2012-08-05T23:05:29.373 回答
0

我知道基本上没有办法运行两次迁移,除非你从以前创建的备份中恢复数据库或直接调整它。对我来说这不是问题,因为在我团队的开发过程中,我们使用本地数据库实例,然后将所有迁移合并到初始迁移并部署到测试/登台环境。

于 2012-08-09T08:43:36.120 回答