Class: Clickhouse::Rails::Migrations::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/clickhouse/rails/migrations/base.rb

Direct Known Subclasses

Init

Constant Summary collapse

MIGRATION_TABLE =
'schema_migrations'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.migrations_listObject

Returns the value of attribute migrations_list.



8
9
10
# File 'lib/clickhouse/rails/migrations/base.rb', line 8

def migrations_list
  @migrations_list
end

Class Method Details

.add_versionObject



36
37
38
39
40
# File 'lib/clickhouse/rails/migrations/base.rb', line 36

def add_version
  connection.insert_rows(MIGRATION_TABLE, names: ['version']) do |row|
    row << [name]
  end
end

.alter_table(table_name) {|table_name| ... } ⇒ Object

Yields:

  • (table_name)


68
69
70
71
72
73
# File 'lib/clickhouse/rails/migrations/base.rb', line 68

def alter_table(table_name)
  @table_info = connection.describe_table(table_name)
  @table_name = table_name

  yield(table_name)
end

.connectionObject



99
100
101
# File 'lib/clickhouse/rails/migrations/base.rb', line 99

def connection
  Clickhouse.connection
end

.create_table(table_name, &block) ⇒ Object



58
59
60
61
# File 'lib/clickhouse/rails/migrations/base.rb', line 58

def create_table(table_name, &block)
  logger.info "# >======= Create #{table_name} ========"
  connection.create_table(table_name, &block)
end

.drop_table(table_name, &block) ⇒ Object



63
64
65
66
# File 'lib/clickhouse/rails/migrations/base.rb', line 63

def drop_table(table_name, &block)
  logger.info "# >======= Drop #{table_name} ========"
  connection.drop_table(table_name, &block)
end

.fetch_column(column, type) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/clickhouse/rails/migrations/base.rb', line 75

def fetch_column(column, type)
  return if @table_info.find { |c_info| c_info.first == column.to_s }

  type = type.to_s.gsub(/(^.|_\w)/) do
    Regexp.last_match(1).upcase
  end
  type = type.gsub('Uint', 'UInt').delete('_')

  query = "ALTER TABLE #{@table_name} ADD COLUMN #{column} #{type}"
  logger.info(query)
  connection.execute(query)
end

.inherited(child) ⇒ Object



10
11
12
13
# File 'lib/clickhouse/rails/migrations/base.rb', line 10

def inherited(child)
  @migrations_list ||= []
  @migrations_list.push(child)
end

.loggerObject



103
104
105
# File 'lib/clickhouse/rails/migrations/base.rb', line 103

def logger
  Logger.new(STDOUT)
end

.passed?Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
# File 'lib/clickhouse/rails/migrations/base.rb', line 25

def passed?
  return false unless table_exists?(MIGRATION_TABLE)

  @rows ||= connection.select_rows(select: 'version', from: MIGRATION_TABLE).flatten
  @rows.include?(name)
rescue Clickhouse::QueryError
  false
end

.run_migration(migration) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/clickhouse/rails/migrations/base.rb', line 88

def run_migration(migration)
  logger.info "# >========== #{migration.name} ==========="
  migration.up
  migration.add_version
rescue Clickhouse::QueryError => e
  logger.info "# Error #{e.class}:"
  logger.info "#  #{e.message}"
ensure
  logger.info "# <========== #{migration.name} ===========\n\n"
end

.run_upObject



15
16
17
18
19
20
21
22
23
# File 'lib/clickhouse/rails/migrations/base.rb', line 15

def run_up
  return unless @migrations_list

  @migrations_list.each do |migration|
    next if migration.passed?

    run_migration(migration)
  end
end

.soft_create_table(table_name, &block) ⇒ Object



46
47
48
49
50
# File 'lib/clickhouse/rails/migrations/base.rb', line 46

def soft_create_table(table_name, &block)
  return if table_exists?(table_name)

  create_table(table_name, &block)
end

.soft_drop_table(table_name) ⇒ Object



52
53
54
55
56
# File 'lib/clickhouse/rails/migrations/base.rb', line 52

def soft_drop_table(table_name)
  return unless table_exists?(table_name)

  drop_table(table_name)
end

.table_exists?(table_name) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/clickhouse/rails/migrations/base.rb', line 42

def table_exists?(table_name)
  connection.execute("EXISTS TABLE #{table_name}").strip == '1'
end

.upObject



34
# File 'lib/clickhouse/rails/migrations/base.rb', line 34

def up; end