Skip to content

Commit

Permalink
add an ability to work with sqlite3 adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
prog-supdex committed Oct 22, 2023
1 parent 5de703e commit 39588a0
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,19 @@

module ActiveRecordSlottedCounters
module Adapters
class PgUpsert
class PgSqliteUpsert
attr_reader :klass

def initialize(klass)
@klass = klass
end

def apply?
ActiveRecord::VERSION::MAJOR < 7 && klass.connection.adapter_name == ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::ADAPTER_NAME
return false if ActiveRecord::VERSION::MAJOR >= 7

adapter_name = klass.connection.adapter_name

postgresql_connection?(adapter_name) || sqlite_connection?(adapter_name)
end

def bulk_insert(attributes, on_duplicate: nil, unique_by: nil)
Expand Down Expand Up @@ -83,6 +87,18 @@ def quote_record(columns, record_values)
def quote_many_records(columns, data)
data.map { |values| quote_record(columns, values) }.join(",")
end

def postgresql_connection?(adapter_name)
return false unless defined?(ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)

adapter_name == ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::ADAPTER_NAME
end

def sqlite_connection?(adapter_name)
return false unless defined?(ActiveRecord::ConnectionAdapters::SQLite3Adapter)

adapter_name == ActiveRecord::ConnectionAdapters::SQLite3Adapter::ADAPTER_NAME
end
end
end
end
2 changes: 1 addition & 1 deletion lib/activerecord_slotted_counters/adapters/rails_upsert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module ActiveRecordSlottedCounters
module Adapters
class RailsUpsert
attr_reader :klass
attr_reader :klass, :current_adapter_name

def initialize(klass)
@klass = klass
Expand Down
6 changes: 3 additions & 3 deletions lib/activerecord_slotted_counters/has_slotted_counter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require "activerecord_slotted_counters/utils"

require "activerecord_slotted_counters/adapters/rails_upsert"
require "activerecord_slotted_counters/adapters/pg_upsert"
require "activerecord_slotted_counters/adapters/pg_sqlite_upsert"

module ActiveRecordSlottedCounters
class SlottedCounter < ::ActiveRecord::Base
Expand Down Expand Up @@ -42,14 +42,14 @@ def slotted_counter_db_adapter
def set_slotted_counter_db_adapter
available_adapters = [
ActiveRecordSlottedCounters::Adapters::RailsUpsert,
ActiveRecordSlottedCounters::Adapters::PgUpsert
ActiveRecordSlottedCounters::Adapters::PgSqliteUpsert
]

adapter = available_adapters
.map { |adapter| adapter.new(self) }
.detect { |adapter| adapter.apply? }

raise NotSupportedAdapter.new(connection.adapter_name) if adapter.nil?
raise NotSupportedAdapter.new(current_adapter_name) if adapter.nil?

adapter
end
Expand Down
11 changes: 9 additions & 2 deletions spec/slotted_counter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,18 @@
def insert_association_sql(association_class, article_id)
association_table = association_class.arel_table
foreign_key = association_class.reflections["article"].foreign_key
current_date_sql_command =
if defined?(ActiveRecord::ConnectionAdapters::SQLite3Adapter)
"date('now')"
else
"now()"
end

insert_manager = Arel::InsertManager.new
insert_manager.insert([
[association_table[foreign_key], article_id],
[association_table[:created_at], Arel.sql("now()")],
[association_table[:updated_at], Arel.sql("now()")]
[association_table[:created_at], Arel.sql(current_date_sql_command)],
[association_table[:updated_at], Arel.sql(current_date_sql_command)]
])

insert_manager.to_sql
Expand Down
11 changes: 9 additions & 2 deletions spec/support/shared_examples_for_cache_counters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,11 +153,18 @@
def insert_comment_sql(comment_class, article_id)
comment_table = comment_class.arel_table
foreign_key = comment_class.reflections["article"].foreign_key
current_date_sql_command =
if defined?(ActiveRecord::ConnectionAdapters::SQLite3Adapter)
"date('now')"
else
"now()"
end

insert_manager = Arel::InsertManager.new
insert_manager.insert([
[comment_table[foreign_key], article_id],
[comment_table[:created_at], Arel.sql("now()")],
[comment_table[:updated_at], Arel.sql("now()")]
[comment_table[:created_at], Arel.sql(current_date_sql_command)],
[comment_table[:updated_at], Arel.sql(current_date_sql_command)]
])

insert_manager.to_sql
Expand Down

0 comments on commit 39588a0

Please sign in to comment.