activerecord 5.1.0 → 5.2.3

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 +5 -5
  2. data/CHANGELOG.md +596 -450
  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.rb +11 -4
  8. data/lib/active_record/aggregations.rb +6 -5
  9. data/lib/active_record/association_relation.rb +7 -5
  10. data/lib/active_record/associations.rb +77 -85
  11. data/lib/active_record/associations/alias_tracker.rb +23 -32
  12. data/lib/active_record/associations/association.rb +49 -35
  13. data/lib/active_record/associations/association_scope.rb +55 -55
  14. data/lib/active_record/associations/belongs_to_association.rb +30 -11
  15. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +8 -8
  16. data/lib/active_record/associations/builder/association.rb +4 -7
  17. data/lib/active_record/associations/builder/belongs_to.rb +21 -8
  18. data/lib/active_record/associations/builder/collection_association.rb +1 -1
  19. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -1
  20. data/lib/active_record/associations/builder/has_many.rb +2 -0
  21. data/lib/active_record/associations/builder/has_one.rb +2 -0
  22. data/lib/active_record/associations/builder/singular_association.rb +2 -0
  23. data/lib/active_record/associations/collection_association.rb +66 -53
  24. data/lib/active_record/associations/collection_proxy.rb +30 -73
  25. data/lib/active_record/associations/foreign_association.rb +2 -0
  26. data/lib/active_record/associations/has_many_association.rb +13 -2
  27. data/lib/active_record/associations/has_many_through_association.rb +37 -19
  28. data/lib/active_record/associations/has_one_association.rb +14 -1
  29. data/lib/active_record/associations/has_one_through_association.rb +13 -8
  30. data/lib/active_record/associations/join_dependency.rb +52 -96
  31. data/lib/active_record/associations/join_dependency/join_association.rb +22 -75
  32. data/lib/active_record/associations/join_dependency/join_base.rb +9 -8
  33. data/lib/active_record/associations/join_dependency/join_part.rb +9 -9
  34. data/lib/active_record/associations/preloader.rb +17 -37
  35. data/lib/active_record/associations/preloader/association.rb +53 -92
  36. data/lib/active_record/associations/preloader/through_association.rb +72 -73
  37. data/lib/active_record/associations/singular_association.rb +14 -16
  38. data/lib/active_record/associations/through_association.rb +27 -12
  39. data/lib/active_record/attribute_assignment.rb +2 -5
  40. data/lib/active_record/attribute_decorators.rb +3 -2
  41. data/lib/active_record/attribute_methods.rb +65 -24
  42. data/lib/active_record/attribute_methods/before_type_cast.rb +2 -0
  43. data/lib/active_record/attribute_methods/dirty.rb +33 -216
  44. data/lib/active_record/attribute_methods/primary_key.rb +10 -13
  45. data/lib/active_record/attribute_methods/query.rb +2 -0
  46. data/lib/active_record/attribute_methods/read.rb +9 -3
  47. data/lib/active_record/attribute_methods/serialization.rb +23 -0
  48. data/lib/active_record/attribute_methods/time_zone_conversion.rb +6 -8
  49. data/lib/active_record/attribute_methods/write.rb +22 -19
  50. data/lib/active_record/attributes.rb +7 -6
  51. data/lib/active_record/autosave_association.rb +15 -13
  52. data/lib/active_record/base.rb +2 -0
  53. data/lib/active_record/callbacks.rb +12 -6
  54. data/lib/active_record/coders/json.rb +2 -0
  55. data/lib/active_record/coders/yaml_column.rb +2 -0
  56. data/lib/active_record/collection_cache_key.rb +15 -11
  57. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +120 -39
  58. data/lib/active_record/connection_adapters/abstract/database_limits.rb +7 -0
  59. data/lib/active_record/connection_adapters/abstract/database_statements.rb +192 -37
  60. data/lib/active_record/connection_adapters/abstract/query_cache.rb +13 -2
  61. data/lib/active_record/connection_adapters/abstract/quoting.rb +15 -25
  62. data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
  63. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +15 -6
  64. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +65 -7
  65. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +31 -53
  66. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +158 -87
  67. data/lib/active_record/connection_adapters/abstract/transaction.rb +66 -21
  68. data/lib/active_record/connection_adapters/abstract_adapter.rb +86 -98
  69. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +126 -189
  70. data/lib/active_record/connection_adapters/column.rb +4 -2
  71. data/lib/active_record/connection_adapters/connection_specification.rb +17 -3
  72. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +13 -2
  73. data/lib/active_record/connection_adapters/mysql/column.rb +2 -0
  74. data/lib/active_record/connection_adapters/mysql/database_statements.rb +45 -15
  75. data/lib/active_record/connection_adapters/mysql/explain_pretty_printer.rb +2 -0
  76. data/lib/active_record/connection_adapters/mysql/quoting.rb +9 -10
  77. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +5 -3
  78. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +7 -10
  79. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +30 -23
  80. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +106 -1
  81. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +2 -0
  82. data/lib/active_record/connection_adapters/mysql2_adapter.rb +8 -2
  83. data/lib/active_record/connection_adapters/postgresql/column.rb +30 -1
  84. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +6 -32
  85. data/lib/active_record/connection_adapters/postgresql/explain_pretty_printer.rb +2 -0
  86. data/lib/active_record/connection_adapters/postgresql/oid.rb +3 -1
  87. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +13 -1
  88. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
  91. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +2 -0
  92. data/lib/active_record/connection_adapters/postgresql/oid/date.rb +23 -0
  93. data/lib/active_record/connection_adapters/postgresql/oid/date_time.rb +2 -0
  94. data/lib/active_record/connection_adapters/postgresql/oid/decimal.rb +3 -1
  95. data/lib/active_record/connection_adapters/postgresql/oid/enum.rb +2 -0
  96. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +2 -0
  97. data/lib/active_record/connection_adapters/postgresql/oid/inet.rb +2 -0
  98. data/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +3 -11
  99. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +2 -0
  100. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -1
  101. data/lib/active_record/connection_adapters/postgresql/oid/oid.rb +2 -0
  102. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +2 -0
  103. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +8 -2
  104. data/lib/active_record/connection_adapters/postgresql/oid/specialized_string.rb +2 -0
  105. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +4 -2
  106. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +3 -1
  107. data/lib/active_record/connection_adapters/postgresql/oid/vector.rb +2 -0
  108. data/lib/active_record/connection_adapters/postgresql/oid/xml.rb +2 -0
  109. data/lib/active_record/connection_adapters/postgresql/quoting.rb +22 -1
  110. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
  111. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +50 -0
  112. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +24 -11
  113. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
  114. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +258 -129
  115. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +2 -0
  116. data/lib/active_record/connection_adapters/postgresql/utils.rb +3 -1
  117. data/lib/active_record/connection_adapters/postgresql_adapter.rb +75 -87
  118. data/lib/active_record/connection_adapters/schema_cache.rb +4 -2
  119. data/lib/active_record/connection_adapters/sql_type_metadata.rb +2 -0
  120. data/lib/active_record/connection_adapters/sqlite3/explain_pretty_printer.rb +2 -0
  121. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +24 -1
  122. data/lib/active_record/connection_adapters/sqlite3/schema_creation.rb +2 -0
  123. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +6 -15
  124. data/lib/active_record/connection_adapters/sqlite3/schema_dumper.rb +3 -2
  125. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +75 -1
  126. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +90 -96
  127. data/lib/active_record/connection_adapters/statement_pool.rb +2 -0
  128. data/lib/active_record/connection_handling.rb +4 -2
  129. data/lib/active_record/core.rb +41 -61
  130. data/lib/active_record/counter_cache.rb +20 -15
  131. data/lib/active_record/define_callbacks.rb +5 -3
  132. data/lib/active_record/dynamic_matchers.rb +9 -9
  133. data/lib/active_record/enum.rb +18 -13
  134. data/lib/active_record/errors.rb +60 -15
  135. data/lib/active_record/explain.rb +3 -1
  136. data/lib/active_record/explain_registry.rb +2 -0
  137. data/lib/active_record/explain_subscriber.rb +2 -0
  138. data/lib/active_record/fixture_set/file.rb +2 -0
  139. data/lib/active_record/fixtures.rb +67 -60
  140. data/lib/active_record/gem_version.rb +4 -2
  141. data/lib/active_record/inheritance.rb +49 -19
  142. data/lib/active_record/integration.rb +58 -19
  143. data/lib/active_record/internal_metadata.rb +2 -0
  144. data/lib/active_record/legacy_yaml_adapter.rb +3 -1
  145. data/lib/active_record/locking/optimistic.rb +30 -42
  146. data/lib/active_record/locking/pessimistic.rb +10 -7
  147. data/lib/active_record/log_subscriber.rb +46 -4
  148. data/lib/active_record/migration.rb +189 -139
  149. data/lib/active_record/migration/command_recorder.rb +11 -9
  150. data/lib/active_record/migration/compatibility.rb +81 -29
  151. data/lib/active_record/migration/join_table.rb +2 -0
  152. data/lib/active_record/model_schema.rb +74 -58
  153. data/lib/active_record/nested_attributes.rb +18 -6
  154. data/lib/active_record/no_touching.rb +3 -1
  155. data/lib/active_record/null_relation.rb +2 -0
  156. data/lib/active_record/persistence.rb +199 -54
  157. data/lib/active_record/query_cache.rb +8 -10
  158. data/lib/active_record/querying.rb +5 -3
  159. data/lib/active_record/railtie.rb +62 -6
  160. data/lib/active_record/railties/console_sandbox.rb +2 -0
  161. data/lib/active_record/railties/controller_runtime.rb +2 -0
  162. data/lib/active_record/railties/databases.rake +48 -38
  163. data/lib/active_record/readonly_attributes.rb +3 -2
  164. data/lib/active_record/reflection.rb +137 -207
  165. data/lib/active_record/relation.rb +132 -207
  166. data/lib/active_record/relation/batches.rb +32 -17
  167. data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
  168. data/lib/active_record/relation/calculations.rb +66 -25
  169. data/lib/active_record/relation/delegation.rb +45 -29
  170. data/lib/active_record/relation/finder_methods.rb +76 -85
  171. data/lib/active_record/relation/from_clause.rb +2 -8
  172. data/lib/active_record/relation/merger.rb +53 -23
  173. data/lib/active_record/relation/predicate_builder.rb +60 -79
  174. data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
  175. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  176. data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
  177. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
  178. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +56 -0
  179. data/lib/active_record/relation/predicate_builder/range_handler.rb +26 -9
  180. data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
  181. data/lib/active_record/relation/query_attribute.rb +28 -2
  182. data/lib/active_record/relation/query_methods.rb +135 -103
  183. data/lib/active_record/relation/record_fetch_warning.rb +2 -0
  184. data/lib/active_record/relation/spawn_methods.rb +4 -2
  185. data/lib/active_record/relation/where_clause.rb +65 -67
  186. data/lib/active_record/relation/where_clause_factory.rb +5 -48
  187. data/lib/active_record/result.rb +2 -0
  188. data/lib/active_record/runtime_registry.rb +2 -0
  189. data/lib/active_record/sanitization.rb +129 -121
  190. data/lib/active_record/schema.rb +4 -2
  191. data/lib/active_record/schema_dumper.rb +36 -26
  192. data/lib/active_record/schema_migration.rb +2 -0
  193. data/lib/active_record/scoping.rb +12 -10
  194. data/lib/active_record/scoping/default.rb +10 -7
  195. data/lib/active_record/scoping/named.rb +40 -12
  196. data/lib/active_record/secure_token.rb +2 -0
  197. data/lib/active_record/serialization.rb +2 -0
  198. data/lib/active_record/statement_cache.rb +22 -12
  199. data/lib/active_record/store.rb +3 -1
  200. data/lib/active_record/suppressor.rb +2 -0
  201. data/lib/active_record/table_metadata.rb +12 -3
  202. data/lib/active_record/tasks/database_tasks.rb +38 -26
  203. data/lib/active_record/tasks/mysql_database_tasks.rb +11 -50
  204. data/lib/active_record/tasks/postgresql_database_tasks.rb +11 -3
  205. data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
  206. data/lib/active_record/timestamp.rb +13 -6
  207. data/lib/active_record/touch_later.rb +2 -0
  208. data/lib/active_record/transactions.rb +32 -27
  209. data/lib/active_record/translation.rb +2 -0
  210. data/lib/active_record/type.rb +4 -1
  211. data/lib/active_record/type/adapter_specific_registry.rb +2 -0
  212. data/lib/active_record/type/date.rb +2 -0
  213. data/lib/active_record/type/date_time.rb +2 -0
  214. data/lib/active_record/type/decimal_without_scale.rb +2 -0
  215. data/lib/active_record/type/hash_lookup_type_map.rb +2 -0
  216. data/lib/active_record/type/internal/timezone.rb +2 -0
  217. data/lib/active_record/type/json.rb +30 -0
  218. data/lib/active_record/type/serialized.rb +6 -0
  219. data/lib/active_record/type/text.rb +2 -0
  220. data/lib/active_record/type/time.rb +2 -0
  221. data/lib/active_record/type/type_map.rb +2 -0
  222. data/lib/active_record/type/unsigned_integer.rb +2 -0
  223. data/lib/active_record/type_caster.rb +2 -0
  224. data/lib/active_record/type_caster/connection.rb +2 -0
  225. data/lib/active_record/type_caster/map.rb +3 -1
  226. data/lib/active_record/validations.rb +2 -0
  227. data/lib/active_record/validations/absence.rb +2 -0
  228. data/lib/active_record/validations/associated.rb +2 -0
  229. data/lib/active_record/validations/length.rb +2 -0
  230. data/lib/active_record/validations/presence.rb +2 -0
  231. data/lib/active_record/validations/uniqueness.rb +36 -6
  232. data/lib/active_record/version.rb +2 -0
  233. data/lib/rails/generators/active_record.rb +3 -1
  234. data/lib/rails/generators/active_record/application_record/application_record_generator.rb +27 -0
  235. data/lib/rails/generators/active_record/{model/templates/application_record.rb → application_record/templates/application_record.rb.tt} +0 -0
  236. data/lib/rails/generators/active_record/migration.rb +2 -0
  237. data/lib/rails/generators/active_record/migration/migration_generator.rb +3 -1
  238. data/lib/rails/generators/active_record/migration/templates/{create_table_migration.rb → create_table_migration.rb.tt} +0 -0
  239. data/lib/rails/generators/active_record/migration/templates/{migration.rb → migration.rb.tt} +0 -0
  240. data/lib/rails/generators/active_record/model/model_generator.rb +2 -23
  241. data/lib/rails/generators/active_record/model/templates/{model.rb → model.rb.tt} +0 -0
  242. data/lib/rails/generators/active_record/model/templates/{module.rb → module.rb.tt} +0 -0
  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.rb +0 -240
  252. data/lib/active_record/attribute/user_provided_default.rb +0 -30
  253. data/lib/active_record/attribute_mutation_tracker.rb +0 -113
  254. data/lib/active_record/attribute_set.rb +0 -113
  255. data/lib/active_record/attribute_set/builder.rb +0 -124
  256. data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
  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 -33
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  class PostgreSQLTypeMetadata < DelegateClass(SqlTypeMetadata)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  module PostgreSQL
@@ -66,7 +68,7 @@ module ActiveRecord
66
68
  # * <tt>"schema_name".table_name</tt>
67
69
  # * <tt>"schema.name"."table name"</tt>
68
70
  def extract_schema_qualified_name(string)
69
- schema, table = string.scan(/[^".\s]+|"[^"]*"/)
71
+ schema, table = string.scan(/[^".]+|"[^"]*"/)
70
72
  if table.nil?
71
73
  table = schema
72
74
  schema = nil
@@ -1,7 +1,17 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Make sure we're using pg high enough for type casts and Ruby 2.2+ compatibility
2
- gem "pg", "~> 0.18"
4
+ gem "pg", ">= 0.18", "< 2.0"
3
5
  require "pg"
4
6
 
7
+ # Use async_exec instead of exec_params on pg versions before 1.1
8
+ class ::PG::Connection
9
+ unless self.public_method_defined?(:async_exec_params)
10
+ remove_method :exec_params
11
+ alias exec_params async_exec
12
+ end
13
+ end
14
+
5
15
  require "active_record/connection_adapters/abstract_adapter"
6
16
  require "active_record/connection_adapters/statement_pool"
7
17
  require "active_record/connection_adapters/postgresql/column"
@@ -62,11 +72,11 @@ module ActiveRecord
62
72
  # defaults to true.
63
73
  #
64
74
  # Any further options are used as connection parameters to libpq. See
65
- # http://www.postgresql.org/docs/current/static/libpq-connect.html for the
75
+ # https://www.postgresql.org/docs/current/static/libpq-connect.html for the
66
76
  # list of parameters.
67
77
  #
68
78
  # In addition, default connection parameters of libpq can be set per environment variables.
69
- # See http://www.postgresql.org/docs/current/static/libpq-envars.html .
79
+ # See https://www.postgresql.org/docs/current/static/libpq-envars.html .
70
80
  class PostgreSQLAdapter < AbstractAdapter
71
81
  ADAPTER_NAME = "PostgreSQL".freeze
72
82
 
@@ -74,7 +84,7 @@ module ActiveRecord
74
84
  primary_key: "bigserial primary key",
75
85
  string: { name: "character varying" },
76
86
  text: { name: "text" },
77
- integer: { name: "integer" },
87
+ integer: { name: "integer", limit: 4 },
78
88
  float: { name: "float" },
79
89
  decimal: { name: "decimal" },
80
90
  datetime: { name: "timestamp" },
@@ -119,19 +129,8 @@ module ActiveRecord
119
129
  include PostgreSQL::ReferentialIntegrity
120
130
  include PostgreSQL::SchemaStatements
121
131
  include PostgreSQL::DatabaseStatements
122
- include PostgreSQL::ColumnDumper
123
132
 
124
- def schema_creation # :nodoc:
125
- PostgreSQL::SchemaCreation.new self
126
- end
127
-
128
- def arel_visitor # :nodoc:
129
- Arel::Visitors::PostgreSQL.new(self)
130
- end
131
-
132
- # Returns true, since this connection adapter supports prepared statement
133
- # caching.
134
- def supports_statement_cache?
133
+ def supports_bulk_alter?
135
134
  true
136
135
  end
137
136
 
@@ -155,6 +154,10 @@ module ActiveRecord
155
154
  true
156
155
  end
157
156
 
157
+ def supports_validate_constraints?
158
+ true
159
+ end
160
+
158
161
  def supports_views?
159
162
  true
160
163
  end
@@ -179,7 +182,7 @@ module ActiveRecord
179
182
  { concurrently: "CONCURRENTLY" }
180
183
  end
181
184
 
182
- class StatementPool < ConnectionAdapters::StatementPool
185
+ class StatementPool < ConnectionAdapters::StatementPool # :nodoc:
183
186
  def initialize(connection, max)
184
187
  super(max)
185
188
  @connection = connection
@@ -195,9 +198,9 @@ module ActiveRecord
195
198
  end
196
199
 
197
200
  private
198
-
199
201
  def dealloc(key)
200
202
  @connection.query "DEALLOCATE #{key}" if connection_active?
203
+ rescue PG::Error
201
204
  end
202
205
 
203
206
  def connection_active?
@@ -229,7 +232,7 @@ module ActiveRecord
229
232
  add_pg_decoders
230
233
 
231
234
  @type_map = Type::HashLookupTypeMap.new
232
- initialize_type_map(type_map)
235
+ initialize_type_map
233
236
  @local_tz = execute("SHOW TIME ZONE", "SCHEMA").first["TimeZone"]
234
237
  @use_insert_returning = @config.key?(:insert_returning) ? self.class.type_cast_config_to_boolean(@config[:insert_returning]) : true
235
238
  end
@@ -285,6 +288,11 @@ module ActiveRecord
285
288
  end
286
289
  end
287
290
 
291
+ def discard! # :nodoc:
292
+ @connection.socket_io.reopen(IO::NULL) rescue nil
293
+ @connection = nil
294
+ end
295
+
288
296
  def native_database_types #:nodoc:
289
297
  NATIVE_DATABASE_TYPES
290
298
  end
@@ -309,8 +317,8 @@ module ActiveRecord
309
317
  true
310
318
  end
311
319
 
312
- # Range datatypes weren't introduced until PostgreSQL 9.2
313
320
  def supports_ranges?
321
+ # Range datatypes weren't introduced until PostgreSQL 9.2
314
322
  postgresql_version >= 90200
315
323
  end
316
324
 
@@ -318,22 +326,26 @@ module ActiveRecord
318
326
  postgresql_version >= 90300
319
327
  end
320
328
 
329
+ def supports_foreign_tables?
330
+ postgresql_version >= 90300
331
+ end
332
+
321
333
  def supports_pgcrypto_uuid?
322
334
  postgresql_version >= 90400
323
335
  end
324
336
 
325
337
  def get_advisory_lock(lock_id) # :nodoc:
326
338
  unless lock_id.is_a?(Integer) && lock_id.bit_length <= 63
327
- raise(ArgumentError, "Postgres requires advisory lock ids to be a signed 64 bit integer")
339
+ raise(ArgumentError, "PostgreSQL requires advisory lock ids to be a signed 64 bit integer")
328
340
  end
329
- select_value("SELECT pg_try_advisory_lock(#{lock_id});")
341
+ query_value("SELECT pg_try_advisory_lock(#{lock_id})")
330
342
  end
331
343
 
332
344
  def release_advisory_lock(lock_id) # :nodoc:
333
345
  unless lock_id.is_a?(Integer) && lock_id.bit_length <= 63
334
- raise(ArgumentError, "Postgres requires advisory lock ids to be a signed 64 bit integer")
346
+ raise(ArgumentError, "PostgreSQL requires advisory lock ids to be a signed 64 bit integer")
335
347
  end
336
- select_value("SELECT pg_advisory_unlock(#{lock_id})")
348
+ query_value("SELECT pg_advisory_unlock(#{lock_id})")
337
349
  end
338
350
 
339
351
  def enable_extension(name)
@@ -349,46 +361,31 @@ module ActiveRecord
349
361
  end
350
362
 
351
363
  def extension_enabled?(name)
352
- if supports_extensions?
353
- res = exec_query "SELECT EXISTS(SELECT * FROM pg_available_extensions WHERE name = '#{name}' AND installed_version IS NOT NULL) as enabled",
354
- "SCHEMA"
355
- res.cast_values.first
356
- end
364
+ res = exec_query("SELECT EXISTS(SELECT * FROM pg_available_extensions WHERE name = '#{name}' AND installed_version IS NOT NULL) as enabled", "SCHEMA")
365
+ res.cast_values.first
357
366
  end
358
367
 
359
368
  def extensions
360
- if supports_extensions?
361
- exec_query("SELECT extname from pg_extension", "SCHEMA").cast_values
362
- else
363
- super
364
- end
369
+ exec_query("SELECT extname FROM pg_extension", "SCHEMA").cast_values
365
370
  end
366
371
 
367
372
  # Returns the configured supported identifier length supported by PostgreSQL
368
- def table_alias_length
369
- @max_identifier_length ||= select_value("SHOW max_identifier_length", "SCHEMA").to_i
373
+ def max_identifier_length
374
+ @max_identifier_length ||= query_value("SHOW max_identifier_length", "SCHEMA").to_i
370
375
  end
371
- alias index_name_length table_alias_length
376
+ alias table_alias_length max_identifier_length
377
+ alias index_name_length max_identifier_length
372
378
 
373
379
  # Set the authorized user for this session
374
380
  def session_auth=(user)
375
381
  clear_cache!
376
- exec_query "SET SESSION AUTHORIZATION #{user}"
382
+ execute("SET SESSION AUTHORIZATION #{user}")
377
383
  end
378
384
 
379
385
  def use_insert_returning?
380
386
  @use_insert_returning
381
387
  end
382
388
 
383
- def update_table_definition(table_name, base) #:nodoc:
384
- PostgreSQL::Table.new(table_name, base)
385
- end
386
-
387
- def lookup_cast_type(sql_type) # :nodoc:
388
- oid = execute("SELECT #{quote(sql_type)}::regtype::oid", "SCHEMA").first["oid"].to_i
389
- super(oid)
390
- end
391
-
392
389
  def column_name_for_operation(operation, node) # :nodoc:
393
390
  OPERATION_ALIASES.fetch(operation) { operation.downcase }
394
391
  end
@@ -409,8 +406,7 @@ module ActiveRecord
409
406
  end
410
407
 
411
408
  private
412
-
413
- # See http://www.postgresql.org/docs/current/static/errcodes-appendix.html
409
+ # See https://www.postgresql.org/docs/current/static/errcodes-appendix.html
414
410
  VALUE_LIMIT_VIOLATION = "22001"
415
411
  NUMERIC_VALUE_OUT_OF_RANGE = "22003"
416
412
  NOT_NULL_VIOLATION = "23502"
@@ -418,6 +414,8 @@ module ActiveRecord
418
414
  UNIQUE_VIOLATION = "23505"
419
415
  SERIALIZATION_FAILURE = "40001"
420
416
  DEADLOCK_DETECTED = "40P01"
417
+ LOCK_NOT_AVAILABLE = "55P03"
418
+ QUERY_CANCELED = "57014"
421
419
 
422
420
  def translate_exception(exception, message)
423
421
  return exception unless exception.respond_to?(:result)
@@ -437,6 +435,10 @@ module ActiveRecord
437
435
  SerializationFailure.new(message)
438
436
  when DEADLOCK_DETECTED
439
437
  Deadlocked.new(message)
438
+ when LOCK_NOT_AVAILABLE
439
+ LockWaitTimeout.new(message)
440
+ when QUERY_CANCELED
441
+ QueryCanceled.new(message)
440
442
  else
441
443
  super
442
444
  end
@@ -444,7 +446,7 @@ module ActiveRecord
444
446
 
445
447
  def get_oid_type(oid, fmod, column_name, sql_type = "".freeze)
446
448
  if !type_map.key?(oid)
447
- load_additional_types(type_map, [oid])
449
+ load_additional_types([oid])
448
450
  end
449
451
 
450
452
  type_map.fetch(oid, fmod, sql_type) {
@@ -455,10 +457,10 @@ module ActiveRecord
455
457
  }
456
458
  end
457
459
 
458
- def initialize_type_map(m)
459
- register_class_with_limit m, "int2", Type::Integer
460
- register_class_with_limit m, "int4", Type::Integer
461
- register_class_with_limit m, "int8", Type::Integer
460
+ def initialize_type_map(m = type_map)
461
+ m.register_type "int2", Type::Integer.new(limit: 2)
462
+ m.register_type "int4", Type::Integer.new(limit: 4)
463
+ m.register_type "int8", Type::Integer.new(limit: 8)
462
464
  m.register_type "oid", OID::Oid.new
463
465
  m.register_type "float4", Type::Float.new
464
466
  m.alias_type "float8", "float4"
@@ -471,13 +473,13 @@ module ActiveRecord
471
473
  register_class_with_limit m, "bit", OID::Bit
472
474
  register_class_with_limit m, "varbit", OID::BitVarying
473
475
  m.alias_type "timestamptz", "timestamp"
474
- m.register_type "date", Type::Date.new
476
+ m.register_type "date", OID::Date.new
475
477
 
476
478
  m.register_type "money", OID::Money.new
477
479
  m.register_type "bytea", OID::Bytea.new
478
480
  m.register_type "point", OID::Point.new
479
481
  m.register_type "hstore", OID::Hstore.new
480
- m.register_type "json", OID::Json.new
482
+ m.register_type "json", Type::Json.new
481
483
  m.register_type "jsonb", OID::Jsonb.new
482
484
  m.register_type "cidr", OID::Cidr.new
483
485
  m.register_type "inet", OID::Inet.new
@@ -522,18 +524,7 @@ module ActiveRecord
522
524
  end
523
525
  end
524
526
 
525
- load_additional_types(m)
526
- end
527
-
528
- def extract_limit(sql_type)
529
- case sql_type
530
- when /^bigint/i, /^int8/i
531
- 8
532
- when /^smallint/i
533
- 2
534
- else
535
- super
536
- end
527
+ load_additional_types
537
528
  end
538
529
 
539
530
  # Extracts the value from a PostgreSQL column default definition.
@@ -571,7 +562,7 @@ module ActiveRecord
571
562
  !default_value && %r{\w+\(.*\)|\(.*\)::\w+|CURRENT_DATE|CURRENT_TIMESTAMP}.match?(default)
572
563
  end
573
564
 
574
- def load_additional_types(type_map, oids = nil)
565
+ def load_additional_types(oids = nil)
575
566
  initializer = OID::TypeMapInitializer.new(type_map)
576
567
 
577
568
  if supports_ranges?
@@ -590,7 +581,7 @@ module ActiveRecord
590
581
  if oids
591
582
  query += "WHERE t.oid::integer IN (%s)" % oids.join(", ")
592
583
  else
593
- query += initializer.query_conditions_for_initial_load(type_map)
584
+ query += initializer.query_conditions_for_initial_load
594
585
  end
595
586
 
596
587
  execute_and_clear(query, "SCHEMA", []) do |records|
@@ -617,7 +608,7 @@ module ActiveRecord
617
608
  type_casted_binds = type_casted_binds(binds)
618
609
  log(sql, name, binds, type_casted_binds) do
619
610
  ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
620
- @connection.async_exec(sql, type_casted_binds)
611
+ @connection.exec_params(sql, type_casted_binds)
621
612
  end
622
613
  end
623
614
  end
@@ -655,7 +646,7 @@ module ActiveRecord
655
646
  # ActiveRecord::PreparedStatementCacheExpired
656
647
  #
657
648
  # Check here for more details:
658
- # http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/cache/plancache.c#l573
649
+ # https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/cache/plancache.c#l573
659
650
  CACHED_PLAN_HEURISTIC = "cached plan must not change result type".freeze
660
651
  def is_cached_plan_failure?(e)
661
652
  pgerror = e.cause
@@ -720,18 +711,20 @@ module ActiveRecord
720
711
  # Use standard-conforming strings so we don't have to do the E'...' dance.
721
712
  set_standard_conforming_strings
722
713
 
714
+ variables = @config.fetch(:variables, {}).stringify_keys
715
+
723
716
  # If using Active Record's time zone support configure the connection to return
724
717
  # TIMESTAMP WITH ZONE types in UTC.
725
- # (SET TIME ZONE does not use an equals sign like other SET variables)
726
- if ActiveRecord::Base.default_timezone == :utc
727
- execute("SET time zone 'UTC'", "SCHEMA")
728
- elsif @local_tz
729
- execute("SET time zone '#{@local_tz}'", "SCHEMA")
718
+ unless variables["timezone"]
719
+ if ActiveRecord::Base.default_timezone == :utc
720
+ variables["timezone"] = "UTC"
721
+ elsif @local_tz
722
+ variables["timezone"] = @local_tz
723
+ end
730
724
  end
731
725
 
732
726
  # SET statements from :variables config hash
733
- # http://www.postgresql.org/docs/current/static/sql-set.html
734
- variables = @config[:variables] || {}
727
+ # https://www.postgresql.org/docs/current/static/sql-set.html
735
728
  variables.map do |k, v|
736
729
  if v == ":default" || v == :default
737
730
  # Sets the value to the global or compile default
@@ -742,11 +735,6 @@ module ActiveRecord
742
735
  end
743
736
  end
744
737
 
745
- # Returns the current ID of a table's sequence.
746
- def last_insert_id_result(sequence_name)
747
- exec_query("SELECT currval('#{sequence_name}')", "SQL")
748
- end
749
-
750
738
  # Returns the list of a table's column names, data types, and default values.
751
739
  #
752
740
  # The underlying query is roughly:
@@ -785,8 +773,8 @@ module ActiveRecord
785
773
  $1.strip if $1
786
774
  end
787
775
 
788
- def create_table_definition(*args)
789
- PostgreSQL::TableDefinition.new(*args)
776
+ def arel_visitor
777
+ Arel::Visitors::PostgreSQL.new(self)
790
778
  end
791
779
 
792
780
  def can_perform_case_insensitive_comparison_for?(column)
@@ -857,12 +845,12 @@ module ActiveRecord
857
845
  ActiveRecord::Type.register(:bit_varying, OID::BitVarying, adapter: :postgresql)
858
846
  ActiveRecord::Type.register(:binary, OID::Bytea, adapter: :postgresql)
859
847
  ActiveRecord::Type.register(:cidr, OID::Cidr, adapter: :postgresql)
848
+ ActiveRecord::Type.register(:date, OID::Date, adapter: :postgresql)
860
849
  ActiveRecord::Type.register(:datetime, OID::DateTime, adapter: :postgresql)
861
850
  ActiveRecord::Type.register(:decimal, OID::Decimal, adapter: :postgresql)
862
851
  ActiveRecord::Type.register(:enum, OID::Enum, adapter: :postgresql)
863
852
  ActiveRecord::Type.register(:hstore, OID::Hstore, adapter: :postgresql)
864
853
  ActiveRecord::Type.register(:inet, OID::Inet, adapter: :postgresql)
865
- ActiveRecord::Type.register(:json, OID::Json, adapter: :postgresql)
866
854
  ActiveRecord::Type.register(:jsonb, OID::Jsonb, adapter: :postgresql)
867
855
  ActiveRecord::Type.register(:money, OID::Money, adapter: :postgresql)
868
856
  ActiveRecord::Type.register(:point, OID::Point, adapter: :postgresql)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  class SchemaCache
@@ -26,7 +28,7 @@ module ActiveRecord
26
28
  coder["columns_hash"] = @columns_hash
27
29
  coder["primary_keys"] = @primary_keys
28
30
  coder["data_sources"] = @data_sources
29
- coder["version"] = ActiveRecord::Migrator.current_version
31
+ coder["version"] = connection.migration_context.current_version
30
32
  end
31
33
 
32
34
  def init_with(coder)
@@ -98,7 +100,7 @@ module ActiveRecord
98
100
 
99
101
  def marshal_dump
100
102
  # if we get current version during initialization, it happens stack over flow.
101
- @version = ActiveRecord::Migrator.current_version
103
+ @version = connection.migration_context.current_version
102
104
  [@version, @columns, @columns_hash, @primary_keys, @data_sources]
103
105
  end
104
106
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  # :stopdoc:
3
5
  module ConnectionAdapters
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  module SQLite3
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  module SQLite3
@@ -10,18 +12,39 @@ module ActiveRecord
10
12
  quote_column_name(attr)
11
13
  end
12
14
 
15
+ def quote_table_name(name)
16
+ @quoted_table_names[name] ||= super.gsub(".", "\".\"").freeze
17
+ end
18
+
13
19
  def quote_column_name(name)
14
20
  @quoted_column_names[name] ||= %Q("#{super.gsub('"', '""')}").freeze
15
21
  end
16
22
 
17
23
  def quoted_time(value)
18
- quoted_date(value)
24
+ value = value.change(year: 2000, month: 1, day: 1)
25
+ quoted_date(value).sub(/\A\d\d\d\d-\d\d-\d\d /, "2000-01-01 ")
19
26
  end
20
27
 
21
28
  def quoted_binary(value)
22
29
  "x'#{value.hex}'"
23
30
  end
24
31
 
32
+ def quoted_true
33
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? "1".freeze : "'t'".freeze
34
+ end
35
+
36
+ def unquoted_true
37
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? 1 : "t".freeze
38
+ end
39
+
40
+ def quoted_false
41
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? "0".freeze : "'f'".freeze
42
+ end
43
+
44
+ def unquoted_false
45
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? 0 : "f".freeze
46
+ end
47
+
25
48
  private
26
49
 
27
50
  def _type_cast(value)