public void Upgrade_ValidParameters_ExpectedProperties()
        {
            // Setup
            string filePath = TestHelper.GetScratchPadPath(nameof(Upgrade_ValidParameters_ExpectedProperties));

            var mockRepository = new MockRepository();
            var versionedFile  = mockRepository.Stub <IVersionedFile>();

            versionedFile.Expect(vf => vf.Location).Return(filePath);
            mockRepository.ReplayAll();

            var createScript    = new TestCreateScript("2");
            var upgradeScript   = new TestUpgradeScript("1", "2");
            var migrationScript = new FileMigrationScript(createScript, upgradeScript);

            using (new FileDisposeHelper(filePath))
            {
                // Call
                IVersionedFile upgradedFile = migrationScript.Upgrade(versionedFile);

                // Assert
                Assert.IsNotNull(upgradedFile);
            }

            mockRepository.VerifyAll();
        }
Example #2
0
        /// <summary>
        /// Uses <paramref name="sourceVersionedFile"/> to upgrade to a new <see cref="IVersionedFile"/>.
        /// </summary>
        /// <param name="sourceVersionedFile">The <see cref="IVersionedFile"/> used to upgrade.</param>
        /// <returns>The upgraded <see cref="IVersionedFile"/>.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="sourceVersionedFile"/> is <c>null</c>.</exception>
        /// <exception cref="CriticalMigrationException">Thrown when migration failed.</exception>
        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);
        }
Example #3
0
        /// <summary>
        /// Returns if <paramref name="versionedFile"/> needs to be upgraded to have equal version
        /// as <paramref name="toVersion"/>.
        /// </summary>
        /// <param name="versionedFile">The versioned file to validate.</param>
        /// <param name="toVersion">The version to upgrade to.</param>
        /// <returns><c>true</c> if <paramref name="versionedFile"/> needs to be upgraded to <paramref name="toVersion"/>,
        /// <c>false</c> otherwise.</returns>
        /// <exception cref="ArgumentNullException">Thrown when any of the input parameters is <c>null</c>.</exception>
        public bool NeedsMigrate(IVersionedFile versionedFile, string toVersion)
        {
            if (versionedFile == null)
            {
                throw new ArgumentNullException(nameof(versionedFile));
            }

            if (toVersion == null)
            {
                throw new ArgumentNullException(nameof(toVersion));
            }

            return(versionedFileComparer.Compare(versionedFile.GetVersion(), toVersion) < 0);
        }
Example #4
0
        public void CreateEmptyVersionedFile_FileDoesNotExist_ReturnsVersionedFile()
        {
            // Setup
            const string version = "Valid version";

            string filePath     = TestHelper.GetScratchPadPath(nameof(CreateEmptyVersionedFile_FileDoesNotExist_ReturnsVersionedFile));
            var    createScript = new TestCreateScript(version);

            // Call
            IVersionedFile versionedFile = createScript.CreateEmptyVersionedFile(filePath);

            // Assert
            Assert.IsTrue(File.Exists(versionedFile.Location));
            File.Delete(filePath);
        }
Example #5
0
        public void CreateEmptyVersionedFile_FileDoesNotExist_ReturnsVersionedFile()
        {
            // Setup
            const string query   = ";";
            string       version = ProjectVersionHelper.GetCurrentDatabaseVersion();

            string filePath     = TestHelper.GetScratchPadPath(nameof(CreateEmptyVersionedFile_FileDoesNotExist_ReturnsVersionedFile));
            var    createScript = new ProjectCreateScript(version, query);

            // Call
            IVersionedFile versionedFile = createScript.CreateEmptyVersionedFile(filePath);

            // Assert
            Assert.IsTrue(File.Exists(versionedFile.Location));
            File.Delete(filePath);
        }
Example #6
0
        /// <summary>
        /// Migrates <paramref name="versionedFile"/> to version <paramref name="toVersion"/> at location <paramref name="newFileLocation"/>.
        /// </summary>
        /// <param name="versionedFile">The source versioned file to migrate from.</param>
        /// <param name="toVersion">The version to upgrade to.</param>
        /// <param name="newFileLocation">The location where the migrated file needs to be saved.</param>
        /// <exception cref="ArgumentNullException">Thrown when any of the input parameters is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Thrown when the path of the <paramref name="versionedFile"/> or
        /// the <paramref name="newFileLocation"/> is invalid.</exception>
        /// <exception cref="CriticalMigrationException">Thrown when migrating <paramref name="versionedFile"/>
        /// to a new version on location <paramref name="newFileLocation"/> failed.</exception>
        public void Migrate(IVersionedFile versionedFile, string toVersion, string newFileLocation)
        {
            ValidateMigrationArguments(versionedFile, toVersion, newFileLocation);
            FileMigrationScript migrationScript = TryGetMigrationScript(versionedFile, toVersion);

            IVersionedFile upgradedVersionFile = migrationScript.Upgrade(versionedFile);

            if (!upgradedVersionFile.GetVersion().Equals(toVersion))
            {
                Migrate(upgradedVersionFile, toVersion, newFileLocation);
            }
            else
            {
                MoveMigratedFile(upgradedVersionFile.Location, newFileLocation);
            }
        }
Example #7
0
        private FileMigrationScript TryGetMigrationScript(IVersionedFile versionedFile, string toVersion)
        {
            string fromVersion = versionedFile.GetVersion();

            if (!IsVersionSupported(fromVersion))
            {
                throw new CriticalMigrationException(string.Format(Resources.Migrate_From_Version_0_To_Version_1_Not_Supported,
                                                                   fromVersion, toVersion));
            }

            FileMigrationScript migrationScript = GetMigrationScript(fromVersion, toVersion);

            if (migrationScript == null)
            {
                throw new CriticalMigrationException(string.Format(Resources.Migrate_From_Version_0_To_Version_1_Not_Supported,
                                                                   fromVersion, toVersion));
            }

            return(migrationScript);
        }
Example #8
0
        /// <summary>
        /// Validate the arguments of the migration command.
        /// </summary>
        /// <exception cref="ArgumentNullException">Thrown when any input parameter is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Thrown when the path of the <paramref name="versionedFile"/> or
        /// the <paramref name="newFileLocation"/> is invalid.</exception>
        private static void ValidateMigrationArguments(IVersionedFile versionedFile, string toVersion, string newFileLocation)
        {
            if (versionedFile == null)
            {
                throw new ArgumentNullException(nameof(versionedFile));
            }

            if (toVersion == null)
            {
                throw new ArgumentNullException(nameof(toVersion));
            }

            if (newFileLocation == null)
            {
                throw new ArgumentNullException(nameof(newFileLocation));
            }

            if (IOUtils.GetFullPath(versionedFile.Location).Equals(IOUtils.GetFullPath(newFileLocation)))
            {
                throw new CriticalMigrationException(Resources.Migrate_Target_File_Path_Must_Differ_From_Source_File_Path);
            }
        }
Example #9
0
 public override void DoImplementation(IVersionedFile file, object userData)
 {
     TransferAgent.DownloadVersionedFile(file.Path, file.Version, userData);
 }
Example #10
0
 public abstract void DoImplementation(IVersionedFile file, object userData);