class Clickhouse::Rails::Migrations::Base

Constants

MIGRATION_TABLE

Attributes

migrations_list[RW]

Public Class Methods

add_version() click to toggle source
# 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| ... } click to toggle source
# 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
connection() click to toggle source
# File lib/clickhouse/rails/migrations/base.rb, line 99
def connection
  Clickhouse.connection
end
create_table(table_name, &block) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/clickhouse/rails/migrations/base.rb, line 10
def inherited(child)
  @migrations_list ||= []
  @migrations_list.push(child)
end
logger() click to toggle source
# File lib/clickhouse/rails/migrations/base.rb, line 103
def logger
  Logger.new(STDOUT)
end
passed?() click to toggle source
# 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) click to toggle source
# 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_up() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/clickhouse/rails/migrations/base.rb, line 42
def table_exists?(table_name)
  connection.execute("EXISTS TABLE #{table_name}").strip == '1'
end
up() click to toggle source
# File lib/clickhouse/rails/migrations/base.rb, line 34
def up; end