activerecord 5.2.4.1 → 6.0.1

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 (269) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +676 -572
  3. data/MIT-LICENSE +3 -1
  4. data/README.rdoc +4 -2
  5. data/examples/performance.rb +1 -1
  6. data/lib/active_record.rb +9 -2
  7. data/lib/active_record/aggregations.rb +4 -2
  8. data/lib/active_record/association_relation.rb +15 -6
  9. data/lib/active_record/associations.rb +20 -15
  10. data/lib/active_record/associations/association.rb +61 -20
  11. data/lib/active_record/associations/association_scope.rb +4 -6
  12. data/lib/active_record/associations/belongs_to_association.rb +36 -42
  13. data/lib/active_record/associations/belongs_to_polymorphic_association.rb +0 -4
  14. data/lib/active_record/associations/builder/association.rb +14 -18
  15. data/lib/active_record/associations/builder/belongs_to.rb +19 -52
  16. data/lib/active_record/associations/builder/collection_association.rb +3 -13
  17. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +17 -38
  18. data/lib/active_record/associations/builder/has_many.rb +2 -0
  19. data/lib/active_record/associations/builder/has_one.rb +35 -1
  20. data/lib/active_record/associations/builder/singular_association.rb +2 -0
  21. data/lib/active_record/associations/collection_association.rb +6 -21
  22. data/lib/active_record/associations/collection_proxy.rb +12 -15
  23. data/lib/active_record/associations/foreign_association.rb +7 -0
  24. data/lib/active_record/associations/has_many_association.rb +2 -10
  25. data/lib/active_record/associations/has_many_through_association.rb +14 -14
  26. data/lib/active_record/associations/has_one_association.rb +28 -30
  27. data/lib/active_record/associations/has_one_through_association.rb +5 -5
  28. data/lib/active_record/associations/join_dependency.rb +28 -28
  29. data/lib/active_record/associations/join_dependency/join_association.rb +9 -10
  30. data/lib/active_record/associations/join_dependency/join_part.rb +2 -2
  31. data/lib/active_record/associations/preloader.rb +40 -32
  32. data/lib/active_record/associations/preloader/association.rb +38 -36
  33. data/lib/active_record/associations/preloader/through_association.rb +48 -39
  34. data/lib/active_record/associations/singular_association.rb +2 -16
  35. data/lib/active_record/attribute_assignment.rb +7 -10
  36. data/lib/active_record/attribute_methods.rb +28 -100
  37. data/lib/active_record/attribute_methods/before_type_cast.rb +4 -1
  38. data/lib/active_record/attribute_methods/dirty.rb +111 -40
  39. data/lib/active_record/attribute_methods/primary_key.rb +15 -22
  40. data/lib/active_record/attribute_methods/query.rb +2 -3
  41. data/lib/active_record/attribute_methods/read.rb +15 -53
  42. data/lib/active_record/attribute_methods/serialization.rb +1 -1
  43. data/lib/active_record/attribute_methods/time_zone_conversion.rb +1 -1
  44. data/lib/active_record/attribute_methods/write.rb +17 -24
  45. data/lib/active_record/attributes.rb +13 -0
  46. data/lib/active_record/autosave_association.rb +2 -2
  47. data/lib/active_record/base.rb +2 -3
  48. data/lib/active_record/callbacks.rb +5 -19
  49. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +104 -16
  50. data/lib/active_record/connection_adapters/abstract/database_limits.rb +17 -4
  51. data/lib/active_record/connection_adapters/abstract/database_statements.rb +99 -123
  52. data/lib/active_record/connection_adapters/abstract/query_cache.rb +18 -8
  53. data/lib/active_record/connection_adapters/abstract/quoting.rb +68 -17
  54. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +19 -12
  55. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +76 -48
  56. data/lib/active_record/connection_adapters/abstract/schema_dumper.rb +1 -3
  57. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +132 -53
  58. data/lib/active_record/connection_adapters/abstract/transaction.rb +96 -56
  59. data/lib/active_record/connection_adapters/abstract_adapter.rb +187 -43
  60. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +138 -195
  61. data/lib/active_record/connection_adapters/column.rb +17 -13
  62. data/lib/active_record/connection_adapters/connection_specification.rb +53 -43
  63. data/lib/active_record/connection_adapters/determine_if_preparable_visitor.rb +6 -10
  64. data/lib/active_record/connection_adapters/mysql/database_statements.rb +75 -13
  65. data/lib/active_record/connection_adapters/mysql/quoting.rb +44 -7
  66. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +3 -4
  67. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +40 -32
  68. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +14 -6
  69. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +129 -13
  70. data/lib/active_record/connection_adapters/mysql/type_metadata.rb +6 -10
  71. data/lib/active_record/connection_adapters/mysql2_adapter.rb +26 -9
  72. data/lib/active_record/connection_adapters/postgresql/column.rb +17 -31
  73. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +22 -1
  74. data/lib/active_record/connection_adapters/postgresql/oid/array.rb +1 -1
  75. data/lib/active_record/connection_adapters/postgresql/oid/bit.rb +1 -4
  76. data/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +1 -1
  77. data/lib/active_record/connection_adapters/postgresql/oid/legacy_point.rb +1 -1
  78. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +2 -2
  79. data/lib/active_record/connection_adapters/postgresql/oid/point.rb +1 -1
  80. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +1 -1
  81. data/lib/active_record/connection_adapters/postgresql/oid/type_map_initializer.rb +9 -7
  82. data/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +6 -3
  83. data/lib/active_record/connection_adapters/postgresql/quoting.rb +44 -7
  84. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +12 -1
  85. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +107 -91
  86. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +55 -53
  87. data/lib/active_record/connection_adapters/postgresql/type_metadata.rb +24 -27
  88. data/lib/active_record/connection_adapters/postgresql_adapter.rb +164 -74
  89. data/lib/active_record/connection_adapters/schema_cache.rb +37 -14
  90. data/lib/active_record/connection_adapters/sql_type_metadata.rb +11 -8
  91. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +120 -0
  92. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +42 -6
  93. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +42 -11
  94. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +129 -141
  95. data/lib/active_record/connection_handling.rb +155 -26
  96. data/lib/active_record/core.rb +103 -59
  97. data/lib/active_record/counter_cache.rb +4 -29
  98. data/lib/active_record/database_configurations.rb +233 -0
  99. data/lib/active_record/database_configurations/database_config.rb +37 -0
  100. data/lib/active_record/database_configurations/hash_config.rb +50 -0
  101. data/lib/active_record/database_configurations/url_config.rb +79 -0
  102. data/lib/active_record/dynamic_matchers.rb +1 -1
  103. data/lib/active_record/enum.rb +37 -7
  104. data/lib/active_record/errors.rb +15 -7
  105. data/lib/active_record/explain.rb +1 -1
  106. data/lib/active_record/fixture_set/model_metadata.rb +33 -0
  107. data/lib/active_record/fixture_set/render_context.rb +17 -0
  108. data/lib/active_record/fixture_set/table_row.rb +153 -0
  109. data/lib/active_record/fixture_set/table_rows.rb +47 -0
  110. data/lib/active_record/fixtures.rb +145 -472
  111. data/lib/active_record/gem_version.rb +4 -4
  112. data/lib/active_record/inheritance.rb +13 -3
  113. data/lib/active_record/insert_all.rb +179 -0
  114. data/lib/active_record/integration.rb +68 -16
  115. data/lib/active_record/internal_metadata.rb +10 -2
  116. data/lib/active_record/locking/optimistic.rb +5 -6
  117. data/lib/active_record/locking/pessimistic.rb +3 -3
  118. data/lib/active_record/log_subscriber.rb +7 -26
  119. data/lib/active_record/middleware/database_selector.rb +75 -0
  120. data/lib/active_record/middleware/database_selector/resolver.rb +88 -0
  121. data/lib/active_record/middleware/database_selector/resolver/session.rb +45 -0
  122. data/lib/active_record/migration.rb +100 -81
  123. data/lib/active_record/migration/command_recorder.rb +50 -6
  124. data/lib/active_record/migration/compatibility.rb +76 -49
  125. data/lib/active_record/model_schema.rb +33 -9
  126. data/lib/active_record/nested_attributes.rb +2 -2
  127. data/lib/active_record/no_touching.rb +7 -0
  128. data/lib/active_record/persistence.rb +228 -24
  129. data/lib/active_record/query_cache.rb +11 -4
  130. data/lib/active_record/querying.rb +32 -20
  131. data/lib/active_record/railtie.rb +80 -43
  132. data/lib/active_record/railties/collection_cache_association_loading.rb +34 -0
  133. data/lib/active_record/railties/controller_runtime.rb +30 -35
  134. data/lib/active_record/railties/databases.rake +196 -46
  135. data/lib/active_record/reflection.rb +32 -30
  136. data/lib/active_record/relation.rb +311 -80
  137. data/lib/active_record/relation/batches.rb +13 -10
  138. data/lib/active_record/relation/calculations.rb +53 -47
  139. data/lib/active_record/relation/delegation.rb +26 -43
  140. data/lib/active_record/relation/finder_methods.rb +23 -27
  141. data/lib/active_record/relation/merger.rb +11 -20
  142. data/lib/active_record/relation/predicate_builder.rb +4 -6
  143. data/lib/active_record/relation/predicate_builder/array_handler.rb +5 -4
  144. data/lib/active_record/relation/predicate_builder/association_query_value.rb +1 -4
  145. data/lib/active_record/relation/predicate_builder/base_handler.rb +1 -2
  146. data/lib/active_record/relation/predicate_builder/basic_object_handler.rb +1 -2
  147. data/lib/active_record/relation/predicate_builder/polymorphic_array_value.rb +1 -4
  148. data/lib/active_record/relation/predicate_builder/range_handler.rb +3 -23
  149. data/lib/active_record/relation/query_attribute.rb +13 -8
  150. data/lib/active_record/relation/query_methods.rb +202 -64
  151. data/lib/active_record/relation/spawn_methods.rb +1 -1
  152. data/lib/active_record/relation/where_clause.rb +14 -10
  153. data/lib/active_record/relation/where_clause_factory.rb +1 -2
  154. data/lib/active_record/result.rb +30 -11
  155. data/lib/active_record/sanitization.rb +32 -40
  156. data/lib/active_record/schema.rb +2 -11
  157. data/lib/active_record/schema_dumper.rb +22 -7
  158. data/lib/active_record/schema_migration.rb +5 -1
  159. data/lib/active_record/scoping.rb +8 -8
  160. data/lib/active_record/scoping/default.rb +4 -5
  161. data/lib/active_record/scoping/named.rb +19 -15
  162. data/lib/active_record/statement_cache.rb +30 -3
  163. data/lib/active_record/store.rb +87 -8
  164. data/lib/active_record/table_metadata.rb +10 -17
  165. data/lib/active_record/tasks/database_tasks.rb +194 -25
  166. data/lib/active_record/tasks/mysql_database_tasks.rb +5 -5
  167. data/lib/active_record/tasks/postgresql_database_tasks.rb +5 -7
  168. data/lib/active_record/tasks/sqlite_database_tasks.rb +2 -8
  169. data/lib/active_record/test_databases.rb +23 -0
  170. data/lib/active_record/test_fixtures.rb +224 -0
  171. data/lib/active_record/timestamp.rb +39 -25
  172. data/lib/active_record/touch_later.rb +4 -2
  173. data/lib/active_record/transactions.rb +56 -65
  174. data/lib/active_record/translation.rb +1 -1
  175. data/lib/active_record/type.rb +3 -4
  176. data/lib/active_record/type/adapter_specific_registry.rb +1 -8
  177. data/lib/active_record/type_caster/connection.rb +15 -14
  178. data/lib/active_record/type_caster/map.rb +1 -4
  179. data/lib/active_record/validations.rb +1 -0
  180. data/lib/active_record/validations/uniqueness.rb +15 -27
  181. data/lib/arel.rb +58 -0
  182. data/lib/arel/alias_predication.rb +9 -0
  183. data/lib/arel/attributes.rb +22 -0
  184. data/lib/arel/attributes/attribute.rb +37 -0
  185. data/lib/arel/collectors/bind.rb +24 -0
  186. data/lib/arel/collectors/composite.rb +31 -0
  187. data/lib/arel/collectors/plain_string.rb +20 -0
  188. data/lib/arel/collectors/sql_string.rb +20 -0
  189. data/lib/arel/collectors/substitute_binds.rb +28 -0
  190. data/lib/arel/crud.rb +42 -0
  191. data/lib/arel/delete_manager.rb +18 -0
  192. data/lib/arel/errors.rb +9 -0
  193. data/lib/arel/expressions.rb +29 -0
  194. data/lib/arel/factory_methods.rb +49 -0
  195. data/lib/arel/insert_manager.rb +49 -0
  196. data/lib/arel/math.rb +45 -0
  197. data/lib/arel/nodes.rb +68 -0
  198. data/lib/arel/nodes/and.rb +32 -0
  199. data/lib/arel/nodes/ascending.rb +23 -0
  200. data/lib/arel/nodes/binary.rb +52 -0
  201. data/lib/arel/nodes/bind_param.rb +36 -0
  202. data/lib/arel/nodes/case.rb +55 -0
  203. data/lib/arel/nodes/casted.rb +50 -0
  204. data/lib/arel/nodes/comment.rb +29 -0
  205. data/lib/arel/nodes/count.rb +12 -0
  206. data/lib/arel/nodes/delete_statement.rb +45 -0
  207. data/lib/arel/nodes/descending.rb +23 -0
  208. data/lib/arel/nodes/equality.rb +18 -0
  209. data/lib/arel/nodes/extract.rb +24 -0
  210. data/lib/arel/nodes/false.rb +16 -0
  211. data/lib/arel/nodes/full_outer_join.rb +8 -0
  212. data/lib/arel/nodes/function.rb +44 -0
  213. data/lib/arel/nodes/grouping.rb +8 -0
  214. data/lib/arel/nodes/in.rb +8 -0
  215. data/lib/arel/nodes/infix_operation.rb +80 -0
  216. data/lib/arel/nodes/inner_join.rb +8 -0
  217. data/lib/arel/nodes/insert_statement.rb +37 -0
  218. data/lib/arel/nodes/join_source.rb +20 -0
  219. data/lib/arel/nodes/matches.rb +18 -0
  220. data/lib/arel/nodes/named_function.rb +23 -0
  221. data/lib/arel/nodes/node.rb +50 -0
  222. data/lib/arel/nodes/node_expression.rb +13 -0
  223. data/lib/arel/nodes/outer_join.rb +8 -0
  224. data/lib/arel/nodes/over.rb +15 -0
  225. data/lib/arel/nodes/regexp.rb +16 -0
  226. data/lib/arel/nodes/right_outer_join.rb +8 -0
  227. data/lib/arel/nodes/select_core.rb +67 -0
  228. data/lib/arel/nodes/select_statement.rb +41 -0
  229. data/lib/arel/nodes/sql_literal.rb +16 -0
  230. data/lib/arel/nodes/string_join.rb +11 -0
  231. data/lib/arel/nodes/table_alias.rb +27 -0
  232. data/lib/arel/nodes/terminal.rb +16 -0
  233. data/lib/arel/nodes/true.rb +16 -0
  234. data/lib/arel/nodes/unary.rb +45 -0
  235. data/lib/arel/nodes/unary_operation.rb +20 -0
  236. data/lib/arel/nodes/unqualified_column.rb +22 -0
  237. data/lib/arel/nodes/update_statement.rb +41 -0
  238. data/lib/arel/nodes/values_list.rb +9 -0
  239. data/lib/arel/nodes/window.rb +126 -0
  240. data/lib/arel/nodes/with.rb +11 -0
  241. data/lib/arel/order_predications.rb +13 -0
  242. data/lib/arel/predications.rb +257 -0
  243. data/lib/arel/select_manager.rb +271 -0
  244. data/lib/arel/table.rb +110 -0
  245. data/lib/arel/tree_manager.rb +72 -0
  246. data/lib/arel/update_manager.rb +34 -0
  247. data/lib/arel/visitors.rb +20 -0
  248. data/lib/arel/visitors/depth_first.rb +204 -0
  249. data/lib/arel/visitors/dot.rb +297 -0
  250. data/lib/arel/visitors/ibm_db.rb +34 -0
  251. data/lib/arel/visitors/informix.rb +62 -0
  252. data/lib/arel/visitors/mssql.rb +157 -0
  253. data/lib/arel/visitors/mysql.rb +83 -0
  254. data/lib/arel/visitors/oracle.rb +159 -0
  255. data/lib/arel/visitors/oracle12.rb +66 -0
  256. data/lib/arel/visitors/postgresql.rb +110 -0
  257. data/lib/arel/visitors/sqlite.rb +39 -0
  258. data/lib/arel/visitors/to_sql.rb +889 -0
  259. data/lib/arel/visitors/visitor.rb +46 -0
  260. data/lib/arel/visitors/where_sql.rb +23 -0
  261. data/lib/arel/window_predications.rb +9 -0
  262. data/lib/rails/generators/active_record/migration.rb +14 -1
  263. data/lib/rails/generators/active_record/migration/migration_generator.rb +2 -5
  264. data/lib/rails/generators/active_record/migration/templates/create_table_migration.rb.tt +1 -1
  265. data/lib/rails/generators/active_record/migration/templates/migration.rb.tt +4 -2
  266. data/lib/rails/generators/active_record/model/model_generator.rb +1 -0
  267. data/lib/rails/generators/active_record/model/templates/model.rb.tt +10 -1
  268. metadata +110 -25
  269. data/lib/active_record/collection_cache_key.rb +0 -53
@@ -2,43 +2,29 @@
2
2
 
3
3
  module ActiveRecord
4
4
  module ConnectionAdapters
5
- # PostgreSQL-specific extensions to column definitions in a table.
6
- class PostgreSQLColumn < Column #:nodoc:
7
- delegate :array, :oid, :fmod, to: :sql_type_metadata
8
- alias :array? :array
5
+ module PostgreSQL
6
+ class Column < ConnectionAdapters::Column # :nodoc:
7
+ delegate :oid, :fmod, to: :sql_type_metadata
9
8
 
10
- def initialize(*, max_identifier_length: 63, **)
11
- super
12
- @max_identifier_length = max_identifier_length
13
- end
14
-
15
- def serial?
16
- return unless default_function
17
-
18
- if %r{\Anextval\('"?(?<sequence_name>.+_(?<suffix>seq\d*))"?'::regclass\)\z} =~ default_function
19
- sequence_name_from_parts(table_name, name, suffix) == sequence_name
9
+ def initialize(*, serial: nil, **)
10
+ super
11
+ @serial = serial
20
12
  end
21
- end
22
-
23
- protected
24
- attr_reader :max_identifier_length
25
13
 
26
- private
27
- def sequence_name_from_parts(table_name, column_name, suffix)
28
- over_length = [table_name, column_name, suffix].map(&:length).sum + 2 - max_identifier_length
29
-
30
- if over_length > 0
31
- column_name_length = [(max_identifier_length - suffix.length - 2) / 2, column_name.length].min
32
- over_length -= column_name.length - column_name_length
33
- column_name = column_name[0, column_name_length - [over_length, 0].min]
34
- end
14
+ def serial?
15
+ @serial
16
+ end
35
17
 
36
- if over_length > 0
37
- table_name = table_name[0, table_name.length - over_length]
38
- end
18
+ def array
19
+ sql_type_metadata.sql_type.end_with?("[]")
20
+ end
21
+ alias :array? :array
39
22
 
40
- "#{table_name}_#{column_name}_#{suffix}"
23
+ def sql_type
24
+ super.sub(/\[\]\z/, "")
41
25
  end
26
+ end
42
27
  end
28
+ PostgreSQLColumn = PostgreSQL::Column # :nodoc:
43
29
  end
44
30
  end
@@ -58,6 +58,8 @@ module ActiveRecord
58
58
 
59
59
  # Queries the database and returns the results in an Array-like object
60
60
  def query(sql, name = nil) #:nodoc:
61
+ materialize_transactions
62
+
61
63
  log(sql, name) do
62
64
  ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
63
65
  result_as_array @connection.async_exec(sql)
@@ -65,11 +67,26 @@ module ActiveRecord
65
67
  end
66
68
  end
67
69
 
70
+ READ_QUERY = ActiveRecord::ConnectionAdapters::AbstractAdapter.build_read_query_regexp(
71
+ :begin, :commit, :explain, :select, :set, :show, :release, :savepoint, :rollback, :with
72
+ ) # :nodoc:
73
+ private_constant :READ_QUERY
74
+
75
+ def write_query?(sql) # :nodoc:
76
+ !READ_QUERY.match?(sql)
77
+ end
78
+
68
79
  # Executes an SQL statement, returning a PG::Result object on success
69
80
  # or raising a PG::Error exception otherwise.
70
81
  # Note: the PG::Result object is manually memory managed; if you don't
71
82
  # need it specifically, you may want consider the <tt>exec_query</tt> wrapper.
72
83
  def execute(sql, name = nil)
84
+ if preventing_writes? && write_query?(sql)
85
+ raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
86
+ end
87
+
88
+ materialize_transactions
89
+
73
90
  log(sql, name) do
74
91
  ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
75
92
  @connection.async_exec(sql)
@@ -95,7 +112,7 @@ module ActiveRecord
95
112
  end
96
113
  alias :exec_update :exec_delete
97
114
 
98
- def sql_for_insert(sql, pk, id_value, sequence_name, binds) # :nodoc:
115
+ def sql_for_insert(sql, pk, binds) # :nodoc:
99
116
  if pk.nil?
100
117
  # Extract the table from the insert sql. Yuck.
101
118
  table_ref = extract_table_ref_from_insert_sql(sql)
@@ -149,6 +166,10 @@ module ActiveRecord
149
166
  end
150
167
 
151
168
  private
169
+ def build_truncate_statements(*table_names)
170
+ "TRUNCATE TABLE #{table_names.map(&method(:quote_table_name)).join(", ")}"
171
+ end
172
+
152
173
  # Returns the current ID of a table's sequence.
153
174
  def last_insert_id_result(sequence_name)
154
175
  exec_query("SELECT currval(#{quote(sequence_name)})", "SQL")
@@ -5,7 +5,7 @@ module ActiveRecord
5
5
  module PostgreSQL
6
6
  module OID # :nodoc:
7
7
  class Array < Type::Value # :nodoc:
8
- include Type::Helpers::Mutable
8
+ include ActiveModel::Type::Helpers::Mutable
9
9
 
10
10
  Data = Struct.new(:encoder, :values) # :nodoc:
11
11
 
@@ -43,10 +43,7 @@ module ActiveRecord
43
43
  /\A[0-9A-F]*\Z/i.match?(value)
44
44
  end
45
45
 
46
- # TODO Change this to private once we've dropped Ruby 2.2 support.
47
- # Workaround for Ruby 2.2 "private attribute?" warning.
48
- protected
49
-
46
+ private
50
47
  attr_reader :value
51
48
  end
52
49
  end
@@ -5,7 +5,7 @@ module ActiveRecord
5
5
  module PostgreSQL
6
6
  module OID # :nodoc:
7
7
  class Hstore < Type::Value # :nodoc:
8
- include Type::Helpers::Mutable
8
+ include ActiveModel::Type::Helpers::Mutable
9
9
 
10
10
  def type
11
11
  :hstore
@@ -5,7 +5,7 @@ module ActiveRecord
5
5
  module PostgreSQL
6
6
  module OID # :nodoc:
7
7
  class LegacyPoint < Type::Value # :nodoc:
8
- include Type::Helpers::Mutable
8
+ include ActiveModel::Type::Helpers::Mutable
9
9
 
10
10
  def type
11
11
  :point
@@ -26,9 +26,9 @@ module ActiveRecord
26
26
 
27
27
  value = value.sub(/^\((.+)\)$/, '-\1') # (4)
28
28
  case value
29
- when /^-?\D+[\d,]+\.\d{2}$/ # (1)
29
+ when /^-?\D*[\d,]+\.\d{2}$/ # (1)
30
30
  value.gsub!(/[^-\d.]/, "")
31
- when /^-?\D+[\d.]+,\d{2}$/ # (2)
31
+ when /^-?\D*[\d.]+,\d{2}$/ # (2)
32
32
  value.gsub!(/[^-\d,]/, "").sub!(/,/, ".")
33
33
  end
34
34
 
@@ -7,7 +7,7 @@ module ActiveRecord
7
7
  module PostgreSQL
8
8
  module OID # :nodoc:
9
9
  class Point < Type::Value # :nodoc:
10
- include Type::Helpers::Mutable
10
+ include ActiveModel::Type::Helpers::Mutable
11
11
 
12
12
  def type
13
13
  :point
@@ -64,7 +64,7 @@ module ActiveRecord
64
64
  end
65
65
 
66
66
  def type_cast_single_for_database(value)
67
- infinity?(value) ? value : @subtype.serialize(value)
67
+ infinity?(value) ? value : @subtype.serialize(@subtype.cast(value))
68
68
  end
69
69
 
70
70
  def extract_bounds(value)
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support/core_ext/array/extract"
4
+
3
5
  module ActiveRecord
4
6
  module ConnectionAdapters
5
7
  module PostgreSQL
@@ -16,12 +18,12 @@ module ActiveRecord
16
18
 
17
19
  def run(records)
18
20
  nodes = records.reject { |row| @store.key? row["oid"].to_i }
19
- mapped, nodes = nodes.partition { |row| @store.key? row["typname"] }
20
- ranges, nodes = nodes.partition { |row| row["typtype"] == "r".freeze }
21
- enums, nodes = nodes.partition { |row| row["typtype"] == "e".freeze }
22
- domains, nodes = nodes.partition { |row| row["typtype"] == "d".freeze }
23
- arrays, nodes = nodes.partition { |row| row["typinput"] == "array_in".freeze }
24
- composites, nodes = nodes.partition { |row| row["typelem"].to_i != 0 }
21
+ mapped = nodes.extract! { |row| @store.key? row["typname"] }
22
+ ranges = nodes.extract! { |row| row["typtype"] == "r" }
23
+ enums = nodes.extract! { |row| row["typtype"] == "e" }
24
+ domains = nodes.extract! { |row| row["typtype"] == "d" }
25
+ arrays = nodes.extract! { |row| row["typinput"] == "array_in" }
26
+ composites = nodes.extract! { |row| row["typelem"].to_i != 0 }
25
27
 
26
28
  mapped.each { |row| register_mapped_type(row) }
27
29
  enums.each { |row| register_enum_type(row) }
@@ -34,7 +36,7 @@ module ActiveRecord
34
36
  def query_conditions_for_initial_load
35
37
  known_type_names = @store.keys.map { |n| "'#{n}'" }
36
38
  known_type_types = %w('r' 'e' 'd')
37
- <<-SQL % [known_type_names.join(", "), known_type_types.join(", ")]
39
+ <<~SQL % [known_type_names.join(", "), known_type_types.join(", ")]
38
40
  WHERE
39
41
  t.typname IN (%s)
40
42
  OR t.typtype IN (%s)
@@ -13,9 +13,12 @@ module ActiveRecord
13
13
  :uuid
14
14
  end
15
15
 
16
- def cast(value)
17
- value.to_s[ACCEPTABLE_UUID, 0]
18
- end
16
+ private
17
+
18
+ def cast_value(value)
19
+ casted = value.to_s
20
+ casted if casted.match?(ACCEPTABLE_UUID)
21
+ end
19
22
  end
20
23
  end
21
24
  end
@@ -30,7 +30,7 @@ module ActiveRecord
30
30
  # - "schema.name".table_name
31
31
  # - "schema.name"."table.name"
32
32
  def quote_table_name(name) # :nodoc:
33
- @quoted_table_names[name] ||= Utils.extract_schema_qualified_name(name.to_s).quoted.freeze
33
+ self.class.quoted_table_names[name] ||= Utils.extract_schema_qualified_name(name.to_s).quoted.freeze
34
34
  end
35
35
 
36
36
  # Quotes schema names for use in SQL queries.
@@ -44,7 +44,7 @@ module ActiveRecord
44
44
 
45
45
  # Quotes column names for use in SQL queries.
46
46
  def quote_column_name(name) # :nodoc:
47
- @quoted_column_names[name] ||= PG::Connection.quote_ident(super).freeze
47
+ self.class.quoted_column_names[name] ||= PG::Connection.quote_ident(super).freeze
48
48
  end
49
49
 
50
50
  # Quote date/time values for use in SQL input.
@@ -78,6 +78,43 @@ module ActiveRecord
78
78
  type_map.lookup(column.oid, column.fmod, column.sql_type)
79
79
  end
80
80
 
81
+ def column_name_matcher
82
+ COLUMN_NAME
83
+ end
84
+
85
+ def column_name_with_order_matcher
86
+ COLUMN_NAME_WITH_ORDER
87
+ end
88
+
89
+ COLUMN_NAME = /
90
+ \A
91
+ (
92
+ (?:
93
+ # "table_name"."column_name"::type_name | function(one or no argument)::type_name
94
+ ((?:\w+\.|"\w+"\.)?(?:\w+|"\w+")(?:::\w+)?) | \w+\((?:|\g<2>)\)(?:::\w+)?
95
+ )
96
+ (?:\s+AS\s+(?:\w+|"\w+"))?
97
+ )
98
+ (?:\s*,\s*\g<1>)*
99
+ \z
100
+ /ix
101
+
102
+ COLUMN_NAME_WITH_ORDER = /
103
+ \A
104
+ (
105
+ (?:
106
+ # "table_name"."column_name"::type_name | function(one or no argument)::type_name
107
+ ((?:\w+\.|"\w+"\.)?(?:\w+|"\w+")(?:::\w+)?) | \w+\((?:|\g<2>)\)(?:::\w+)?
108
+ )
109
+ (?:\s+ASC|\s+DESC)?
110
+ (?:\s+NULLS\s+(?:FIRST|LAST))?
111
+ )
112
+ (?:\s*,\s*\g<1>)*
113
+ \z
114
+ /ix
115
+
116
+ private_constant :COLUMN_NAME, :COLUMN_NAME_WITH_ORDER
117
+
81
118
  private
82
119
  def lookup_cast_type(sql_type)
83
120
  super(query_value("SELECT #{quote(sql_type)}::regtype::oid", "SCHEMA").to_i)
@@ -93,11 +130,11 @@ module ActiveRecord
93
130
  elsif value.hex?
94
131
  "X'#{value}'"
95
132
  end
96
- when Float
97
- if value.infinite? || value.nan?
98
- "'#{value}'"
99
- else
133
+ when Numeric
134
+ if value.finite?
100
135
  super
136
+ else
137
+ "'#{value}'"
101
138
  end
102
139
  when OID::Array::Data
103
140
  _quote(encode_array(value))
@@ -138,7 +175,7 @@ module ActiveRecord
138
175
  end
139
176
 
140
177
  def encode_range(range)
141
- "[#{type_cast_range_value(range.first)},#{type_cast_range_value(range.last)}#{range.exclude_end? ? ')' : ']'}"
178
+ "[#{type_cast_range_value(range.begin)},#{type_cast_range_value(range.end)}#{range.exclude_end? ? ')' : ']'}"
142
179
  end
143
180
 
144
181
  def determine_encoding_of_strings_in_array(value)
@@ -22,7 +22,7 @@ module ActiveRecord
22
22
  column.sql_type = type_to_sql(column.type, column.options)
23
23
  quoted_column_name = quote_column_name(o.name)
24
24
 
25
- change_column_sql = "ALTER COLUMN #{quoted_column_name} TYPE #{column.sql_type}".dup
25
+ change_column_sql = +"ALTER COLUMN #{quoted_column_name} TYPE #{column.sql_type}"
26
26
 
27
27
  options = column_options(column)
28
28
 
@@ -59,6 +59,17 @@ module ActiveRecord
59
59
  end
60
60
  super
61
61
  end
62
+
63
+ # Returns any SQL string to go between CREATE and TABLE. May be nil.
64
+ def table_modifier_in_create(o)
65
+ # A table cannot be both TEMPORARY and UNLOGGED, since all TEMPORARY
66
+ # tables are already UNLOGGED.
67
+ if o.temporary
68
+ " TEMPORARY"
69
+ elsif o.unlogged
70
+ " UNLOGGED"
71
+ end
72
+ end
62
73
  end
63
74
  end
64
75
  end
@@ -4,6 +4,8 @@ module ActiveRecord
4
4
  module ConnectionAdapters
5
5
  module PostgreSQL
6
6
  module ColumnMethods
7
+ extend ActiveSupport::Concern
8
+
7
9
  # Defines the primary key field.
8
10
  # Use of the native PostgreSQL UUID type is supported, and can be used
9
11
  # by defining your tables as such:
@@ -13,10 +15,10 @@ module ActiveRecord
13
15
  # t.timestamps
14
16
  # end
15
17
  #
16
- # By default, this will use the +gen_random_uuid()+ function from the
18
+ # By default, this will use the <tt>gen_random_uuid()</tt> function from the
17
19
  # +pgcrypto+ extension. As that extension is only available in
18
20
  # PostgreSQL 9.4+, for earlier versions an explicit default can be set
19
- # to use +uuid_generate_v4()+ from the +uuid-ossp+ extension instead:
21
+ # to use <tt>uuid_generate_v4()</tt> from the +uuid-ossp+ extension instead:
20
22
  #
21
23
  # create_table :stuffs, id: false do |t|
22
24
  # t.primary_key :id, :uuid, default: "uuid_generate_v4()"
@@ -51,130 +53,144 @@ module ActiveRecord
51
53
  super
52
54
  end
53
55
 
54
- def bigserial(*args, **options)
55
- args.each { |name| column(name, :bigserial, options) }
56
- end
56
+ ##
57
+ # :method: bigserial
58
+ # :call-seq: bigserial(*names, **options)
57
59
 
58
- def bit(*args, **options)
59
- args.each { |name| column(name, :bit, options) }
60
- end
60
+ ##
61
+ # :method: bit
62
+ # :call-seq: bit(*names, **options)
61
63
 
62
- def bit_varying(*args, **options)
63
- args.each { |name| column(name, :bit_varying, options) }
64
- end
64
+ ##
65
+ # :method: bit_varying
66
+ # :call-seq: bit_varying(*names, **options)
65
67
 
66
- def cidr(*args, **options)
67
- args.each { |name| column(name, :cidr, options) }
68
- end
68
+ ##
69
+ # :method: cidr
70
+ # :call-seq: cidr(*names, **options)
69
71
 
70
- def citext(*args, **options)
71
- args.each { |name| column(name, :citext, options) }
72
- end
72
+ ##
73
+ # :method: citext
74
+ # :call-seq: citext(*names, **options)
73
75
 
74
- def daterange(*args, **options)
75
- args.each { |name| column(name, :daterange, options) }
76
- end
76
+ ##
77
+ # :method: daterange
78
+ # :call-seq: daterange(*names, **options)
77
79
 
78
- def hstore(*args, **options)
79
- args.each { |name| column(name, :hstore, options) }
80
- end
80
+ ##
81
+ # :method: hstore
82
+ # :call-seq: hstore(*names, **options)
81
83
 
82
- def inet(*args, **options)
83
- args.each { |name| column(name, :inet, options) }
84
- end
84
+ ##
85
+ # :method: inet
86
+ # :call-seq: inet(*names, **options)
85
87
 
86
- def interval(*args, **options)
87
- args.each { |name| column(name, :interval, options) }
88
- end
88
+ ##
89
+ # :method: interval
90
+ # :call-seq: interval(*names, **options)
89
91
 
90
- def int4range(*args, **options)
91
- args.each { |name| column(name, :int4range, options) }
92
- end
92
+ ##
93
+ # :method: int4range
94
+ # :call-seq: int4range(*names, **options)
93
95
 
94
- def int8range(*args, **options)
95
- args.each { |name| column(name, :int8range, options) }
96
- end
96
+ ##
97
+ # :method: int8range
98
+ # :call-seq: int8range(*names, **options)
97
99
 
98
- def jsonb(*args, **options)
99
- args.each { |name| column(name, :jsonb, options) }
100
- end
100
+ ##
101
+ # :method: jsonb
102
+ # :call-seq: jsonb(*names, **options)
101
103
 
102
- def ltree(*args, **options)
103
- args.each { |name| column(name, :ltree, options) }
104
- end
104
+ ##
105
+ # :method: ltree
106
+ # :call-seq: ltree(*names, **options)
105
107
 
106
- def macaddr(*args, **options)
107
- args.each { |name| column(name, :macaddr, options) }
108
- end
108
+ ##
109
+ # :method: macaddr
110
+ # :call-seq: macaddr(*names, **options)
109
111
 
110
- def money(*args, **options)
111
- args.each { |name| column(name, :money, options) }
112
- end
112
+ ##
113
+ # :method: money
114
+ # :call-seq: money(*names, **options)
113
115
 
114
- def numrange(*args, **options)
115
- args.each { |name| column(name, :numrange, options) }
116
- end
116
+ ##
117
+ # :method: numrange
118
+ # :call-seq: numrange(*names, **options)
117
119
 
118
- def oid(*args, **options)
119
- args.each { |name| column(name, :oid, options) }
120
- end
120
+ ##
121
+ # :method: oid
122
+ # :call-seq: oid(*names, **options)
121
123
 
122
- def point(*args, **options)
123
- args.each { |name| column(name, :point, options) }
124
- end
124
+ ##
125
+ # :method: point
126
+ # :call-seq: point(*names, **options)
125
127
 
126
- def line(*args, **options)
127
- args.each { |name| column(name, :line, options) }
128
- end
128
+ ##
129
+ # :method: line
130
+ # :call-seq: line(*names, **options)
129
131
 
130
- def lseg(*args, **options)
131
- args.each { |name| column(name, :lseg, options) }
132
- end
132
+ ##
133
+ # :method: lseg
134
+ # :call-seq: lseg(*names, **options)
133
135
 
134
- def box(*args, **options)
135
- args.each { |name| column(name, :box, options) }
136
- end
136
+ ##
137
+ # :method: box
138
+ # :call-seq: box(*names, **options)
137
139
 
138
- def path(*args, **options)
139
- args.each { |name| column(name, :path, options) }
140
- end
140
+ ##
141
+ # :method: path
142
+ # :call-seq: path(*names, **options)
141
143
 
142
- def polygon(*args, **options)
143
- args.each { |name| column(name, :polygon, options) }
144
- end
144
+ ##
145
+ # :method: polygon
146
+ # :call-seq: polygon(*names, **options)
145
147
 
146
- def circle(*args, **options)
147
- args.each { |name| column(name, :circle, options) }
148
- end
148
+ ##
149
+ # :method: circle
150
+ # :call-seq: circle(*names, **options)
149
151
 
150
- def serial(*args, **options)
151
- args.each { |name| column(name, :serial, options) }
152
- end
152
+ ##
153
+ # :method: serial
154
+ # :call-seq: serial(*names, **options)
153
155
 
154
- def tsrange(*args, **options)
155
- args.each { |name| column(name, :tsrange, options) }
156
- end
156
+ ##
157
+ # :method: tsrange
158
+ # :call-seq: tsrange(*names, **options)
157
159
 
158
- def tstzrange(*args, **options)
159
- args.each { |name| column(name, :tstzrange, options) }
160
- end
160
+ ##
161
+ # :method: tstzrange
162
+ # :call-seq: tstzrange(*names, **options)
161
163
 
162
- def tsvector(*args, **options)
163
- args.each { |name| column(name, :tsvector, options) }
164
- end
164
+ ##
165
+ # :method: tsvector
166
+ # :call-seq: tsvector(*names, **options)
165
167
 
166
- def uuid(*args, **options)
167
- args.each { |name| column(name, :uuid, options) }
168
- end
168
+ ##
169
+ # :method: uuid
170
+ # :call-seq: uuid(*names, **options)
171
+
172
+ ##
173
+ # :method: xml
174
+ # :call-seq: xml(*names, **options)
169
175
 
170
- def xml(*args, **options)
171
- args.each { |name| column(name, :xml, options) }
176
+ included do
177
+ define_column_methods :bigserial, :bit, :bit_varying, :cidr, :citext, :daterange,
178
+ :hstore, :inet, :interval, :int4range, :int8range, :jsonb, :ltree, :macaddr,
179
+ :money, :numrange, :oid, :point, :line, :lseg, :box, :path, :polygon, :circle,
180
+ :serial, :tsrange, :tstzrange, :tsvector, :uuid, :xml
172
181
  end
173
182
  end
174
183
 
175
184
  class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
176
185
  include ColumnMethods
177
186
 
187
+ attr_reader :unlogged
188
+
189
+ def initialize(*)
190
+ super
191
+ @unlogged = ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.create_unlogged_tables
192
+ end
193
+
178
194
  private
179
195
  def integer_like_primary_key_type(type, options)
180
196
  if type == :bigint || options[:limit] == 8