class ClickhouseActiverecord::Tasks

Public Class Methods

new(configuration) click to toggle source
# File lib/clickhouse-activerecord/tasks.rb, line 8
def initialize(configuration)
  @configuration = configuration.with_indifferent_access
end

Public Instance Methods

create() click to toggle source
# File lib/clickhouse-activerecord/tasks.rb, line 12
def create
  establish_master_connection
  connection.create_database @configuration["database"]
rescue ActiveRecord::StatementInvalid => e
  if e.cause.to_s.include?('already exists')
    raise ActiveRecord::Tasks::DatabaseAlreadyExists
  else
    raise
  end
end
drop() click to toggle source
# File lib/clickhouse-activerecord/tasks.rb, line 23
def drop
  establish_master_connection
  connection.drop_database @configuration["database"]
end
migrate() click to toggle source
# File lib/clickhouse-activerecord/tasks.rb, line 49
def migrate
  check_target_version

  verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] != "false" : true
  scope = ENV["SCOPE"]
  verbose_was, ActiveRecord::Migration.verbose = ActiveRecord::Migration.verbose, verbose
  connection.migration_context.migrate(target_version) do |migration|
    scope.blank? || scope == migration.scope
  end
  ActiveRecord::Base.clear_cache!
ensure
  ActiveRecord::Migration.verbose = verbose_was
end
purge() click to toggle source
# File lib/clickhouse-activerecord/tasks.rb, line 28
def purge
  clear_active_connections!
  drop
  create
end
structure_dump(*args) click to toggle source
# File lib/clickhouse-activerecord/tasks.rb, line 34
def structure_dump(*args)
  tables = connection.execute("SHOW TABLES FROM #{@configuration['database']}")['data'].flatten

  File.open(args.first, 'w:utf-8') do |file|
    tables.each do |table|
      next if table.match(/\.inner/)
      file.puts connection.execute("SHOW CREATE TABLE #{table}")['data'].try(:first).try(:first).gsub("#{@configuration['database']}.", '') + ";\n\n"
    end
  end
end
structure_load(*args) click to toggle source
# File lib/clickhouse-activerecord/tasks.rb, line 45
def structure_load(*args)
  File.read(args.first).split(";\n\n").each { |sql| connection.execute(sql) }
end

Private Instance Methods

check_target_version() click to toggle source
# File lib/clickhouse-activerecord/tasks.rb, line 69
def check_target_version
  if target_version && !(ActiveRecord::Migration::MigrationFilenameRegexp.match?(ENV["VERSION"]) || /\A\d+\z/.match?(ENV["VERSION"]))
    raise "Invalid format of target version: `VERSION=#{ENV['VERSION']}`"
  end
end
establish_master_connection() click to toggle source
# File lib/clickhouse-activerecord/tasks.rb, line 65
def establish_master_connection
  establish_connection @configuration
end
target_version() click to toggle source
# File lib/clickhouse-activerecord/tasks.rb, line 75
def target_version
  ENV["VERSION"].to_i if ENV["VERSION"] && !ENV["VERSION"].empty?
end