Skip to content

Commit

Permalink
Merge pull request #11 from getlago/sync-upstream
Browse files Browse the repository at this point in the history
Sync with upstream
  • Loading branch information
vincent-pochet authored Oct 8, 2024
2 parents 3c4b369 + 27c3ee0 commit 683125a
Show file tree
Hide file tree
Showing 58 changed files with 609 additions and 287 deletions.
36 changes: 28 additions & 8 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ jobs:
fail-fast: true
max-parallel: 1
matrix:
ruby-version: [ '2.7', '3.0', '3.2' ]
clickhouse: [ '22.1' ]
version:
- ruby: 2.7
rails: 7.1.3
- ruby: 3.0
rails: 7.1.3
- ruby: 3.2
rails: 7.1.3
- ruby: 3.2
rails: 7.2.0
clickhouse: [ '22.1', '24.6' ]

steps:
- uses: actions/checkout@v4
Expand All @@ -33,10 +41,12 @@ jobs:
compose-file: '.docker/docker-compose.yml'
down-flags: '--volumes'

- name: Set up Ruby ${{ matrix.ruby-version }}
- run: echo 'gem "activerecord", "~> ${{ matrix.version.rails }}"' >> Gemfile

- name: Set up Ruby ${{ matrix.version.ruby }}
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
ruby-version: ${{ matrix.version.ruby }}
bundler-cache: true

- run: bundle exec rspec spec/single
Expand All @@ -54,8 +64,16 @@ jobs:
fail-fast: true
max-parallel: 1
matrix:
ruby-version: [ '2.7', '3.0', '3.2' ]
clickhouse: [ '22.1' ]
version:
- ruby: 2.7
rails: 7.1.3
- ruby: 3.0
rails: 7.1.3
- ruby: 3.2
rails: 7.1.3
- ruby: 3.2
rails: 7.2.0
clickhouse: [ '22.1', '24.6' ]

steps:
- uses: actions/checkout@v4
Expand All @@ -68,10 +86,12 @@ jobs:
compose-file: '.docker/docker-compose.cluster.yml'
down-flags: '--volumes'

- name: Set up Ruby ${{ matrix.ruby-version }}
- run: echo 'gem "activerecord", "~> ${{ matrix.version.rails }}"' >> Gemfile

- name: Set up Ruby ${{ matrix.version.ruby }}
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
ruby-version: ${{ matrix.version.ruby }}
bundler-cache: true

- run: bundle exec rspec spec/cluster
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ crashlytics.properties
crashlytics-build.properties
fabric.properties
.rspec_status
.tool-versions
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
### Version 1.1.2 (Aug 27, 2024)
* 🎉 Support for rails 7.2 #156
* Add method `views` for getting table `View` list in #152
* Add support for Map datatype in #144
* Add support window named functions
* Fix schema dumper default values for number
* Normalize table name in schema dump in #148
* Noop savepoint functionality in #150
* Fix `#find_by` in #153
* Add RSpec configure
* Fix detect model primary key

### Version 1.0.7 (Apr 27, 2024)

* Support table indexes
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,15 @@ Structure load from `db/clickhouse_structure.sql` file:
$ rake db:schema:dump
$ rake db:schema:load
$ rake db:structure:dump
$ rake db:structure:load
$ rake db:structure:load

### RSpec

For auto truncate tables before each test add to `spec/rails_helper.rb` file:

```
require 'clickhouse-activerecord/rspec'
```

### Insert and select data

Expand Down Expand Up @@ -199,6 +207,7 @@ false`. The default integer is `UInt32`
| UInt64 | 0 to 18446744073709551615 | 5,6,7,8 |
| UInt256 | 0 to ... | 8+ |
| Array | ... | ... |
| Map | ... | ... |

Example:

Expand Down
2 changes: 1 addition & 1 deletion clickhouse-activerecord.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ['lib']

spec.add_runtime_dependency 'bundler', '>= 1.13.4'
spec.add_runtime_dependency 'activerecord', '>= 7.1'
spec.add_runtime_dependency 'activerecord', '~> 7.1'

spec.add_development_dependency 'rake', '~> 13.0'
spec.add_development_dependency 'rspec', '~> 3.4'
Expand Down
92 changes: 41 additions & 51 deletions lib/active_record/connection_adapters/clickhouse/oid/map.rb
Original file line number Diff line number Diff line change
@@ -1,76 +1,66 @@
# frozen_string_literal: true

require 'yaml'

module ActiveRecord
module ConnectionAdapters
module Clickhouse
module OID # :nodoc:
class Map < Type::Value # :nodoc:
attr_reader :key_type, :value_type

def initialize(sql_type)
types = sql_type.match(/Map\((.+),\s?(.+)\)/).captures

@key_type = cast_type(types.first)
@value_type = cast_type(types.last)
@subtype = case sql_type
when /U?Int\d+/
:integer
when /DateTime/
:datetime
when /Date/
:date
else
:string
end
end

def type
:map
end

def cast(value)
value
@subtype
end

def deserialize(value)
return value if value.is_a?(Hash)

YAML.safe_load(value)
end

def serialize(value)
return '{}' if value.nil?

res = value.map { |k, v| "#{quote(k, key_type)}: #{quote(v, value_type)}" }.join(', ')
"{#{res}}"
end

private

def cast_type(type)
return type if type.nil?

case type
when /U?Int\d+/
:integer
when /DateTime/
:datetime
when /Date/
:date
when /Array\(.*\)/
type
if value.is_a?(::Hash)
value.map { |k, item| [k.to_s, deserialize(item)] }.to_h
else
:string
return value if value.nil?
case @subtype
when :integer
value.to_i
when :datetime
::DateTime.parse(value)
when :date
::Date.parse(value)
else
super
end
end
end

def quote(value, type)
case cast_type(type)
when :string
"'#{value}'"
when :integer
value
when :datetime, :date
"'#{value.iso8601}'"
when /Array\(.*\)/
sub_type = type.match(/Array\((.+)\)/).captures.first
"[#{value.map { |v| quote(v, sub_type) }.join(', ')}]"
def serialize(value)
if value.is_a?(::Hash)
value.map { |k, item| [k.to_s, serialize(item)] }.to_h
else
value
return value if value.nil?
case @subtype
when :integer
value.to_i
when :datetime
DateTime.new.serialize(value)
when :date
Date.new.serialize(value)
when :string
value.to_s
else
super
end
end
end

end
end
end
Expand Down
19 changes: 19 additions & 0 deletions lib/active_record/connection_adapters/clickhouse/quoting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module ActiveRecord
module ConnectionAdapters
module Clickhouse
module Quoting
extend ActiveSupport::Concern

module ClassMethods # :nodoc:
def quote_column_name(name)
name.to_s.include?('.') ? "`#{name}`" : name.to_s
end

def quote_table_name(name)
name.to_s
end
end
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ def add_column_options!(sql, options)
if options[:array]
sql.gsub!(/\s+(.*)/, ' Array(\1)')
end
if options[:map]
sql.gsub!(/\s+(.*)/, ' Map(String, \1)')
end
sql.gsub!(/(\sString)\(\d+\)/, '\1')
sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
sql
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,10 @@ def enum(*args, **options)
args.each { |name| column(name, kind, **options.except(:limit)) }
end

def map(*args, **options)
key_type = options[:key_type].to_s.camelize
value_type = options[:value_type].to_s.camelize
private

args.each { |name| column(name, :"Map(#{key_type}, #{value_type})", **options.except(:limit, :key_type, :value_type)) }
def valid_column_definition_options
super + [:array, :low_cardinality, :fixed_string, :value, :type, :map]
end
end

Expand Down
Loading

0 comments on commit 683125a

Please sign in to comment.