clickhouse-activerecord 0.4.0 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8020d332d2b7b707fa2776a37fbb8d2c217ec50703962d06938be53eaae22796
4
- data.tar.gz: 6979255cb79396c6574e4b58c0e82ae39a42612fe1068b1308d98a1222c58b64
3
+ metadata.gz: b584e48174f94997dc9dd04d3ffde3ff7c4b24535117c565e580c646f2f2581c
4
+ data.tar.gz: 9e7c01a185d23ac6e5ac16da635b118a59e7a7c7cdf86ce1c28e92aebacd3baa
5
5
  SHA512:
6
- metadata.gz: 3aa6d799adf1fc66d49c3dff6d02c5ad0349e467fb1067a1453de8bfcb3316abb46681367b7dfd3ebd8f82f13460cb658c4f167ff23398bba8b9cc459c5b6df1
7
- data.tar.gz: a1c8e252279678efb2fe1f8c36a8a7e1c5bfda24896438245921deee5862e94c047dced14b85408b5e1343606ec7055100b813af399568f5ee405a255c268777
6
+ metadata.gz: 8b3132522dbc7abaffa180049e350d4ceea82863ab3c208611c4b3227e6ebece1f706262daebec562bd13822fdcd31d44e6321ec97083543484e238e3e07ed2f
7
+ data.tar.gz: 44f3c58ff30e78b4e83066fbf9609bd656bf9c3e64f56a6113713068b98393c680ec949b5d8832da533ea6cc12216584a9fcf7b34279efc8c499ec9ff376a166
data/CHANGELOG.md CHANGED
@@ -3,6 +3,7 @@
3
3
  * Full support migration and rollback database
4
4
  * Support cluster and replica. Auto inject to SQL queries.
5
5
  * Fix schema dump/load
6
+ * Can dump schema for using PostgreSQL
6
7
 
7
8
  ### Version 0.3.10 (Dec 20, 2019)
8
9
 
data/README.md CHANGED
@@ -125,7 +125,11 @@ Schema load from `db/clickhouse_schema.rb` file:
125
125
 
126
126
  $ rake clickhouse:schema:load
127
127
 
128
- We use schema for emulate development or tests environment on PostgreSQL adapter.
128
+ For export schema to PostgreSQL, you need use:
129
+
130
+ $ rake clickhouse:schema:dump -- --simple
131
+
132
+ Schema will be dump to `db/clickhouse_schema_simple.rb`. If default file exists, it will be auto update after migration.
129
133
 
130
134
  Structure dump to `db/clickhouse_structure.sql` file:
131
135
 
@@ -244,6 +244,10 @@ module ActiveRecord
244
244
  end
245
245
  end
246
246
 
247
+ def rename_table(table_name, new_name)
248
+ do_execute apply_cluster "RENAME TABLE #{quote_table_name(table_name)} TO #{quote_table_name(new_name)}"
249
+ end
250
+
247
251
  def drop_table(table_name, options = {}) # :nodoc:
248
252
  do_execute apply_cluster "DROP TABLE#{' IF EXISTS' if options[:if_exists]} #{quote_table_name(table_name)}"
249
253
  end
@@ -277,7 +281,7 @@ module ActiveRecord
277
281
  end
278
282
 
279
283
  def apply_replica(table, options)
280
- if replica && cluster
284
+ if replica && cluster && options[:options]
281
285
  match = options[:options].match(/^(.*?MergeTree)\(([^\)]*)\)(.*?)$/)
282
286
  if match
283
287
  options[:options] = "Replicated#{match[1]}(#{([replica_path(table), replica].map{|v| "'#{v}'"} + [match[2].presence]).compact.join(', ')})#{match[3]}"
@@ -1,6 +1,19 @@
1
1
  module ClickhouseActiverecord
2
2
  class SchemaDumper < ::ActiveRecord::ConnectionAdapters::SchemaDumper
3
3
 
4
+ attr_accessor :simple
5
+
6
+ class << self
7
+ def dump(connection = ActiveRecord::Base.connection, stream = STDOUT, config = ActiveRecord::Base, default = false)
8
+ dumper = connection.create_schema_dumper(generate_options(config))
9
+ dumper.simple = default
10
+ dumper.dump(stream)
11
+ stream
12
+ end
13
+ end
14
+
15
+ private
16
+
4
17
  def header(stream)
5
18
  stream.puts <<HEADER
6
19
  # This file is auto-generated from the current state of the database. Instead
@@ -8,27 +21,29 @@ module ClickhouseActiverecord
8
21
  # incrementally modify your database, and then regenerate this schema definition.
9
22
  #
10
23
  # This file is the source Rails uses to define your schema when running `rails
11
- # clickhouse:schema:load`. When creating a new database, `rails clickhouse:schema:load` tends to
24
+ # #{simple ? 'db' : 'clickhouse'}:schema:load`. When creating a new database, `rails #{simple ? 'db' : 'clickhouse'}:schema:load` tends to
12
25
  # be faster and is potentially less error prone than running all of your
13
26
  # migrations from scratch. Old migrations may fail to apply correctly if those
14
27
  # migrations use external dependencies or application code.
15
28
  #
16
29
  # It's strongly recommended that you check this file into your version control system.
17
30
 
18
- ClickhouseActiverecord::Schema.define(#{define_params}) do
31
+ #{simple ? 'ActiveRecord' : 'ClickhouseActiverecord'}::Schema.define(#{define_params}) do
19
32
 
20
33
  HEADER
21
34
  end
22
35
 
23
36
  def table(table, stream)
24
37
  if table.match(/^\.inner\./).nil?
25
- stream.puts " # TABLE: #{table}"
26
- sql = @connection.do_system_execute("SHOW CREATE TABLE `#{table.gsub(/^\.inner\./, '')}`")['data'].try(:first).try(:first)
27
- stream.puts " # SQL: #{sql.gsub(/ENGINE = Replicated(.*?)\('[^']+',\s*'[^']+',?\s?([^\)]*)?\)/, "ENGINE = \\1(\\2)")}" if sql
28
- # super(table.gsub(/^\.inner\./, ''), stream)
29
-
30
- # detect view table
31
- match = sql.match(/^CREATE\s+(MATERIALIZED)\s+VIEW/)
38
+ unless simple
39
+ stream.puts " # TABLE: #{table}"
40
+ sql = @connection.do_system_execute("SHOW CREATE TABLE `#{table.gsub(/^\.inner\./, '')}`")['data'].try(:first).try(:first)
41
+ stream.puts " # SQL: #{sql.gsub(/ENGINE = Replicated(.*?)\('[^']+',\s*'[^']+',?\s?([^\)]*)?\)/, "ENGINE = \\1(\\2)")}" if sql
42
+ # super(table.gsub(/^\.inner\./, ''), stream)
43
+
44
+ # detect view table
45
+ match = sql.match(/^CREATE\s+(MATERIALIZED)\s+VIEW/)
46
+ end
32
47
 
33
48
  # Copy from original dumper
34
49
  columns = @connection.columns(table)
@@ -40,9 +55,11 @@ HEADER
40
55
 
41
56
  tbl.print " create_table #{remove_prefix_and_suffix(table).inspect}"
42
57
 
43
- # Add materialize flag
44
- tbl.print ', view: true' if match
45
- tbl.print ', materialized: true' if match && match[1].presence
58
+ unless simple
59
+ # Add materialize flag
60
+ tbl.print ', view: true' if match
61
+ tbl.print ', materialized: true' if match && match[1].presence
62
+ end
46
63
 
47
64
  case pk
48
65
  when String
@@ -58,9 +75,11 @@ HEADER
58
75
  tbl.print ", id: false"
59
76
  end
60
77
 
61
- table_options = @connection.table_options(table)
62
- if table_options.present?
63
- tbl.print ", #{format_options(table_options)}"
78
+ unless simple
79
+ table_options = @connection.table_options(table)
80
+ if table_options.present?
81
+ tbl.print ", #{format_options(table_options)}"
82
+ end
64
83
  end
65
84
 
66
85
  tbl.puts ", force: :cascade do |t|"
@@ -96,5 +115,13 @@ HEADER
96
115
  end
97
116
  super
98
117
  end
118
+
119
+ def format_colspec(colspec)
120
+ if simple
121
+ super.gsub(/CAST\(([^,]+),.*?\)/, "\\1")
122
+ else
123
+ super
124
+ end
125
+ end
99
126
  end
100
127
  end
@@ -1,3 +1,3 @@
1
1
  module ClickhouseActiverecord
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.3'
3
3
  end
@@ -3,11 +3,11 @@
3
3
  namespace :clickhouse do
4
4
 
5
5
  task prepare_schema_migration_table: :environment do
6
- ClickhouseActiverecord::SchemaMigration.create_table
6
+ ClickhouseActiverecord::SchemaMigration.create_table unless ENV['simple'] || ARGV.map{|a| a.include?('--simple') ? true : nil}.compact.any?
7
7
  end
8
8
 
9
9
  task prepare_internal_metadata_table: :environment do
10
- ClickhouseActiverecord::InternalMetadata.create_table
10
+ ClickhouseActiverecord::InternalMetadata.create_table unless ENV['simple'] || ARGV.map{|a| a.include?('--simple') ? true : nil}.compact.any?
11
11
  end
12
12
 
13
13
  task load_config: :environment do
@@ -20,16 +20,18 @@ namespace :clickhouse do
20
20
 
21
21
  # todo not testing
22
22
  desc 'Load database schema'
23
- task load: [:load_config, :prepare_schema_migration_table, :prepare_internal_metadata_table] do
24
- load("#{Rails.root}/db/clickhouse_schema.rb")
23
+ task load: [:load_config, :prepare_schema_migration_table, :prepare_internal_metadata_table] do |t, args|
24
+ simple = ENV['simple'] || ARGV.map{|a| a.include?('--simple') ? true : nil}.compact.any? ? '_simple' : nil
25
+ load("#{Rails.root}/db/clickhouse_schema#{simple}.rb")
25
26
  end
26
27
 
27
28
  desc 'Dump database schema'
28
- task dump: :environment do
29
- filename = "#{Rails.root}/db/clickhouse_schema.rb"
29
+ task dump: :environment do |t, args|
30
+ simple = ENV['simple'] || args[:simple] || ARGV.map{|a| a.include?('--simple') ? true : nil}.compact.any? ? '_simple' : nil
31
+ filename = "#{Rails.root}/db/clickhouse_schema#{simple}.rb"
30
32
  File.open(filename, 'w:utf-8') do |file|
31
33
  ActiveRecord::Base.establish_connection(:"#{Rails.env}_clickhouse")
32
- ClickhouseActiverecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
34
+ ClickhouseActiverecord::SchemaDumper.dump(ActiveRecord::Base.connection, file, ActiveRecord::Base, !!simple)
33
35
  end
34
36
  end
35
37
 
@@ -71,6 +73,9 @@ namespace :clickhouse do
71
73
  desc 'Migrate the clickhouse database'
72
74
  task migrate: [:load_config, :prepare_schema_migration_table, :prepare_internal_metadata_table] do
73
75
  Rake::Task['db:migrate'].execute
76
+ if File.exists? "#{Rails.root}/db/clickhouse_schema_simple.rb"
77
+ Rake::Task['clickhouse:schema:dump'].execute(simple: true)
78
+ end
74
79
  end
75
80
 
76
81
  desc 'Rollback the clickhouse database'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clickhouse-activerecord
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Odintsov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-09-18 00:00:00.000000000 Z
11
+ date: 2020-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler