private static void UpgradeFromRev299ToRev492(IVault vault) { (vault.GetStructureVersionToken() == "v1.rev299").AssertTrue(); #if DOWNGRADE_STRUCTURE_VERSION_TO_REV299 AssertionHelper.Fail(); #else // branches: remove all empty default values from branches foreach (var branch in vault.GetBranchesRecursive().Concat(vault.Root.MkArray())) { var @default = branch.GetValue("default"); if (@default != null && @default.ContentString.IsNullOrEmpty()) { @default.Delete(); } } // branches: upgrade metadata -> id is no more stored in general-purpose section foreach (var branch in vault.GetBranchesRecursive().Concat(vault.Root.MkArray())) { try { var oldSchoolId = branch.GetId() ?? Guid.NewGuid(); branch.SetMetadata(null); branch.Metadata["$id"] = oldSchoolId.ToString(); (branch.Id == oldSchoolId).AssertTrue(); } catch (Exception) { // do nothing in case of invalid format } } // values: // 1) upgrade metadata -> id is no more stored in general-purpose section // 2) upgrade metadata -> type token is no more stored in general-purpose section foreach (var value in vault.GetValuesRecursive()) { try { var oldSchoolId = value.GetId() ?? Guid.NewGuid(); var oldSchoolTypeToken = value.GetTypeToken(); value.SetMetadata(null); value.Metadata["$id"] = oldSchoolId.ToString(); value.Metadata["type-token"] = oldSchoolTypeToken; (value.Id == oldSchoolId).AssertTrue(); (value.GetTypeToken2() == oldSchoolTypeToken).AssertTrue(); } catch (Exception) { // do nothing in case of invalid format } } #endif }
// so that neither Resharper no compiler whine about using legacy routines in code that supports legacy storage formats #pragma warning disable 618 private static void UpgradeFromNullToRev122(IVault vault) { (vault.GetStructureVersionToken().IsNullOrEmpty()).AssertTrue(); foreach (var branch in vault.GetBranchesRecursive().Concat(vault.Root.MkArray())) { // all branches get a GUID generated and associated if they don't have one yet if (branch.GetId() == null) { branch.SetId(Guid.NewGuid()); } // all branches get a default value created if they don't have one yet var @default = branch.GetValue("default"); if (@default == null) { branch.CreateValue("default").SetDefault(); } } }
public static bool EnsureIsOfLatestStructureVersion(this IVault vault) { if (vault == null) { return(false); } if (vault.GetBranchesRecursive().Count() == 0 && vault.GetValuesRecursive().Count() == 0 && vault.GetStructureVersionToken().IsNullOrEmpty()) { vault.UpdateStructureVersionToken(StructureVersion.Current); vault.SetDefault2(); vault.Save(); } if (!vault.IsOfLatestStructureVersion()) { var oldVersion = vault.GetStructureVersionToken(); if (History.Any(kvp => kvp.First == oldVersion)) { Func <String, String> fmt = s => String.Format(s, oldVersion ?? Resources.Version_Empty, StructureVersion.Current); if (MessageBox.Show(fmt(Resources.Version_UpgradeVaultToLatestVersion), Resources.Confirmation_Title, MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { try { vault.UpgradeToLatestStructureVersion(); MessageBox.Show(fmt(Resources.Version_UpgradeSucceeded), Resources.Version_UpgradeSucceeded_Title, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return(true); } catch (Exception) { MessageBox.Show(fmt(Resources.Version_UpgradeFailed), Resources.Version_UpgradeFailed_Title, MessageBoxButtons.OK, MessageBoxIcon.Error); vault.Dispose(); return(false); } } else { vault.Dispose(); return(false); } } else { Func <String, String> fmt = s => String.Format(s, oldVersion ?? Resources.Version_Empty, StructureVersion.Current); MessageBox.Show(fmt(Resources.Version_UnknownVersion), Resources.Version_UnknownVersion_Title, MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } } else { return(true); } }