Ejemplo n.º 1
0
        public async Task StartAsync(CancellationToken cToken)
        {
            _logger.LogInformation("Мигратор запущен");

            var builder = new MigratorBuilder(_services);

            builder.UsePostgreSQL(
                _configuration.MigrationOptions.ConnectionString,
                lcCollate: _configuration.MigrationOptions.LC_Collate,
                lcCtype: _configuration.MigrationOptions.LC_Type,
                databaseEncoding: _configuration.MigrationOptions.DataBaseEncoding,
                migrationTableHistoryName: "migration_history");
            builder.UseScriptMigrations()
            .FromAssembly(typeof(MigrationService).Assembly, "Migrations.Scripts.");
            builder.UseCodeMigrations()
            .FromAssembly <IMigration>(typeof(MigrationService).Assembly);
            builder.UseUpgradeMigrationPolicy(MigrationPolicy.Allowed);
            builder.UserLogger(_logger);

            if (_configuration.MigrationOptions.LogSql)
            {
                builder.UseLoggerForSql(_logger);
            }

            if (!String.IsNullOrWhiteSpace(_configuration.MigrationOptions.GrantUser))
            {
                builder.UseVariable(DefaultVariables.User, _configuration.MigrationOptions.GrantUser);
            }

            var migrator = builder.Build();
            var result   = await migrator.MigrateSafeAsync(cToken);

            _logger.LogInformation(result.IsSuccessfully
                ? "Миграции успешно выполнены"
                : $"Ошибка миграции: {result.ErrorMessage}");
        }