// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.IO; using Migration.Scripts.Data.Exceptions; namespace Migration.Scripts.Data { /// /// Class that provides methods for migration a . /// public class FileMigrationScript { private readonly CreateScript createScript; private readonly UpgradeScript upgradeScript; /// /// Creates a new instance of . /// /// The create script to use for migrating. /// The upgrade script to use for migrating. /// Thrown when any of the input parameters is null. public FileMigrationScript(CreateScript createScript, UpgradeScript upgradeScript) { if (createScript == null) { throw new ArgumentNullException(nameof(createScript)); } if (upgradeScript == null) { throw new ArgumentNullException(nameof(upgradeScript)); } this.createScript = createScript; this.upgradeScript = upgradeScript; } /// /// Gets the version number from which files can be upgraded by the script. /// /// The supported version. public string SupportedVersion() { return upgradeScript.FromVersion(); } /// /// Gets the version number to which files will be upgraded by the script. /// /// The target version. public string TargetVersion() { return upgradeScript.ToVersion(); } /// /// Uses to upgrade to a new . /// /// The used to upgrade. /// The upgraded . /// Thrown when is null. /// Thrown when migration failed. public IVersionedFile Upgrade(IVersionedFile sourceVersionedFile) { if (sourceVersionedFile == null) { throw new ArgumentNullException(nameof(sourceVersionedFile)); } string newLocation = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName()); IVersionedFile newVersionedFile = createScript.CreateEmptyVersionedFile(newLocation); upgradeScript.Upgrade(sourceVersionedFile.Location, newVersionedFile.Location); return newVersionedFile; } } }