Exemple #1
0
        /// <summary>
        /// Adds a record to the version table
        /// </summary>
        /// <param name="version">InstalledVersion to add</param>
        public void AddInstalledVersion(SqlInstalledVersion version)
        {
            this.CheckVersion(version);

            object param   = null;
            var    command = this.BuildAddInstalledVersionCommand(version, out param);

            using (var connection = this.OpenConnection())
            {
                using (var transaction = connection.BeginTransaction())
                {
                    try
                    {
                        connection.Query(
                            sql: command,
                            param: param,
                            transaction: transaction);

                        transaction.Commit();
                    }
                    catch (Exception e)
                    {
                        transaction.Rollback();

                        throw new AddInstalledVersionException(
                                  $"Failed to add installed version record", e);
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Creates the command used to insert the installed version.
        /// </summary>
        /// <param name="version">Version to add</param>
        /// <param name="param">Used params</param>
        /// <returns>Insert command</returns>
        protected string BuildAddInstalledVersionCommand(SqlInstalledVersion version, out dynamic param)
        {
            var command = this.BuildCommand(
                $@"INSERT INTO {VERSION_TABLE} (major, minor, revision, installDateUtc)
                    VALUES (@major, @minor, @revision, @installDateUtc);", true);

            param = new
            {
                major          = version.Major,
                minor          = version.Minor,
                revision       = version.Revision,
                installDateUtc = version.InstallDateUtc
            };

            return(command);
        }
Exemple #3
0
        /// <summary>
        /// Checks if there is a newer version installed. Throws AddLowerVersionException if
        /// equal or higher version was found.
        /// </summary>
        /// <param name="installedVersionToCheck"></param>
        public void CheckVersion(SqlInstalledVersion installedVersionToCheck)
        {
            var currentInstalledVersion = this.GetInstalledVersion();

            if (currentInstalledVersion != null)
            {
                var currentVersion = currentInstalledVersion.GetVersion();
                var versionToCheck = installedVersionToCheck.GetVersion();

                if (currentVersion != null && currentVersion.CompareTo(versionToCheck) >= 0)
                {
                    throw new LowerOrEqualVersionException(
                              $"Attempt to add lower version. Current version is {currentVersion}");
                }
            }
        }
Exemple #4
0
        /// <summary>
        /// Executes the specified script on the database. If successfull, the installed version will be added
        /// </summary>
        /// <param name="script"></param>
        public void ExecuteScript(SqlScript script)
        {
            var version = new SqlInstalledVersion(
                script.Version.Major,
                script.Version.Minor,
                script.Version.Revision,
                DateTime.UtcNow);

            this.CheckVersion(version);

            object addVersionParam   = null;
            var    addVersionCommand = this.BuildAddInstalledVersionCommand(version, out addVersionParam);

            try
            {
                var command = this.BuildCommand(script.Command, useSpecifiedDatabase: script.UseSpecifiedDatabase);

                using (var connection = this.OpenConnection())
                {
                    using (var transaction = connection.BeginTransaction())
                    {
                        try
                        {
                            connection.Query(sql: command, param: null, transaction: transaction);
                            connection.Query(sql: addVersionCommand, param: addVersionParam, transaction: transaction);
                            transaction.Commit();
                        }
                        catch
                        {
                            transaction.Rollback();
                            throw;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw new ExecuteScriptException(
                          $"Failed to execute script {script.Name}.", e);
            }
        }