forked from PNixx/clickhouse-activerecord
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
158 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Arel # :nodoc: all | ||
module Nodes | ||
class Final < Arel::Nodes::Unary | ||
delegate :empty?, to: :expr | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module ClickhouseActiverecord | ||
VERSION = '1.0.2' | ||
VERSION = '1.0.3' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
module CoreExtensions | ||
module ActiveRecord | ||
module InternalMetadata | ||
module ClassMethods | ||
|
||
def []=(key, value) | ||
row = final.find_by(key: key) | ||
if row.nil? || row.value != value | ||
create!(key: key, value: value) | ||
end | ||
end | ||
|
||
def [](key) | ||
final.where(key: key).pluck(:value).first | ||
end | ||
|
||
def create_table | ||
return super unless connection.is_a?(::ActiveRecord::ConnectionAdapters::ClickhouseAdapter) | ||
return if table_exists? || !enabled? | ||
|
||
key_options = connection.internal_string_options_for_primary_key | ||
table_options = { | ||
id: false, | ||
options: connection.adapter_name.downcase == 'clickhouse' ? 'ReplacingMergeTree(created_at) PARTITION BY key ORDER BY key' : '', | ||
if_not_exists: true | ||
} | ||
full_config = connection.instance_variable_get(:@full_config) || {} | ||
|
||
if full_config[:distributed_service_tables] | ||
table_options.merge!(with_distributed: table_name, sharding_key: 'cityHash64(created_at)') | ||
|
||
distributed_suffix = "_#{full_config[:distributed_service_tables_suffix] || 'distributed'}" | ||
else | ||
distributed_suffix = '' | ||
end | ||
|
||
connection.create_table(table_name + distributed_suffix.to_s, **table_options) do |t| | ||
t.string :key, **key_options | ||
t.string :value | ||
t.timestamps | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
module CoreExtensions | ||
module ActiveRecord | ||
module SchemaMigration | ||
module ClassMethods | ||
|
||
def create_table | ||
return super unless connection.is_a?(::ActiveRecord::ConnectionAdapters::ClickhouseAdapter) | ||
|
||
return if table_exists? | ||
|
||
version_options = connection.internal_string_options_for_primary_key | ||
table_options = { | ||
id: false, options: 'ReplacingMergeTree(ver) ORDER BY (version)', if_not_exists: true | ||
} | ||
full_config = connection.instance_variable_get(:@full_config) || {} | ||
|
||
if full_config[:distributed_service_tables] | ||
table_options.merge!(with_distributed: table_name, sharding_key: 'cityHash64(version)') | ||
|
||
distributed_suffix = "_#{full_config[:distributed_service_tables_suffix] || 'distributed'}" | ||
else | ||
distributed_suffix = '' | ||
end | ||
|
||
connection.create_table(table_name + distributed_suffix.to_s, **table_options) do |t| | ||
t.string :version, **version_options | ||
t.column :active, 'Int8', null: false, default: '1' | ||
t.datetime :ver, null: false, default: -> { 'now()' } | ||
end | ||
end | ||
|
||
def delete_version(version) | ||
return super unless connection.is_a?(::ActiveRecord::ConnectionAdapters::ClickhouseAdapter) | ||
|
||
im = Arel::InsertManager.new(arel_table) | ||
im.insert(arel_table[primary_key] => version.to_s, arel_table['active'] => 0) | ||
connection.insert(im, "#{self.class} Create Rollback Version", primary_key, version) | ||
end | ||
|
||
def all_versions | ||
return super unless connection.is_a?(::ActiveRecord::ConnectionAdapters::ClickhouseAdapter) | ||
|
||
final.where(active: 1).order(:version).pluck(:version) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module CoreExtensions | ||
module Arel # :nodoc: all | ||
module Nodes | ||
module SelectCore | ||
attr_accessor :final | ||
|
||
def source | ||
return super unless final | ||
|
||
::Arel::Nodes::Final.new(super) | ||
end | ||
|
||
def eql?(other) | ||
super && final == other.final | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
module CoreExtensions | ||
module Arel | ||
module Table | ||
attr_accessor :final | ||
|
||
def is_view | ||
type_caster.is_view | ||
end | ||
|
Oops, something went wrong.