activerecord-tablefree 3.1.8 → 3.1.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,6 +22,16 @@ module ActiveRecord::Tablefree
22
22
  args
23
23
  end
24
24
 
25
+ # Needed for ActiveRecord 7.2+
26
+ def serialized?
27
+ false
28
+ end
29
+
30
+ # Needed for ActiveRecord 7.2+
31
+ def mutable?
32
+ false
33
+ end
34
+
25
35
  def changed?(*_args)
26
36
  false
27
37
  end
@@ -32,7 +42,7 @@ module ActiveRecord::Tablefree
32
42
 
33
43
  def force_equality?(value)
34
44
  case value
35
- when ::Array, ::Range then
45
+ when ::Array, ::Range
36
46
  true
37
47
  else
38
48
  false
@@ -1,13 +1,13 @@
1
1
  module ActiveRecord::Tablefree
2
2
  class Connection < ActiveRecord::ConnectionAdapters::AbstractAdapter
3
3
  def initialize
4
- @connection = Object.new # The Raw Connection
5
- @owner = nil
6
- @instrumenter = ActiveSupport::Notifications.instrumenter
7
- @logger = Object.new
8
- @config = Object.new
9
- @pool = nil
10
- @schema_cache = ActiveRecord::Tablefree::SchemaCache.new
4
+ @connection = Object.new # The Raw Connection
5
+ @owner = nil
6
+ @instrumenter = ActiveSupport::Notifications.instrumenter
7
+ @logger = Object.new
8
+ @config = Object.new
9
+ @pool = nil
10
+ @schema_cache = ActiveRecord::Tablefree::SchemaCache.new
11
11
  @quoted_column_names, @quoted_table_names = {}, {}
12
12
  @visitor = Object.new
13
13
  @lock = Object.new
@@ -15,11 +15,11 @@ module ActiveRecord::Tablefree
15
15
  end
16
16
 
17
17
  def quote_table_name(*_args)
18
- ''
18
+ ""
19
19
  end
20
20
 
21
21
  def quote_column_name(*_args)
22
- ''
22
+ ""
23
23
  end
24
24
 
25
25
  def substitute_at(*_args)
@@ -41,8 +41,8 @@ module ActiveRecord::Tablefree
41
41
  def sanitize_limit(limit)
42
42
  if limit.is_a?(Integer) || limit.is_a?(Arel::Nodes::SqlLiteral)
43
43
  limit
44
- elsif limit.to_s.include?(',')
45
- Arel.sql limit.to_s.split(',').map { |i| Integer(i) }.join(',')
44
+ elsif limit.to_s.include?(",")
45
+ Arel.sql limit.to_s.split(",").map { |i| Integer(i) }.join(",")
46
46
  else
47
47
  Integer(limit)
48
48
  end
@@ -52,10 +52,18 @@ module ActiveRecord::Tablefree
52
52
  # Returns a SQL string with the from, join, where, and having clauses,
53
53
  # in addition to the limit and offset.
54
54
  def combine_bind_parameters(**_args)
55
- ''
55
+ ""
56
56
  end
57
57
 
58
- def lookup_cast_type_from_column(*_args)
58
+ def lookup_cast_type_from_column(column = nil, *_args)
59
+ type = column.respond_to?(:type) ? column.type : nil
60
+ if type
61
+ # ActiveModel's registry needs no adapter (and so no database connection).
62
+ ActiveModel::Type.lookup(type)
63
+ else
64
+ @_cast_type ||= ActiveRecord::Tablefree::CastType.new
65
+ end
66
+ rescue ArgumentError
59
67
  @_cast_type ||= ActiveRecord::Tablefree::CastType.new
60
68
  end
61
69
 
@@ -67,6 +75,15 @@ module ActiveRecord::Tablefree
67
75
  {}
68
76
  end
69
77
 
78
+ # Tablefree models never persist, so transactions just run their block.
79
+ def transaction(*_args, **_kwargs)
80
+ yield
81
+ end
82
+
83
+ def pool
84
+ @_pool ||= ActiveRecord::Tablefree::ConnectionPool.new(self)
85
+ end
86
+
70
87
  # This is used in the StatementCache object.
71
88
  def cacheable_query(arel) # :nodoc:
72
89
  ActiveRecord::Tablefree::StatementCache.partial_query visitor, arel.ast, collector
@@ -0,0 +1,28 @@
1
+ module ActiveRecord::Tablefree
2
+ # Minimal stand-in for ActiveRecord's connection pool. Newer ActiveRecord
3
+ # (7.2+) reaches the connection through the pool for scoping, alias
4
+ # tracking, and transactions.
5
+ class ConnectionPool
6
+ attr_reader :connection
7
+
8
+ def initialize(connection)
9
+ @connection = connection
10
+ end
11
+
12
+ def lease_connection
13
+ connection
14
+ end
15
+
16
+ def with_connection(**)
17
+ yield connection
18
+ end
19
+
20
+ def with_pool_transaction_isolation_level(*)
21
+ yield
22
+ end
23
+
24
+ def schema_cache
25
+ connection.schema_cache
26
+ end
27
+ end
28
+ end
@@ -2,6 +2,12 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module Tablefree
5
- VERSION = '3.1.8'.freeze
5
+ # Version namespace for this gem.
6
+ module Version
7
+ # Current gem version.
8
+ VERSION = "3.1.9"
9
+ end
10
+ # Current gem version exposed at the traditional constant location.
11
+ VERSION = Version::VERSION # Traditional Constant Location
6
12
  end
7
13
  end
@@ -1,11 +1,13 @@
1
- require 'cgi'
2
- require 'active_record'
1
+ require_relative "tablefree/version"
3
2
 
4
- require 'activerecord/tablefree/version'
5
- require 'activerecord/tablefree/cast_type'
6
- require 'activerecord/tablefree/schema_cache'
7
- require 'activerecord/tablefree/connection'
8
- require 'activerecord/tablefree/transaction'
3
+ require "cgi"
4
+ require "active_record"
5
+
6
+ require "activerecord/tablefree/cast_type"
7
+ require "activerecord/tablefree/schema_cache"
8
+ require "activerecord/tablefree/connection"
9
+ require "activerecord/tablefree/connection_pool"
10
+ require "activerecord/tablefree/transaction"
9
11
 
10
12
  module ActiveRecord
11
13
  # = ActiveRecord::Tablefree
@@ -45,18 +47,19 @@ module ActiveRecord
45
47
  module ActsMethods #:nodoc:
46
48
  # A model that needs to be tablefree will call this method to indicate
47
49
  # it.
48
- def has_no_table(options = { database: :fail_fast })
50
+ def has_no_table(options = {database: :fail_fast})
49
51
  raise ArgumentError, "Invalid database option '#{options[:database]}'" unless %i[fail_fast pretend_success].member? options[:database]
50
52
  # keep our options handy
51
53
  class_attribute :tablefree_options
52
54
  self.tablefree_options = {
53
55
  database: options[:database],
54
- columns_hash: {}
56
+ columns_hash: {},
57
+ cast_types: {}
55
58
  }
56
59
 
57
60
  # extend
58
- extend ActiveRecord::Tablefree::SingletonMethods
59
- extend ActiveRecord::Tablefree::ClassMethods
61
+ extend ActiveRecord::Tablefree::SingletonMethods
62
+ extend ActiveRecord::Tablefree::ClassMethods
60
63
 
61
64
  # include
62
65
  include ActiveRecord::Tablefree::InstanceMethods
@@ -78,7 +81,8 @@ module ActiveRecord
78
81
  @columns_hash.each do |name, column|
79
82
  define_attribute(
80
83
  name,
81
- connection.lookup_cast_type_from_column(column),
84
+ # Hash#fetch, not Thread#fetch.
85
+ tablefree_options[:cast_types].fetch(name) { column.sql_type_metadata }, # rubocop:disable Lint/LtsRuby/UnavailableMethod
82
86
  default: column.default,
83
87
  user_provided_default: false
84
88
  )
@@ -88,13 +92,30 @@ module ActiveRecord
88
92
  # Register a new column.
89
93
  def column(name, sql_type = nil, default = nil, null = true, cast_class_params = nil)
90
94
  cast_class = if sql_type.is_a?(Class) && sql_type < ActiveModel::Type::Value
91
- sql_type
92
- else
93
- "ActiveRecord::Type::#{sql_type.to_s.camelize}".constantize rescue nil
94
- end
95
+ sql_type
96
+ else
97
+ begin
98
+ "ActiveRecord::Type::#{sql_type.to_s.camelize}".constantize
99
+ rescue
100
+ nil
101
+ end
102
+ end
95
103
  raise InvalidColumnType, "sql_type is #{sql_type} (#{sql_type.class}), which is not supported" unless cast_class.respond_to?(:new)
96
104
  cast_type = cast_class.new(*cast_class_params)
97
- tablefree_options[:columns_hash][name.to_s] = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, cast_type, null)
105
+ tablefree_options[:cast_types][name.to_s] = cast_type
106
+ # Hash#[]= and String#to_s on column names, not Matrix/Vector#[]= or Set#to_s.
107
+ # rubocop:disable Lint/LtsRuby/UnavailableMethod
108
+ tablefree_options[:columns_hash][name.to_s] =
109
+ if ActiveRecord.gem_version >= Gem::Version.new("8.1")
110
+ ActiveRecord::ConnectionAdapters::Column.new(name.to_s, cast_type, default, nil, null)
111
+ elsif ActiveRecord::VERSION::MAJOR >= 6
112
+ type_name = cast_type.respond_to?(:type) ? cast_type.type : nil
113
+ metadata = type_name && ActiveRecord::ConnectionAdapters::SqlTypeMetadata.new(sql_type: type_name.to_s, type: type_name)
114
+ ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, metadata, null)
115
+ else
116
+ ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, cast_type, null)
117
+ end
118
+ # rubocop:enable Lint/LtsRuby/UnavailableMethod
98
119
  end
99
120
 
100
121
  # Register a set of columns with the same SQL type
@@ -113,6 +134,12 @@ module ActiveRecord
113
134
  end
114
135
  end
115
136
 
137
+ def all(*_args)
138
+ return [] if tablefree_options[:database] == :pretend_success
139
+
140
+ super
141
+ end
142
+
116
143
  def destroy_all(*_args)
117
144
  case tablefree_options[:database]
118
145
  when :pretend_success
@@ -122,18 +149,13 @@ module ActiveRecord
122
149
  end
123
150
  end
124
151
 
125
- case ActiveRecord::VERSION::MAJOR
126
- when 5
127
- def find_by_sql(*_args)
128
- case tablefree_options[:database]
129
- when :pretend_success
130
- []
131
- when :fail_fast
132
- raise NoDatabase, "Can't #find_by_sql on Tablefree class"
133
- end
152
+ def find_by_sql(*_args)
153
+ case tablefree_options[:database]
154
+ when :pretend_success
155
+ []
156
+ when :fail_fast
157
+ raise NoDatabase, "Can't #find_by_sql on Tablefree class"
134
158
  end
135
- else
136
- raise Unsupported, 'Unsupported ActiveRecord version'
137
159
  end
138
160
 
139
161
  def transaction
@@ -160,9 +182,9 @@ module ActiveRecord
160
182
  if query_string.blank?
161
183
  new
162
184
  else
163
- params = query_string.split('&').collect do |chunk|
185
+ params = query_string.split("&").collect do |chunk|
164
186
  next if chunk.empty?
165
- key, value = chunk.split('=', 2)
187
+ key, value = chunk.split("=", 2)
166
188
  next if key.empty?
167
189
  value = value.nil? ? nil : CGI.unescape(value)
168
190
  [CGI.unescape(key), value]
@@ -175,15 +197,27 @@ module ActiveRecord
175
197
  def connection
176
198
  @_connection ||= ActiveRecord::Tablefree::Connection.new
177
199
  end
200
+
201
+ def lease_connection
202
+ connection
203
+ end
204
+
205
+ def with_connection(**)
206
+ yield connection
207
+ end
208
+
209
+ def connection_pool
210
+ connection.pool
211
+ end
178
212
  end
179
213
 
180
214
  module InstanceMethods
181
215
  def to_query_string(prefix = nil)
182
- attributes.to_a.collect { |(name, value)| escaped_var_name(name, prefix) + '=' + escape_for_url(value) if value }.compact.join('&')
216
+ attributes.to_a.collect { |(name, value)| escaped_var_name(name, prefix) + "=" + escape_for_url(value) if value }.compact.join("&")
183
217
  end
184
218
 
185
219
  def quote_value(_value, _column = nil)
186
- ''
220
+ ""
187
221
  end
188
222
 
189
223
  %w[create create_record _create_record update update_record _update_record].each do |method_name|
@@ -216,7 +250,8 @@ module ActiveRecord
216
250
  end
217
251
  end
218
252
 
219
- def add_to_transaction; end
253
+ def add_to_transaction(*_args)
254
+ end
220
255
 
221
256
  private
222
257
 
@@ -226,13 +261,13 @@ module ActiveRecord
226
261
 
227
262
  def escape_for_url(value)
228
263
  case value
229
- when true then '1'
230
- when false then '0'
231
- when nil then ''
264
+ when true then "1"
265
+ when false then "0"
266
+ when nil then ""
232
267
  else CGI.escape(value.to_s)
233
268
  end
234
269
  rescue
235
- ''
270
+ ""
236
271
  end
237
272
  end
238
273
  end
@@ -1,2 +1,8 @@
1
1
  # legacy automatic require support
2
- require 'activerecord/tablefree'
2
+ require "activerecord/tablefree"
3
+ require "version_gem"
4
+ require_relative "activerecord/tablefree/version"
5
+
6
+ ActiveRecord::Tablefree::Version.class_eval do
7
+ extend VersionGem::Basic
8
+ end
@@ -0,0 +1,21 @@
1
+ module ActiveRecord
2
+ module Tablefree
3
+ module ActsMethods
4
+ module Version
5
+ VERSION: String
6
+ end
7
+ VERSION: String
8
+ end
9
+
10
+ module Version
11
+ VERSION: String
12
+ end
13
+ VERSION: String
14
+ module NoDatabase
15
+ module Version
16
+ VERSION: String
17
+ end
18
+ VERSION: String
19
+ end
20
+ end
21
+ end
data.tar.gz.sig ADDED
Binary file