Esempio n. 1
0
        /// <returns>true when migration is performed; false otherwise.</returns>
        private bool HandleDatabaseMigration(MigrationExecutionInfo pendingMigration)
        {
            // create a new DbMigrator using a specific target database because
            // the GenericMigrator in pendingMigration can't be used directly as
            // it doesn't have the specific database information, and the connection
            // can't be changed after the fact
            var targetedMigrator = GetDbMigrator(
                pendingMigration.Migration.Assembly,
                pendingMigration.Migration.ConfigurationType,
                m_ConnectionInfoBuilder.Build(m_Config.Database, m_Config.AuthMode, m_Config.SqlLogin, m_Config.SqlPassword));

            if (targetedMigrator == null)
            {
                Log.Warning(
                    "Cannot perform migration on {endPointServer}\\{endpointDatabase} to {targetMigrationId} " +
                    "for configuration {configType} using migration source {migrationSource} assembly.",
                    m_Config.Database.ServerName,
                    m_Config.Database.DatabaseName,
                    string.IsNullOrEmpty(pendingMigration.TargetMigrationId)
                        ? "LATEST"
                        : pendingMigration.TargetMigrationId,
                    pendingMigration.Migration.ConfigurationType,
                    pendingMigration.Migration.Source);
                return(false);
            }

            Log.Information(
                "Migrating {endPointServer}\\{endpointDatabase} to {targetMigrationId} for " +
                "configuration {configType} using a migration source of {migrationSource} assembly",
                m_Config.Database.ServerName,
                m_Config.Database.DatabaseName,
                string.IsNullOrEmpty(pendingMigration.TargetMigrationId)
                    ? "LATEST"
                    : pendingMigration.TargetMigrationId,
                pendingMigration.Migration.ConfigurationType,
                pendingMigration.Migration.Source);

            using (var migrationTransaction = new TransactionScope(TransactionScopeOption.Required))
            {
                if (string.IsNullOrEmpty(pendingMigration.TargetMigrationId))
                {
                    //Null target migration id means we're going to the latest version
                    targetedMigrator.Update();
                }
                else
                {
                    targetedMigrator.Update(pendingMigration.TargetMigrationId);
                }

                migrationTransaction.Complete();
            }

            return(true);
        }
Esempio n. 2
0
        public void Deploy()
        {
            ValidateConfiguredAssemblies();

            var isPerformInitialization = m_Config.Mode == DeploymentMode.InitializeOnly ||
                                          m_Config.Mode == DeploymentMode.InitializeOrMigrate;

            var isPerformMigration = m_Config.Mode != DeploymentMode.InitializeOnly &&
                                     m_Config.Mode != DeploymentMode.SeedOnly;

            TransactionScope multiMigrationTxScope = null;

            try
            {
                MigrationExecutionInfo pendingMigration = null;
                if (isPerformMigration)
                {
                    pendingMigration   = GetPendingMigration();
                    isPerformMigration = (pendingMigration != null);
                }

                var isInitializationPerformed = isPerformInitialization && HandleDatabaseInitialization();

                if (isPerformMigration && pendingMigration.Migration.ContextKeySchema != null)
                {
                    SetupDeploymentHistory(pendingMigration.Migration.ContextKeySchema.SchemaName);
                }
                else if (isPerformInitialization)
                {
                    var contextKeySchema = GetTargetAssemblyContextKeySchema();
                    SetupDeploymentHistory(contextKeySchema.SchemaName);
                }

                if (m_Config.RunServerMigrationsInTransaction)
                {
                    multiMigrationTxScope = new TransactionScope(TransactionScopeOption.Required);
                }

                if (isPerformMigration)
                {
                    var isMigrationPerformed = HandleDatabaseMigration(pendingMigration);
                    if (isMigrationPerformed)
                    {
                        HandleDeploymentHistory(
                            pendingMigration.Migration.ContextKeySchema.SchemaName,
                            pendingMigration.Migration.ContextKeySchema.ContextKey);
                    }
                }
                else if (isInitializationPerformed)
                {
                    var contextKeySchema = GetTargetAssemblyContextKeySchema();
                    HandleDeploymentHistory(contextKeySchema.SchemaName, contextKeySchema.ContextKey);
                }

                multiMigrationTxScope?.Complete();

                if (!isInitializationPerformed)
                {
                    HandleDataSeeding();
                }
            }
            finally
            {
                multiMigrationTxScope.SafeDispose();
            }
        }
        /// <returns>true when migration is performed; false otherwise.</returns>
        private bool HandleDatabaseMigration(MigrationExecutionInfo pendingMigration)
        {
            // create a new DbMigrator using a specific target database because
            // the GenericMigrator in pendingMigration can't be used directly as
            // it doesn't have the specific database information, and the connection 
            // can't be changed after the fact
            var targetedMigrator = GetDbMigrator(
                pendingMigration.Migration.Assembly,
                pendingMigration.Migration.ConfigurationType, 
                m_ConnectionInfoBuilder.Build(m_Config.Database, m_Config.AuthMode, m_Config.SqlLogin, m_Config.SqlPassword));

            if (targetedMigrator == null)
            {
                Log.Warning(
                    "Cannot perform migration on {endPointServer}\\{endpointDatabase} to {targetMigrationId} " +
                    "for configuration {configType} using migration source {migrationSource} assembly.",
                    m_Config.Database.ServerName,
                    m_Config.Database.DatabaseName,
                    string.IsNullOrEmpty(pendingMigration.TargetMigrationId)
                        ? "LATEST"
                        : pendingMigration.TargetMigrationId,
                    pendingMigration.Migration.ConfigurationType,
                    pendingMigration.Migration.Source);
                return false;
            }

            Log.Information(
                "Migrating {endPointServer}\\{endpointDatabase} to {targetMigrationId} for " +
                "configuration {configType} using a migration source of {migrationSource} assembly",
                m_Config.Database.ServerName,
                m_Config.Database.DatabaseName,
                string.IsNullOrEmpty(pendingMigration.TargetMigrationId)
                    ? "LATEST"
                    : pendingMigration.TargetMigrationId,
                pendingMigration.Migration.ConfigurationType,
                pendingMigration.Migration.Source);

            using (var migrationTransaction = new TransactionScope(TransactionScopeOption.Required))
            {
                if (string.IsNullOrEmpty(pendingMigration.TargetMigrationId))
                {
                    //Null target migration id means we're going to the latest version
                    targetedMigrator.Update();
                }
                else
                {
                    targetedMigrator.Update(pendingMigration.TargetMigrationId);
                }

                migrationTransaction.Complete();
            }

            return true;
        }