activerecord 5.1.7 → 5.2.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activerecord might be problematic. Click here for more details.

Files changed (261) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +372 -765
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +5 -5
  5. data/examples/performance.rb +2 -0
  6. data/examples/simple.rb +2 -0
  7. data/lib/active_record/aggregations.rb +6 -5
  8. data/lib/active_record/association_relation.rb +4 -2
  9. data/lib/active_record/associations/alias_tracker.rb +19 -27
  10. data/lib/active_record/associations/association.rb +16 -27
  11. data/lib/active_record/associations/association_scope.rb +38 -50
  12. data/lib/active_record/associations/belongs_to_association.rb +20 -10
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +4 -7
  14. data/lib/active_record/associations/builder/association.rb +4 -7
  15. data/lib/active_record/associations/builder/belongs_to.rb +4 -5
  16. data/lib/active_record/associations/builder/collection_association.rb +1 -1
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -1
  18. data/lib/active_record/associations/builder/has_many.rb +2 -0
  19. data/lib/active_record/associations/builder/has_one.rb +2 -0
  20. data/lib/active_record/associations/builder/singular_association.rb +2 -0
  21. data/lib/active_record/associations/collection_association.rb +43 -35
  22. data/lib/active_record/associations/collection_proxy.rb +12 -15
  23. data/lib/active_record/associations/foreign_association.rb +2 -0
  24. data/lib/active_record/associations/has_many_association.rb +3 -1
  25. data/lib/active_record/associations/has_many_through_association.rb +7 -18
  26. data/lib/active_record/associations/has_one_association.rb +4 -1
  27. data/lib/active_record/associations/has_one_through_association.rb +8 -7
  28. data/lib/active_record/associations/join_dependency/join_association.rb +17 -56
  29. data/lib/active_record/associations/join_dependency/join_base.rb +9 -8
  30. data/lib/active_record/associations/join_dependency/join_part.rb +2 -9
  31. data/lib/active_record/associations/join_dependency.rb +23 -43
  32. data/lib/active_record/associations/preloader/association.rb +45 -61
  33. data/lib/active_record/associations/preloader/through_association.rb +71 -79
  34. data/lib/active_record/associations/preloader.rb +17 -37
  35. data/lib/active_record/associations/singular_association.rb +14 -10
  36. data/lib/active_record/associations/through_association.rb +25 -10
  37. data/lib/active_record/associations.rb +31 -54
  38. data/lib/active_record/attribute_assignment.rb +2 -5
  39. data/lib/active_record/attribute_decorators.rb +3 -2
  40. data/lib/active_record/attribute_methods/before_type_cast.rb +2 -0
  41. data/lib/active_record/attribute_methods/dirty.rb +25 -214
  42. data/lib/active_record/attribute_methods/primary_key.rb +7 -6
  43. data/lib/active_record/attribute_methods/query.rb +2 -0
  44. data/lib/active_record/attribute_methods/read.rb +8 -2
  45. data/lib/active_record/attribute_methods/serialization.rb +23 -0
  46. data/lib/active_record/attribute_methods/time_zone_conversion.rb +6 -8
  47. data/lib/active_record/attribute_methods/write.rb +21 -9
  48. data/lib/active_record/attribute_methods.rb +65 -24
  49. data/lib/active_record/attributes.rb +6 -5
  50. data/lib/active_record/autosave_association.rb +8 -11
  51. data/lib/active_record/base.rb +2 -0
  52. data/lib/active_record/callbacks.rb +8 -10
  53. data/lib/active_record/coders/json.rb +2 -0
  54. data/lib/active_record/coders/yaml_column.rb +2 -0
  55. data/lib/active_record/collection_cache_key.rb +11 -7
  56. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +111 -38
  57. data/lib/active_record/connection_adapters/abstract/database_limits.rb +2 -0
  58. data/lib/active_record/connection_adapters/abstract/database_statements.rb +157 -29
  59. data/lib/active_record/connection_adapters/abstract/query_cache.rb +7 -2
  60. data/lib/active_record/connection_adapters/abstract/quoting.rb +13 -32
  61. data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
  62. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +14 -5
  63. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +57 -2
  64. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +31 -53
  65. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +158 -78
  66. data/lib/active_record/connection_adapters/abstract/transaction.rb +45 -9
  67. data/lib/active_record/connection_adapters/abstract_adapter.rb +81 -96
  68. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +111 -183
  69. data/lib/active_record/connection_adapters/column.rb +3 -1
  70. data/lib/active_record/connection_adapters/connection_specification.rb +17 -3
  71. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +2 -0
  72. data/lib/active_record/connection_adapters/mysql/column.rb +2 -0
  73. data/lib/active_record/connection_adapters/mysql/database_statements.rb +11 -2
  74. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +2 -0
  75. data/lib/active_record/connection_adapters/mysql/quoting.rb +9 -10
  76. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +5 -3
  77. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +7 -10
  78. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +30 -30
  79. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +106 -1
  80. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +2 -0
  81. data/lib/active_record/connection_adapters/mysql2_adapter.rb +8 -2
  82. data/lib/active_record/connection_adapters/postgresql/column.rb +2 -0
  83. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +6 -0
  84. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +2 -0
  85. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +3 -11
  86. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -0
  87. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +2 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +2 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +2 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +2 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -1
  97. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -1
  99. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +2 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -0
  101. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +4 -6
  102. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +4 -2
  104. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +3 -1
  105. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
  107. data/lib/active_record/connection_adapters/postgresql/oid.rb +3 -1
  108. data/lib/active_record/connection_adapters/postgresql/quoting.rb +18 -0
  109. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
  110. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +14 -0
  111. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +24 -11
  112. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
  113. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +246 -110
  114. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
  115. data/lib/active_record/connection_adapters/postgresql/utils.rb +2 -0
  116. data/lib/active_record/connection_adapters/postgresql_adapter.rb +58 -82
  117. data/lib/active_record/connection_adapters/schema_cache.rb +4 -2
  118. data/lib/active_record/connection_adapters/sql_type_metadata.rb +2 -0
  119. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +2 -0
  120. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +18 -1
  121. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +2 -0
  122. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +6 -15
  123. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +3 -2
  124. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +71 -1
  125. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +80 -90
  126. data/lib/active_record/connection_adapters/statement_pool.rb +2 -0
  127. data/lib/active_record/connection_handling.rb +4 -2
  128. data/lib/active_record/core.rb +39 -60
  129. data/lib/active_record/counter_cache.rb +15 -12
  130. data/lib/active_record/define_callbacks.rb +5 -3
  131. data/lib/active_record/dynamic_matchers.rb +9 -9
  132. data/lib/active_record/enum.rb +17 -13
  133. data/lib/active_record/errors.rb +54 -21
  134. data/lib/active_record/explain.rb +3 -1
  135. data/lib/active_record/explain_registry.rb +2 -0
  136. data/lib/active_record/explain_subscriber.rb +2 -0
  137. data/lib/active_record/fixture_set/file.rb +2 -0
  138. data/lib/active_record/fixtures.rb +67 -60
  139. data/lib/active_record/gem_version.rb +4 -2
  140. data/lib/active_record/inheritance.rb +49 -19
  141. data/lib/active_record/integration.rb +58 -19
  142. data/lib/active_record/internal_metadata.rb +2 -0
  143. data/lib/active_record/legacy_yaml_adapter.rb +3 -1
  144. data/lib/active_record/locking/optimistic.rb +14 -17
  145. data/lib/active_record/locking/pessimistic.rb +9 -6
  146. data/lib/active_record/log_subscriber.rb +43 -0
  147. data/lib/active_record/migration/command_recorder.rb +11 -9
  148. data/lib/active_record/migration/compatibility.rb +40 -2
  149. data/lib/active_record/migration/join_table.rb +2 -0
  150. data/lib/active_record/migration.rb +189 -139
  151. data/lib/active_record/model_schema.rb +16 -21
  152. data/lib/active_record/nested_attributes.rb +18 -6
  153. data/lib/active_record/no_touching.rb +3 -1
  154. data/lib/active_record/null_relation.rb +2 -0
  155. data/lib/active_record/persistence.rb +166 -16
  156. data/lib/active_record/query_cache.rb +11 -6
  157. data/lib/active_record/querying.rb +3 -1
  158. data/lib/active_record/railtie.rb +61 -3
  159. data/lib/active_record/railties/console_sandbox.rb +2 -0
  160. data/lib/active_record/railties/controller_runtime.rb +2 -0
  161. data/lib/active_record/railties/databases.rake +46 -36
  162. data/lib/active_record/readonly_attributes.rb +3 -2
  163. data/lib/active_record/reflection.rb +110 -192
  164. data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
  165. data/lib/active_record/relation/batches.rb +20 -5
  166. data/lib/active_record/relation/calculations.rb +30 -8
  167. data/lib/active_record/relation/delegation.rb +15 -27
  168. data/lib/active_record/relation/finder_methods.rb +75 -78
  169. data/lib/active_record/relation/from_clause.rb +2 -8
  170. data/lib/active_record/relation/merger.rb +51 -20
  171. data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
  172. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  173. data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
  174. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
  175. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
  176. data/lib/active_record/relation/predicate_builder/range_handler.rb +26 -9
  177. data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
  178. data/lib/active_record/relation/predicate_builder.rb +53 -78
  179. data/lib/active_record/relation/query_attribute.rb +26 -2
  180. data/lib/active_record/relation/query_methods.rb +89 -88
  181. data/lib/active_record/relation/record_fetch_warning.rb +2 -0
  182. data/lib/active_record/relation/spawn_methods.rb +3 -1
  183. data/lib/active_record/relation/where_clause.rb +65 -68
  184. data/lib/active_record/relation/where_clause_factory.rb +5 -48
  185. data/lib/active_record/relation.rb +95 -208
  186. data/lib/active_record/result.rb +2 -0
  187. data/lib/active_record/runtime_registry.rb +2 -0
  188. data/lib/active_record/sanitization.rb +129 -121
  189. data/lib/active_record/schema.rb +4 -2
  190. data/lib/active_record/schema_dumper.rb +36 -26
  191. data/lib/active_record/schema_migration.rb +2 -0
  192. data/lib/active_record/scoping/default.rb +6 -7
  193. data/lib/active_record/scoping/named.rb +21 -7
  194. data/lib/active_record/scoping.rb +9 -8
  195. data/lib/active_record/secure_token.rb +2 -0
  196. data/lib/active_record/serialization.rb +2 -0
  197. data/lib/active_record/statement_cache.rb +22 -12
  198. data/lib/active_record/store.rb +3 -1
  199. data/lib/active_record/suppressor.rb +2 -0
  200. data/lib/active_record/table_metadata.rb +12 -3
  201. data/lib/active_record/tasks/database_tasks.rb +26 -15
  202. data/lib/active_record/tasks/mysql_database_tasks.rb +9 -48
  203. data/lib/active_record/tasks/postgresql_database_tasks.rb +10 -2
  204. data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
  205. data/lib/active_record/timestamp.rb +5 -12
  206. data/lib/active_record/touch_later.rb +2 -0
  207. data/lib/active_record/transactions.rb +9 -7
  208. data/lib/active_record/translation.rb +2 -0
  209. data/lib/active_record/type/adapter_specific_registry.rb +2 -0
  210. data/lib/active_record/type/date.rb +2 -0
  211. data/lib/active_record/type/date_time.rb +2 -0
  212. data/lib/active_record/type/decimal_without_scale.rb +2 -0
  213. data/lib/active_record/type/hash_lookup_type_map.rb +2 -0
  214. data/lib/active_record/type/internal/timezone.rb +2 -0
  215. data/lib/active_record/type/json.rb +30 -0
  216. data/lib/active_record/type/serialized.rb +2 -4
  217. data/lib/active_record/type/text.rb +2 -0
  218. data/lib/active_record/type/time.rb +2 -0
  219. data/lib/active_record/type/type_map.rb +2 -0
  220. data/lib/active_record/type/unsigned_integer.rb +2 -0
  221. data/lib/active_record/type.rb +4 -1
  222. data/lib/active_record/type_caster/connection.rb +2 -0
  223. data/lib/active_record/type_caster/map.rb +3 -1
  224. data/lib/active_record/type_caster.rb +2 -0
  225. data/lib/active_record/validations/absence.rb +2 -0
  226. data/lib/active_record/validations/associated.rb +2 -0
  227. data/lib/active_record/validations/length.rb +2 -0
  228. data/lib/active_record/validations/presence.rb +2 -0
  229. data/lib/active_record/validations/uniqueness.rb +35 -5
  230. data/lib/active_record/validations.rb +2 -0
  231. data/lib/active_record/version.rb +2 -0
  232. data/lib/active_record.rb +11 -4
  233. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  234. data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
  235. data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -1
  236. data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +0 -0
  237. data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +0 -0
  238. data/lib/rails/generators/active_record/migration.rb +2 -0
  239. data/lib/rails/generators/active_record/model/model_generator.rb +2 -23
  240. data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +0 -0
  241. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
  242. data/lib/rails/generators/active_record.rb +3 -1
  243. metadata +24 -36
  244. data/lib/active_record/associations/preloader/belongs_to.rb +0 -15
  245. data/lib/active_record/associations/preloader/collection_association.rb +0 -17
  246. data/lib/active_record/associations/preloader/has_many.rb +0 -15
  247. data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
  248. data/lib/active_record/associations/preloader/has_one.rb +0 -15
  249. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  250. data/lib/active_record/associations/preloader/singular_association.rb +0 -18
  251. data/lib/active_record/attribute/user_provided_default.rb +0 -30
  252. data/lib/active_record/attribute.rb +0 -240
  253. data/lib/active_record/attribute_mutation_tracker.rb +0 -122
  254. data/lib/active_record/attribute_set/builder.rb +0 -126
  255. data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
  256. data/lib/active_record/attribute_set.rb +0 -113
  257. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -10
  258. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  259. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
  260. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -59
  261. data/lib/active_record/type/internal/abstract_json.rb +0 -37
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_record/connection_adapters/abstract_adapter"
2
4
  require "active_record/connection_adapters/statement_pool"
3
5
  require "active_record/connection_adapters/sqlite3/explain_pretty_printer"
@@ -7,7 +9,7 @@ require "active_record/connection_adapters/sqlite3/schema_definitions"
7
9
  require "active_record/connection_adapters/sqlite3/schema_dumper"
8
10
  require "active_record/connection_adapters/sqlite3/schema_statements"
9
11
 
10
- gem "sqlite3", "~> 1.3", ">= 1.3.6"
12
+ gem "sqlite3", "~> 1.3.6"
11
13
  require "sqlite3"
12
14
 
13
15
  module ActiveRecord
@@ -55,11 +57,10 @@ module ActiveRecord
55
57
  ADAPTER_NAME = "SQLite".freeze
56
58
 
57
59
  include SQLite3::Quoting
58
- include SQLite3::ColumnDumper
59
60
  include SQLite3::SchemaStatements
60
61
 
61
62
  NATIVE_DATABASE_TYPES = {
62
- primary_key: "INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL",
63
+ primary_key: "integer PRIMARY KEY AUTOINCREMENT NOT NULL",
63
64
  string: { name: "varchar" },
64
65
  text: { name: "text" },
65
66
  integer: { name: "integer" },
@@ -69,33 +70,38 @@ module ActiveRecord
69
70
  time: { name: "time" },
70
71
  date: { name: "date" },
71
72
  binary: { name: "blob" },
72
- boolean: { name: "boolean" }
73
+ boolean: { name: "boolean" },
74
+ json: { name: "json" },
73
75
  }
74
76
 
75
- class StatementPool < ConnectionAdapters::StatementPool
76
- private
77
+ ##
78
+ # :singleton-method:
79
+ # Indicates whether boolean values are stored in sqlite3 databases as 1
80
+ # and 0 or 't' and 'f'. Leaving <tt>ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer</tt>
81
+ # set to false is deprecated. SQLite databases have used 't' and 'f' to
82
+ # serialize boolean values and must have old data converted to 1 and 0
83
+ # (its native boolean serialization) before setting this flag to true.
84
+ # Conversion can be accomplished by setting up a rake task which runs
85
+ #
86
+ # ExampleModel.where("boolean_column = 't'").update_all(boolean_column: 1)
87
+ # ExampleModel.where("boolean_column = 'f'").update_all(boolean_column: 0)
88
+ # for all models and all boolean columns, after which the flag must be set
89
+ # to true by adding the following to your <tt>application.rb</tt> file:
90
+ #
91
+ # Rails.application.config.active_record.sqlite3.represent_boolean_as_integer = true
92
+ class_attribute :represent_boolean_as_integer, default: false
77
93
 
94
+ class StatementPool < ConnectionAdapters::StatementPool # :nodoc:
95
+ private
78
96
  def dealloc(stmt)
79
97
  stmt[:stmt].close unless stmt[:stmt].closed?
80
98
  end
81
99
  end
82
100
 
83
- def update_table_definition(table_name, base) # :nodoc:
84
- SQLite3::Table.new(table_name, base)
85
- end
86
-
87
- def schema_creation # :nodoc:
88
- SQLite3::SchemaCreation.new self
89
- end
90
-
91
- def arel_visitor # :nodoc:
92
- Arel::Visitors::SQLite.new(self)
93
- end
94
-
95
101
  def initialize(connection, logger, connection_options, config)
96
102
  super(connection, logger, config)
97
103
 
98
- @active = nil
104
+ @active = true
99
105
  @statements = StatementPool.new(self.class.type_cast_config_to_integer(config[:statement_limit]))
100
106
 
101
107
  configure_connection
@@ -113,12 +119,6 @@ module ActiveRecord
113
119
  sqlite_version >= "3.8.0"
114
120
  end
115
121
 
116
- # Returns true, since this connection adapter supports prepared statement
117
- # caching.
118
- def supports_statement_cache?
119
- true
120
- end
121
-
122
122
  def requires_reloading?
123
123
  true
124
124
  end
@@ -135,12 +135,16 @@ module ActiveRecord
135
135
  true
136
136
  end
137
137
 
138
+ def supports_json?
139
+ true
140
+ end
141
+
138
142
  def supports_multi_insert?
139
143
  sqlite_version >= "3.7.11"
140
144
  end
141
145
 
142
146
  def active?
143
- @active != false
147
+ @active
144
148
  end
145
149
 
146
150
  # Disconnects from the database if already connected. Otherwise, this
@@ -267,53 +271,6 @@ module ActiveRecord
267
271
 
268
272
  # SCHEMA STATEMENTS ========================================
269
273
 
270
- def new_column_from_field(table_name, field) # :nondoc:
271
- case field["dflt_value"]
272
- when /^null$/i
273
- field["dflt_value"] = nil
274
- when /^'(.*)'$/m
275
- field["dflt_value"] = $1.gsub("''", "'")
276
- when /^"(.*)"$/m
277
- field["dflt_value"] = $1.gsub('""', '"')
278
- end
279
-
280
- collation = field["collation"]
281
- sql_type = field["type"]
282
- type_metadata = fetch_type_metadata(sql_type)
283
- new_column(field["name"], field["dflt_value"], type_metadata, field["notnull"].to_i == 0, table_name, nil, collation)
284
- end
285
-
286
- # Returns an array of indexes for the given table.
287
- def indexes(table_name, name = nil) #:nodoc:
288
- if name
289
- ActiveSupport::Deprecation.warn(<<-MSG.squish)
290
- Passing name to #indexes is deprecated without replacement.
291
- MSG
292
- end
293
-
294
- exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", "SCHEMA").map do |row|
295
- sql = <<-SQL
296
- SELECT sql
297
- FROM sqlite_master
298
- WHERE name=#{quote(row['name'])} AND type='index'
299
- UNION ALL
300
- SELECT sql
301
- FROM sqlite_temp_master
302
- WHERE name=#{quote(row['name'])} AND type='index'
303
- SQL
304
- index_sql = exec_query(sql).first["sql"]
305
- match = /\sWHERE\s+(.+)$/i.match(index_sql)
306
- where = match[1] if match
307
- IndexDefinition.new(
308
- table_name,
309
- row["name"],
310
- row["unique"] != 0,
311
- exec_query("PRAGMA index_info('#{row['name']}')", "SCHEMA").map { |col|
312
- col["name"]
313
- }, nil, nil, where)
314
- end
315
- end
316
-
317
274
  def primary_keys(table_name) # :nodoc:
318
275
  pks = table_structure(table_name).select { |f| f["pk"] > 0 }
319
276
  pks.sort_by { |f| f["pk"] }.map { |f| f["name"] }
@@ -333,19 +290,18 @@ module ActiveRecord
333
290
  rename_table_indexes(table_name, new_name)
334
291
  end
335
292
 
336
- # See: http://www.sqlite.org/lang_altertable.html
337
- # SQLite has an additional restriction on the ALTER TABLE statement
338
- def valid_alter_table_type?(type)
339
- type.to_sym != :primary_key
293
+ def valid_alter_table_type?(type, options = {})
294
+ !invalid_alter_table_type?(type, options)
340
295
  end
296
+ deprecate :valid_alter_table_type?
341
297
 
342
298
  def add_column(table_name, column_name, type, options = {}) #:nodoc:
343
- if valid_alter_table_type?(type) && !options[:primary_key]
344
- super(table_name, column_name, type, options)
345
- else
299
+ if invalid_alter_table_type?(type, options)
346
300
  alter_table(table_name) do |definition|
347
301
  definition.column(column_name, type, options)
348
302
  end
303
+ else
304
+ super
349
305
  end
350
306
  end
351
307
 
@@ -410,7 +366,31 @@ module ActiveRecord
410
366
  end
411
367
  end
412
368
 
369
+ def insert_fixtures(rows, table_name)
370
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
371
+ `insert_fixtures` is deprecated and will be removed in the next version of Rails.
372
+ Consider using `insert_fixtures_set` for performance improvement.
373
+ MSG
374
+ insert_fixtures_set(table_name => rows)
375
+ end
376
+
377
+ def insert_fixtures_set(fixture_set, tables_to_delete = [])
378
+ disable_referential_integrity do
379
+ transaction(requires_new: true) do
380
+ tables_to_delete.each { |table| delete "DELETE FROM #{quote_table_name(table)}", "Fixture Delete" }
381
+
382
+ fixture_set.each do |table_name, rows|
383
+ rows.each { |row| insert_fixture(row, table_name) }
384
+ end
385
+ end
386
+ end
387
+ end
388
+
413
389
  private
390
+ def initialize_type_map(m = type_map)
391
+ super
392
+ register_class_with_limit m, %r(int)i, SQLite3Integer
393
+ end
414
394
 
415
395
  def table_structure(table_name)
416
396
  structure = exec_query("PRAGMA table_info(#{quote_table_name(table_name)})", "SCHEMA")
@@ -419,6 +399,12 @@ module ActiveRecord
419
399
  end
420
400
  alias column_definitions table_structure
421
401
 
402
+ # See: https://www.sqlite.org/lang_altertable.html
403
+ # SQLite has an additional restriction on the ALTER TABLE statement
404
+ def invalid_alter_table_type?(type, options)
405
+ type.to_sym == :primary_key || options[:primary_key]
406
+ end
407
+
422
408
  def alter_table(table_name, options = {})
423
409
  altered_table_name = "a#{table_name}"
424
410
  caller = lambda { |definition| yield definition if block_given? }
@@ -566,21 +552,25 @@ module ActiveRecord
566
552
  end
567
553
  end
568
554
 
569
- def create_table_definition(*args)
570
- SQLite3::TableDefinition.new(*args)
571
- end
572
-
573
- def extract_foreign_key_action(specifier)
574
- case specifier
575
- when "CASCADE"; :cascade
576
- when "SET NULL"; :nullify
577
- when "RESTRICT"; :restrict
578
- end
555
+ def arel_visitor
556
+ Arel::Visitors::SQLite.new(self)
579
557
  end
580
558
 
581
559
  def configure_connection
582
560
  execute("PRAGMA foreign_keys = ON", "SCHEMA")
583
561
  end
562
+
563
+ class SQLite3Integer < Type::Integer # :nodoc:
564
+ private
565
+ def _limit
566
+ # INTEGER storage class can be stored 8 bytes value.
567
+ # See https://www.sqlite.org/datatype3.html#storage_classes_and_datatypes
568
+ limit || 8
569
+ end
570
+ end
571
+
572
+ ActiveRecord::Type.register(:integer, SQLite3Integer, adapter: :sqlite3)
584
573
  end
574
+ ActiveSupport.run_load_hooks(:active_record_sqlite3adapter, SQLite3Adapter)
585
575
  end
586
576
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  class StatementPool # :nodoc:
@@ -1,6 +1,8 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionHandling
3
- RAILS_ENV = -> { (Rails.env if defined?(Rails.env)) || ENV["RAILS_ENV"] || ENV["RACK_ENV"] }
5
+ RAILS_ENV = -> { (Rails.env if defined?(Rails.env)) || ENV["RAILS_ENV"].presence || ENV["RACK_ENV"].presence }
4
6
  DEFAULT_ENV = -> { RAILS_ENV.call || "default_env" }
5
7
 
6
8
  # Establishes the connection to the database. Accepts a hash as input where
@@ -138,6 +140,6 @@ module ActiveRecord
138
140
  end
139
141
 
140
142
  delegate :clear_active_connections!, :clear_reloadable_connections!,
141
- :clear_all_connections!, to: :connection_handler
143
+ :clear_all_connections!, :flush_idle_connections!, to: :connection_handler
142
144
  end
143
145
  end
@@ -1,7 +1,8 @@
1
- require "thread"
1
+ # frozen_string_literal: true
2
+
2
3
  require "active_support/core_ext/hash/indifferent_access"
3
- require "active_support/core_ext/object/duplicable"
4
4
  require "active_support/core_ext/string/filters"
5
+ require "concurrent/map"
5
6
 
6
7
  module ActiveRecord
7
8
  module Core
@@ -16,6 +17,13 @@ module ActiveRecord
16
17
  # retrieved on both a class and instance level by calling +logger+.
17
18
  mattr_accessor :logger, instance_writer: false
18
19
 
20
+ ##
21
+ # :singleton-method:
22
+ #
23
+ # Specifies if the methods calling database queries should be logged below
24
+ # their relevant queries. Defaults to false.
25
+ mattr_accessor :verbose_query_logs, instance_writer: false, default: false
26
+
19
27
  ##
20
28
  # Contains the database configuration - as is typically stored in config/database.yml -
21
29
  # as a Hash.
@@ -56,8 +64,7 @@ module ActiveRecord
56
64
  # :singleton-method:
57
65
  # Determines whether to use Time.utc (using :utc) or Time.local (using :local) when pulling
58
66
  # dates and times from the database. This is set to :utc by default.
59
- mattr_accessor :default_timezone, instance_writer: false
60
- self.default_timezone = :utc
67
+ mattr_accessor :default_timezone, instance_writer: false, default: :utc
61
68
 
62
69
  ##
63
70
  # :singleton-method:
@@ -67,42 +74,27 @@ module ActiveRecord
67
74
  # ActiveRecord::Schema file which can be loaded into any database that
68
75
  # supports migrations. Use :ruby if you want to have different database
69
76
  # adapters for, e.g., your development and test environments.
70
- mattr_accessor :schema_format, instance_writer: false
71
- self.schema_format = :ruby
77
+ mattr_accessor :schema_format, instance_writer: false, default: :ruby
72
78
 
73
79
  ##
74
80
  # :singleton-method:
75
81
  # Specifies if an error should be raised if the query has an order being
76
82
  # ignored when doing batch queries. Useful in applications where the
77
83
  # scope being ignored is error-worthy, rather than a warning.
78
- mattr_accessor :error_on_ignored_order, instance_writer: false
79
- self.error_on_ignored_order = false
80
-
81
- def self.error_on_ignored_order_or_limit
82
- ActiveSupport::Deprecation.warn(<<-MSG.squish)
83
- The flag error_on_ignored_order_or_limit is deprecated. Limits are
84
- now supported. Please use error_on_ignored_order instead.
85
- MSG
86
- error_on_ignored_order
87
- end
88
-
89
- def error_on_ignored_order_or_limit
90
- self.class.error_on_ignored_order_or_limit
91
- end
84
+ mattr_accessor :error_on_ignored_order, instance_writer: false, default: false
92
85
 
93
- def self.error_on_ignored_order_or_limit=(value)
94
- ActiveSupport::Deprecation.warn(<<-MSG.squish)
95
- The flag error_on_ignored_order_or_limit is deprecated. Limits are
96
- now supported. Please use error_on_ignored_order= instead.
97
- MSG
98
- self.error_on_ignored_order = value
99
- end
86
+ # :singleton-method:
87
+ # Specify the behavior for unsafe raw query methods. Values are as follows
88
+ # deprecated - Warnings are logged when unsafe raw SQL is passed to
89
+ # query methods.
90
+ # disabled - Unsafe raw SQL passed to query methods results in
91
+ # UnknownAttributeReference exception.
92
+ mattr_accessor :allow_unsafe_raw_sql, instance_writer: false, default: :deprecated
100
93
 
101
94
  ##
102
95
  # :singleton-method:
103
96
  # Specify whether or not to use timestamps for migration versions
104
- mattr_accessor :timestamped_migrations, instance_writer: false
105
- self.timestamped_migrations = true
97
+ mattr_accessor :timestamped_migrations, instance_writer: false, default: true
106
98
 
107
99
  ##
108
100
  # :singleton-method:
@@ -110,8 +102,7 @@ module ActiveRecord
110
102
  # db:migrate rake task. This is true by default, which is useful for the
111
103
  # development environment. This should ideally be false in the production
112
104
  # environment where dumping schema is rarely needed.
113
- mattr_accessor :dump_schema_after_migration, instance_writer: false
114
- self.dump_schema_after_migration = true
105
+ mattr_accessor :dump_schema_after_migration, instance_writer: false, default: true
115
106
 
116
107
  ##
117
108
  # :singleton-method:
@@ -120,8 +111,7 @@ module ActiveRecord
120
111
  # schema_search_path are dumped. Use :all to dump all schemas regardless
121
112
  # of schema_search_path, or a string of comma separated schemas for a
122
113
  # custom list.
123
- mattr_accessor :dump_schemas, instance_writer: false
124
- self.dump_schemas = :schema_search_path
114
+ mattr_accessor :dump_schemas, instance_writer: false, default: :schema_search_path
125
115
 
126
116
  ##
127
117
  # :singleton-method:
@@ -130,7 +120,6 @@ module ActiveRecord
130
120
  # be used to identify queries which load thousands of records and
131
121
  # potentially cause memory bloat.
132
122
  mattr_accessor :warn_on_records_fetched_greater_than, instance_writer: false
133
- self.warn_on_records_fetched_greater_than = nil
134
123
 
135
124
  mattr_accessor :maintain_test_schema, instance_accessor: false
136
125
 
@@ -149,14 +138,14 @@ module ActiveRecord
149
138
  self.default_connection_handler = ConnectionAdapters::ConnectionHandler.new
150
139
  end
151
140
 
152
- module ClassMethods
141
+ module ClassMethods # :nodoc:
153
142
  def allocate
154
143
  define_attribute_methods
155
144
  super
156
145
  end
157
146
 
158
147
  def initialize_find_by_cache # :nodoc:
159
- @find_by_statement_cache = { true => {}.extend(Mutex_m), false => {}.extend(Mutex_m) }
148
+ @find_by_statement_cache = { true => Concurrent::Map.new, false => Concurrent::Map.new }
160
149
  end
161
150
 
162
151
  def inherited(child_class) # :nodoc:
@@ -175,8 +164,7 @@ module ActiveRecord
175
164
 
176
165
  id = ids.first
177
166
 
178
- return super if id.kind_of?(Array) ||
179
- id.is_a?(ActiveRecord::Base)
167
+ return super if StatementCache.unsupported_value?(id)
180
168
 
181
169
  key = primary_key
182
170
 
@@ -184,7 +172,7 @@ module ActiveRecord
184
172
  where(key => params.bind).limit(1)
185
173
  }
186
174
 
187
- record = statement.execute([id], self, connection).first
175
+ record = statement.execute([id], connection).first
188
176
  unless record
189
177
  raise RecordNotFound.new("Couldn't find #{name} with '#{primary_key}'=#{id}",
190
178
  name, primary_key, id)
@@ -201,7 +189,7 @@ module ActiveRecord
201
189
  hash = args.first
202
190
 
203
191
  return super if !(Hash === hash) || hash.values.any? { |v|
204
- v.nil? || Array === v || Hash === v || Relation === v || Base === v
192
+ StatementCache.unsupported_value?(v)
205
193
  }
206
194
 
207
195
  # We can't cache Post.find_by(author: david) ...yet
@@ -216,7 +204,7 @@ module ActiveRecord
216
204
  where(wheres).limit(1)
217
205
  }
218
206
  begin
219
- statement.execute(hash.values, self, connection).first
207
+ statement.execute(hash.values, connection).first
220
208
  rescue TypeError
221
209
  raise ActiveRecord::StatementInvalid
222
210
  rescue ::RangeError
@@ -258,7 +246,7 @@ module ActiveRecord
258
246
  end
259
247
  end
260
248
 
261
- # Overwrite the default class equality method to provide support for association proxies.
249
+ # Overwrite the default class equality method to provide support for decorated models.
262
250
  def ===(object)
263
251
  object.is_a?(self)
264
252
  end
@@ -272,16 +260,6 @@ module ActiveRecord
272
260
  @arel_table ||= Arel::Table.new(table_name, type_caster: type_caster)
273
261
  end
274
262
 
275
- # Returns the Arel engine.
276
- def arel_engine # :nodoc:
277
- @arel_engine ||=
278
- if Base == self || connection_handler.retrieve_connection_pool(connection_specification_name)
279
- self
280
- else
281
- superclass.arel_engine
282
- end
283
- end
284
-
285
263
  def arel_attribute(name, table = arel_table) # :nodoc:
286
264
  name = attribute_alias(name) if attribute_alias?(name)
287
265
  table[name]
@@ -299,16 +277,15 @@ module ActiveRecord
299
277
 
300
278
  def cached_find_by_statement(key, &block)
301
279
  cache = @find_by_statement_cache[connection.prepared_statements]
302
- cache[key] || cache.synchronize {
303
- cache[key] ||= StatementCache.create(connection, &block)
304
- }
280
+ cache.compute_if_absent(key) { StatementCache.create(connection, &block) }
305
281
  end
306
282
 
307
283
  def relation
308
- relation = Relation.create(self, arel_table, predicate_builder)
284
+ relation = Relation.create(self)
309
285
 
310
286
  if finder_needs_type_condition? && !ignore_default_scope?
311
- relation.where(type_condition).create_with(inheritance_column.to_s => sti_name)
287
+ relation.where!(type_condition)
288
+ relation.create_with!(inheritance_column.to_s => sti_name)
312
289
  else
313
290
  relation
314
291
  end
@@ -405,8 +382,10 @@ module ActiveRecord
405
382
 
406
383
  _run_initialize_callbacks
407
384
 
408
- @new_record = true
409
- @destroyed = false
385
+ @new_record = true
386
+ @destroyed = false
387
+ @_start_transaction_state = {}
388
+ @transaction_state = nil
410
389
 
411
390
  super
412
391
  end
@@ -548,7 +527,7 @@ module ActiveRecord
548
527
  #
549
528
  # So we can avoid the +method_missing+ hit by explicitly defining +#to_ary+ as +nil+ here.
550
529
  #
551
- # See also http://tenderlovemaking.com/2011/06/28/til-its-ok-to-return-nil-from-to_ary.html
530
+ # See also https://tenderlovemaking.com/2011/06/28/til-its-ok-to-return-nil-from-to_ary.html
552
531
  def to_ary
553
532
  nil
554
533
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  # = Active Record Counter Cache
3
5
  module CounterCache
@@ -45,17 +47,13 @@ module ActiveRecord
45
47
  reflection = child_class._reflections.values.find { |e| e.belongs_to? && e.foreign_key.to_s == foreign_key && e.options[:counter_cache].present? }
46
48
  counter_name = reflection.counter_cache_column
47
49
 
48
- updates = { counter_name => object.send(counter_association).count(:all) }
49
-
50
- if touch
51
- names = touch if touch != true
52
- updates.merge!(touch_attributes_with_time(*names))
53
- end
50
+ updates = { counter_name.to_sym => object.send(counter_association).count(:all) }
51
+ updates.merge!(touch_updates(touch)) if touch
54
52
 
55
53
  unscoped.where(primary_key => object.id).update_all(updates)
56
54
  end
57
55
 
58
- return true
56
+ true
59
57
  end
60
58
 
61
59
  # A generic "counter updater" implementation, intended primarily to be
@@ -70,8 +68,8 @@ module ActiveRecord
70
68
  # * +counters+ - A Hash containing the names of the fields
71
69
  # to update as keys and the amount to update the field by as values.
72
70
  # * <tt>:touch</tt> option - Touch timestamp columns when updating.
73
- # If attribute names are passed, they are updated along with updated_at/on
74
- # attributes.
71
+ # Pass +true+ to touch +updated_at+ and/or +updated_on+. Pass a symbol to
72
+ # touch that column or an array of symbols to touch just those ones.
75
73
  #
76
74
  # ==== Examples
77
75
  #
@@ -109,8 +107,7 @@ module ActiveRecord
109
107
  end
110
108
 
111
109
  if touch
112
- names = touch if touch != true
113
- touch_updates = touch_attributes_with_time(*names)
110
+ touch_updates = touch_updates(touch)
114
111
  updates << sanitize_sql_for_assignment(touch_updates) unless touch_updates.empty?
115
112
  end
116
113
 
@@ -168,6 +165,13 @@ module ActiveRecord
168
165
  def decrement_counter(counter_name, id, touch: nil)
169
166
  update_counters(id, counter_name => -1, touch: touch)
170
167
  end
168
+
169
+ private
170
+ def touch_updates(touch)
171
+ touch = timestamp_attributes_for_update_in_model if touch == true
172
+ touch_time = current_time_from_proper_timezone
173
+ Array(touch).map { |column| [ column, touch_time ] }.to_h
174
+ end
171
175
  end
172
176
 
173
177
  private
@@ -178,7 +182,6 @@ module ActiveRecord
178
182
  each_counter_cached_associations do |association|
179
183
  if send(association.reflection.name)
180
184
  association.increment_counters
181
- @_after_create_counter_called = true
182
185
  end
183
186
  end
184
187
 
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
- # This module exists because `ActiveRecord::AttributeMethods::Dirty` needs to
3
- # define callbacks, but continue to have its version of `save` be the super
4
- # method of `ActiveRecord::Callbacks`. This will be removed when the removal
4
+ # This module exists because ActiveRecord::AttributeMethods::Dirty needs to
5
+ # define callbacks, but continue to have its version of +save+ be the super
6
+ # method of ActiveRecord::Callbacks. This will be removed when the removal
5
7
  # of deprecated code removes this need.
6
8
  module DefineCallbacks
7
9
  extend ActiveSupport::Concern
@@ -1,16 +1,16 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module ActiveRecord
3
4
  module DynamicMatchers #:nodoc:
4
- def respond_to_missing?(name, include_private = false)
5
- if self == Base
6
- super
7
- else
8
- match = Method.match(self, name)
9
- match && match.valid? || super
10
- end
11
- end
12
-
13
5
  private
6
+ def respond_to_missing?(name, _)
7
+ if self == Base
8
+ super
9
+ else
10
+ match = Method.match(self, name)
11
+ match && match.valid? || super
12
+ end
13
+ end
14
14
 
15
15
  def method_missing(name, *arguments, &block)
16
16
  match = Method.match(self, name)