Welcome to mirror list, hosted at ThFree Co, Russian Federation.

click_house_test_helpers.rb « helpers « support « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5befeabd82b47123812e7954a701b81c09ed6c0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# frozen_string_literal: true

module ClickHouseTestHelpers
  def migrate(migration_context, target_version, step = nil)
    quietly { migration_context.up(target_version, step) }
  end

  def rollback(migration_context, target_version, step = 1)
    quietly { migration_context.down(target_version, step) }
  end

  def table_names(database = :main, configuration = ClickHouse::Client.configuration)
    ClickHouse::Client.select('SHOW TABLES', database, configuration).pluck('name')
  end

  def active_schema_migrations_count(database = :main, configuration = ClickHouse::Client.configuration)
    query = <<~SQL
      SELECT COUNT(*) AS count FROM schema_migrations FINAL WHERE active = 1
    SQL

    ClickHouse::Client.select(query, database, configuration).first['count']
  end

  def describe_table(table_name, database = :main, configuration = ClickHouse::Client.configuration)
    ClickHouse::Client
      .select("DESCRIBE TABLE #{table_name} FORMAT JSON", database, configuration)
      .map(&:symbolize_keys)
      .index_by { |h| h[:name].to_sym }
  end

  def schema_migrations(database = :main, configuration = ClickHouse::Client.configuration)
    ClickHouse::Client
      .select('SELECT * FROM schema_migrations FINAL ORDER BY version ASC', database, configuration)
      .map(&:symbolize_keys)
  end

  def clear_db(configuration = ClickHouse::Client.configuration)
    configuration.databases.each_key do |db|
      connection = ::ClickHouse::Connection.new(db, configuration)
      # drop all tables
      lookup_tables(db, configuration).each do |table|
        connection.execute("DROP TABLE IF EXISTS #{table}")
      end

      ClickHouse::MigrationSupport::SchemaMigration.new(connection).ensure_table
    end
  end

  def register_database(config, database_identifier, db_config)
    config.register_database(
      database_identifier,
      database: db_config[:database],
      url: db_config[:url],
      username: db_config[:username],
      password: db_config[:password],
      variables: db_config[:variables] || {}
    )
  end

  private

  def lookup_tables(db, configuration = ClickHouse::Client.configuration)
    ClickHouse::Client.select('SHOW TABLES', db, configuration).pluck('name')
  end

  def quietly(&_block)
    was_verbose = ClickHouse::Migration.verbose
    ClickHouse::Migration.verbose = false

    yield
  ensure
    ClickHouse::Migration.verbose = was_verbose
  end

  def unload_click_house_migration_classes(fixtures_path)
    $LOADED_FEATURES.select { |file| file.include? fixtures_path }.each do |file|
      const = File.basename(file)
                  .scan(ClickHouse::Migration::MIGRATION_FILENAME_REGEXP)[0][1]
                  .camelcase
                  .safe_constantize

      Object.send(:remove_const, const.to_s) if const
      $LOADED_FEATURES.delete(file)
    end
  end
end