activerecord 5.1.0 → 5.2.0.rc1

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 (260) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +410 -530
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +4 -4
  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 +23 -32
  10. data/lib/active_record/associations/association.rb +20 -21
  11. data/lib/active_record/associations/association_scope.rb +49 -49
  12. data/lib/active_record/associations/belongs_to_association.rb +12 -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 +10 -6
  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 +50 -41
  22. data/lib/active_record/associations/collection_proxy.rb +22 -39
  23. data/lib/active_record/associations/foreign_association.rb +2 -0
  24. data/lib/active_record/associations/has_many_association.rb +4 -2
  25. data/lib/active_record/associations/has_many_through_association.rb +12 -18
  26. data/lib/active_record/associations/has_one_association.rb +5 -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 -64
  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 +27 -44
  32. data/lib/active_record/associations/preloader/association.rb +53 -92
  33. data/lib/active_record/associations/preloader/through_association.rb +72 -73
  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 +26 -11
  37. data/lib/active_record/associations.rb +68 -76
  38. data/lib/active_record/attribute_assignment.rb +2 -0
  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 +24 -214
  42. data/lib/active_record/attribute_methods/primary_key.rb +10 -13
  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 +22 -19
  48. data/lib/active_record/attribute_methods.rb +48 -12
  49. data/lib/active_record/attributes.rb +7 -6
  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 -6
  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 +14 -10
  56. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +110 -35
  57. data/lib/active_record/connection_adapters/abstract/database_limits.rb +2 -0
  58. data/lib/active_record/connection_adapters/abstract/database_statements.rb +175 -33
  59. data/lib/active_record/connection_adapters/abstract/query_cache.rb +8 -2
  60. data/lib/active_record/connection_adapters/abstract/quoting.rb +13 -24
  61. data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
  62. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +15 -6
  63. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +58 -3
  64. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +31 -53
  65. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +165 -85
  66. data/lib/active_record/connection_adapters/abstract/transaction.rb +45 -9
  67. data/lib/active_record/connection_adapters/abstract_adapter.rb +83 -97
  68. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +118 -180
  69. data/lib/active_record/connection_adapters/column.rb +4 -2
  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 -17
  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 -23
  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 +30 -1
  83. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +6 -32
  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 +2 -0
  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_time.rb +2 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
  92. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +2 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +2 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
  95. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
  96. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -1
  98. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +2 -0
  99. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +4 -2
  101. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +4 -2
  103. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +3 -1
  104. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
  106. data/lib/active_record/connection_adapters/postgresql/oid.rb +2 -1
  107. data/lib/active_record/connection_adapters/postgresql/quoting.rb +22 -1
  108. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
  109. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +14 -0
  110. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +24 -11
  111. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
  112. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +269 -126
  113. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
  114. data/lib/active_record/connection_adapters/postgresql/utils.rb +2 -0
  115. data/lib/active_record/connection_adapters/postgresql_adapter.rb +64 -85
  116. data/lib/active_record/connection_adapters/schema_cache.rb +4 -2
  117. data/lib/active_record/connection_adapters/sql_type_metadata.rb +2 -0
  118. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +2 -0
  119. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +18 -0
  120. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +2 -0
  121. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +6 -15
  122. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +3 -2
  123. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +71 -1
  124. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +92 -95
  125. data/lib/active_record/connection_adapters/statement_pool.rb +2 -0
  126. data/lib/active_record/connection_handling.rb +4 -2
  127. data/lib/active_record/core.rb +39 -60
  128. data/lib/active_record/counter_cache.rb +3 -2
  129. data/lib/active_record/define_callbacks.rb +5 -3
  130. data/lib/active_record/dynamic_matchers.rb +9 -9
  131. data/lib/active_record/enum.rb +17 -13
  132. data/lib/active_record/errors.rb +42 -3
  133. data/lib/active_record/explain.rb +3 -1
  134. data/lib/active_record/explain_registry.rb +2 -0
  135. data/lib/active_record/explain_subscriber.rb +2 -0
  136. data/lib/active_record/fixture_set/file.rb +2 -0
  137. data/lib/active_record/fixtures.rb +67 -60
  138. data/lib/active_record/gem_version.rb +4 -2
  139. data/lib/active_record/inheritance.rb +9 -9
  140. data/lib/active_record/integration.rb +58 -19
  141. data/lib/active_record/internal_metadata.rb +2 -0
  142. data/lib/active_record/legacy_yaml_adapter.rb +3 -1
  143. data/lib/active_record/locking/optimistic.rb +8 -6
  144. data/lib/active_record/locking/pessimistic.rb +9 -6
  145. data/lib/active_record/log_subscriber.rb +46 -4
  146. data/lib/active_record/migration/command_recorder.rb +11 -9
  147. data/lib/active_record/migration/compatibility.rb +74 -22
  148. data/lib/active_record/migration/join_table.rb +2 -0
  149. data/lib/active_record/migration.rb +181 -137
  150. data/lib/active_record/model_schema.rb +73 -58
  151. data/lib/active_record/nested_attributes.rb +18 -6
  152. data/lib/active_record/no_touching.rb +3 -1
  153. data/lib/active_record/null_relation.rb +2 -0
  154. data/lib/active_record/persistence.rb +153 -18
  155. data/lib/active_record/query_cache.rb +17 -12
  156. data/lib/active_record/querying.rb +4 -2
  157. data/lib/active_record/railtie.rb +61 -3
  158. data/lib/active_record/railties/console_sandbox.rb +2 -0
  159. data/lib/active_record/railties/controller_runtime.rb +2 -0
  160. data/lib/active_record/railties/databases.rake +47 -37
  161. data/lib/active_record/readonly_attributes.rb +3 -2
  162. data/lib/active_record/reflection.rb +131 -204
  163. data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
  164. data/lib/active_record/relation/batches.rb +32 -17
  165. data/lib/active_record/relation/calculations.rb +58 -20
  166. data/lib/active_record/relation/delegation.rb +10 -29
  167. data/lib/active_record/relation/finder_methods.rb +74 -85
  168. data/lib/active_record/relation/from_clause.rb +2 -8
  169. data/lib/active_record/relation/merger.rb +51 -20
  170. data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
  171. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  172. data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
  173. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
  174. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +54 -0
  175. data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -6
  176. data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
  177. data/lib/active_record/relation/predicate_builder.rb +53 -78
  178. data/lib/active_record/relation/query_attribute.rb +9 -2
  179. data/lib/active_record/relation/query_methods.rb +101 -95
  180. data/lib/active_record/relation/record_fetch_warning.rb +2 -0
  181. data/lib/active_record/relation/spawn_methods.rb +3 -1
  182. data/lib/active_record/relation/where_clause.rb +65 -67
  183. data/lib/active_record/relation/where_clause_factory.rb +5 -48
  184. data/lib/active_record/relation.rb +99 -202
  185. data/lib/active_record/result.rb +2 -0
  186. data/lib/active_record/runtime_registry.rb +2 -0
  187. data/lib/active_record/sanitization.rb +129 -121
  188. data/lib/active_record/schema.rb +4 -2
  189. data/lib/active_record/schema_dumper.rb +36 -26
  190. data/lib/active_record/schema_migration.rb +2 -0
  191. data/lib/active_record/scoping/default.rb +10 -7
  192. data/lib/active_record/scoping/named.rb +38 -12
  193. data/lib/active_record/scoping.rb +12 -10
  194. data/lib/active_record/secure_token.rb +2 -0
  195. data/lib/active_record/serialization.rb +2 -0
  196. data/lib/active_record/statement_cache.rb +22 -12
  197. data/lib/active_record/store.rb +3 -1
  198. data/lib/active_record/suppressor.rb +2 -0
  199. data/lib/active_record/table_metadata.rb +12 -3
  200. data/lib/active_record/tasks/database_tasks.rb +37 -25
  201. data/lib/active_record/tasks/mysql_database_tasks.rb +11 -50
  202. data/lib/active_record/tasks/postgresql_database_tasks.rb +11 -3
  203. data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
  204. data/lib/active_record/timestamp.rb +5 -5
  205. data/lib/active_record/touch_later.rb +2 -0
  206. data/lib/active_record/transactions.rb +9 -7
  207. data/lib/active_record/translation.rb +2 -0
  208. data/lib/active_record/type/adapter_specific_registry.rb +2 -0
  209. data/lib/active_record/type/date.rb +2 -0
  210. data/lib/active_record/type/date_time.rb +2 -0
  211. data/lib/active_record/type/decimal_without_scale.rb +2 -0
  212. data/lib/active_record/type/hash_lookup_type_map.rb +2 -0
  213. data/lib/active_record/type/internal/timezone.rb +2 -0
  214. data/lib/active_record/type/json.rb +30 -0
  215. data/lib/active_record/type/serialized.rb +2 -0
  216. data/lib/active_record/type/text.rb +2 -0
  217. data/lib/active_record/type/time.rb +2 -0
  218. data/lib/active_record/type/type_map.rb +2 -0
  219. data/lib/active_record/type/unsigned_integer.rb +2 -0
  220. data/lib/active_record/type.rb +4 -1
  221. data/lib/active_record/type_caster/connection.rb +2 -0
  222. data/lib/active_record/type_caster/map.rb +3 -1
  223. data/lib/active_record/type_caster.rb +2 -0
  224. data/lib/active_record/validations/absence.rb +2 -0
  225. data/lib/active_record/validations/associated.rb +2 -0
  226. data/lib/active_record/validations/length.rb +2 -0
  227. data/lib/active_record/validations/presence.rb +2 -0
  228. data/lib/active_record/validations/uniqueness.rb +35 -5
  229. data/lib/active_record/validations.rb +2 -0
  230. data/lib/active_record/version.rb +2 -0
  231. data/lib/active_record.rb +11 -4
  232. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  233. data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
  234. data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -1
  235. data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +0 -0
  236. data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +0 -0
  237. data/lib/rails/generators/active_record/migration.rb +2 -0
  238. data/lib/rails/generators/active_record/model/model_generator.rb +2 -23
  239. data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +0 -0
  240. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
  241. data/lib/rails/generators/active_record.rb +3 -1
  242. metadata +25 -37
  243. data/lib/active_record/associations/preloader/belongs_to.rb +0 -15
  244. data/lib/active_record/associations/preloader/collection_association.rb +0 -17
  245. data/lib/active_record/associations/preloader/has_many.rb +0 -15
  246. data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
  247. data/lib/active_record/associations/preloader/has_one.rb +0 -15
  248. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  249. data/lib/active_record/associations/preloader/singular_association.rb +0 -18
  250. data/lib/active_record/attribute/user_provided_default.rb +0 -30
  251. data/lib/active_record/attribute.rb +0 -240
  252. data/lib/active_record/attribute_mutation_tracker.rb +0 -113
  253. data/lib/active_record/attribute_set/builder.rb +0 -124
  254. data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
  255. data/lib/active_record/attribute_set.rb +0 -113
  256. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -10
  257. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  258. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
  259. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -59
  260. data/lib/active_record/type/internal/abstract_json.rb +0 -33
@@ -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"
@@ -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
@@ -183,7 +187,7 @@ module ActiveRecord
183
187
  # REFERENTIAL INTEGRITY ====================================
184
188
 
185
189
  def disable_referential_integrity # :nodoc:
186
- old = select_value("PRAGMA foreign_keys")
190
+ old = query_value("PRAGMA foreign_keys")
187
191
 
188
192
  begin
189
193
  execute("PRAGMA foreign_keys = OFF")
@@ -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)
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
 
@@ -398,7 +354,7 @@ module ActiveRecord
398
354
  alias :add_belongs_to :add_reference
399
355
 
400
356
  def foreign_keys(table_name)
401
- fk_info = select_all("PRAGMA foreign_key_list(#{quote(table_name)})", "SCHEMA")
357
+ fk_info = exec_query("PRAGMA foreign_key_list(#{quote(table_name)})", "SCHEMA")
402
358
  fk_info.map do |row|
403
359
  options = {
404
360
  column: row["from"],
@@ -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? }
@@ -440,18 +426,21 @@ module ActiveRecord
440
426
  options[:id] = false
441
427
  create_table(to, options) do |definition|
442
428
  @definition = definition
443
- @definition.primary_key(from_primary_key) if from_primary_key.present?
429
+ if from_primary_key.is_a?(Array)
430
+ @definition.primary_keys from_primary_key
431
+ end
444
432
  columns(from).each do |column|
445
433
  column_name = options[:rename] ?
446
434
  (options[:rename][column.name] ||
447
435
  options[:rename][column.name.to_sym] ||
448
436
  column.name) : column.name
449
- next if column_name == from_primary_key
450
437
 
451
438
  @definition.column(column_name, column.type,
452
439
  limit: column.limit, default: column.default,
453
440
  precision: column.precision, scale: column.scale,
454
- null: column.null, collation: column.collation)
441
+ null: column.null, collation: column.collation,
442
+ primary_key: column_name == from_primary_key
443
+ )
455
444
  end
456
445
  yield @definition if block_given?
457
446
  end
@@ -464,6 +453,9 @@ module ActiveRecord
464
453
  def copy_table_indexes(from, to, rename = {})
465
454
  indexes(from).each do |index|
466
455
  name = index.name
456
+ # indexes sqlite creates for internal use start with `sqlite_` and
457
+ # don't need to be copied
458
+ next if name.starts_with?("sqlite_")
467
459
  if to == "a#{from}"
468
460
  name = "t#{name}"
469
461
  elsif from == "a#{to}"
@@ -479,6 +471,7 @@ module ActiveRecord
479
471
  # index name can't be the same
480
472
  opts = { name: name.gsub(/(^|_)(#{from})_/, "\\1#{to}_"), internal: true }
481
473
  opts[:unique] = true if index.unique
474
+ opts[:where] = index.where if index.where
482
475
  add_index(to, columns, opts)
483
476
  end
484
477
  end
@@ -498,7 +491,7 @@ module ActiveRecord
498
491
  end
499
492
 
500
493
  def sqlite_version
501
- @sqlite_version ||= SQLite3Adapter::Version.new(select_value("SELECT sqlite_version(*)"))
494
+ @sqlite_version ||= SQLite3Adapter::Version.new(query_value("SELECT sqlite_version(*)"))
502
495
  end
503
496
 
504
497
  def translate_exception(exception, message)
@@ -559,21 +552,25 @@ module ActiveRecord
559
552
  end
560
553
  end
561
554
 
562
- def create_table_definition(*args)
563
- SQLite3::TableDefinition.new(*args)
564
- end
565
-
566
- def extract_foreign_key_action(specifier)
567
- case specifier
568
- when "CASCADE"; :cascade
569
- when "SET NULL"; :nullify
570
- when "RESTRICT"; :restrict
571
- end
555
+ def arel_visitor
556
+ Arel::Visitors::SQLite.new(self)
572
557
  end
573
558
 
574
559
  def configure_connection
575
560
  execute("PRAGMA foreign_keys = ON", "SCHEMA")
576
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)
577
573
  end
574
+ ActiveSupport.run_load_hooks(:active_record_sqlite3adapter, SQLite3Adapter)
578
575
  end
579
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
@@ -51,7 +53,7 @@ module ActiveRecord
51
53
  unscoped.where(primary_key => object.id).update_all(updates)
52
54
  end
53
55
 
54
- return true
56
+ true
55
57
  end
56
58
 
57
59
  # A generic "counter updater" implementation, intended primarily to be
@@ -180,7 +182,6 @@ module ActiveRecord
180
182
  each_counter_cached_associations do |association|
181
183
  if send(association.reflection.name)
182
184
  association.increment_counters
183
- @_after_create_counter_called = true
184
185
  end
185
186
  end
186
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)