Class: Clickhouse::Rails::Migrations::Base
- Inherits:
-
Object
- Object
- Clickhouse::Rails::Migrations::Base
- Defined in:
- lib/clickhouse/rails/migrations/base.rb
Direct Known Subclasses
Constant Summary collapse
- MIGRATION_TABLE =
'schema_migrations'.freeze
Class Attribute Summary collapse
-
.migrations_list ⇒ Object
Returns the value of attribute migrations_list.
Class Method Summary collapse
- .add_version ⇒ Object
- .alter_table(table_name) {|table_name| ... } ⇒ Object
- .connection ⇒ Object
- .fetch_column(column, type) ⇒ Object
- .inherited(child) ⇒ Object
- .passed? ⇒ Boolean
- .run_migration(migration) ⇒ Object
- .run_up ⇒ Object
- .soft_create_table(table_name, &block) ⇒ Object
- .soft_drop_table(table_name) ⇒ Object
- .table_exists?(table_name) ⇒ Boolean
- .up ⇒ Object
Class Attribute Details
.migrations_list ⇒ Object
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_version ⇒ Object
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
61 62 63 64 65 66 |
# File 'lib/clickhouse/rails/migrations/base.rb', line 61 def alter_table(table_name) @table_info = connection.describe_table(table_name) @table_name = table_name yield(table_name) end |
.connection ⇒ Object
92 93 94 |
# File 'lib/clickhouse/rails/migrations/base.rb', line 92 def connection Clickhouse.connection end |
.fetch_column(column, type) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/clickhouse/rails/migrations/base.rb', line 68 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 .gsub('Uint', 'UInt') .delete('_') connection.execute("ALTER TABLE #{@table_name} ADD COLUMN #{column} #{type}") 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 |
.passed? ⇒ 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
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/clickhouse/rails/migrations/base.rb', line 81 def run_migration(migration) puts "# >========== #{migration.name} ===========" migration.up migration.add_version rescue Clickhouse::QueryError => e puts "# Error #{e.class}:" puts "# #{e.}" ensure puts "# <========== #{migration.name} ===========\n\n" end |
.run_up ⇒ Object
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
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 |
.up ⇒ Object
34 |
# File 'lib/clickhouse/rails/migrations/base.rb', line 34 def up; end |