Ejemplo n.º 1
0
        private void RunInstallCommands(InstallationContext installationContext, IEnumerable <DatabaseCommandInfo> commands)
        {
            // create log event that will be used to render all layouts
            LogEventInfo logEvent = installationContext.CreateLogEvent();

            try
            {
                foreach (var commandInfo in commands)
                {
                    string cs;

                    if (commandInfo.ConnectionString != null)
                    {
                        // if there is connection string specified on the command info, use it
                        cs = commandInfo.ConnectionString.Render(logEvent);
                    }
                    else if (this.InstallConnectionString != null)
                    {
                        // next, try InstallConnectionString
                        cs = this.InstallConnectionString.Render(logEvent);
                    }
                    else
                    {
                        // if it's not defined, fall back to regular connection string
                        cs = this.BuildConnectionString(logEvent);
                    }

                    this.EnsureConnectionOpen(cs);

                    var command = this.activeConnection.CreateCommand();
                    command.CommandType = commandInfo.CommandType;
                    command.CommandText = commandInfo.Text.Render(logEvent);

                    try
                    {
                        installationContext.Trace("Executing {0} '{1}'", command.CommandType, command.CommandText);
                        command.ExecuteNonQuery();
                    }
                    catch (Exception exception)
                    {
                        if (exception.MustBeRethrown())
                        {
                            throw;
                        }

                        if (commandInfo.IgnoreFailures || installationContext.IgnoreFailures)
                        {
                            installationContext.Warning(exception.Message);
                        }
                        else
                        {
                            installationContext.Error(exception.Message);
                            throw;
                        }
                    }
                }
            }
            finally
            {
                this.CloseConnection();
            }
        }
Ejemplo n.º 2
0
        private void RunInstallCommands(InstallationContext installationContext, IEnumerable <DatabaseCommandInfo> commands)
        {
            // create log event that will be used to render all layouts
            LogEventInfo logEvent = installationContext.CreateLogEvent();

            try
            {
                foreach (var commandInfo in commands)
                {
                    string cs;

                    if (commandInfo.ConnectionString != null)
                    {
                        // if there is connection string specified on the command info, use it
                        cs = RenderLogEvent(commandInfo.ConnectionString, logEvent);
                    }
                    else if (InstallConnectionString != null)
                    {
                        // next, try InstallConnectionString
                        cs = RenderLogEvent(InstallConnectionString, logEvent);
                    }
                    else
                    {
                        // if it's not defined, fall back to regular connection string
                        cs = BuildConnectionString(logEvent);
                    }

                    // Set ConnectionType if it has not been initialized already
                    if (ConnectionType == null)
                    {
                        SetConnectionType();
                    }

                    EnsureConnectionOpen(cs);

                    using (var command = _activeConnection.CreateCommand())
                    {
                        command.CommandType = commandInfo.CommandType;
                        command.CommandText = RenderLogEvent(commandInfo.Text, logEvent);

                        try
                        {
                            installationContext.Trace("Executing {0} '{1}'", command.CommandType, command.CommandText);
                            command.ExecuteNonQuery();
                        }
                        catch (Exception exception)
                        {
                            if (exception.MustBeRethrownImmediately())
                            {
                                throw;
                            }

                            if (commandInfo.IgnoreFailures || installationContext.IgnoreFailures)
                            {
                                installationContext.Warning(exception.Message);
                            }
                            else
                            {
                                installationContext.Error(exception.Message);
                                throw;
                            }
                        }
                    }
                }
            }
            finally
            {
                InternalLogger.Trace("DatabaseTarget: close connection after install.");

                CloseConnection();
            }
        }