Class: ClickhouseActiverecord::Tasks

Inherits:
Object
  • Object
show all
Defined in:
lib/clickhouse-activerecord/tasks.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Tasks

Returns a new instance of Tasks.



11
12
13
# File 'lib/clickhouse-activerecord/tasks.rb', line 11

def initialize(configuration)
  @configuration = configuration
end

Class Method Details

.using_database_configurations?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/clickhouse-activerecord/tasks.rb', line 7

def self.using_database_configurations?
  true
end

Instance Method Details

#createObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/clickhouse-activerecord/tasks.rb', line 15

def create
  establish_master_connection
  connection.create_database @configuration.database
rescue ActiveRecord::StatementInvalid => e
  if e.cause.to_s.include?('already exists')
    raise ActiveRecord::DatabaseAlreadyExists
  else
    raise
  end
end

#dropObject



26
27
28
29
# File 'lib/clickhouse-activerecord/tasks.rb', line 26

def drop
  establish_master_connection
  connection.drop_database @configuration.database
end

#migrateObject



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/clickhouse-activerecord/tasks.rb', line 76

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

#purgeObject



31
32
33
34
35
# File 'lib/clickhouse-activerecord/tasks.rb', line 31

def purge
  ActiveRecord::Base.connection_handler.clear_active_connections!(:all)
  drop
  create
end

#structure_dump(*args) ⇒ Object



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
# File 'lib/clickhouse-activerecord/tasks.rb', line 37

def structure_dump(*args)
  establish_master_connection

  # get all tables
  tables = connection.execute("SHOW TABLES FROM #{@configuration.database} WHERE name NOT LIKE '.inner_id.%'")['data'].flatten.map do |table|
    next if %w[schema_migrations ar_internal_metadata].include?(table)
    connection.show_create_table(table).gsub("#{@configuration.database}.", '')
  end.compact

  # sort view to last
  tables.sort_by! {|table| table.match(/^CREATE\s+(MATERIALIZED\s+)?VIEW/) ? 1 : 0}

  # get all functions
  functions = connection.execute("SELECT create_query FROM system.functions WHERE origin = 'SQLUserDefined'")['data'].flatten

  # put to file
  File.open(args.first, 'w:utf-8') do |file|
    functions.each do |function|
      file.puts function + ";\n\n"
    end

    tables.each do |table|
      file.puts table + ";\n\n"
    end
  end
end

#structure_load(*args) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/clickhouse-activerecord/tasks.rb', line 64

def structure_load(*args)
  File.read(args.first).split(";\n\n").each do |sql|
    if sql.gsub(/[a-z]/i, '').blank?
      next
    elsif sql =~ /^INSERT INTO/
      connection.do_execute(sql, nil, format: nil)
    else
      connection.execute(sql)
    end
  end
end