示例#1
0
文件: ApplyTask.cs 项目: defize/Gus
        private static bool ApplyScripts(IEnumerable<FileInfo> scriptsToApply, Server server, Database database, DatabaseHelper databaseHelper, FileSystemHelper fileSystemHelper, bool recordOnly, GusTaskExecutionContext context, bool haltOnError)
        {
            uint index = 0;
            var hasErrors = false;

            foreach (var script in scriptsToApply)
            {
                ++index;

                server.ConnectionContext.BeginTransaction();
                try
                {
                    context.RaiseExecutionEvent(ExecutionEventType.Default, string.Format("Applying script '{0}'.", script.Name), index);

                    var hash = fileSystemHelper.GetFileHash(script);

                    if (!recordOnly)
                    {
                        using (var sr = script.OpenText())
                        {
                            database.ExecuteNonQuery(sr.ReadToEnd());
                        }
                    }

                    databaseHelper.RecordScript(database, script.Name, hash);

                    server.ConnectionContext.CommitTransaction();

                    context.RaiseExecutionEvent(ExecutionEventType.Success, string.Format("Applied script '{0}'.", script.Name), index);
                }
                catch (FailedOperationException ex)
                {
                    server.ConnectionContext.RollBackTransaction();

                    Exception sqlException = FindSqlException(ex);
                    var exceptionMessage = (sqlException ?? ex).Message;

                    context.RaiseExecutionEvent(ExecutionEventType.Error, string.Format("Error applying script '{0}': {1}", script.Name, exceptionMessage), index);

                    if (haltOnError)
                    {
                        return false;
                    }

                    hasErrors = true;
                }
            }

            return !hasErrors;
        }