diff --git a/lib/activerecord_slotted_counters/adapters/pg_upsert.rb b/lib/activerecord_slotted_counters/adapters/pg_sqlite_upsert.rb similarity index 77% rename from lib/activerecord_slotted_counters/adapters/pg_upsert.rb rename to lib/activerecord_slotted_counters/adapters/pg_sqlite_upsert.rb index b16ad8f..ec9b92e 100644 --- a/lib/activerecord_slotted_counters/adapters/pg_upsert.rb +++ b/lib/activerecord_slotted_counters/adapters/pg_sqlite_upsert.rb @@ -2,7 +2,7 @@ module ActiveRecordSlottedCounters module Adapters - class PgUpsert + class PgSqliteUpsert attr_reader :klass def initialize(klass) @@ -10,7 +10,11 @@ def initialize(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) @@ -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 diff --git a/lib/activerecord_slotted_counters/adapters/rails_upsert.rb b/lib/activerecord_slotted_counters/adapters/rails_upsert.rb index 1dd103e..6c551ff 100644 --- a/lib/activerecord_slotted_counters/adapters/rails_upsert.rb +++ b/lib/activerecord_slotted_counters/adapters/rails_upsert.rb @@ -3,7 +3,7 @@ module ActiveRecordSlottedCounters module Adapters class RailsUpsert - attr_reader :klass + attr_reader :klass, :current_adapter_name def initialize(klass) @klass = klass diff --git a/lib/activerecord_slotted_counters/has_slotted_counter.rb b/lib/activerecord_slotted_counters/has_slotted_counter.rb index 259db8c..ab86ee6 100644 --- a/lib/activerecord_slotted_counters/has_slotted_counter.rb +++ b/lib/activerecord_slotted_counters/has_slotted_counter.rb @@ -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 @@ -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 diff --git a/spec/slotted_counter_spec.rb b/spec/slotted_counter_spec.rb index 6645412..cc41d44 100644 --- a/spec/slotted_counter_spec.rb +++ b/spec/slotted_counter_spec.rb @@ -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 diff --git a/spec/support/shared_examples_for_cache_counters.rb b/spec/support/shared_examples_for_cache_counters.rb index c1e1200..c9342e3 100644 --- a/spec/support/shared_examples_for_cache_counters.rb +++ b/spec/support/shared_examples_for_cache_counters.rb @@ -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