Esempio n. 1
0
        /// <summary>
        /// Execute the command with the specified arguments.
        /// </summary>
        /// <param name='args'>
        /// The arguments.
        /// </param>
        /// <param name="arguments">
        /// The parsed arguments.
        /// </param>
        /// <param name="archive">
        /// The database archive containing the versions.
        /// </param>
        /// <returns>Returns the result of Executing the Command</returns>
        protected override bool Execute(string[] args, CreateArguments arguments, IDatabaseArchive archive)
        {
            if (string.IsNullOrEmpty(arguments.Archive))
            {
                this.MessageService.WriteLine("Please specify an archive using the -a switch");
                return(false);
            }

            if (archive == null)
            {
                this.MessageService.WriteLine("The specified archive is not supported");
                return(false);
            }

            try
            {
                return(this.Creator.Create(archive, arguments.Version, CreateTaskExecuter(arguments),
                                           !arguments.ShouldRollbackChanges && !arguments.IsSimulatingUpdate,
                                           arguments.ShouldExecuteMissingTasks));
            }
            catch (VersionNotFoundException v)
            {
                this.MessageService.WriteLine(v.Message);
            }
            catch (TaskExecutionException t)
            {
                this.MessageService.WriteLine(t.Message);
            }
            return(false);
        }
Esempio n. 2
0
        protected override bool Execute(string[] args, CreateArguments arguments, IDatabaseArchive archive)
        {
            if (archive == null)
            {
                this.MessageService.WriteLine("Please specify an archive using the -a switch.");
                return(false);
            }

            return(this.CheckDatabase(arguments, archive));
        }
Esempio n. 3
0
 private void AddTasksToExecuter(IDatabaseArchive archive, ITaskExecuter executer, VersionBase currentVersion, VersionBase targetVersion, bool executeMissingTasks, ISession session, bool targetVersionSpecified)
 {
     if (executeMissingTasks)
     {
         this.AddAllTasks(archive, executer, targetVersion, session, targetVersionSpecified);
     }
     else
     {
         this.AddNewTasks(archive, executer, currentVersion, targetVersion, session);
     }
 }
Esempio n. 4
0
        private void AddAllTasks(IDatabaseArchive archive, ITaskExecuter executer, VersionBase targetVersion, ISession session, bool targetVersionSpecified)
        {
            // If we're executing missing tasks, we need to go through each version and check whether any tasks are missing
            // In this case, if the target version is specified, we only look in that version
            var versions =
                archive.Versions.OrderBy(v => v.Version, this.VersionProvider.GetComparer())
                .Where(
                    v =>
                    !targetVersionSpecified || this.VersionProvider.GetComparer().Compare(targetVersion, v.Version) == 0);

            this.AddTasksForVersions(executer, versions, session);
        }
        public void ShouldCreateAFileDatabaseArchive()
        {
            // Arrange
            FileDatabaseArchiveFactory factory = new FileDatabaseArchiveFactory();
            DirectoryInfo testDirectory        = FileUtil.CreateTempDirectory();

            // Act
            IDatabaseArchive archive = factory.Create(testDirectory.FullName);

            // Assert
            Assert.IsType <FileDatabaseArchive>(archive);
        }
        public void ShouldCreateArchiveWithTheSpecifiedPath()
        {
            // Arrange
            FileDatabaseArchiveFactory factory = new FileDatabaseArchiveFactory();
            DirectoryInfo testDirectory        = FileUtil.CreateTempDirectory();

            // Act
            IDatabaseArchive archive = factory.Create(testDirectory.FullName);

            // Assert
            Assert.Equal(testDirectory.FullName, archive.ArchivePath);

            testDirectory.Delete();
        }
Esempio n. 7
0
        private void AddNewTasks(IDatabaseArchive archive, ITaskExecuter executer, VersionBase currentVersion,
                                 VersionBase targetVersion, ISession session)
        {
            IEnumerable <IDatabaseVersion> versionsToExecute = archive.Versions
                                                               .OrderBy(v => v.Version, this.VersionProvider.GetComparer())
                                                               .Where(
                v =>
                currentVersion == null ||
                this.VersionProvider.GetComparer()
                .Compare(currentVersion, v.Version) <= 0)
                                                               .TakeWhile(
                v =>
                this.VersionProvider.GetComparer()
                .Compare(targetVersion, v.Version) >= 0);

            this.AddTasksForVersions(executer, versionsToExecute, session);
        }
Esempio n. 8
0
        public void ShouldCreateAnArchiveWithTheCorrectArchivePath()
        {
            // Arrange
            using (ZipFile zipFile = new ZipFile())
            {
                ZipDatabaseArchiveFactory factory = new ZipDatabaseArchiveFactory();
                factory.ManifestReader = new Mock <IManifestReader>().Object;
                string fileName = this.CreateTempFile();
                zipFile.Save(fileName);

                // Act
                IDatabaseArchive archive = factory.Create(fileName);

                // Assert
                Assert.Equal(fileName, archive.ArchivePath);
            }
        }
Esempio n. 9
0
        public void ShouldCreateAZipDatabaseArchive()
        {
            using (ZipFile zipFile = new ZipFile())
            {
                // Arrange
                ZipDatabaseArchiveFactory factory = new ZipDatabaseArchiveFactory();
                factory.ManifestReader = new Mock <IManifestReader>().Object;
                string fileName = this.CreateTempFile();
                zipFile.Save(fileName);

                // Act
                IDatabaseArchive archive = factory.Create(fileName);

                // Assert
                Assert.IsType <ZipDatabaseArchive>(archive);
            }
        }
Esempio n. 10
0
        public IDatabaseVersion Read(Stream stream, string manifestPath, IDatabaseArchive archive)
        {
            Validate.NotNull(() => stream);
            Validate.NotNull(() => manifestPath);
            Validate.NotNull(() => archive);

            XmlReader reader = XmlReader.Create(stream);

            reader.MoveToContent();
            XElement element = XElement.ReadFrom(reader) as XElement;

            string      versionString = element.Attributes().First(a => a.Name == "version").Value;
            VersionBase version       = this.VersionProvider.CreateVersion(versionString);

            DatabaseVersion databaseVersion = new DatabaseVersion(version, manifestPath, archive);

            databaseVersion.Tasks = CreateTasks(element, databaseVersion);

            return(databaseVersion);
        }
Esempio n. 11
0
        /// <summary>
        /// Checks whether the database has all the versions and tasks installed.
        /// </summary>
        /// <param name="arguments">The command line arguments.</param>
        /// <param name="archive">The database archive.</param>
        /// <returns>true if no errors occurred, otherwise false.</returns>
        private bool CheckDatabase(CreateArguments arguments, IDatabaseArchive archive)
        {
            bool errorOccurred      = false;
            bool isDatabaseUpToDate = true;

            using (var sessionFactory = this.SessionFactoryProvider.CreateSessionFactory())
                using (var session = sessionFactory.OpenSession())
                    using (session.BeginTransaction())
                    {
                        if (!this.VersionProvider.VersionTableExists(session))
                        {
                            this.MessageService.WriteLine("No versions are currently installed.");
                            return(true);
                        }

                        if (!string.IsNullOrEmpty(arguments.Version))
                        {
                            isDatabaseUpToDate = CheckSpecificVersion(arguments, archive, session, out errorOccurred);
                        }
                        else
                        {
                            foreach (var version in archive.Versions.OrderBy(v => v.Version, this.VersionProvider.GetComparer())
                                     )
                            {
                                if (!this.CheckVersion(session, version))
                                {
                                    isDatabaseUpToDate = false;
                                }
                            }
                        }
                    }

            if (isDatabaseUpToDate)
            {
                this.MessageService.WriteLine("The database is up to date.");
            }

            return(!errorOccurred);
        }
Esempio n. 12
0
        /// <summary>
        /// Checks whether a specific version has been installed in the database.
        /// </summary>
        /// <param name="arguments">The command line arguments</param>
        /// <param name="archive">The database archive.</param>
        /// <param name="session">The database session.</param>
        /// <param name="errorOccurred">true if an error occurs, otherwise false.</param>
        /// <returns>true if the version and all its tasks exist, otherwise false.</returns>
        private bool CheckSpecificVersion(CreateArguments arguments, IDatabaseArchive archive, ISession session, out bool errorOccurred)
        {
            errorOccurred = false;

            var targetVersion  = this.VersionProvider.CreateVersion(arguments.Version);
            var archiveVersion = archive.Versions
                                 .SingleOrDefault(
                v =>
                this.VersionProvider.GetComparer().Compare(v.Version, targetVersion) ==
                0);

            if (archiveVersion != null)
            {
                return(this.CheckVersion(session, archiveVersion));
            }

            this.MessageService.WriteLine(string.Format("Version {0} does not exist in the archive.",
                                                        arguments.Version));
            errorOccurred = true;

            return(false);
        }
 protected abstract bool Execute(string[] args, T arguments, IDatabaseArchive archive);
Esempio n. 14
0
        /// <summary>
        /// Creates a database at the specified version or upgrades the existing database to the specified version.
        /// </summary>
        /// <param name="archive">The archive containing the tasks to run.</param>
        /// <param name="version">The version of database to create.</param>
        /// <param name="executer">The object used to execute the tasks.</param>
        /// <param name="commit">true if any changes should be committed, false if they should be rolled back.</param>
        /// <param name="executeMissingTasks">true if any missing tasks detected should be executed, otherwise false.</param>
        /// <returns>Returns the result of Executing the Command</returns>
        /// <exception cref="VersionNotFoundException">
        /// Thrown if the version to create could not be found.
        /// </exception>
        /// <exception cref="TaskExecutionException">
        /// Thrown if an error occurs while executing one of the tasks in the archive.
        /// </exception>
        public bool Create(IDatabaseArchive archive, string version, ITaskExecuter executer, bool commit, bool executeMissingTasks)
        {
            using (var sessionFactory = this.SessionFactoryProvider.CreateSessionFactory())
            {
                using (var session = sessionFactory.OpenSession())
                {
                    // Set the session to always flush to make sure we execute everything
                    // in order
                    session.FlushMode = NHibernate.FlushMode.Always;

                    using (var transaction = session.BeginTransaction())
                    {
                        LogUpdateStart();

                        if (!this.VersionProvider.VersionTableExists(session))
                        {
                            this.VersionProvider.CreateVersionTable(session);
                        }

                        VersionBase currentVersion = this.VersionProvider.GetCurrentVersion(session);

                        //Log the current version
                        MessageService.WriteLine(currentVersion == null
                                                     ? "Current Database Version Unknown"
                                                     : string.Format("Current Database Version: {0}", currentVersion));

                        VersionBase targetVersion;
                        bool        targetVersionSpecified = !string.IsNullOrEmpty(version);
                        if (!targetVersionSpecified)
                        {
                            targetVersion = archive.Versions
                                            .OrderByDescending(v => v.Version, this.VersionProvider.GetComparer())
                                            .First()
                                            .Version;
                        }
                        else
                        {
                            targetVersion = this.VersionProvider.CreateVersion(version);
                        }

                        MessageService.WriteLine(string.Format("Target Version: {0}", targetVersion));

                        if (!archive.ContainsVersion(targetVersion))
                        {
                            MessageService.WriteLine(string.Format("Target Version Not Found in Archive"));
                            throw new VersionNotFoundException(targetVersion);
                        }

                        this.AddTasksToExecuter(archive, executer, currentVersion, targetVersion, executeMissingTasks,
                                                session, targetVersionSpecified);

                        executer.ExecuteTasks(session);

                        if (commit)
                        {
                            transaction.Commit();
                        }
                        else
                        {
                            transaction.Rollback();
                        }

                        LogUpdateComplete(commit);
                    }
                }
            }
            return(true);
        }
Esempio n. 15
0
 public DatabaseVersion(VersionBase version, string manifestPath, IDatabaseArchive archive)
 {
     this.Version      = version;
     this.ManifestPath = manifestPath;
     this.Archive      = archive;
 }