activerecord 7.0.0 → 7.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (251) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1701 -1039
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +18 -18
  5. data/lib/active_record/aggregations.rb +16 -13
  6. data/lib/active_record/association_relation.rb +1 -1
  7. data/lib/active_record/associations/association.rb +18 -3
  8. data/lib/active_record/associations/association_scope.rb +16 -9
  9. data/lib/active_record/associations/belongs_to_association.rb +14 -6
  10. data/lib/active_record/associations/builder/association.rb +3 -3
  11. data/lib/active_record/associations/builder/belongs_to.rb +21 -8
  12. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -5
  13. data/lib/active_record/associations/builder/singular_association.rb +4 -0
  14. data/lib/active_record/associations/collection_association.rb +17 -12
  15. data/lib/active_record/associations/collection_proxy.rb +22 -12
  16. data/lib/active_record/associations/foreign_association.rb +10 -3
  17. data/lib/active_record/associations/has_many_association.rb +27 -17
  18. data/lib/active_record/associations/has_many_through_association.rb +10 -6
  19. data/lib/active_record/associations/has_one_association.rb +10 -3
  20. data/lib/active_record/associations/join_dependency.rb +20 -14
  21. data/lib/active_record/associations/preloader/association.rb +27 -6
  22. data/lib/active_record/associations/preloader/through_association.rb +1 -1
  23. data/lib/active_record/associations/preloader.rb +13 -10
  24. data/lib/active_record/associations/singular_association.rb +1 -1
  25. data/lib/active_record/associations/through_association.rb +22 -11
  26. data/lib/active_record/associations.rb +362 -236
  27. data/lib/active_record/attribute_assignment.rb +0 -2
  28. data/lib/active_record/attribute_methods/before_type_cast.rb +17 -0
  29. data/lib/active_record/attribute_methods/dirty.rb +52 -34
  30. data/lib/active_record/attribute_methods/primary_key.rb +76 -24
  31. data/lib/active_record/attribute_methods/query.rb +28 -16
  32. data/lib/active_record/attribute_methods/read.rb +18 -5
  33. data/lib/active_record/attribute_methods/serialization.rb +172 -69
  34. data/lib/active_record/attribute_methods/write.rb +3 -3
  35. data/lib/active_record/attribute_methods.rb +110 -28
  36. data/lib/active_record/attributes.rb +3 -3
  37. data/lib/active_record/autosave_association.rb +56 -10
  38. data/lib/active_record/base.rb +10 -5
  39. data/lib/active_record/callbacks.rb +16 -32
  40. data/lib/active_record/coders/column_serializer.rb +61 -0
  41. data/lib/active_record/coders/json.rb +1 -1
  42. data/lib/active_record/coders/yaml_column.rb +70 -34
  43. data/lib/active_record/connection_adapters/abstract/connection_handler.rb +164 -89
  44. data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +2 -0
  45. data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +3 -1
  46. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +63 -43
  47. data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -0
  48. data/lib/active_record/connection_adapters/abstract/database_statements.rb +129 -31
  49. data/lib/active_record/connection_adapters/abstract/query_cache.rb +60 -22
  50. data/lib/active_record/connection_adapters/abstract/quoting.rb +52 -8
  51. data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
  52. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +18 -4
  53. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +163 -29
  54. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +14 -1
  55. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +302 -131
  56. data/lib/active_record/connection_adapters/abstract/transaction.rb +287 -58
  57. data/lib/active_record/connection_adapters/abstract_adapter.rb +513 -106
  58. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +217 -104
  59. data/lib/active_record/connection_adapters/column.rb +9 -0
  60. data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
  61. data/lib/active_record/connection_adapters/mysql/database_statements.rb +23 -144
  62. data/lib/active_record/connection_adapters/mysql/quoting.rb +29 -12
  63. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
  64. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +10 -1
  65. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +8 -2
  66. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +38 -14
  67. data/lib/active_record/connection_adapters/mysql2/database_statements.rb +151 -0
  68. data/lib/active_record/connection_adapters/mysql2_adapter.rb +98 -53
  69. data/lib/active_record/connection_adapters/pool_config.rb +14 -5
  70. data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
  71. data/lib/active_record/connection_adapters/postgresql/column.rb +16 -3
  72. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +75 -45
  73. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -1
  74. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +2 -2
  75. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
  76. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +11 -2
  77. data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +4 -2
  78. data/lib/active_record/connection_adapters/postgresql/quoting.rb +41 -8
  79. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +6 -10
  80. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -6
  81. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +131 -2
  82. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +53 -0
  83. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +372 -63
  84. data/lib/active_record/connection_adapters/postgresql/utils.rb +9 -10
  85. data/lib/active_record/connection_adapters/postgresql_adapter.rb +359 -197
  86. data/lib/active_record/connection_adapters/schema_cache.rb +287 -59
  87. data/lib/active_record/connection_adapters/sqlite3/column.rb +49 -0
  88. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +52 -39
  89. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +22 -5
  90. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +7 -0
  91. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +41 -22
  92. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +242 -81
  93. data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
  94. data/lib/active_record/connection_adapters/trilogy/database_statements.rb +99 -0
  95. data/lib/active_record/connection_adapters/trilogy_adapter.rb +254 -0
  96. data/lib/active_record/connection_adapters.rb +3 -1
  97. data/lib/active_record/connection_handling.rb +73 -96
  98. data/lib/active_record/core.rb +142 -153
  99. data/lib/active_record/counter_cache.rb +46 -25
  100. data/lib/active_record/database_configurations/connection_url_resolver.rb +1 -0
  101. data/lib/active_record/database_configurations/database_config.rb +9 -3
  102. data/lib/active_record/database_configurations/hash_config.rb +22 -12
  103. data/lib/active_record/database_configurations/url_config.rb +17 -11
  104. data/lib/active_record/database_configurations.rb +87 -34
  105. data/lib/active_record/delegated_type.rb +9 -4
  106. data/lib/active_record/deprecator.rb +7 -0
  107. data/lib/active_record/destroy_association_async_job.rb +2 -0
  108. data/lib/active_record/disable_joins_association_relation.rb +1 -1
  109. data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
  110. data/lib/active_record/encryption/cipher/aes256_gcm.rb +4 -1
  111. data/lib/active_record/encryption/config.rb +25 -1
  112. data/lib/active_record/encryption/configurable.rb +13 -14
  113. data/lib/active_record/encryption/context.rb +10 -3
  114. data/lib/active_record/encryption/contexts.rb +8 -4
  115. data/lib/active_record/encryption/derived_secret_key_provider.rb +9 -3
  116. data/lib/active_record/encryption/deterministic_key_provider.rb +1 -1
  117. data/lib/active_record/encryption/encryptable_record.rb +38 -22
  118. data/lib/active_record/encryption/encrypted_attribute_type.rb +19 -8
  119. data/lib/active_record/encryption/encryptor.rb +7 -7
  120. data/lib/active_record/encryption/envelope_encryption_key_provider.rb +3 -3
  121. data/lib/active_record/encryption/extended_deterministic_queries.rb +83 -86
  122. data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +3 -3
  123. data/lib/active_record/encryption/key_generator.rb +12 -1
  124. data/lib/active_record/encryption/message.rb +1 -1
  125. data/lib/active_record/encryption/message_serializer.rb +2 -0
  126. data/lib/active_record/encryption/properties.rb +4 -4
  127. data/lib/active_record/encryption/scheme.rb +20 -23
  128. data/lib/active_record/encryption.rb +1 -0
  129. data/lib/active_record/enum.rb +113 -29
  130. data/lib/active_record/errors.rb +108 -15
  131. data/lib/active_record/explain.rb +23 -3
  132. data/lib/active_record/explain_subscriber.rb +1 -1
  133. data/lib/active_record/fixture_set/model_metadata.rb +14 -4
  134. data/lib/active_record/fixture_set/render_context.rb +2 -0
  135. data/lib/active_record/fixture_set/table_row.rb +29 -8
  136. data/lib/active_record/fixtures.rb +121 -73
  137. data/lib/active_record/future_result.rb +30 -5
  138. data/lib/active_record/gem_version.rb +3 -3
  139. data/lib/active_record/inheritance.rb +30 -16
  140. data/lib/active_record/insert_all.rb +57 -10
  141. data/lib/active_record/integration.rb +10 -10
  142. data/lib/active_record/internal_metadata.rb +120 -30
  143. data/lib/active_record/locking/optimistic.rb +32 -18
  144. data/lib/active_record/locking/pessimistic.rb +8 -5
  145. data/lib/active_record/log_subscriber.rb +39 -17
  146. data/lib/active_record/marshalling.rb +56 -0
  147. data/lib/active_record/message_pack.rb +124 -0
  148. data/lib/active_record/middleware/database_selector/resolver.rb +4 -0
  149. data/lib/active_record/middleware/database_selector.rb +18 -13
  150. data/lib/active_record/middleware/shard_selector.rb +7 -5
  151. data/lib/active_record/migration/command_recorder.rb +108 -10
  152. data/lib/active_record/migration/compatibility.rb +158 -64
  153. data/lib/active_record/migration/default_strategy.rb +23 -0
  154. data/lib/active_record/migration/execution_strategy.rb +19 -0
  155. data/lib/active_record/migration/pending_migration_connection.rb +21 -0
  156. data/lib/active_record/migration.rb +274 -117
  157. data/lib/active_record/model_schema.rb +86 -54
  158. data/lib/active_record/nested_attributes.rb +24 -6
  159. data/lib/active_record/normalization.rb +167 -0
  160. data/lib/active_record/persistence.rb +200 -47
  161. data/lib/active_record/promise.rb +84 -0
  162. data/lib/active_record/query_cache.rb +3 -21
  163. data/lib/active_record/query_logs.rb +87 -51
  164. data/lib/active_record/query_logs_formatter.rb +41 -0
  165. data/lib/active_record/querying.rb +16 -3
  166. data/lib/active_record/railtie.rb +128 -62
  167. data/lib/active_record/railties/controller_runtime.rb +12 -8
  168. data/lib/active_record/railties/databases.rake +145 -146
  169. data/lib/active_record/railties/job_runtime.rb +23 -0
  170. data/lib/active_record/readonly_attributes.rb +32 -5
  171. data/lib/active_record/reflection.rb +189 -45
  172. data/lib/active_record/relation/batches/batch_enumerator.rb +5 -3
  173. data/lib/active_record/relation/batches.rb +190 -61
  174. data/lib/active_record/relation/calculations.rb +208 -83
  175. data/lib/active_record/relation/delegation.rb +23 -9
  176. data/lib/active_record/relation/finder_methods.rb +77 -16
  177. data/lib/active_record/relation/merger.rb +2 -0
  178. data/lib/active_record/relation/predicate_builder/association_query_value.rb +31 -3
  179. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +4 -6
  180. data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
  181. data/lib/active_record/relation/predicate_builder.rb +26 -14
  182. data/lib/active_record/relation/query_attribute.rb +25 -1
  183. data/lib/active_record/relation/query_methods.rb +430 -77
  184. data/lib/active_record/relation/spawn_methods.rb +18 -1
  185. data/lib/active_record/relation.rb +98 -41
  186. data/lib/active_record/result.rb +25 -9
  187. data/lib/active_record/runtime_registry.rb +10 -1
  188. data/lib/active_record/sanitization.rb +57 -16
  189. data/lib/active_record/schema.rb +36 -22
  190. data/lib/active_record/schema_dumper.rb +65 -23
  191. data/lib/active_record/schema_migration.rb +68 -33
  192. data/lib/active_record/scoping/default.rb +20 -12
  193. data/lib/active_record/scoping/named.rb +2 -2
  194. data/lib/active_record/scoping.rb +2 -1
  195. data/lib/active_record/secure_password.rb +60 -0
  196. data/lib/active_record/secure_token.rb +21 -3
  197. data/lib/active_record/serialization.rb +5 -0
  198. data/lib/active_record/signed_id.rb +9 -7
  199. data/lib/active_record/store.rb +16 -11
  200. data/lib/active_record/suppressor.rb +3 -1
  201. data/lib/active_record/table_metadata.rb +16 -3
  202. data/lib/active_record/tasks/database_tasks.rb +138 -107
  203. data/lib/active_record/tasks/mysql_database_tasks.rb +15 -6
  204. data/lib/active_record/tasks/postgresql_database_tasks.rb +17 -15
  205. data/lib/active_record/tasks/sqlite_database_tasks.rb +15 -7
  206. data/lib/active_record/test_fixtures.rb +123 -99
  207. data/lib/active_record/timestamp.rb +27 -15
  208. data/lib/active_record/token_for.rb +113 -0
  209. data/lib/active_record/touch_later.rb +11 -6
  210. data/lib/active_record/transactions.rb +39 -13
  211. data/lib/active_record/translation.rb +1 -1
  212. data/lib/active_record/type/adapter_specific_registry.rb +1 -8
  213. data/lib/active_record/type/internal/timezone.rb +7 -2
  214. data/lib/active_record/type/serialized.rb +8 -4
  215. data/lib/active_record/type/time.rb +4 -0
  216. data/lib/active_record/validations/absence.rb +1 -1
  217. data/lib/active_record/validations/associated.rb +3 -3
  218. data/lib/active_record/validations/numericality.rb +5 -4
  219. data/lib/active_record/validations/presence.rb +5 -28
  220. data/lib/active_record/validations/uniqueness.rb +50 -5
  221. data/lib/active_record/validations.rb +8 -4
  222. data/lib/active_record/version.rb +1 -1
  223. data/lib/active_record.rb +143 -16
  224. data/lib/arel/errors.rb +10 -0
  225. data/lib/arel/factory_methods.rb +4 -0
  226. data/lib/arel/filter_predications.rb +1 -1
  227. data/lib/arel/nodes/and.rb +4 -0
  228. data/lib/arel/nodes/binary.rb +6 -1
  229. data/lib/arel/nodes/bound_sql_literal.rb +61 -0
  230. data/lib/arel/nodes/cte.rb +36 -0
  231. data/lib/arel/nodes/filter.rb +1 -1
  232. data/lib/arel/nodes/fragments.rb +35 -0
  233. data/lib/arel/nodes/homogeneous_in.rb +1 -9
  234. data/lib/arel/nodes/leading_join.rb +8 -0
  235. data/lib/arel/nodes/node.rb +111 -2
  236. data/lib/arel/nodes/sql_literal.rb +6 -0
  237. data/lib/arel/nodes/table_alias.rb +4 -0
  238. data/lib/arel/nodes.rb +4 -0
  239. data/lib/arel/predications.rb +2 -0
  240. data/lib/arel/table.rb +9 -5
  241. data/lib/arel/visitors/mysql.rb +8 -1
  242. data/lib/arel/visitors/to_sql.rb +81 -17
  243. data/lib/arel/visitors/visitor.rb +2 -2
  244. data/lib/arel.rb +16 -2
  245. data/lib/rails/generators/active_record/application_record/USAGE +8 -0
  246. data/lib/rails/generators/active_record/migration.rb +3 -1
  247. data/lib/rails/generators/active_record/model/USAGE +113 -0
  248. data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
  249. metadata +51 -15
  250. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
  251. data/lib/active_record/null_relation.rb +0 -63
@@ -0,0 +1,254 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record/connection_adapters/abstract_mysql_adapter"
4
+
5
+ gem "trilogy", "~> 2.4"
6
+ require "trilogy"
7
+
8
+ require "active_record/connection_adapters/trilogy/database_statements"
9
+
10
+ module ActiveRecord
11
+ module ConnectionHandling # :nodoc:
12
+ def trilogy_adapter_class
13
+ ConnectionAdapters::TrilogyAdapter
14
+ end
15
+
16
+ # Establishes a connection to the database that's used by all Active Record objects.
17
+ def trilogy_connection(config)
18
+ configuration = config.dup
19
+
20
+ # Set FOUND_ROWS capability on the connection so UPDATE queries returns number of rows
21
+ # matched rather than number of rows updated.
22
+ configuration[:found_rows] = true
23
+
24
+ options = [
25
+ configuration[:host],
26
+ configuration[:port],
27
+ configuration[:database],
28
+ configuration[:username],
29
+ configuration[:password],
30
+ configuration[:socket],
31
+ 0
32
+ ]
33
+
34
+ trilogy_adapter_class.new nil, logger, options, configuration
35
+ end
36
+ end
37
+ module ConnectionAdapters
38
+ class TrilogyAdapter < AbstractMysqlAdapter
39
+ ER_BAD_DB_ERROR = 1049
40
+ ER_DBACCESS_DENIED_ERROR = 1044
41
+ ER_ACCESS_DENIED_ERROR = 1045
42
+ ER_SERVER_SHUTDOWN = 1053
43
+
44
+ ADAPTER_NAME = "Trilogy"
45
+
46
+ include Trilogy::DatabaseStatements
47
+
48
+ SSL_MODES = {
49
+ SSL_MODE_DISABLED: ::Trilogy::SSL_DISABLED,
50
+ SSL_MODE_PREFERRED: ::Trilogy::SSL_PREFERRED_NOVERIFY,
51
+ SSL_MODE_REQUIRED: ::Trilogy::SSL_REQUIRED_NOVERIFY,
52
+ SSL_MODE_VERIFY_CA: ::Trilogy::SSL_VERIFY_CA,
53
+ SSL_MODE_VERIFY_IDENTITY: ::Trilogy::SSL_VERIFY_IDENTITY
54
+ }.freeze
55
+
56
+ class << self
57
+ def new_client(config)
58
+ config[:ssl_mode] = parse_ssl_mode(config[:ssl_mode]) if config[:ssl_mode]
59
+ ::Trilogy.new(config)
60
+ rescue ::Trilogy::ConnectionError, ::Trilogy::ProtocolError => error
61
+ raise translate_connect_error(config, error)
62
+ end
63
+
64
+ def parse_ssl_mode(mode)
65
+ return mode if mode.is_a? Integer
66
+
67
+ m = mode.to_s.upcase
68
+ m = "SSL_MODE_#{m}" unless m.start_with? "SSL_MODE_"
69
+
70
+ SSL_MODES.fetch(m.to_sym, mode)
71
+ end
72
+
73
+ def translate_connect_error(config, error)
74
+ case error.error_code
75
+ when ER_DBACCESS_DENIED_ERROR, ER_BAD_DB_ERROR
76
+ ActiveRecord::NoDatabaseError.db_error(config[:database])
77
+ when ER_ACCESS_DENIED_ERROR
78
+ ActiveRecord::DatabaseConnectionError.username_error(config[:username])
79
+ else
80
+ if error.message.include?("TRILOGY_DNS_ERROR")
81
+ ActiveRecord::DatabaseConnectionError.hostname_error(config[:host])
82
+ else
83
+ ActiveRecord::ConnectionNotEstablished.new(error.message)
84
+ end
85
+ end
86
+ end
87
+
88
+ private
89
+ def initialize_type_map(m)
90
+ super
91
+
92
+ m.register_type(%r(char)i) do |sql_type|
93
+ limit = extract_limit(sql_type)
94
+ Type.lookup(:string, adapter: :trilogy, limit: limit)
95
+ end
96
+
97
+ m.register_type %r(^enum)i, Type.lookup(:string, adapter: :trilogy)
98
+ m.register_type %r(^set)i, Type.lookup(:string, adapter: :trilogy)
99
+ end
100
+ end
101
+
102
+ TYPE_MAP = Type::TypeMap.new.tap { |m| initialize_type_map(m) }
103
+
104
+ def supports_json?
105
+ !mariadb? && database_version >= "5.7.8"
106
+ end
107
+
108
+ def supports_comments?
109
+ true
110
+ end
111
+
112
+ def supports_comments_in_create?
113
+ true
114
+ end
115
+
116
+ def supports_savepoints?
117
+ true
118
+ end
119
+
120
+ def savepoint_errors_invalidate_transactions?
121
+ true
122
+ end
123
+
124
+ def supports_lazy_transactions?
125
+ true
126
+ end
127
+
128
+ def quote_string(string)
129
+ with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn|
130
+ conn.escape(string)
131
+ end
132
+ end
133
+
134
+ def active?
135
+ connection&.ping || false
136
+ rescue ::Trilogy::Error
137
+ false
138
+ end
139
+
140
+ alias reset! reconnect!
141
+
142
+ def disconnect!
143
+ super
144
+ unless connection.nil?
145
+ connection.close
146
+ self.connection = nil
147
+ end
148
+ end
149
+
150
+ def discard!
151
+ super
152
+ unless connection.nil?
153
+ connection.discard!
154
+ self.connection = nil
155
+ end
156
+ end
157
+
158
+ private
159
+ def text_type?(type)
160
+ TYPE_MAP.lookup(type).is_a?(Type::String) || TYPE_MAP.lookup(type).is_a?(Type::Text)
161
+ end
162
+
163
+ def each_hash(result)
164
+ return to_enum(:each_hash, result) unless block_given?
165
+
166
+ keys = result.fields.map(&:to_sym)
167
+ result.rows.each do |row|
168
+ hash = {}
169
+ idx = 0
170
+ row.each do |value|
171
+ hash[keys[idx]] = value
172
+ idx += 1
173
+ end
174
+ yield hash
175
+ end
176
+
177
+ nil
178
+ end
179
+
180
+ def error_number(exception)
181
+ exception.error_code if exception.respond_to?(:error_code)
182
+ end
183
+
184
+ def connection
185
+ @raw_connection
186
+ end
187
+
188
+ def connection=(conn)
189
+ @raw_connection = conn
190
+ end
191
+
192
+ def connect
193
+ self.connection = self.class.new_client(@config)
194
+ rescue ConnectionNotEstablished => ex
195
+ raise ex.set_pool(@pool)
196
+ end
197
+
198
+ def reconnect
199
+ connection&.close
200
+ self.connection = nil
201
+ connect
202
+ end
203
+
204
+ def full_version
205
+ schema_cache.database_version.full_version_string
206
+ end
207
+
208
+ def get_full_version
209
+ with_raw_connection(allow_retry: true, materialize_transactions: false) do |conn|
210
+ conn.server_info[:version]
211
+ end
212
+ end
213
+
214
+ def translate_exception(exception, message:, sql:, binds:)
215
+ if exception.is_a?(::Trilogy::TimeoutError) && !exception.error_code
216
+ return ActiveRecord::AdapterTimeout.new(message, sql: sql, binds: binds, connection_pool: @pool)
217
+ end
218
+ error_code = exception.error_code if exception.respond_to?(:error_code)
219
+
220
+ case error_code
221
+ when ER_SERVER_SHUTDOWN
222
+ return ConnectionFailed.new(message, connection_pool: @pool)
223
+ end
224
+
225
+ case exception
226
+ when Errno::EPIPE, SocketError, IOError
227
+ return ConnectionFailed.new(message, connection_pool: @pool)
228
+ when ::Trilogy::Error
229
+ if /Connection reset by peer|TRILOGY_CLOSED_CONNECTION|TRILOGY_INVALID_SEQUENCE_ID|TRILOGY_UNEXPECTED_PACKET/.match?(exception.message)
230
+ return ConnectionFailed.new(message, connection_pool: @pool)
231
+ end
232
+ end
233
+
234
+ super
235
+ end
236
+
237
+ def default_prepared_statements
238
+ false
239
+ end
240
+
241
+ ActiveRecord::Type.register(:immutable_string, adapter: :trilogy) do |_, **args|
242
+ Type::ImmutableString.new(true: "1", false: "0", **args)
243
+ end
244
+
245
+ ActiveRecord::Type.register(:string, adapter: :trilogy) do |_, **args|
246
+ Type::String.new(true: "1", false: "0", **args)
247
+ end
248
+
249
+ ActiveRecord::Type.register(:unsigned_integer, Type::UnsignedInteger, adapter: :trilogy)
250
+ end
251
+
252
+ ActiveSupport.run_load_hooks(:active_record_trilogyadapter, TrilogyAdapter)
253
+ end
254
+ end
@@ -11,14 +11,16 @@ module ActiveRecord
11
11
  autoload :Column
12
12
  autoload :PoolConfig
13
13
  autoload :PoolManager
14
- autoload :LegacyPoolManager
15
14
  autoload :SchemaCache
15
+ autoload :BoundSchemaReflection, "active_record/connection_adapters/schema_cache"
16
+ autoload :SchemaReflection, "active_record/connection_adapters/schema_cache"
16
17
  autoload :Deduplicable
17
18
 
18
19
  autoload_at "active_record/connection_adapters/abstract/schema_definitions" do
19
20
  autoload :IndexDefinition
20
21
  autoload :ColumnDefinition
21
22
  autoload :ChangeColumnDefinition
23
+ autoload :ChangeColumnDefaultDefinition
22
24
  autoload :ForeignKeyDefinition
23
25
  autoload :CheckConstraintDefinition
24
26
  autoload :TableDefinition
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActiveRecord
4
+ # = Active Record Connection Handling
4
5
  module ConnectionHandling
5
6
  RAILS_ENV = -> { (Rails.env if defined?(Rails.env)) || ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence }
6
7
  DEFAULT_ENV = -> { RAILS_ENV.call || "default_env" }
@@ -38,26 +39,25 @@ module ActiveRecord
38
39
  # )
39
40
  #
40
41
  # In case {ActiveRecord::Base.configurations}[rdoc-ref:Core.configurations]
41
- # is set (Rails automatically loads the contents of config/database.yml into it),
42
+ # is set (\Rails automatically loads the contents of config/database.yml into it),
42
43
  # a symbol can also be given as argument, representing a key in the
43
44
  # configuration hash:
44
45
  #
45
46
  # ActiveRecord::Base.establish_connection(:production)
46
47
  #
47
- # The exceptions AdapterNotSpecified, AdapterNotFound and +ArgumentError+
48
+ # The exceptions AdapterNotSpecified, AdapterNotFound, and +ArgumentError+
48
49
  # may be returned on an error.
49
50
  def establish_connection(config_or_env = nil)
50
51
  config_or_env ||= DEFAULT_ENV.call.to_sym
51
- db_config, owner_name = resolve_config_for_connection(config_or_env)
52
- connection_handler.establish_connection(db_config, owner_name: owner_name, role: current_role, shard: current_shard)
52
+ db_config = resolve_config_for_connection(config_or_env)
53
+ connection_handler.establish_connection(db_config, owner_name: self, role: current_role, shard: current_shard)
53
54
  end
54
55
 
55
56
  # Connects a model to the databases specified. The +database+ keyword
56
57
  # takes a hash consisting of a +role+ and a +database_key+.
57
58
  #
58
- # This will create a connection handler for switching between connections,
59
- # look up the config hash using the +database_key+ and finally
60
- # establishes a connection to that config.
59
+ # This will look up the database config using the +database_key+ and
60
+ # establish a connection to that config.
61
61
  #
62
62
  # class AnimalsModel < ApplicationRecord
63
63
  # self.abstract_class = true
@@ -66,7 +66,7 @@ module ActiveRecord
66
66
  # end
67
67
  #
68
68
  # +connects_to+ also supports horizontal sharding. The horizontal sharding API
69
- # also supports read replicas. Connect a model to a list of shards like this:
69
+ # supports read replicas as well. You can connect a model to a list of shards like this:
70
70
  #
71
71
  # class AnimalsModel < ApplicationRecord
72
72
  # self.abstract_class = true
@@ -87,28 +87,25 @@ module ActiveRecord
87
87
 
88
88
  connections = []
89
89
 
90
- database.each do |role, database_key|
91
- db_config, owner_name = resolve_config_for_connection(database_key)
92
- handler = lookup_connection_handler(role.to_sym)
93
-
94
- self.connection_class = true
95
- connections << handler.establish_connection(db_config, owner_name: owner_name, role: role)
90
+ if shards.empty?
91
+ shards[:default] = database
96
92
  end
97
93
 
94
+ self.default_shard = shards.keys.first
95
+
98
96
  shards.each do |shard, database_keys|
99
97
  database_keys.each do |role, database_key|
100
- db_config, owner_name = resolve_config_for_connection(database_key)
101
- handler = lookup_connection_handler(role.to_sym)
98
+ db_config = resolve_config_for_connection(database_key)
102
99
 
103
100
  self.connection_class = true
104
- connections << handler.establish_connection(db_config, owner_name: owner_name, role: role, shard: shard.to_sym)
101
+ connections << connection_handler.establish_connection(db_config, owner_name: self, role: role, shard: shard.to_sym)
105
102
  end
106
103
  end
107
104
 
108
105
  connections
109
106
  end
110
107
 
111
- # Connects to a role (ex writing, reading or a custom role) and/or
108
+ # Connects to a role (e.g. writing, reading, or a custom role) and/or
112
109
  # shard for the duration of the block. At the end of the block the
113
110
  # connection will be returned to the original role / shard.
114
111
  #
@@ -135,18 +132,12 @@ module ActiveRecord
135
132
  # Dog.first # finds first Dog record stored on the shard one replica
136
133
  # end
137
134
  def connected_to(role: nil, shard: nil, prevent_writes: false, &blk)
138
- if ActiveRecord.legacy_connection_handling
139
- if self != Base
140
- raise NotImplementedError, "`connected_to` can only be called on ActiveRecord::Base with legacy connection handling."
141
- end
142
- else
143
- if self != Base && !abstract_class
144
- raise NotImplementedError, "calling `connected_to` is only allowed on ActiveRecord::Base or abstract classes."
145
- end
135
+ if self != Base && !abstract_class
136
+ raise NotImplementedError, "calling `connected_to` is only allowed on ActiveRecord::Base or abstract classes."
137
+ end
146
138
 
147
- if name != connection_specification_name && !primary_class?
148
- raise NotImplementedError, "calling `connected_to` is only allowed on the abstract class that established the connection."
149
- end
139
+ if !connection_class? && !primary_class?
140
+ raise NotImplementedError, "calling `connected_to` is only allowed on the abstract class that established the connection."
150
141
  end
151
142
 
152
143
  unless role || shard
@@ -172,10 +163,6 @@ module ActiveRecord
172
163
  def connected_to_many(*classes, role:, shard: nil, prevent_writes: false)
173
164
  classes = classes.flatten
174
165
 
175
- if ActiveRecord.legacy_connection_handling
176
- raise NotImplementedError, "connected_to_many is not available with legacy connection handling"
177
- end
178
-
179
166
  if self != Base || classes.include?(Base)
180
167
  raise NotImplementedError, "connected_to_many can only be called on ActiveRecord::Base."
181
168
  end
@@ -196,10 +183,6 @@ module ActiveRecord
196
183
  # It is not recommended to use this method in a request since it
197
184
  # does not yield to a block like +connected_to+.
198
185
  def connecting_to(role: default_role, shard: default_shard, prevent_writes: false)
199
- if ActiveRecord.legacy_connection_handling
200
- raise NotImplementedError, "`connecting_to` is not available with `legacy_connection_handling`."
201
- end
202
-
203
186
  prevent_writes = true if role == ActiveRecord.reading_role
204
187
 
205
188
  append_to_connected_to_stack(role: role, shard: shard, prevent_writes: prevent_writes, klasses: [self])
@@ -236,40 +219,31 @@ module ActiveRecord
236
219
  # See +READ_QUERY+ for the queries that are blocked by this
237
220
  # method.
238
221
  def while_preventing_writes(enabled = true, &block)
239
- if ActiveRecord.legacy_connection_handling
240
- connection_handler.while_preventing_writes(enabled, &block)
241
- else
242
- connected_to(role: current_role, prevent_writes: enabled, &block)
243
- end
222
+ connected_to(role: current_role, prevent_writes: enabled, &block)
244
223
  end
245
224
 
246
- # Returns true if role is the current connected role.
225
+ # Returns true if role is the current connected role and/or
226
+ # current connected shard. If no shard is passed, the default will be
227
+ # used.
247
228
  #
248
229
  # ActiveRecord::Base.connected_to(role: :writing) do
249
230
  # ActiveRecord::Base.connected_to?(role: :writing) #=> true
250
231
  # ActiveRecord::Base.connected_to?(role: :reading) #=> false
251
232
  # end
233
+ #
234
+ # ActiveRecord::Base.connected_to(role: :reading, shard: :shard_one) do
235
+ # ActiveRecord::Base.connected_to?(role: :reading, shard: :shard_one) #=> true
236
+ # ActiveRecord::Base.connected_to?(role: :reading, shard: :default) #=> false
237
+ # ActiveRecord::Base.connected_to?(role: :writing, shard: :shard_one) #=> true
238
+ # end
252
239
  def connected_to?(role:, shard: ActiveRecord::Base.default_shard)
253
240
  current_role == role.to_sym && current_shard == shard.to_sym
254
241
  end
255
242
 
256
- def lookup_connection_handler(handler_key) # :nodoc:
257
- if ActiveRecord.legacy_connection_handling
258
- handler_key ||= ActiveRecord.writing_role
259
- connection_handlers[handler_key] ||= ActiveRecord::ConnectionAdapters::ConnectionHandler.new
260
- else
261
- ActiveRecord::Base.connection_handler
262
- end
263
- end
264
-
265
243
  # Clears the query cache for all connections associated with the current thread.
266
244
  def clear_query_caches_for_current_thread
267
- if ActiveRecord.legacy_connection_handling
268
- ActiveRecord::Base.connection_handlers.each_value do |handler|
269
- clear_on_handler(handler)
270
- end
271
- else
272
- clear_on_handler(ActiveRecord::Base.connection_handler)
245
+ connection_handler.each_connection_pool do |pool|
246
+ pool.connection.clear_query_cache if pool.active_connection?
273
247
  end
274
248
  end
275
249
 
@@ -298,7 +272,7 @@ module ActiveRecord
298
272
  #
299
273
  # ActiveRecord::Base.connection_db_config
300
274
  # #<ActiveRecord::DatabaseConfigurations::HashConfig:0x00007fd1acbded10 @env_name="development",
301
- # @name="primary", @config={pool: 5, timeout: 5000, database: "db/development.sqlite3", adapter: "sqlite3"}>
275
+ # @name="primary", @config={pool: 5, timeout: 5000, database: "storage/development.sqlite3", adapter: "sqlite3"}>
302
276
  #
303
277
  # Use only for reading.
304
278
  def connection_db_config
@@ -319,6 +293,14 @@ module ActiveRecord
319
293
  end
320
294
 
321
295
  def remove_connection(name = nil)
296
+ if name
297
+ ActiveRecord.deprecator.warn(<<-MSG.squish)
298
+ The name argument for `#remove_connection` is deprecated without replacement
299
+ and will be removed in Rails 7.2. `#remove_connection` should always be called
300
+ on the connection class directly, which makes the name argument obsolete.
301
+ MSG
302
+ end
303
+
322
304
  name ||= @connection_specification_name if defined?(@connection_specification_name)
323
305
  # if removing a connection that has a pool, we reset the
324
306
  # connection_specification_name so it will use the parent
@@ -334,47 +316,51 @@ module ActiveRecord
334
316
  connection.schema_cache.clear!
335
317
  end
336
318
 
337
- delegate :clear_active_connections!, :clear_reloadable_connections!,
338
- :clear_all_connections!, :flush_idle_connections!, to: :connection_handler
319
+ def clear_active_connections!(role = nil)
320
+ deprecation_for_delegation(__method__)
321
+ connection_handler.clear_active_connections!(role)
322
+ end
323
+
324
+ def clear_reloadable_connections!(role = nil)
325
+ deprecation_for_delegation(__method__)
326
+ connection_handler.clear_reloadable_connections!(role)
327
+ end
328
+
329
+ def clear_all_connections!(role = nil)
330
+ deprecation_for_delegation(__method__)
331
+ connection_handler.clear_all_connections!(role)
332
+ end
333
+
334
+ def flush_idle_connections!(role = nil)
335
+ deprecation_for_delegation(__method__)
336
+ connection_handler.flush_idle_connections!(role)
337
+ end
339
338
 
340
339
  private
341
- def clear_on_handler(handler)
342
- handler.all_connection_pools.each do |pool|
343
- pool.connection.clear_query_cache if pool.active_connection?
344
- end
340
+ def deprecation_for_delegation(method)
341
+ ActiveRecord.deprecator.warn(<<-MSG.squish)
342
+ Calling `ActiveRecord::Base.#{method} is deprecated. Please
343
+ call the method directly on the connection handler; for
344
+ example: `ActiveRecord::Base.connection_handler.#{method}`.
345
+ MSG
345
346
  end
346
347
 
347
348
  def resolve_config_for_connection(config_or_env)
348
349
  raise "Anonymous class is not allowed." unless name
349
350
 
350
- owner_name = primary_class? ? Base.name : name
351
- self.connection_specification_name = owner_name
351
+ connection_name = primary_class? ? Base.name : name
352
+ self.connection_specification_name = connection_name
352
353
 
353
- db_config = Base.configurations.resolve(config_or_env)
354
- [db_config, self]
355
- end
356
-
357
- def with_handler(handler_key, &blk)
358
- handler = lookup_connection_handler(handler_key)
359
- swap_connection_handler(handler, &blk)
354
+ Base.configurations.resolve(config_or_env)
360
355
  end
361
356
 
362
357
  def with_role_and_shard(role, shard, prevent_writes)
363
358
  prevent_writes = true if role == ActiveRecord.reading_role
364
359
 
365
- if ActiveRecord.legacy_connection_handling
366
- with_handler(role.to_sym) do
367
- connection_handler.while_preventing_writes(prevent_writes) do
368
- append_to_connected_to_stack(shard: shard, klasses: [self])
369
- yield
370
- end
371
- end
372
- else
373
- append_to_connected_to_stack(role: role, shard: shard, prevent_writes: prevent_writes, klasses: [self])
374
- return_value = yield
375
- return_value.load if return_value.is_a? ActiveRecord::Relation
376
- return_value
377
- end
360
+ append_to_connected_to_stack(role: role, shard: shard, prevent_writes: prevent_writes, klasses: [self])
361
+ return_value = yield
362
+ return_value.load if return_value.is_a? ActiveRecord::Relation
363
+ return_value
378
364
  ensure
379
365
  self.connected_to_stack.pop
380
366
  end
@@ -386,14 +372,5 @@ module ActiveRecord
386
372
 
387
373
  connected_to_stack << entry
388
374
  end
389
-
390
- def swap_connection_handler(handler, &blk) # :nodoc:
391
- old_handler, ActiveRecord::Base.connection_handler = ActiveRecord::Base.connection_handler, handler
392
- return_value = yield
393
- return_value.load if return_value.is_a? ActiveRecord::Relation
394
- return_value
395
- ensure
396
- ActiveRecord::Base.connection_handler = old_handler
397
- end
398
375
  end
399
376
  end