Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Luuk Peters 82 posts 322 karma points
    Dec 08, 2022 @ 14:39
    Luuk Peters
    1

    Migration with templates: only override physical file if it doesn't exist

    When running a migration with a package.xml file that contains templates, it creates the templates in the database of Umbraco, but it also creates the physical file on the disk.

    The problem is that it will also override the physical file if it already exists. Is there a way to prevent this and only create the file if it does not exist?

    Some context to clarify: We have a package that acts more or less as a very simple starter kit that head starts the development of new projects. One of the things this package does is setup the basic content structure in the tree, including an (empty) homepage with a template.

    During development, this template will be updated, changed and put into source control. However, if there is a developer that joins further into development, he will get the template from source control, but because it's the first time running Umbraco, the migration will override the template possibly causing loss of data.

  • Huw Reddick 1737 posts 6098 karma points MVP c-trib
    Dec 15, 2022 @ 14:00
    Huw Reddick
    100

    In case my solution gets lost in our other lengthy discussion, here is my proposed solution to the problem.


    Instead of including the template views in a package xml/zip.

    I removed them from the package and added the views to the Razor class library, they are then copied to the website project using the build targets file in the same way as the App_Plugins folder. Yo should then be able to use a Condition to only copy if newer or desn't exist etc.

            <PropertyGroup>
                <MediaWizForumsContentFilesPath>$(MSBuildThisFileDirectory)..\App_Plugins\MediaWizards\**\*.*</MediaWizForumsContentFilesPath>
                <MediaWizViewsFilesPath>$(MSBuildThisFileDirectory)..\Views\*.*</MediaWizViewsFilesPath>
            </PropertyGroup>
    
          <Target Name="CopyMediaWizForumViews" BeforeTargets="Build">
            <ItemGroup>
              <MediaWizForumsViews Include="$(MediaWizViewsFilesPath)" />
            </ItemGroup>
            <Message Text="Copying MediaWiz.Forums Views: $(MediaWizViewsFilesPath) - #@(MediaWizForumsViews->Count()) files" Importance="high" />
            <Copy SourceFiles="@(MediaWizForumsViews)" DestinationFiles="@(MediaWizForumsViews->'$(MSBuildProjectDirectory)\Views\%(Filename)%(Extension)')" SkipUnchangedFiles="true" 
              Condition="%(Filename)!='' AND (!Exists('$(MSBuildProjectDirectory)\Views\%(Filename)%(Extension)') OR $([System.DateTime]::Parse('%(ModifiedTime)').Ticks) &gt; $([System.IO.File]::GetLastWriteTime('$(MSBuildProjectDirectory)\Views\%(Filename)%(Extension)').Ticks))"/>
          </Target>
    

    Then in my PackageMigrationbase, I registered the views as templates.

        protected override void Migrate()
        {
            //register the views as templates first
            _fileService.CreateTemplateWithIdentity("ForumMaster", "forumMaster",null);
            var master = _fileService.GetTemplate("forumMaster");
    
            if (master != null)
            {
                var templatesToFind = new[] { "forum", "forumPost", "login", "members", "profile", "register", "reset", "verify", "searchPage" };
                foreach (var template in templatesToFind)
                {
                    _fileService.CreateTemplateWithIdentity(template.FirstCharToUpper(), template,null,master);
                }
    
            }
            //Now the templates are registered we can import the package xml
            ImportPackage.FromEmbeddedResource<ImportPackageXmlMigration>().Do();
            Context.AddPostMigration<PublishRootBranchPostMigration>();
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft