コード例 #1
0
        /// <summary>
        ///     This API supports the Entity Framework Core infrastructure and is not intended to be used
        ///     directly from your code. This API may change or be removed in future releases.
        /// </summary>
        public virtual void Migrate(string targetMigration = null)
        {
            var connection = _connection.DbConnection;

            _logger.LogDebug(
                RelationalEventId.MigrateUsingConnection,
                () => RelationalStrings.UsingConnection(connection.Database, connection.DataSource));

            if (!_historyRepository.Exists())
            {
                if (!_databaseCreator.Exists())
                {
                    _databaseCreator.Create();
                }

                var command = _rawSqlCommandBuilder.Build(_historyRepository.GetCreateScript());

                command.ExecuteNonQuery(_connection);
            }

            var commandLists = GetMigrationCommandLists(_historyRepository.GetAppliedMigrations(), targetMigration);

            foreach (var commandList in commandLists)
            {
                _migrationCommandExecutor.ExecuteNonQuery(commandList(), _connection);
            }
        }
コード例 #2
0
ファイル: Migrator.cs プロジェクト: zhengwu0591/efcore
        /// <summary>
        ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to
        ///     the same compatibility standards as public APIs. It may be changed or removed without notice in
        ///     any release. You should only use it directly in your code with extreme caution and knowing that
        ///     doing so can result in application failures when updating to a new Entity Framework Core release.
        /// </summary>
        public virtual void Migrate(string targetMigration = null)
        {
            _logger.MigrateUsingConnection(this, _connection);

            if (!_historyRepository.Exists())
            {
                if (!_databaseCreator.Exists())
                {
                    _databaseCreator.Create();
                }

                var command = _rawSqlCommandBuilder.Build(
                    _historyRepository.GetCreateScript());

                command.ExecuteNonQuery(
                    new RelationalCommandParameterObject(
                        _connection,
                        null,
                        null,
                        _currentContext.Context,
                        _commandLogger));
            }

            var commandLists = GetMigrationCommandLists(_historyRepository.GetAppliedMigrations(), targetMigration);

            foreach (var commandList in commandLists)
            {
                _migrationCommandExecutor.ExecuteNonQuery(commandList(), _connection);
            }
        }
コード例 #3
0
        public virtual void Migrate(string targetMigration = null)
        {
            var connection = _connection.DbConnection;

            _logger.LogDebug(RelationalStrings.UsingConnection(connection.Database, connection.DataSource));

            if (!_historyRepository.Exists())
            {
                if (!_databaseCreator.Exists())
                {
                    _databaseCreator.Create();
                }

                var command = _rawSqlCommandBuilder.Build(_historyRepository.GetCreateScript());

                Execute(new[] { command });
            }

            var commands = GetMigrationCommands(_historyRepository.GetAppliedMigrations(), targetMigration);

            foreach (var command in commands)
            {
                Execute(command());
            }
        }
コード例 #4
0
        protected virtual void Execute([NotNull] IEnumerable <SqlBatch> sqlBatches, bool ensureDatabase = false)
        {
            Check.NotNull(sqlBatches, nameof(sqlBatches));

            if (ensureDatabase && !_databaseCreator.Exists())
            {
                _databaseCreator.Create();
            }

            using (var transaction = _connection.BeginTransaction())
            {
                _executor.ExecuteNonQuery(_connection, transaction.DbTransaction, sqlBatches);
                transaction.Commit();
            }
        }
コード例 #5
0
        /// <summary>
        /// Executes pending migrations and update procedures.
        /// </summary>
        public void ApplayMigrations()
        {
            if (_databaseOptions.Type == DatabaseTypes.InMemory)
            {
                return;
            }

            _logger.LogInformation("===== Release Management =====");

            try
            {
                _logger.LogInformation($"Hosting env: {_hostingEnvironment}");

                IRelationalDatabaseCreator relationalDatabaseCreator = _context.Database.GetService <IDatabaseCreator>() as IRelationalDatabaseCreator;

                if (!relationalDatabaseCreator.Exists())
                {
                    _logger.LogInformation($"Creating new Database");
                    relationalDatabaseCreator.Create();
                    _logger.LogInformation($"New Database was created");
                }

                List <IUpdate> updates = AllUpdates();
                _logger.LogInformation($"All updates(also already applied): {string.Join(", ", updates)}");

                List <string> applyedMigrations = _context.Database
                                                  .GetAppliedMigrations()
                                                  .ToList();

                foreach (var update in updates)
                {
                    if (update.ShouldExecute(applyedMigrations))
                    {
                        PerformUpdateInTransaction(update);
                    }
                }

                _logger.LogInformation("===== Release Management Finished =====");
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error during ReleaseManagment. {ex}");
                throw new Exception($"ReleaseManagment Error. {ex.Message}");
            }
        }
コード例 #6
0
        /// <summary>
        /// Executes pending migrations and update procedures.
        /// </summary>
        public void ApplayMigrations()
        {
            _logger.LogInformation("===== Release Management =====");

            try
            {
                _logger.LogInformation($"Hosting env: {_hostingEnvironment}");

                IRelationalDatabaseCreator relationalDatabaseCreator = _context.Database.GetService <IDatabaseCreator>() as IRelationalDatabaseCreator;

                if (!relationalDatabaseCreator.Exists())
                {
                    _logger.LogInformation($"Creating new Database");
                    relationalDatabaseCreator.Create();
                    _logger.LogInformation($"New Database was created");
                }

                IEnumerable <IUpdate> updates = _updateList.Get();

                List <string> applyedMigrations = _context.Database
                                                  .GetAppliedMigrations()
                                                  .ToList();

                foreach (var update in updates)
                {
                    if (update.ShouldExecute(applyedMigrations))
                    {
                        PerformUpdateInTransaction(update);
                    }
                }

                _logger.LogInformation("===== Release Management Finished =====");
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error during ReleaseManagment. {ex}");
                throw new Exception($"ReleaseManagment Error. {ex.Message}");
            }
        }
コード例 #7
0
        /// <summary>
        /// Executes pending migrations and update procedures.
        /// </summary>
        public static void ApplyIdentityMigrations(this IApplicationBuilder app)
        {
            using (var serviceScope = app.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope())
            {
                ILoggerFactory loggerFactory = serviceScope.ServiceProvider.GetService <ILoggerFactory>();
                ILogger        logger        = loggerFactory.CreateLogger(typeof(ReleaseManagement));

                IOptionsSnapshot <DatabaseOptions> databaseOptions = serviceScope.ServiceProvider.GetRequiredService <IOptionsSnapshot <DatabaseOptions> >();
                if (databaseOptions.Value.Type == DatabaseTypes.InMemory)
                {
                    return;
                }

                logger.LogInformation("===== Release Management =====");

                try
                {
#if NET_CORE2
                    IHostingEnvironment hostingEnvironment = serviceScope.ServiceProvider.GetRequiredService <IHostingEnvironment>();
#endif
#if NET_CORE3
                    IWebHostEnvironment hostingEnvironment = serviceScope.ServiceProvider.GetRequiredService <IWebHostEnvironment>();
#endif
                    IConfiguration             configuration   = serviceScope.ServiceProvider.GetRequiredService <IConfiguration>();
                    IdentityDbContext          context         = serviceScope.ServiceProvider.GetRequiredService <IdentityDbContext>();
                    IRelationalDatabaseCreator databaseCreator = context.Database.GetService <IDatabaseCreator>() as IRelationalDatabaseCreator;


                    logger.LogInformation($"Hosting env: {hostingEnvironment}");

                    if (!databaseCreator.Exists())
                    {
                        logger.LogInformation($"Creating new Database");

                        databaseCreator.Create();

                        logger.LogInformation($"New Database was created");
                    }

                    List <IUpdate> updates = AllUpdates();
                    logger.LogInformation($"All updates(also already applied): {string.Join(", ", updates)}");

                    List <string> applyedMigrations = context.Database
                                                      .GetAppliedMigrations()
                                                      .ToList();

                    foreach (var update in updates)
                    {
                        if (update.ShouldExecute(applyedMigrations))
                        {
                            PerformUpdateInTransaction(context, update, logger);
                        }
                    }

                    logger.LogInformation("===== Release Management Finished =====");
                }
                catch (Exception ex)
                {
                    logger.LogError($"Error during ReleaseManagment. {ex}");
                    throw new Exception($"ReleaseManagment Error. {ex.Message}");
                }
            }

            return;
        }