activerecord 5.1.7 → 5.2.0.beta1

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 (259) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +221 -900
  3. data/README.rdoc +3 -3
  4. data/examples/performance.rb +2 -0
  5. data/examples/simple.rb +2 -0
  6. data/lib/active_record.rb +10 -3
  7. data/lib/active_record/aggregations.rb +2 -0
  8. data/lib/active_record/association_relation.rb +2 -0
  9. data/lib/active_record/associations.rb +13 -42
  10. data/lib/active_record/associations/alias_tracker.rb +17 -17
  11. data/lib/active_record/associations/association.rb +11 -22
  12. data/lib/active_record/associations/association_scope.rb +32 -44
  13. data/lib/active_record/associations/belongs_to_association.rb +6 -4
  14. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +3 -1
  15. data/lib/active_record/associations/builder/association.rb +2 -5
  16. data/lib/active_record/associations/builder/belongs_to.rb +7 -12
  17. data/lib/active_record/associations/builder/collection_association.rb +1 -1
  18. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +3 -1
  19. data/lib/active_record/associations/builder/has_many.rb +2 -0
  20. data/lib/active_record/associations/builder/has_one.rb +2 -0
  21. data/lib/active_record/associations/builder/singular_association.rb +2 -0
  22. data/lib/active_record/associations/collection_association.rb +41 -33
  23. data/lib/active_record/associations/collection_proxy.rb +11 -14
  24. data/lib/active_record/associations/foreign_association.rb +2 -0
  25. data/lib/active_record/associations/has_many_association.rb +4 -2
  26. data/lib/active_record/associations/has_many_through_association.rb +4 -2
  27. data/lib/active_record/associations/has_one_association.rb +3 -1
  28. data/lib/active_record/associations/has_one_through_association.rb +3 -1
  29. data/lib/active_record/associations/join_dependency.rb +22 -40
  30. data/lib/active_record/associations/join_dependency/join_association.rb +17 -56
  31. data/lib/active_record/associations/join_dependency/join_base.rb +9 -8
  32. data/lib/active_record/associations/join_dependency/join_part.rb +2 -9
  33. data/lib/active_record/associations/preloader.rb +17 -37
  34. data/lib/active_record/associations/preloader/association.rb +42 -58
  35. data/lib/active_record/associations/preloader/through_association.rb +71 -79
  36. data/lib/active_record/associations/singular_association.rb +14 -10
  37. data/lib/active_record/associations/through_association.rb +3 -1
  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.rb +47 -7
  41. data/lib/active_record/attribute_methods/before_type_cast.rb +2 -0
  42. data/lib/active_record/attribute_methods/dirty.rb +25 -214
  43. data/lib/active_record/attribute_methods/primary_key.rb +7 -6
  44. data/lib/active_record/attribute_methods/query.rb +2 -0
  45. data/lib/active_record/attribute_methods/read.rb +8 -2
  46. data/lib/active_record/attribute_methods/serialization.rb +23 -0
  47. data/lib/active_record/attribute_methods/time_zone_conversion.rb +6 -8
  48. data/lib/active_record/attribute_methods/write.rb +21 -9
  49. data/lib/active_record/attributes.rb +7 -6
  50. data/lib/active_record/autosave_association.rb +5 -11
  51. data/lib/active_record/base.rb +2 -0
  52. data/lib/active_record/callbacks.rb +6 -8
  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 +10 -5
  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 +120 -28
  59. data/lib/active_record/connection_adapters/abstract/query_cache.rb +7 -2
  60. data/lib/active_record/connection_adapters/abstract/quoting.rb +14 -33
  61. data/lib/active_record/connection_adapters/abstract/savepoints.rb +2 -0
  62. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +13 -5
  63. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +40 -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 +103 -63
  66. data/lib/active_record/connection_adapters/abstract/transaction.rb +45 -9
  67. data/lib/active_record/connection_adapters/abstract_adapter.rb +62 -90
  68. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +75 -138
  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 +3 -1
  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 -6
  78. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +30 -30
  79. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +91 -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.rb +2 -1
  86. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +3 -11
  87. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +2 -0
  88. data/lib/active_record/connection_adapters/postgresql/oid/bit_varying.rb +2 -0
  89. data/lib/active_record/connection_adapters/postgresql/oid/bytea.rb +2 -0
  90. data/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +2 -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 +3 -1
  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 +3 -5
  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/quoting.rb +10 -0
  108. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +19 -25
  109. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +2 -0
  110. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +11 -7
  111. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +20 -13
  112. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +79 -65
  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 +47 -82
  116. data/lib/active_record/connection_adapters/schema_cache.rb +2 -0
  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 +19 -2
  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 +34 -89
  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 +27 -57
  128. data/lib/active_record/counter_cache.rb +15 -12
  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 +15 -13
  132. data/lib/active_record/errors.rb +54 -21
  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 +40 -24
  138. data/lib/active_record/gem_version.rb +5 -3
  139. data/lib/active_record/inheritance.rb +6 -5
  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 +31 -20
  144. data/lib/active_record/locking/pessimistic.rb +10 -7
  145. data/lib/active_record/log_subscriber.rb +2 -0
  146. data/lib/active_record/migration.rb +47 -21
  147. data/lib/active_record/migration/command_recorder.rb +11 -9
  148. data/lib/active_record/migration/compatibility.rb +20 -2
  149. data/lib/active_record/migration/join_table.rb +2 -0
  150. data/lib/active_record/model_schema.rb +29 -38
  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 +184 -40
  155. data/lib/active_record/query_cache.rb +17 -12
  156. data/lib/active_record/querying.rb +3 -1
  157. data/lib/active_record/railtie.rb +54 -1
  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 +41 -28
  161. data/lib/active_record/readonly_attributes.rb +3 -2
  162. data/lib/active_record/reflection.rb +100 -182
  163. data/lib/active_record/relation.rb +61 -193
  164. data/lib/active_record/relation/batches.rb +20 -5
  165. data/lib/active_record/relation/batches/batch_enumerator.rb +2 -0
  166. data/lib/active_record/relation/calculations.rb +40 -23
  167. data/lib/active_record/relation/delegation.rb +10 -27
  168. data/lib/active_record/relation/finder_methods.rb +53 -49
  169. data/lib/active_record/relation/from_clause.rb +2 -8
  170. data/lib/active_record/relation/merger.rb +22 -19
  171. data/lib/active_record/relation/predicate_builder.rb +42 -79
  172. data/lib/active_record/relation/predicate_builder/array_handler.rb +10 -7
  173. data/lib/active_record/relation/predicate_builder/association_query_value.rb +46 -0
  174. data/lib/active_record/relation/predicate_builder/base_handler.rb +2 -2
  175. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +12 -1
  176. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +54 -0
  177. data/lib/active_record/relation/predicate_builder/range_handler.rb +22 -6
  178. data/lib/active_record/relation/predicate_builder/relation_handler.rb +6 -0
  179. data/lib/active_record/relation/query_attribute.rb +9 -2
  180. data/lib/active_record/relation/query_methods.rb +80 -69
  181. data/lib/active_record/relation/record_fetch_warning.rb +2 -0
  182. data/lib/active_record/relation/spawn_methods.rb +2 -0
  183. data/lib/active_record/relation/where_clause.rb +50 -67
  184. data/lib/active_record/relation/where_clause_factory.rb +4 -46
  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 +15 -9
  188. data/lib/active_record/schema.rb +3 -1
  189. data/lib/active_record/schema_dumper.rb +24 -23
  190. data/lib/active_record/schema_migration.rb +2 -0
  191. data/lib/active_record/scoping.rb +9 -8
  192. data/lib/active_record/scoping/default.rb +6 -7
  193. data/lib/active_record/scoping/named.rb +15 -7
  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 +2 -0
  198. data/lib/active_record/suppressor.rb +2 -0
  199. data/lib/active_record/table_metadata.rb +3 -1
  200. data/lib/active_record/tasks/database_tasks.rb +23 -12
  201. data/lib/active_record/tasks/mysql_database_tasks.rb +9 -48
  202. data/lib/active_record/tasks/postgresql_database_tasks.rb +10 -2
  203. data/lib/active_record/tasks/sqlite_database_tasks.rb +25 -3
  204. data/lib/active_record/timestamp.rb +5 -12
  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.rb +4 -1
  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_caster.rb +2 -0
  222. data/lib/active_record/type_caster/connection.rb +2 -0
  223. data/lib/active_record/type_caster/map.rb +2 -0
  224. data/lib/active_record/validations.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 +36 -6
  230. data/lib/active_record/version.rb +2 -0
  231. data/lib/rails/generators/active_record.rb +3 -1
  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.rb +2 -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/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. metadata +25 -38
  242. data/lib/active_record/associations/preloader/belongs_to.rb +0 -15
  243. data/lib/active_record/associations/preloader/collection_association.rb +0 -17
  244. data/lib/active_record/associations/preloader/has_many.rb +0 -15
  245. data/lib/active_record/associations/preloader/has_many_through.rb +0 -19
  246. data/lib/active_record/associations/preloader/has_one.rb +0 -15
  247. data/lib/active_record/associations/preloader/has_one_through.rb +0 -9
  248. data/lib/active_record/associations/preloader/singular_association.rb +0 -18
  249. data/lib/active_record/attribute.rb +0 -240
  250. data/lib/active_record/attribute/user_provided_default.rb +0 -30
  251. data/lib/active_record/attribute_mutation_tracker.rb +0 -122
  252. data/lib/active_record/attribute_set.rb +0 -113
  253. data/lib/active_record/attribute_set/builder.rb +0 -126
  254. data/lib/active_record/attribute_set/yaml_encoder.rb +0 -41
  255. data/lib/active_record/connection_adapters/postgresql/oid/json.rb +0 -10
  256. data/lib/active_record/railties/jdbcmysql_error.rb +0 -16
  257. data/lib/active_record/relation/predicate_builder/association_query_handler.rb +0 -88
  258. data/lib/active_record/relation/predicate_builder/polymorphic_array_handler.rb +0 -59
  259. data/lib/active_record/type/internal/abstract_json.rb +0 -37
@@ -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
@@ -1,15 +1,9 @@
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", "< 2.0"
4
+ gem "pg", "~> 0.18"
3
5
  require "pg"
4
6
 
5
- # Use async_exec instead of exec_params on pg versions before 1.1
6
- class ::PG::Connection
7
- unless self.public_method_defined?(:async_exec_params)
8
- remove_method :exec_params
9
- alias exec_params async_exec
10
- end
11
- end
12
-
13
7
  require "active_record/connection_adapters/abstract_adapter"
14
8
  require "active_record/connection_adapters/statement_pool"
15
9
  require "active_record/connection_adapters/postgresql/column"
@@ -70,11 +64,11 @@ module ActiveRecord
70
64
  # defaults to true.
71
65
  #
72
66
  # Any further options are used as connection parameters to libpq. See
73
- # http://www.postgresql.org/docs/current/static/libpq-connect.html for the
67
+ # https://www.postgresql.org/docs/current/static/libpq-connect.html for the
74
68
  # list of parameters.
75
69
  #
76
70
  # In addition, default connection parameters of libpq can be set per environment variables.
77
- # See http://www.postgresql.org/docs/current/static/libpq-envars.html .
71
+ # See https://www.postgresql.org/docs/current/static/libpq-envars.html .
78
72
  class PostgreSQLAdapter < AbstractAdapter
79
73
  ADAPTER_NAME = "PostgreSQL".freeze
80
74
 
@@ -82,7 +76,7 @@ module ActiveRecord
82
76
  primary_key: "bigserial primary key",
83
77
  string: { name: "character varying" },
84
78
  text: { name: "text" },
85
- integer: { name: "integer" },
79
+ integer: { name: "integer", limit: 4 },
86
80
  float: { name: "float" },
87
81
  decimal: { name: "decimal" },
88
82
  datetime: { name: "timestamp" },
@@ -127,21 +121,6 @@ module ActiveRecord
127
121
  include PostgreSQL::ReferentialIntegrity
128
122
  include PostgreSQL::SchemaStatements
129
123
  include PostgreSQL::DatabaseStatements
130
- include PostgreSQL::ColumnDumper
131
-
132
- def schema_creation # :nodoc:
133
- PostgreSQL::SchemaCreation.new self
134
- end
135
-
136
- def arel_visitor # :nodoc:
137
- Arel::Visitors::PostgreSQL.new(self)
138
- end
139
-
140
- # Returns true, since this connection adapter supports prepared statement
141
- # caching.
142
- def supports_statement_cache?
143
- true
144
- end
145
124
 
146
125
  def supports_index_sort_order?
147
126
  true
@@ -238,7 +217,7 @@ module ActiveRecord
238
217
  add_pg_decoders
239
218
 
240
219
  @type_map = Type::HashLookupTypeMap.new
241
- initialize_type_map(type_map)
220
+ initialize_type_map
242
221
  @local_tz = execute("SHOW TIME ZONE", "SCHEMA").first["TimeZone"]
243
222
  @use_insert_returning = @config.key?(:insert_returning) ? self.class.type_cast_config_to_boolean(@config[:insert_returning]) : true
244
223
  end
@@ -294,6 +273,11 @@ module ActiveRecord
294
273
  end
295
274
  end
296
275
 
276
+ def discard! # :nodoc:
277
+ @connection.socket_io.reopen(IO::NULL)
278
+ @connection = nil
279
+ end
280
+
297
281
  def native_database_types #:nodoc:
298
282
  NATIVE_DATABASE_TYPES
299
283
  end
@@ -318,8 +302,8 @@ module ActiveRecord
318
302
  true
319
303
  end
320
304
 
321
- # Range datatypes weren't introduced until PostgreSQL 9.2
322
305
  def supports_ranges?
306
+ # Range datatypes weren't introduced until PostgreSQL 9.2
323
307
  postgresql_version >= 90200
324
308
  end
325
309
 
@@ -333,14 +317,14 @@ module ActiveRecord
333
317
 
334
318
  def get_advisory_lock(lock_id) # :nodoc:
335
319
  unless lock_id.is_a?(Integer) && lock_id.bit_length <= 63
336
- raise(ArgumentError, "Postgres requires advisory lock ids to be a signed 64 bit integer")
320
+ raise(ArgumentError, "PostgreSQL requires advisory lock ids to be a signed 64 bit integer")
337
321
  end
338
322
  query_value("SELECT pg_try_advisory_lock(#{lock_id})")
339
323
  end
340
324
 
341
325
  def release_advisory_lock(lock_id) # :nodoc:
342
326
  unless lock_id.is_a?(Integer) && lock_id.bit_length <= 63
343
- raise(ArgumentError, "Postgres requires advisory lock ids to be a signed 64 bit integer")
327
+ raise(ArgumentError, "PostgreSQL requires advisory lock ids to be a signed 64 bit integer")
344
328
  end
345
329
  query_value("SELECT pg_advisory_unlock(#{lock_id})")
346
330
  end
@@ -358,18 +342,12 @@ module ActiveRecord
358
342
  end
359
343
 
360
344
  def extension_enabled?(name)
361
- if supports_extensions?
362
- res = exec_query("SELECT EXISTS(SELECT * FROM pg_available_extensions WHERE name = '#{name}' AND installed_version IS NOT NULL) as enabled", "SCHEMA")
363
- res.cast_values.first
364
- end
345
+ res = exec_query("SELECT EXISTS(SELECT * FROM pg_available_extensions WHERE name = '#{name}' AND installed_version IS NOT NULL) as enabled", "SCHEMA")
346
+ res.cast_values.first
365
347
  end
366
348
 
367
349
  def extensions
368
- if supports_extensions?
369
- exec_query("SELECT extname FROM pg_extension", "SCHEMA").cast_values
370
- else
371
- super
372
- end
350
+ exec_query("SELECT extname FROM pg_extension", "SCHEMA").cast_values
373
351
  end
374
352
 
375
353
  # Returns the configured supported identifier length supported by PostgreSQL
@@ -389,10 +367,6 @@ module ActiveRecord
389
367
  @use_insert_returning
390
368
  end
391
369
 
392
- def update_table_definition(table_name, base) #:nodoc:
393
- PostgreSQL::Table.new(table_name, base)
394
- end
395
-
396
370
  def column_name_for_operation(operation, node) # :nodoc:
397
371
  OPERATION_ALIASES.fetch(operation) { operation.downcase }
398
372
  end
@@ -414,7 +388,7 @@ module ActiveRecord
414
388
 
415
389
  private
416
390
 
417
- # See http://www.postgresql.org/docs/current/static/errcodes-appendix.html
391
+ # See https://www.postgresql.org/docs/current/static/errcodes-appendix.html
418
392
  VALUE_LIMIT_VIOLATION = "22001"
419
393
  NUMERIC_VALUE_OUT_OF_RANGE = "22003"
420
394
  NOT_NULL_VIOLATION = "23502"
@@ -422,6 +396,8 @@ module ActiveRecord
422
396
  UNIQUE_VIOLATION = "23505"
423
397
  SERIALIZATION_FAILURE = "40001"
424
398
  DEADLOCK_DETECTED = "40P01"
399
+ LOCK_NOT_AVAILABLE = "55P03"
400
+ QUERY_CANCELED = "57014"
425
401
 
426
402
  def translate_exception(exception, message)
427
403
  return exception unless exception.respond_to?(:result)
@@ -441,6 +417,10 @@ module ActiveRecord
441
417
  SerializationFailure.new(message)
442
418
  when DEADLOCK_DETECTED
443
419
  Deadlocked.new(message)
420
+ when LOCK_NOT_AVAILABLE
421
+ LockWaitTimeout.new(message)
422
+ when QUERY_CANCELED
423
+ QueryCanceled.new(message)
444
424
  else
445
425
  super
446
426
  end
@@ -448,7 +428,7 @@ module ActiveRecord
448
428
 
449
429
  def get_oid_type(oid, fmod, column_name, sql_type = "".freeze)
450
430
  if !type_map.key?(oid)
451
- load_additional_types(type_map, [oid])
431
+ load_additional_types([oid])
452
432
  end
453
433
 
454
434
  type_map.fetch(oid, fmod, sql_type) {
@@ -459,10 +439,10 @@ module ActiveRecord
459
439
  }
460
440
  end
461
441
 
462
- def initialize_type_map(m)
463
- register_class_with_limit m, "int2", Type::Integer
464
- register_class_with_limit m, "int4", Type::Integer
465
- register_class_with_limit m, "int8", Type::Integer
442
+ def initialize_type_map(m = type_map)
443
+ m.register_type "int2", Type::Integer.new(limit: 2)
444
+ m.register_type "int4", Type::Integer.new(limit: 4)
445
+ m.register_type "int8", Type::Integer.new(limit: 8)
466
446
  m.register_type "oid", OID::Oid.new
467
447
  m.register_type "float4", Type::Float.new
468
448
  m.alias_type "float8", "float4"
@@ -481,7 +461,7 @@ module ActiveRecord
481
461
  m.register_type "bytea", OID::Bytea.new
482
462
  m.register_type "point", OID::Point.new
483
463
  m.register_type "hstore", OID::Hstore.new
484
- m.register_type "json", OID::Json.new
464
+ m.register_type "json", Type::Json.new
485
465
  m.register_type "jsonb", OID::Jsonb.new
486
466
  m.register_type "cidr", OID::Cidr.new
487
467
  m.register_type "inet", OID::Inet.new
@@ -526,18 +506,7 @@ module ActiveRecord
526
506
  end
527
507
  end
528
508
 
529
- load_additional_types(m)
530
- end
531
-
532
- def extract_limit(sql_type)
533
- case sql_type
534
- when /^bigint/i, /^int8/i
535
- 8
536
- when /^smallint/i
537
- 2
538
- else
539
- super
540
- end
509
+ load_additional_types
541
510
  end
542
511
 
543
512
  # Extracts the value from a PostgreSQL column default definition.
@@ -575,7 +544,7 @@ module ActiveRecord
575
544
  !default_value && %r{\w+\(.*\)|\(.*\)::\w+|CURRENT_DATE|CURRENT_TIMESTAMP}.match?(default)
576
545
  end
577
546
 
578
- def load_additional_types(type_map, oids = nil)
547
+ def load_additional_types(oids = nil)
579
548
  initializer = OID::TypeMapInitializer.new(type_map)
580
549
 
581
550
  if supports_ranges?
@@ -594,7 +563,7 @@ module ActiveRecord
594
563
  if oids
595
564
  query += "WHERE t.oid::integer IN (%s)" % oids.join(", ")
596
565
  else
597
- query += initializer.query_conditions_for_initial_load(type_map)
566
+ query += initializer.query_conditions_for_initial_load
598
567
  end
599
568
 
600
569
  execute_and_clear(query, "SCHEMA", []) do |records|
@@ -621,7 +590,7 @@ module ActiveRecord
621
590
  type_casted_binds = type_casted_binds(binds)
622
591
  log(sql, name, binds, type_casted_binds) do
623
592
  ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
624
- @connection.exec_params(sql, type_casted_binds)
593
+ @connection.async_exec(sql, type_casted_binds)
625
594
  end
626
595
  end
627
596
  end
@@ -659,7 +628,7 @@ module ActiveRecord
659
628
  # ActiveRecord::PreparedStatementCacheExpired
660
629
  #
661
630
  # Check here for more details:
662
- # http://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/cache/plancache.c#l573
631
+ # https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/backend/utils/cache/plancache.c#l573
663
632
  CACHED_PLAN_HEURISTIC = "cached plan must not change result type".freeze
664
633
  def is_cached_plan_failure?(e)
665
634
  pgerror = e.cause
@@ -724,18 +693,20 @@ module ActiveRecord
724
693
  # Use standard-conforming strings so we don't have to do the E'...' dance.
725
694
  set_standard_conforming_strings
726
695
 
696
+ variables = @config.fetch(:variables, {}).stringify_keys
697
+
727
698
  # If using Active Record's time zone support configure the connection to return
728
699
  # TIMESTAMP WITH ZONE types in UTC.
729
- # (SET TIME ZONE does not use an equals sign like other SET variables)
730
- if ActiveRecord::Base.default_timezone == :utc
731
- execute("SET time zone 'UTC'", "SCHEMA")
732
- elsif @local_tz
733
- execute("SET time zone '#{@local_tz}'", "SCHEMA")
700
+ unless variables["timezone"]
701
+ if ActiveRecord::Base.default_timezone == :utc
702
+ variables["timezone"] = "UTC"
703
+ elsif @local_tz
704
+ variables["timezone"] = @local_tz
705
+ end
734
706
  end
735
707
 
736
708
  # SET statements from :variables config hash
737
- # http://www.postgresql.org/docs/current/static/sql-set.html
738
- variables = @config[:variables] || {}
709
+ # https://www.postgresql.org/docs/current/static/sql-set.html
739
710
  variables.map do |k, v|
740
711
  if v == ":default" || v == :default
741
712
  # Sets the value to the global or compile default
@@ -746,11 +717,6 @@ module ActiveRecord
746
717
  end
747
718
  end
748
719
 
749
- # Returns the current ID of a table's sequence.
750
- def last_insert_id_result(sequence_name)
751
- exec_query("SELECT currval('#{sequence_name}')", "SQL")
752
- end
753
-
754
720
  # Returns the list of a table's column names, data types, and default values.
755
721
  #
756
722
  # The underlying query is roughly:
@@ -789,8 +755,8 @@ module ActiveRecord
789
755
  $1.strip if $1
790
756
  end
791
757
 
792
- def create_table_definition(*args)
793
- PostgreSQL::TableDefinition.new(*args)
758
+ def arel_visitor
759
+ Arel::Visitors::PostgreSQL.new(self)
794
760
  end
795
761
 
796
762
  def can_perform_case_insensitive_comparison_for?(column)
@@ -866,7 +832,6 @@ module ActiveRecord
866
832
  ActiveRecord::Type.register(:enum, OID::Enum, adapter: :postgresql)
867
833
  ActiveRecord::Type.register(:hstore, OID::Hstore, adapter: :postgresql)
868
834
  ActiveRecord::Type.register(:inet, OID::Inet, adapter: :postgresql)
869
- ActiveRecord::Type.register(:json, OID::Json, adapter: :postgresql)
870
835
  ActiveRecord::Type.register(:jsonb, OID::Jsonb, adapter: :postgresql)
871
836
  ActiveRecord::Type.register(:money, OID::Money, adapter: :postgresql)
872
837
  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
@@ -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
@@ -15,14 +17,29 @@ module ActiveRecord
15
17
  end
16
18
 
17
19
  def quoted_time(value)
18
- value = value.change(year: 2000, month: 1, day: 1)
19
- quoted_date(value).sub(/\A\d\d\d\d-\d\d-\d\d /, "2000-01-01 ")
20
+ quoted_date(value)
20
21
  end
21
22
 
22
23
  def quoted_binary(value)
23
24
  "x'#{value.hex}'"
24
25
  end
25
26
 
27
+ def quoted_true
28
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? "1".freeze : "'t'".freeze
29
+ end
30
+
31
+ def unquoted_true
32
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? 1 : "t".freeze
33
+ end
34
+
35
+ def quoted_false
36
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? "0".freeze : "'f'".freeze
37
+ end
38
+
39
+ def unquoted_false
40
+ ActiveRecord::ConnectionAdapters::SQLite3Adapter.represent_boolean_as_integer ? 0 : "f".freeze
41
+ end
42
+
26
43
  private
27
44
 
28
45
  def _type_cast(value)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  module SQLite3
@@ -1,27 +1,18 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  module SQLite3
4
- module ColumnMethods
5
- def primary_key(name, type = :primary_key, **options)
6
- if %i(integer bigint).include?(type) && (options.delete(:auto_increment) == true || !options.key?(:default))
7
- type = :primary_key
8
- end
9
-
10
- super
11
- end
12
- end
13
-
14
6
  class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
15
- include ColumnMethods
16
-
17
7
  def references(*args, **options)
18
8
  super(*args, type: :integer, **options)
19
9
  end
20
10
  alias :belongs_to :references
21
- end
22
11
 
23
- class Table < ActiveRecord::ConnectionAdapters::Table
24
- include ColumnMethods
12
+ private
13
+ def integer_like_primary_key_type(type, options)
14
+ :primary_key
15
+ end
25
16
  end
26
17
  end
27
18
  end
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  module SQLite3
4
- module ColumnDumper # :nodoc:
6
+ class SchemaDumper < ConnectionAdapters::SchemaDumper # :nodoc:
5
7
  private
6
-
7
8
  def default_primary_key?(column)
8
9
  schema_type(column) == :integer
9
10
  end
@@ -1,13 +1,83 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module ActiveRecord
2
4
  module ConnectionAdapters
3
5
  module SQLite3
4
6
  module SchemaStatements # :nodoc:
7
+ # Returns an array of indexes for the given table.
8
+ def indexes(table_name)
9
+ exec_query("PRAGMA index_list(#{quote_table_name(table_name)})", "SCHEMA").map do |row|
10
+ index_sql = query_value(<<-SQL, "SCHEMA")
11
+ SELECT sql
12
+ FROM sqlite_master
13
+ WHERE name = #{quote(row['name'])} AND type = 'index'
14
+ UNION ALL
15
+ SELECT sql
16
+ FROM sqlite_temp_master
17
+ WHERE name = #{quote(row['name'])} AND type = 'index'
18
+ SQL
19
+
20
+ /\sWHERE\s+(?<where>.+)$/i =~ index_sql
21
+
22
+ columns = exec_query("PRAGMA index_info(#{quote(row['name'])})", "SCHEMA").map do |col|
23
+ col["name"]
24
+ end
25
+
26
+ # Add info on sort order for columns (only desc order is explicitly specified, asc is
27
+ # the default)
28
+ orders = {}
29
+ if index_sql # index_sql can be null in case of primary key indexes
30
+ index_sql.scan(/"(\w+)" DESC/).flatten.each { |order_column|
31
+ orders[order_column] = :desc
32
+ }
33
+ end
34
+
35
+ IndexDefinition.new(
36
+ table_name,
37
+ row["name"],
38
+ row["unique"] != 0,
39
+ columns,
40
+ where: where,
41
+ orders: orders
42
+ )
43
+ end
44
+ end
45
+
46
+ def create_schema_dumper(options)
47
+ SQLite3::SchemaDumper.create(self, options)
48
+ end
49
+
5
50
  private
51
+ def schema_creation
52
+ SQLite3::SchemaCreation.new(self)
53
+ end
54
+
55
+ def create_table_definition(*args)
56
+ SQLite3::TableDefinition.new(*args)
57
+ end
58
+
59
+ def new_column_from_field(table_name, field)
60
+ default = \
61
+ case field["dflt_value"]
62
+ when /^null$/i
63
+ nil
64
+ when /^'(.*)'$/m
65
+ $1.gsub("''", "'")
66
+ when /^"(.*)"$/m
67
+ $1.gsub('""', '"')
68
+ else
69
+ field["dflt_value"]
70
+ end
71
+
72
+ type_metadata = fetch_type_metadata(field["type"])
73
+ Column.new(field["name"], default, type_metadata, field["notnull"].to_i == 0, table_name, nil, field["collation"])
74
+ end
75
+
6
76
  def data_source_sql(name = nil, type: nil)
7
77
  scope = quoted_scope(name, type: type)
8
78
  scope[:type] ||= "'table','view'"
9
79
 
10
- sql = "SELECT name FROM sqlite_master WHERE name <> 'sqlite_sequence'"
80
+ sql = "SELECT name FROM sqlite_master WHERE name <> 'sqlite_sequence'".dup
11
81
  sql << " AND name = #{scope[:name]}" if scope[:name]
12
82
  sql << " AND type IN (#{scope[:type]})"
13
83
  sql