Hi,

How can we use the CanUpgradeFrom override of the UpgradeHandler? Can we for instance use it to do versioning for the upgrade tool?

Regards Paul

asked May 01 '14 at 06:58

Paul%20Sinnema's gravatar image

Paul Sinnema
261878896


2 Answers:

Hello Paul,

sorry for delay. There were official holidays in Russia.

DataObjects.Net will consider your persistent assembly version when choosing upgrade handler. Old assembly version is stored in the database and passed to CanUpgradeFrom method for each UpgradeHandler type in the assembly during upgrade process. Handler that returns true will be chosen for using in this upgrade. If more than one upgrade handler "accepts" particular assembly version an exception would be thrown.

answered May 06 '14 at 03:22

Denis%20Krjuchkov's gravatar image

Denis Krjuchkov
179325

Hi Denis, No problem. Did you have a nice holiday?

This mechanism could really help us.

You say: "Old assembly version is stored in the database". Questions:

  • Does DO store the version in the DB?

  • Where do I specify this version?

(May 06 '14 at 06:20) Paul Sinnema Paul%20Sinnema's gravatar image

Did you have a nice holiday?

Sure they are always good after a long winter :-)

Assembly versions are stored in the Metadata.Assembly table.

DataObjects.Net will use our custom AssemblyInfoAttribute. It could be applied to UpgradeHandler type or an assembly itself.

If this attribute is not specified in either way regular .NET assembly version is used.

Finally you can override DetectAssemblyVersion method in upgrade handler to provide completely custom assembly version for the next upgrade.

(May 06 '14 at 06:36) Denis Krjuchkov Denis%20Krjuchkov's gravatar image

Can you give me an example on how and where this [AssemblyInfo()] attribute is defined?

(May 06 '14 at 10:25) Paul Sinnema Paul%20Sinnema's gravatar image

Ok I think I understand how this works. Here's what I've done. Maybe this can be an example for other developers:

In AssemblyInfo.cs we changed the Version. You can also use the mentioned AssemblyInfoAttribute like so:

[Assembly: AssemblyInfo("Fortrus.Model", "1.0.0.2")]

but, as also mentioned, when absent the .Net Assembly version will be taken

using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using Xtensive.Orm.Upgrade;

// General Information about an assembly is controlled through the following 
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("Fortrus.Model")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Contractors")]
[assembly: AssemblyProduct("Fortrus.Model")]
[assembly: AssemblyCopyright("Copyright © Contractors 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("1374f51f-1432-479d-81be-c43cd4e104ea")]

[assembly: AssemblyVersion("1.0.0.2")]
[assembly: AssemblyFileVersion("1.0.0.2")]

I created an UpgradeHandler that can be used for several versions. The only thing you have to remember is to change the version number in the AssemblyInfo.cs on each version that required additional hints and processing after changing the model (f.i. simple changes like adding a new Field don't need extra processing).

Because we are upgrading from several models to 1 the oldVersion in the UpgradeFrom() override is null before that upgrade takes place.

Notice also the 'goto case "1.0.0.2"' needed to cause a fallthrough to the next level of processing (I avoid goto as much as possible but it comes in handy here).

using Fortrus.Model.Entities.Autorisatie;
using Fortrus.Model.Entities.Businessrules;
using Fortrus.Model.Entities.Messaging;
using Fortrus.Model.Entities.Projecten;
using Fortrus.Model.Entities.Reporting;
using System;
using Xtensive.Orm.Upgrade;

namespace Fortrus.Model.Upgrade
{
public class Upgrader : UpgradeHandler
{
    private string m_UpgradeFrom;

    public override bool CanUpgradeFrom(string oldVersion)
    {
        m_UpgradeFrom = oldVersion;
        return true;
    }

    protected override void AddUpgradeHints(Xtensive.Collections.ISet<UpgradeHint> hints)
    {
        switch (m_UpgradeFrom)
        {
            case null: // Upgrade to 1.0.0.1
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.ActiviteitEntity", typeof(ActiviteitEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.BaselineActiviteitEntity", typeof(BaselineActiviteitEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.BaselineEntity", typeof(BaselineEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.BaselineFolderEntity", typeof(BaselineFolderEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.BaselineObjectEntity", typeof(BaselineObjectEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.BestandEntity", typeof(BestandEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.CitMeldingEntity", typeof(CitMeldingEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.CitObjectEntity", typeof(CitObjectEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.FactuurEntity", typeof(FactuurEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.FactuurRegelEntity", typeof(FactuurRegelEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.FolderEntity", typeof(FolderEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.GunningsVoorstelEntity", typeof(GunningsVoorstelEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.GunningsVoorstelRegelEntity", typeof(GunningsVoorstelRegelEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.HistorieEntity", typeof(HistorieEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.HistorieForeignKeyEntity", typeof(HistorieForeignKeyEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.MOSPadActiviteitEntity", typeof(MOSPadActiviteitEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.MOSPadExportEntity", typeof(MOSPadExportEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.NotitieEntity", typeof(NotitieEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.OfferteEntity", typeof(OfferteEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.OfferteRegelEntity", typeof(OfferteRegelEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.OpdrachtEntity", typeof(OpdrachtEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.OpdrachtgeverEntity", typeof(OpdrachtgeverEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.OpdrachtRegelEntity", typeof(OpdrachtRegelEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.PadActiviteitEntity", typeof(PadActiviteitEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.PadFolderEntity", typeof(PadFolderEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.PadObjectEntity", typeof(PadObjectEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.PadRootEntity", typeof(PadRootEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.PadStatusEntity", typeof(PadStatusEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.PadStatusGroupEntity", typeof(PadStatusGroupEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.PadStatusMomentEntity", typeof(PadStatusMomentEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.PadStatusPeriodeEntity", typeof(PadStatusPeriodeEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.PadWerkRegelStatusMomentEntity", typeof(PadWerkRegelStatusMomentEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.ProjectEntity", typeof(ProjectEntity)));
                hints.Add(new RenameTypeHint("Fortrus.Reporting.Model.Entities.ReportEntity", typeof(ReportEntity)));
                hints.Add(new RenameTypeHint("Fortrus.Reporting.Model.Entities.ReportParameterEntity", typeof(ReportParameterEntity)));
                hints.Add(new RenameTypeHint("Fortrus.Reporting.Model.Entities.ReportParameterValueEntity", typeof(ReportParameterValueEntity)));
                hints.Add(new RenameTypeHint("Fortrus.Reporting.Model.Entities.RepRootEntity", typeof(RepRootEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.UitvoerderEntity", typeof(UitvoerderEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.WerkEntity", typeof(WerkEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.WerkpakketEntity", typeof(WerkpakketEntity)));
                hints.Add(new RenameTypeHint("Projecten.Model.Entities.WerkRegelEntity", typeof(WerkRegelEntity)));
                hints.Add(new RenameTypeHint("Fortrus.Businessrules.Model.Entities.AssemblyEntity", typeof(AssemblyEntity)));
                hints.Add(new RenameTypeHint("Fortrus.Autorisatie.Model.Entities.AutRootEntity", typeof(AutRootEntity)));
                hints.Add(new RenameTypeHint("Fortrus.Businessrules.Model.Entities.BreRootEntity", typeof(BreRootEntity)));
                hints.Add(new RenameTypeHint("Fortrus.Businessrules.Model.Entities.BusinessruleEntity", typeof(BusinessruleEntity)));
                hints.Add(new RenameTypeHint("Fortrus.Businessrules.Model.Entities.GroupEntity", typeof(GroupEntity)));
                hints.Add(new RenameTypeHint("Fortrus.Messaging.Model.Entities.MesRootEntity", typeof(MesRootEntity)));
                hints.Add(new RenameTypeHint("Fortrus.Messaging.Model.Entities.MessageEntity", typeof(MessageEntity)));
                hints.Add(new RenameTypeHint("Fortrus.Autorisatie.Model.Entities.UserEntity", typeof(UserEntity)));
                goto case "1.0.0.1";

            case "1.0.0.1": // Upgrade to 1.0.0.2
                goto case "1.0.0.2";

            case "1.0.0.2": // Upgrade to 1.0.0.3
                break;

            default:
                throw new Exception(string.Format("Upgrade from {0} not possible", m_UpgradeFrom));
        }

        base.AddUpgradeHints(hints);
    }

}
}

answered May 07 '14 at 03:09

Paul%20Sinnema's gravatar image

Paul Sinnema
261878896

edited May 07 '14 at 03:13

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

Subscription:

Once you sign in you will be able to subscribe for any updates here

Tags:

×4

Asked: May 01 '14 at 06:58

Seen: 6,146 times

Last updated: May 07 '14 at 03:30

powered by OSQA