Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ jobs:
bundle install

- name: Run tests
run: bundle exec rspec
run: bundle exec rspec --backtrace
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,5 @@ tmp
crate_test_server*
*.log
log/*
crate-*.tar.gz
parts/
2 changes: 1 addition & 1 deletion DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ docker run --rm -it --publish=44200:4200 crate:5.1.1 \
Install project and invoke test suite.
```shell
bundle install
bundle exec rspec
bundle exec rspec --backtrace
```


Expand Down
2 changes: 1 addition & 1 deletion activerecord-crate-adapter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec", "~> 2.14"

spec.add_dependency('activerecord', '~> 4.1.0')
spec.add_dependency('activerecord', '~> 6')
spec.add_dependency('arel', '>= 5.0.0')
spec.add_dependency('crate_ruby', '~> 0.2.0')
end
2 changes: 2 additions & 0 deletions history.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

=== unreleased

* Updated to ActiveRecord 6 and crate_ruby 0.2

=== 0.0.4

* Updated crate version to 0.45.7
Expand Down
13 changes: 11 additions & 2 deletions lib/active_record/connection_adapters/crate/schema_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@
module ActiveRecord
module ConnectionAdapters
module Crate
class SchemaCreation < AbstractAdapter::SchemaCreation

class SchemaCreation < ActiveRecord::ConnectionAdapters::SchemaCreation
private

def add_column_options!(sql, options)
Expand All @@ -36,6 +35,16 @@ def add_column_options!(sql, options)
end

module SchemaStatements
def data_source_sql(name = nil, type: nil)
# TODO implement
nil
end

def quoted_scope(name = nil, type: nil)
# TODO implement
nil
end

def primary_key(table_name)
res = @connection.execute("select constraint_name from information_schema.table_constraints
where table_name = '#{quote_table_name(table_name)}' and constraint_type = 'PRIMARY_KEY'")
Expand Down
42 changes: 27 additions & 15 deletions lib/active_record/connection_adapters/crate_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@

require 'active_record'
require 'active_record/base'
require 'active_record/base'
require 'arel/arel_crate'
require 'arel/visitors/bind_visitor'
require 'arel/visitors/visitor'
require 'active_support/dependencies/autoload'
require 'active_support/callbacks'
require 'active_support/core_ext/string'
Expand All @@ -45,7 +44,7 @@ module ActiveRecord
class Base
def self.crate_connection(config) #:nodoc:
config = config.symbolize_keys
ConnectionAdapters::CrateAdapter.new(nil, logger, nil, config)
ConnectionAdapters::CrateAdapter.new(nil, logger, config)
end
end

Expand All @@ -61,7 +60,7 @@ class ColumnDefinition < ActiveRecord::ConnectionAdapters::ColumnDefinition
ADAPTER_NAME = 'Crate'.freeze

def schema_creation # :nodoc:
Crate::SchemaCreation.new self
Crate::SchemaCreation.new self
end

NATIVE_DATABASE_TYPES = {
Expand All @@ -77,13 +76,13 @@ def schema_creation # :nodoc:
}

class BindSubstitution < Arel::Visitors::Crate # :nodoc:
include Arel::Visitors::BindVisitor
include Arel::Visitors
end

def initialize(connection, logger, pool, config)
def initialize(connection, logger, config)
@port = config[:port]
@host = config[:host]
super(connection, logger, pool)
super(connection, logger, config)
@schema_cache = SchemaCache.new self
@visitor = Arel::Visitors::Crate.new self
@quoted_column_names = {}
Expand Down Expand Up @@ -125,12 +124,25 @@ def supports_migrations?
true
end

def supports_datetime_with_precision?
true
end

def connect
@connection = CrateRuby::Client.new(["#{@host}:#{@port}"])

# Monkeypatch to make the client instance provide `supports_datetime_with_precision`.
# FIXME: Implement within `CrateRuby::Client`?
@adapter_supports_datetime_with_precision = supports_datetime_with_precision?
def @connection.supports_datetime_with_precision?
return @adapter_supports_datetime_with_precision
end

end

def columns(table_name) #:nodoc:
cols = @connection.table_structure(table_name).map do |field|
# TODO: Review. What if the table structure changes again?
name = dotted_name(field[12])
CrateColumn.new(name, nil, field[4], nil)
end
Expand Down Expand Up @@ -172,10 +184,10 @@ class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
# +SecureRandom.uuid+ method and a +before_save+ callback, for instance.
def primary_key(name, type = :primary_key, options = {})
options[:primary_key] = true
column name, "STRING PRIMARY KEY", options
column name, "STRING PRIMARY KEY"
end

def column(name, type = nil, options = {})
def column(name, type = nil, **options)
super
column = self[name]
column.array = options[:array]
Expand All @@ -189,19 +201,19 @@ def object(name, options = {})
schema = options.delete(:object_schema)
type = "#{type} as (#{object_schema_to_string(schema)})" if schema

column name, type, options.merge(object: true)
column name, type, **options.merge(object: true)
end

def array(name, options = {})
array_type = options.delete(:array_type)
raise "Array columns must specify an :array_type (e.g. array_type: :string)" unless array_type.present?
column name, "array(#{array_type})", options.merge(array: true)
column name, "array(#{array_type})", **options.merge(array: true)
end

private

def create_column_definition(name, type)
ColumnDefinition.new name, type
def create_column_definition(name, type, options)
ColumnDefinition.new(name, type, options)
end

def object_schema_to_string(s)
Expand All @@ -220,8 +232,8 @@ def object_schema_to_string(s)

end

def create_table_definition(name, temporary, options, as = nil)
TableDefinition.new native_database_types, name, temporary, options, as
def create_table_definition(name)
TableDefinition.new @connection, name
end

def native_database_types
Expand Down
2 changes: 1 addition & 1 deletion lib/arel/arel_crate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@

require 'arel'
require 'arel/visitors/crate'
require 'arel/visitors/bind_visitor'
require 'arel/visitors/visitor'
2 changes: 0 additions & 2 deletions lib/arel/visitors/crate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,3 @@ class ToSql < Arel::Visitors::Visitor
end
end
end

Arel::Visitors::VISITORS['crate'] = Arel::Visitors::Crate
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

describe '#object_schema_to_string' do

let(:td) { ActiveRecord::ConnectionAdapters::CrateAdapter::TableDefinition.new(nil, nil, nil, nil) }
let(:td) { ActiveRecord::ConnectionAdapters::CrateAdapter::TableDefinition.new(nil, nil) }

it 'should simply set the key and values' do
s = {street: :string, city: :string}
Expand Down
2 changes: 1 addition & 1 deletion spec/models/post_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@
describe 'sql input sanitization' do

before do
@post = Post.create!(params)
@post = Post.create!(**params)
end

after do
Expand Down