public void ApplyMigrations(Assembly assembly, bool createDatabaseIfNotExists)
        {
            if (createDatabaseIfNotExists)
            {
                CreateDatabaseIfNotExists();
            }

            _dbProvider.Open();
            if (!MigrationTableExists())
            {
                CreateMigrationTableIfNotExists();
            }

            var migrations           = _locator.Locate(assembly);
            var notAppliedMigrations = GetNotAppliedMigration(migrations.ToList(), AllAppliedMigrations());
            var success = ApplyMigrations(notAppliedMigrations);

            if (success)
            {
                _logger.LogInformation("All migrations were successfully applied");
            }
            else
            {
                _logger.LogError("Migrations were not applied");
            }

            _dbProvider.Close();
        }
Exemple #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);
            }
        }