Exemplo n.º 1
0
        private bool ApplyMigration(Migration migration, ClickHouseNode node, ClickHouseConnection connection)
        {
            _logger.LogInformation($"Applying {migration}...");

            var success = migration.Process(node, connection) && ClickHouseDbProvider.MakeMigrationRecord(connection, migration.DbEntity, _migrationSettings.MigrationsTableName);

            if (success)
            {
                _logger.LogInformation($"{migration} applied");
            }
            else
            {
                _logger.LogError($"Can't apply {migration}");
            }

            return(success);
        }
Exemplo n.º 2
0
        private bool TryApplyMigrations(ClickHouseNode node, Assembly assembly)
        {
            try
            {
                using (var connection = new ClickHouseConnection(node.ConnectionSettings))
                {
                    _logger.LogInformation($"Applying migrations for the node {node.Name}");
                    connection.Open();
                    ClickHouseDbProvider.CreateMigrationTableIfNotExists(connection,
                                                                         _migrationSettings.MigrationsTableName);

                    var migrations        = _locator.Locate(assembly);
                    var appliedMigrations =
                        ClickHouseDbProvider.GetAppliedMigrations(connection, _migrationSettings.MigrationsTableName);

                    var notAppliedMigrations = GetNotAppliedMigrations(migrations.ToList(), appliedMigrations).ToList();

                    _logger.LogInformation(
                        $"Not applied migrations:{Environment.NewLine}{string.Join(Environment.NewLine, notAppliedMigrations.Select(m => m.ToString()))}");

                    var success = TryApplyMigrations(notAppliedMigrations, node, connection);
                    if (success)
                    {
                        _logger.LogInformation($"All migrations for the node {node.Name} successfully applied");
                    }
                    else
                    {
                        _logger.LogError($"Migrations for {node.Name} were not applied");
                    }

                    return(success);
                }
            }
            catch (Exception e)
            {
                _logger.LogError(e, "Migration applying error.");
                return(false);
            }
        }