Ejemplo n.º 1
0
        public async Task ExecuteChangeScriptAsync(ChangeScriptId scriptId, string scriptName, string scriptText, CancellationToken cancellationToken)
        {
            var scripts = await this.GetStateAsync(cancellationToken).ConfigureAwait(false);

            if (scripts.Scripts.Any(s => scriptId.ScriptId == s.Id.ScriptId))
            {
                this.LogInformation(scriptName + " already executed. Skipping...");
                return;
            }

            bool success;

            try
            {
                await this.ExecuteNonQueryAsync(scriptText, cancellationToken).ConfigureAwait(false);

                success = true;
                this.LogInformation(scriptName + " executed successfully.");
            }
            catch (Exception ex)
            {
                success = false;
                this.LogError(scriptName + " failed: " + ex.Message);
            }

            await this.RecordResultAsync(scriptId, scriptName, success).ConfigureAwait(false);
        }
Ejemplo n.º 2
0
        private Task RecordResultAsync(ChangeScriptId scriptId, string scriptName, bool success)
        {
            var query = "insert into __buildmaster_dbschemachanges"
                        + " (numeric_release_number, script_id, script_name, executed_date, success_indicator)"
                        + " values"
                        + $" ({scriptId.LegacyReleaseSequence}, {scriptId.ScriptId}, '{scriptName.Replace("'", "''")}', now(), '{(YNIndicator)success}')";

            return(this.ExecuteNonQueryAsync(query, CancellationToken.None));
        }
Ejemplo n.º 3
0
        public Task ExecuteChangeScriptAsync(ChangeScriptId scriptId, string scriptName, string scriptText, CancellationToken cancellationToken)
        {
            var state = this.GetStateAsync(cancellationToken).Result;

            if (!state.IsInitialized)
            {
                throw new InvalidOperationException("The database has not been initialized.");
            }

            if (state.Scripts.Any(s => s.Id.ScriptId == scriptId.ScriptId))
            {
                this.LogInformation(scriptName + " already executed. Skipping...");
                return(Complete);
            }

            Exception ex = null;

            try
            {
                this.ExecuteQueryAsync(scriptText, cancellationToken);
                this.LogInformation(scriptName + " executed successfully.");
            }
            catch (Exception _ex)
            {
                ex = _ex;
                this.LogError(scriptName + " failed: " + ex.Message);
            }

            this.ExecuteQueryAsync(
                string.Format(
                    "INSERT INTO __BuildMaster_DbSchemaChanges "
                    + " (Numeric_Release_Number, Script_Id, Script_Name, Executed_Date, Success_Indicator) "
                    + "VALUES "
                    + "({0}, {1}, '{2}', NOW(), '{3}')",
                    scriptId.LegacyReleaseSequence,
                    scriptId.ScriptId,
                    scriptName.Replace("'", "''"),
                    ex == null ? "Y" : "N"
                    ),
                cancellationToken
                );

            return(Complete);
        }
Ejemplo n.º 4
0
        public async Task ExecuteChangeScriptAsync(ChangeScriptId scriptId, string scriptName, string scriptText, CancellationToken cancellationToken)
        {
            int version = await this.GetChangeScriptVersionAsync(cancellationToken).ConfigureAwait(false);

            if (version < 1 || version > 2)
            {
                throw new InvalidOperationException("The database has not been initialized.");
            }

            string getQuery;

            if (version == 2)
            {
                getQuery = $"SELECT [Script_Name] FROM [__BuildMaster_DbSchemaChanges2] WHERE [Script_Guid] = '{scriptId.Guid:D}'";
            }
            else
            {
                getQuery = "SELECT [Batch_Name] FROM [__BuildMaster_DbSchemaChanges] WHERE [Script_Id] = " + scriptId.ScriptId;
            }

            bool alreadyRan = (await this.ExecuteTableAsync(getQuery, r => r[0]?.ToString(), cancellationToken).ConfigureAwait(false)).Count > 0;

            if (alreadyRan)
            {
                this.LogInformation($"Script {scriptName} has already been executed; skipping...");
                return;
            }

            this.LogInformation($"Executing {scriptName}...");

            YNIndicator success;

            try
            {
                await this.ExecuteQueryAsync(scriptText, cancellationToken).ConfigureAwait(false);

                success = true;
                this.LogInformation(scriptName + " executed successfully.");
            }
            catch (SqlException ex)
            {
                foreach (SqlError error in ex.Errors)
                {
                    if (error.Class > 10)
                    {
                        this.LogError(error.Message);
                    }
                    else
                    {
                        this.LogInformation(error.Message);
                    }
                }

                success = false;
            }

            string updateQuery;

            if (version == 2)
            {
                updateQuery = $"INSERT INTO [__BuildMaster_DbSchemaChanges2] ([Script_Id], [Script_Guid], [Script_Name], [Executed_Date], [Success_Indicator]) VALUES ({scriptId.ScriptId}, '{scriptId.Guid}', N'{EscapeString(scriptName)}', GETUTCDATE(), '{success}')";
            }
            else
            {
                updateQuery = $"INSERT INTO [__BuildMaster_DbSchemaChanges] ([Numeric_Release_Number], [Script_Id], [Script_Sequence], [Batch_Name], [Executed_Date], [Success_Indicator]) VALUES ({scriptId.LegacyReleaseSequence}, {scriptId.ScriptId}, 1, N'{EscapeString(scriptName)}', GETDATE(), '{success}')";
            }

            this.LogDebug($"Recording execution result for {scriptName}...");
            await this.ExecuteQueryAsync(updateQuery, cancellationToken).ConfigureAwait(false);

            this.LogDebug($"Execution result for {scriptName} recorded.");
        }