activerecord 7.0.8.6 → 7.1.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (227) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1340 -1568
  3. data/MIT-LICENSE +1 -1
  4. data/README.rdoc +15 -16
  5. data/lib/active_record/aggregations.rb +16 -13
  6. data/lib/active_record/association_relation.rb +1 -1
  7. data/lib/active_record/associations/association.rb +18 -3
  8. data/lib/active_record/associations/association_scope.rb +16 -9
  9. data/lib/active_record/associations/belongs_to_association.rb +14 -6
  10. data/lib/active_record/associations/builder/association.rb +3 -3
  11. data/lib/active_record/associations/builder/belongs_to.rb +21 -8
  12. data/lib/active_record/associations/builder/has_and_belongs_to_many.rb +1 -5
  13. data/lib/active_record/associations/builder/singular_association.rb +4 -0
  14. data/lib/active_record/associations/collection_association.rb +17 -9
  15. data/lib/active_record/associations/collection_proxy.rb +16 -11
  16. data/lib/active_record/associations/foreign_association.rb +10 -3
  17. data/lib/active_record/associations/has_many_association.rb +20 -13
  18. data/lib/active_record/associations/has_many_through_association.rb +10 -6
  19. data/lib/active_record/associations/has_one_association.rb +10 -3
  20. data/lib/active_record/associations/join_dependency.rb +10 -8
  21. data/lib/active_record/associations/preloader/association.rb +27 -6
  22. data/lib/active_record/associations/preloader.rb +12 -9
  23. data/lib/active_record/associations/singular_association.rb +1 -1
  24. data/lib/active_record/associations/through_association.rb +22 -11
  25. data/lib/active_record/associations.rb +193 -97
  26. data/lib/active_record/attribute_assignment.rb +0 -2
  27. data/lib/active_record/attribute_methods/before_type_cast.rb +17 -0
  28. data/lib/active_record/attribute_methods/dirty.rb +40 -26
  29. data/lib/active_record/attribute_methods/primary_key.rb +76 -24
  30. data/lib/active_record/attribute_methods/query.rb +28 -16
  31. data/lib/active_record/attribute_methods/read.rb +18 -5
  32. data/lib/active_record/attribute_methods/serialization.rb +150 -31
  33. data/lib/active_record/attribute_methods/write.rb +3 -3
  34. data/lib/active_record/attribute_methods.rb +105 -21
  35. data/lib/active_record/attributes.rb +3 -3
  36. data/lib/active_record/autosave_association.rb +55 -9
  37. data/lib/active_record/base.rb +7 -2
  38. data/lib/active_record/callbacks.rb +10 -24
  39. data/lib/active_record/coders/column_serializer.rb +61 -0
  40. data/lib/active_record/coders/json.rb +1 -1
  41. data/lib/active_record/coders/yaml_column.rb +70 -42
  42. data/lib/active_record/connection_adapters/abstract/connection_handler.rb +163 -88
  43. data/lib/active_record/connection_adapters/abstract/connection_pool/queue.rb +2 -0
  44. data/lib/active_record/connection_adapters/abstract/connection_pool/reaper.rb +3 -1
  45. data/lib/active_record/connection_adapters/abstract/connection_pool.rb +63 -43
  46. data/lib/active_record/connection_adapters/abstract/database_limits.rb +5 -0
  47. data/lib/active_record/connection_adapters/abstract/database_statements.rb +109 -32
  48. data/lib/active_record/connection_adapters/abstract/query_cache.rb +60 -22
  49. data/lib/active_record/connection_adapters/abstract/quoting.rb +41 -6
  50. data/lib/active_record/connection_adapters/abstract/savepoints.rb +4 -3
  51. data/lib/active_record/connection_adapters/abstract/schema_creation.rb +18 -4
  52. data/lib/active_record/connection_adapters/abstract/schema_definitions.rb +137 -11
  53. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +289 -122
  54. data/lib/active_record/connection_adapters/abstract/transaction.rb +280 -58
  55. data/lib/active_record/connection_adapters/abstract_adapter.rb +502 -91
  56. data/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +200 -108
  57. data/lib/active_record/connection_adapters/column.rb +9 -0
  58. data/lib/active_record/connection_adapters/mysql/column.rb +1 -0
  59. data/lib/active_record/connection_adapters/mysql/database_statements.rb +22 -143
  60. data/lib/active_record/connection_adapters/mysql/quoting.rb +16 -12
  61. data/lib/active_record/connection_adapters/mysql/schema_creation.rb +9 -0
  62. data/lib/active_record/connection_adapters/mysql/schema_definitions.rb +6 -0
  63. data/lib/active_record/connection_adapters/mysql/schema_dumper.rb +1 -1
  64. data/lib/active_record/connection_adapters/mysql/schema_statements.rb +17 -12
  65. data/lib/active_record/connection_adapters/mysql2/database_statements.rb +148 -0
  66. data/lib/active_record/connection_adapters/mysql2_adapter.rb +98 -53
  67. data/lib/active_record/connection_adapters/pool_config.rb +14 -5
  68. data/lib/active_record/connection_adapters/pool_manager.rb +19 -9
  69. data/lib/active_record/connection_adapters/postgresql/column.rb +1 -2
  70. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +76 -29
  71. data/lib/active_record/connection_adapters/postgresql/oid/range.rb +11 -2
  72. data/lib/active_record/connection_adapters/postgresql/quoting.rb +9 -6
  73. data/lib/active_record/connection_adapters/postgresql/referential_integrity.rb +3 -9
  74. data/lib/active_record/connection_adapters/postgresql/schema_creation.rb +76 -6
  75. data/lib/active_record/connection_adapters/postgresql/schema_definitions.rb +131 -2
  76. data/lib/active_record/connection_adapters/postgresql/schema_dumper.rb +42 -0
  77. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +351 -54
  78. data/lib/active_record/connection_adapters/postgresql_adapter.rb +336 -168
  79. data/lib/active_record/connection_adapters/schema_cache.rb +287 -59
  80. data/lib/active_record/connection_adapters/sqlite3/column.rb +49 -0
  81. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +42 -36
  82. data/lib/active_record/connection_adapters/sqlite3/quoting.rb +4 -3
  83. data/lib/active_record/connection_adapters/sqlite3/schema_definitions.rb +1 -0
  84. data/lib/active_record/connection_adapters/sqlite3/schema_statements.rb +26 -7
  85. data/lib/active_record/connection_adapters/sqlite3_adapter.rb +162 -77
  86. data/lib/active_record/connection_adapters/statement_pool.rb +7 -0
  87. data/lib/active_record/connection_adapters/trilogy/database_statements.rb +98 -0
  88. data/lib/active_record/connection_adapters/trilogy_adapter.rb +254 -0
  89. data/lib/active_record/connection_adapters.rb +3 -1
  90. data/lib/active_record/connection_handling.rb +71 -94
  91. data/lib/active_record/core.rb +128 -138
  92. data/lib/active_record/counter_cache.rb +46 -25
  93. data/lib/active_record/database_configurations/database_config.rb +9 -3
  94. data/lib/active_record/database_configurations/hash_config.rb +22 -12
  95. data/lib/active_record/database_configurations/url_config.rb +17 -11
  96. data/lib/active_record/database_configurations.rb +86 -33
  97. data/lib/active_record/delegated_type.rb +8 -3
  98. data/lib/active_record/deprecator.rb +7 -0
  99. data/lib/active_record/destroy_association_async_job.rb +2 -0
  100. data/lib/active_record/encryption/auto_filtered_parameters.rb +66 -0
  101. data/lib/active_record/encryption/cipher/aes256_gcm.rb +4 -1
  102. data/lib/active_record/encryption/config.rb +25 -1
  103. data/lib/active_record/encryption/configurable.rb +12 -19
  104. data/lib/active_record/encryption/context.rb +10 -3
  105. data/lib/active_record/encryption/contexts.rb +5 -1
  106. data/lib/active_record/encryption/derived_secret_key_provider.rb +8 -2
  107. data/lib/active_record/encryption/encryptable_record.rb +36 -18
  108. data/lib/active_record/encryption/encrypted_attribute_type.rb +17 -6
  109. data/lib/active_record/encryption/extended_deterministic_queries.rb +66 -54
  110. data/lib/active_record/encryption/extended_deterministic_uniqueness_validator.rb +2 -2
  111. data/lib/active_record/encryption/key_generator.rb +12 -1
  112. data/lib/active_record/encryption/message_serializer.rb +2 -0
  113. data/lib/active_record/encryption/properties.rb +3 -3
  114. data/lib/active_record/encryption/scheme.rb +19 -22
  115. data/lib/active_record/encryption.rb +1 -0
  116. data/lib/active_record/enum.rb +113 -26
  117. data/lib/active_record/errors.rb +89 -15
  118. data/lib/active_record/explain.rb +23 -3
  119. data/lib/active_record/fixture_set/model_metadata.rb +14 -4
  120. data/lib/active_record/fixture_set/render_context.rb +2 -0
  121. data/lib/active_record/fixture_set/table_row.rb +29 -8
  122. data/lib/active_record/fixtures.rb +119 -71
  123. data/lib/active_record/future_result.rb +30 -5
  124. data/lib/active_record/gem_version.rb +4 -4
  125. data/lib/active_record/inheritance.rb +30 -16
  126. data/lib/active_record/insert_all.rb +55 -8
  127. data/lib/active_record/integration.rb +8 -8
  128. data/lib/active_record/internal_metadata.rb +118 -30
  129. data/lib/active_record/locking/pessimistic.rb +5 -2
  130. data/lib/active_record/log_subscriber.rb +29 -12
  131. data/lib/active_record/marshalling.rb +56 -0
  132. data/lib/active_record/message_pack.rb +124 -0
  133. data/lib/active_record/middleware/database_selector/resolver.rb +4 -0
  134. data/lib/active_record/middleware/database_selector.rb +5 -7
  135. data/lib/active_record/middleware/shard_selector.rb +3 -1
  136. data/lib/active_record/migration/command_recorder.rb +100 -4
  137. data/lib/active_record/migration/compatibility.rb +131 -5
  138. data/lib/active_record/migration/default_strategy.rb +23 -0
  139. data/lib/active_record/migration/execution_strategy.rb +19 -0
  140. data/lib/active_record/migration.rb +213 -109
  141. data/lib/active_record/model_schema.rb +47 -27
  142. data/lib/active_record/nested_attributes.rb +28 -3
  143. data/lib/active_record/normalization.rb +158 -0
  144. data/lib/active_record/persistence.rb +183 -33
  145. data/lib/active_record/promise.rb +84 -0
  146. data/lib/active_record/query_cache.rb +3 -21
  147. data/lib/active_record/query_logs.rb +77 -52
  148. data/lib/active_record/query_logs_formatter.rb +41 -0
  149. data/lib/active_record/querying.rb +15 -2
  150. data/lib/active_record/railtie.rb +107 -45
  151. data/lib/active_record/railties/controller_runtime.rb +10 -5
  152. data/lib/active_record/railties/databases.rake +139 -145
  153. data/lib/active_record/railties/job_runtime.rb +23 -0
  154. data/lib/active_record/readonly_attributes.rb +32 -5
  155. data/lib/active_record/reflection.rb +169 -45
  156. data/lib/active_record/relation/batches/batch_enumerator.rb +5 -3
  157. data/lib/active_record/relation/batches.rb +190 -61
  158. data/lib/active_record/relation/calculations.rb +152 -63
  159. data/lib/active_record/relation/delegation.rb +22 -8
  160. data/lib/active_record/relation/finder_methods.rb +85 -15
  161. data/lib/active_record/relation/merger.rb +2 -0
  162. data/lib/active_record/relation/predicate_builder/association_query_value.rb +11 -2
  163. data/lib/active_record/relation/predicate_builder/relation_handler.rb +5 -1
  164. data/lib/active_record/relation/predicate_builder.rb +26 -14
  165. data/lib/active_record/relation/query_attribute.rb +2 -1
  166. data/lib/active_record/relation/query_methods.rb +351 -62
  167. data/lib/active_record/relation/spawn_methods.rb +18 -1
  168. data/lib/active_record/relation.rb +76 -35
  169. data/lib/active_record/result.rb +19 -5
  170. data/lib/active_record/runtime_registry.rb +10 -1
  171. data/lib/active_record/sanitization.rb +51 -11
  172. data/lib/active_record/schema.rb +2 -3
  173. data/lib/active_record/schema_dumper.rb +41 -7
  174. data/lib/active_record/schema_migration.rb +68 -33
  175. data/lib/active_record/scoping/default.rb +15 -5
  176. data/lib/active_record/scoping/named.rb +2 -2
  177. data/lib/active_record/scoping.rb +2 -1
  178. data/lib/active_record/secure_password.rb +60 -0
  179. data/lib/active_record/secure_token.rb +21 -3
  180. data/lib/active_record/signed_id.rb +7 -5
  181. data/lib/active_record/store.rb +8 -8
  182. data/lib/active_record/suppressor.rb +3 -1
  183. data/lib/active_record/table_metadata.rb +10 -1
  184. data/lib/active_record/tasks/database_tasks.rb +127 -105
  185. data/lib/active_record/tasks/mysql_database_tasks.rb +15 -6
  186. data/lib/active_record/tasks/postgresql_database_tasks.rb +16 -13
  187. data/lib/active_record/tasks/sqlite_database_tasks.rb +14 -7
  188. data/lib/active_record/test_fixtures.rb +113 -96
  189. data/lib/active_record/timestamp.rb +26 -14
  190. data/lib/active_record/token_for.rb +113 -0
  191. data/lib/active_record/touch_later.rb +11 -6
  192. data/lib/active_record/transactions.rb +36 -10
  193. data/lib/active_record/type/adapter_specific_registry.rb +1 -8
  194. data/lib/active_record/type/internal/timezone.rb +7 -2
  195. data/lib/active_record/type/time.rb +4 -0
  196. data/lib/active_record/validations/absence.rb +1 -1
  197. data/lib/active_record/validations/numericality.rb +5 -4
  198. data/lib/active_record/validations/presence.rb +5 -28
  199. data/lib/active_record/validations/uniqueness.rb +47 -2
  200. data/lib/active_record/validations.rb +8 -4
  201. data/lib/active_record/version.rb +1 -1
  202. data/lib/active_record.rb +121 -16
  203. data/lib/arel/errors.rb +10 -0
  204. data/lib/arel/factory_methods.rb +4 -0
  205. data/lib/arel/nodes/binary.rb +6 -1
  206. data/lib/arel/nodes/bound_sql_literal.rb +61 -0
  207. data/lib/arel/nodes/cte.rb +36 -0
  208. data/lib/arel/nodes/fragments.rb +35 -0
  209. data/lib/arel/nodes/homogeneous_in.rb +0 -8
  210. data/lib/arel/nodes/leading_join.rb +8 -0
  211. data/lib/arel/nodes/node.rb +111 -2
  212. data/lib/arel/nodes/sql_literal.rb +6 -0
  213. data/lib/arel/nodes/table_alias.rb +4 -0
  214. data/lib/arel/nodes.rb +4 -0
  215. data/lib/arel/predications.rb +2 -0
  216. data/lib/arel/table.rb +9 -5
  217. data/lib/arel/visitors/mysql.rb +8 -1
  218. data/lib/arel/visitors/to_sql.rb +81 -17
  219. data/lib/arel/visitors/visitor.rb +2 -2
  220. data/lib/arel.rb +16 -2
  221. data/lib/rails/generators/active_record/application_record/USAGE +8 -0
  222. data/lib/rails/generators/active_record/migration.rb +3 -1
  223. data/lib/rails/generators/active_record/model/USAGE +113 -0
  224. data/lib/rails/generators/active_record/model/model_generator.rb +15 -6
  225. metadata +52 -17
  226. data/lib/active_record/connection_adapters/legacy_pool_manager.rb +0 -35
  227. data/lib/active_record/null_relation.rb +0 -63
@@ -74,11 +74,11 @@ module ActiveRecord
74
74
  FROM pg_class t
75
75
  INNER JOIN pg_index d ON t.oid = d.indrelid
76
76
  INNER JOIN pg_class i ON d.indexrelid = i.oid
77
- LEFT JOIN pg_namespace n ON n.oid = i.relnamespace
77
+ LEFT JOIN pg_namespace n ON n.oid = t.relnamespace
78
78
  WHERE i.relkind IN ('i', 'I')
79
79
  AND i.relname = #{index[:name]}
80
80
  AND t.relname = #{table[:name]}
81
- AND n.nspname = #{index[:schema]}
81
+ AND n.nspname = #{table[:schema]}
82
82
  SQL
83
83
  end
84
84
 
@@ -88,11 +88,11 @@ module ActiveRecord
88
88
 
89
89
  result = query(<<~SQL, "SCHEMA")
90
90
  SELECT distinct i.relname, d.indisunique, d.indkey, pg_get_indexdef(d.indexrelid), t.oid,
91
- pg_catalog.obj_description(i.oid, 'pg_class') AS comment
91
+ pg_catalog.obj_description(i.oid, 'pg_class') AS comment, d.indisvalid
92
92
  FROM pg_class t
93
93
  INNER JOIN pg_index d ON t.oid = d.indrelid
94
94
  INNER JOIN pg_class i ON d.indexrelid = i.oid
95
- LEFT JOIN pg_namespace n ON n.oid = i.relnamespace
95
+ LEFT JOIN pg_namespace n ON n.oid = t.relnamespace
96
96
  WHERE i.relkind IN ('i', 'I')
97
97
  AND d.indisprimary = 'f'
98
98
  AND t.relname = #{scope[:name]}
@@ -107,25 +107,24 @@ module ActiveRecord
107
107
  inddef = row[3]
108
108
  oid = row[4]
109
109
  comment = row[5]
110
-
111
- using, expressions, where = inddef.scan(/ USING (\w+?) \((.+?)\)(?: WHERE (.+))?\z/m).flatten
110
+ valid = row[6]
111
+ using, expressions, include, nulls_not_distinct, where = inddef.scan(/ USING (\w+?) \((.+?)\)(?: INCLUDE \((.+?)\))?( NULLS NOT DISTINCT)?(?: WHERE (.+))?\z/m).flatten
112
112
 
113
113
  orders = {}
114
114
  opclasses = {}
115
+ include_columns = include ? include.split(",").map(&:strip) : []
115
116
 
116
117
  if indkey.include?(0)
117
118
  columns = expressions
118
119
  else
119
- columns = Hash[query(<<~SQL, "SCHEMA")].values_at(*indkey).compact
120
- SELECT a.attnum, a.attname
121
- FROM pg_attribute a
122
- WHERE a.attrelid = #{oid}
123
- AND a.attnum IN (#{indkey.join(",")})
124
- SQL
120
+ columns = column_names_from_column_numbers(oid, indkey)
121
+
122
+ # prevent INCLUDE columns from being matched
123
+ columns.reject! { |c| include_columns.include?(c) }
125
124
 
126
125
  # add info on sort order (only desc order is explicitly specified, asc is the default)
127
126
  # and non-default opclasses
128
- expressions.scan(/(?<column>\w+)"?\s?(?<opclass>\w+_ops)?\s?(?<desc>DESC)?\s?(?<nulls>NULLS (?:FIRST|LAST))?/).each do |column, opclass, desc, nulls|
127
+ expressions.scan(/(?<column>\w+)"?\s?(?<opclass>\w+_ops(_\w+)?)?\s?(?<desc>DESC)?\s?(?<nulls>NULLS (?:FIRST|LAST))?/).each do |column, opclass, desc, nulls|
129
128
  opclasses[column] = opclass.to_sym if opclass
130
129
  if nulls
131
130
  orders[column] = [desc, nulls].compact.join(" ")
@@ -144,7 +143,10 @@ module ActiveRecord
144
143
  opclasses: opclasses,
145
144
  where: where,
146
145
  using: using.to_sym,
147
- comment: comment.presence
146
+ include: include_columns.presence,
147
+ nulls_not_distinct: nulls_not_distinct.present?,
148
+ comment: comment.presence,
149
+ valid: valid
148
150
  )
149
151
  end
150
152
  end
@@ -223,7 +225,7 @@ module ActiveRecord
223
225
  # This should be not be called manually but set in database.yml.
224
226
  def schema_search_path=(schema_csv)
225
227
  if schema_csv
226
- execute("SET search_path TO #{schema_csv}", "SCHEMA")
228
+ internal_execute("SET search_path TO #{schema_csv}")
227
229
  @schema_search_path = schema_csv
228
230
  end
229
231
  end
@@ -240,7 +242,7 @@ module ActiveRecord
240
242
 
241
243
  # Set the client message level.
242
244
  def client_min_messages=(level)
243
- execute("SET client_min_messages TO '#{level}'", "SCHEMA")
245
+ internal_execute("SET client_min_messages TO '#{level}'")
244
246
  end
245
247
 
246
248
  # Returns the sequence name for a table's primary key or some other specified key.
@@ -288,14 +290,14 @@ module ActiveRecord
288
290
  quoted_sequence = quote_table_name(sequence)
289
291
  max_pk = query_value("SELECT MAX(#{quote_column_name pk}) FROM #{quote_table_name(table)}", "SCHEMA")
290
292
  if max_pk.nil?
291
- if database_version >= 100000
293
+ if database_version >= 10_00_00
292
294
  minvalue = query_value("SELECT seqmin FROM pg_sequence WHERE seqrelid = #{quote(quoted_sequence)}::regclass", "SCHEMA")
293
295
  else
294
296
  minvalue = query_value("SELECT min_value FROM #{quoted_sequence}", "SCHEMA")
295
297
  end
296
298
  end
297
299
 
298
- query_value("SELECT setval(#{quote(quoted_sequence)}, #{max_pk ? max_pk : minvalue}, #{max_pk ? true : false})", "SCHEMA")
300
+ query_value("SELECT setval(#{quote(quoted_sequence)}, #{max_pk || minvalue}, #{max_pk ? true : false})", "SCHEMA")
299
301
  end
300
302
  end
301
303
 
@@ -375,7 +377,8 @@ module ActiveRecord
375
377
  #
376
378
  # Example:
377
379
  # rename_table('octopuses', 'octopi')
378
- def rename_table(table_name, new_name)
380
+ def rename_table(table_name, new_name, **options)
381
+ validate_table_length!(new_name) unless options[:_uses_legacy_table_name]
379
382
  clear_cache!
380
383
  schema_cache.clear_data_source_cache!(table_name.to_s)
381
384
  schema_cache.clear_data_source_cache!(new_name.to_s)
@@ -406,18 +409,39 @@ module ActiveRecord
406
409
  procs.each(&:call)
407
410
  end
408
411
 
412
+ # Builds a ChangeColumnDefinition object.
413
+ #
414
+ # This definition object contains information about the column change that would occur
415
+ # if the same arguments were passed to #change_column. See #change_column for information about
416
+ # passing a +table_name+, +column_name+, +type+ and other options that can be passed.
417
+ def build_change_column_definition(table_name, column_name, type, **options) # :nodoc:
418
+ td = create_table_definition(table_name)
419
+ cd = td.new_column_definition(column_name, type, **options)
420
+ ChangeColumnDefinition.new(cd, column_name)
421
+ end
422
+
409
423
  # Changes the default value of a table column.
410
424
  def change_column_default(table_name, column_name, default_or_changes) # :nodoc:
411
425
  execute "ALTER TABLE #{quote_table_name(table_name)} #{change_column_default_for_alter(table_name, column_name, default_or_changes)}"
412
426
  end
413
427
 
428
+ def build_change_column_default_definition(table_name, column_name, default_or_changes) # :nodoc:
429
+ column = column_for(table_name, column_name)
430
+ return unless column
431
+
432
+ default = extract_new_default_value(default_or_changes)
433
+ ChangeColumnDefaultDefinition.new(column, default)
434
+ end
435
+
414
436
  def change_column_null(table_name, column_name, null, default = nil) # :nodoc:
437
+ validate_change_column_null_argument!(null)
438
+
415
439
  clear_cache!
416
440
  unless null || default.nil?
417
441
  column = column_for(table_name, column_name)
418
442
  execute "UPDATE #{quote_table_name(table_name)} SET #{quote_column_name(column_name)}=#{quote_default_expression(default, column)} WHERE #{quote_column_name(column_name)} IS NULL" if column
419
443
  end
420
- execute "ALTER TABLE #{quote_table_name(table_name)} #{change_column_null_for_alter(table_name, column_name, null, default)}"
444
+ execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL"
421
445
  end
422
446
 
423
447
  # Adds comment for given table column or drops it if +comment+ is a +nil+
@@ -442,15 +466,19 @@ module ActiveRecord
442
466
  end
443
467
 
444
468
  def add_index(table_name, column_name, **options) # :nodoc:
445
- index, algorithm, if_not_exists = add_index_options(table_name, column_name, **options)
446
-
447
- create_index = CreateIndexDefinition.new(index, algorithm, if_not_exists)
469
+ create_index = build_create_index_definition(table_name, column_name, **options)
448
470
  result = execute schema_creation.accept(create_index)
449
471
 
472
+ index = create_index.index
450
473
  execute "COMMENT ON INDEX #{quote_column_name(index.name)} IS #{quote(index.comment)}" if index.comment
451
474
  result
452
475
  end
453
476
 
477
+ def build_create_index_definition(table_name, column_name, **options) # :nodoc:
478
+ index, algorithm, if_not_exists = add_index_options(table_name, column_name, **options)
479
+ CreateIndexDefinition.new(index, algorithm, if_not_exists)
480
+ end
481
+
454
482
  def remove_index(table_name, column_name = nil, **options) # :nodoc:
455
483
  table = Utils.extract_schema_qualified_name(table_name.to_s)
456
484
 
@@ -477,13 +505,33 @@ module ActiveRecord
477
505
  def rename_index(table_name, old_name, new_name)
478
506
  validate_index_length!(table_name, new_name)
479
507
 
480
- execute "ALTER INDEX #{quote_column_name(old_name)} RENAME TO #{quote_table_name(new_name)}"
508
+ schema, = extract_schema_qualified_name(table_name)
509
+ execute "ALTER INDEX #{quote_table_name(schema) + '.' if schema}#{quote_column_name(old_name)} RENAME TO #{quote_table_name(new_name)}"
510
+ end
511
+
512
+ def index_name(table_name, options) # :nodoc:
513
+ _schema, table_name = extract_schema_qualified_name(table_name.to_s)
514
+ super
515
+ end
516
+
517
+ def add_foreign_key(from_table, to_table, **options)
518
+ if options[:deferrable] == true
519
+ ActiveRecord.deprecator.warn(<<~MSG)
520
+ `deferrable: true` is deprecated in favor of `deferrable: :immediate`, and will be removed in Rails 7.2.
521
+ MSG
522
+
523
+ options[:deferrable] = :immediate
524
+ end
525
+
526
+ assert_valid_deferrable(options[:deferrable])
527
+
528
+ super
481
529
  end
482
530
 
483
531
  def foreign_keys(table_name)
484
532
  scope = quoted_scope(table_name)
485
- fk_info = exec_query(<<~SQL, "SCHEMA")
486
- SELECT t2.oid::regclass::text AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete, c.convalidated AS valid, c.condeferrable AS deferrable, c.condeferred AS deferred
533
+ fk_info = internal_exec_query(<<~SQL, "SCHEMA", allow_retry: true, materialize_transactions: false)
534
+ SELECT t2.oid::regclass::text AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete, c.convalidated AS valid, c.condeferrable AS deferrable, c.condeferred AS deferred, c.conkey, c.confkey, c.conrelid, c.confrelid
487
535
  FROM pg_constraint c
488
536
  JOIN pg_class t1 ON c.conrelid = t1.oid
489
537
  JOIN pg_class t2 ON c.confrelid = t2.oid
@@ -497,18 +545,29 @@ module ActiveRecord
497
545
  SQL
498
546
 
499
547
  fk_info.map do |row|
548
+ to_table = Utils.unquote_identifier(row["to_table"])
549
+ conkey = row["conkey"].scan(/\d+/).map(&:to_i)
550
+ confkey = row["confkey"].scan(/\d+/).map(&:to_i)
551
+
552
+ if conkey.size > 1
553
+ column = column_names_from_column_numbers(row["conrelid"], conkey)
554
+ primary_key = column_names_from_column_numbers(row["confrelid"], confkey)
555
+ else
556
+ column = Utils.unquote_identifier(row["column"])
557
+ primary_key = row["primary_key"]
558
+ end
559
+
500
560
  options = {
501
- column: Utils.unquote_identifier(row["column"]),
561
+ column: column,
502
562
  name: row["name"],
503
- primary_key: row["primary_key"]
563
+ primary_key: primary_key
504
564
  }
505
565
 
506
566
  options[:on_delete] = extract_foreign_key_action(row["on_delete"])
507
567
  options[:on_update] = extract_foreign_key_action(row["on_update"])
508
- options[:deferrable] = extract_foreign_key_deferrable(row["deferrable"], row["deferred"])
568
+ options[:deferrable] = extract_constraint_deferrable(row["deferrable"], row["deferred"])
509
569
 
510
570
  options[:validate] = row["valid"]
511
- to_table = Utils.unquote_identifier(row["to_table"])
512
571
 
513
572
  ForeignKeyDefinition.new(table_name, to_table, options)
514
573
  end
@@ -525,7 +584,7 @@ module ActiveRecord
525
584
  def check_constraints(table_name) # :nodoc:
526
585
  scope = quoted_scope(table_name)
527
586
 
528
- check_info = exec_query(<<-SQL, "SCHEMA")
587
+ check_info = internal_exec_query(<<-SQL, "SCHEMA", allow_retry: true, materialize_transactions: false)
529
588
  SELECT conname, pg_get_constraintdef(c.oid, true) AS constraintdef, c.convalidated AS valid
530
589
  FROM pg_constraint c
531
590
  JOIN pg_class t ON c.conrelid = t.oid
@@ -546,6 +605,175 @@ module ActiveRecord
546
605
  end
547
606
  end
548
607
 
608
+ # Returns an array of exclusion constraints for the given table.
609
+ # The exclusion constraints are represented as ExclusionConstraintDefinition objects.
610
+ def exclusion_constraints(table_name)
611
+ scope = quoted_scope(table_name)
612
+
613
+ exclusion_info = internal_exec_query(<<-SQL, "SCHEMA")
614
+ SELECT conname, pg_get_constraintdef(c.oid) AS constraintdef, c.condeferrable, c.condeferred
615
+ FROM pg_constraint c
616
+ JOIN pg_class t ON c.conrelid = t.oid
617
+ JOIN pg_namespace n ON n.oid = c.connamespace
618
+ WHERE c.contype = 'x'
619
+ AND t.relname = #{scope[:name]}
620
+ AND n.nspname = #{scope[:schema]}
621
+ SQL
622
+
623
+ exclusion_info.map do |row|
624
+ method_and_elements, predicate = row["constraintdef"].split(" WHERE ")
625
+ method_and_elements_parts = method_and_elements.match(/EXCLUDE(?: USING (?<using>\S+))? \((?<expression>.+)\)/)
626
+ predicate.remove!(/ DEFERRABLE(?: INITIALLY (?:IMMEDIATE|DEFERRED))?/) if predicate
627
+ predicate = predicate.from(2).to(-3) if predicate # strip 2 opening and closing parentheses
628
+
629
+ deferrable = extract_constraint_deferrable(row["condeferrable"], row["condeferred"])
630
+
631
+ options = {
632
+ name: row["conname"],
633
+ using: method_and_elements_parts["using"].to_sym,
634
+ where: predicate,
635
+ deferrable: deferrable
636
+ }
637
+
638
+ ExclusionConstraintDefinition.new(table_name, method_and_elements_parts["expression"], options)
639
+ end
640
+ end
641
+
642
+ # Returns an array of unique constraints for the given table.
643
+ # The unique constraints are represented as UniqueKeyDefinition objects.
644
+ def unique_keys(table_name)
645
+ scope = quoted_scope(table_name)
646
+
647
+ unique_info = internal_exec_query(<<~SQL, "SCHEMA", allow_retry: true, materialize_transactions: false)
648
+ SELECT c.conname, c.conindid, c.condeferrable, c.condeferred
649
+ FROM pg_constraint c
650
+ JOIN pg_class t ON c.conrelid = t.oid
651
+ JOIN pg_namespace n ON n.oid = c.connamespace
652
+ WHERE c.contype = 'u'
653
+ AND t.relname = #{scope[:name]}
654
+ AND n.nspname = #{scope[:schema]}
655
+ SQL
656
+
657
+ unique_info.map do |row|
658
+ deferrable = extract_constraint_deferrable(row["condeferrable"], row["condeferred"])
659
+
660
+ columns = query_values(<<~SQL, "SCHEMA")
661
+ SELECT a.attname
662
+ FROM pg_attribute a
663
+ WHERE a.attrelid = #{row['conindid']}
664
+ ORDER BY a.attnum
665
+ SQL
666
+
667
+ options = {
668
+ name: row["conname"],
669
+ deferrable: deferrable
670
+ }
671
+
672
+ UniqueKeyDefinition.new(table_name, columns, options)
673
+ end
674
+ end
675
+
676
+ # Adds a new exclusion constraint to the table. +expression+ is a String
677
+ # representation of a list of exclusion elements and operators.
678
+ #
679
+ # add_exclusion_constraint :products, "price WITH =, availability_range WITH &&", using: :gist, name: "price_check"
680
+ #
681
+ # generates:
682
+ #
683
+ # ALTER TABLE "products" ADD CONSTRAINT price_check EXCLUDE USING gist (price WITH =, availability_range WITH &&)
684
+ #
685
+ # The +options+ hash can include the following keys:
686
+ # [<tt>:name</tt>]
687
+ # The constraint name. Defaults to <tt>excl_rails_<identifier></tt>.
688
+ # [<tt>:deferrable</tt>]
689
+ # Specify whether or not the exclusion constraint should be deferrable. Valid values are +false+ or +:immediate+ or +:deferred+ to specify the default behavior. Defaults to +false+.
690
+ def add_exclusion_constraint(table_name, expression, **options)
691
+ options = exclusion_constraint_options(table_name, expression, options)
692
+ at = create_alter_table(table_name)
693
+ at.add_exclusion_constraint(expression, options)
694
+
695
+ execute schema_creation.accept(at)
696
+ end
697
+
698
+ def exclusion_constraint_options(table_name, expression, options) # :nodoc:
699
+ assert_valid_deferrable(options[:deferrable])
700
+
701
+ options = options.dup
702
+ options[:name] ||= exclusion_constraint_name(table_name, expression: expression, **options)
703
+ options
704
+ end
705
+
706
+ # Removes the given exclusion constraint from the table.
707
+ #
708
+ # remove_exclusion_constraint :products, name: "price_check"
709
+ #
710
+ # The +expression+ parameter will be ignored if present. It can be helpful
711
+ # to provide this in a migration's +change+ method so it can be reverted.
712
+ # In that case, +expression+ will be used by #add_exclusion_constraint.
713
+ def remove_exclusion_constraint(table_name, expression = nil, **options)
714
+ excl_name_to_delete = exclusion_constraint_for!(table_name, expression: expression, **options).name
715
+
716
+ at = create_alter_table(table_name)
717
+ at.drop_exclusion_constraint(excl_name_to_delete)
718
+
719
+ execute schema_creation.accept(at)
720
+ end
721
+
722
+ # Adds a new unique constraint to the table.
723
+ #
724
+ # add_unique_key :sections, [:position], deferrable: :deferred, name: "unique_position"
725
+ #
726
+ # generates:
727
+ #
728
+ # ALTER TABLE "sections" ADD CONSTRAINT unique_position UNIQUE (position) DEFERRABLE INITIALLY DEFERRED
729
+ #
730
+ # If you want to change an existing unique index to deferrable, you can use :using_index to create deferrable unique constraints.
731
+ #
732
+ # add_unique_key :sections, deferrable: :deferred, name: "unique_position", using_index: "index_sections_on_position"
733
+ #
734
+ # The +options+ hash can include the following keys:
735
+ # [<tt>:name</tt>]
736
+ # The constraint name. Defaults to <tt>uniq_rails_<identifier></tt>.
737
+ # [<tt>:deferrable</tt>]
738
+ # Specify whether or not the unique constraint should be deferrable. Valid values are +false+ or +:immediate+ or +:deferred+ to specify the default behavior. Defaults to +false+.
739
+ # [<tt>:using_index</tt>]
740
+ # To specify an existing unique index name. Defaults to +nil+.
741
+ def add_unique_key(table_name, column_name = nil, **options)
742
+ options = unique_key_options(table_name, column_name, options)
743
+ at = create_alter_table(table_name)
744
+ at.add_unique_key(column_name, options)
745
+
746
+ execute schema_creation.accept(at)
747
+ end
748
+
749
+ def unique_key_options(table_name, column_name, options) # :nodoc:
750
+ assert_valid_deferrable(options[:deferrable])
751
+
752
+ if column_name && options[:using_index]
753
+ raise ArgumentError, "Cannot specify both column_name and :using_index options."
754
+ end
755
+
756
+ options = options.dup
757
+ options[:name] ||= unique_key_name(table_name, column: column_name, **options)
758
+ options
759
+ end
760
+
761
+ # Removes the given unique constraint from the table.
762
+ #
763
+ # remove_unique_key :sections, name: "unique_position"
764
+ #
765
+ # The +column_name+ parameter will be ignored if present. It can be helpful
766
+ # to provide this in a migration's +change+ method so it can be reverted.
767
+ # In that case, +column_name+ will be used by #add_unique_key.
768
+ def remove_unique_key(table_name, column_name = nil, **options)
769
+ unique_name_to_delete = unique_key_for!(table_name, column: column_name, **options).name
770
+
771
+ at = create_alter_table(table_name)
772
+ at.drop_unique_key(unique_name_to_delete)
773
+
774
+ execute schema_creation.accept(at)
775
+ end
776
+
549
777
  # Maps logical Rails types to PostgreSQL-specific data types.
550
778
  def type_to_sql(type, limit: nil, precision: nil, scale: nil, array: nil, enum_type: nil, **) # :nodoc:
551
779
  sql = \
@@ -649,11 +877,32 @@ module ActiveRecord
649
877
  validate_constraint table_name, chk_name_to_validate
650
878
  end
651
879
 
652
- private
653
- def schema_creation
654
- PostgreSQL::SchemaCreation.new(self)
880
+ def foreign_key_column_for(table_name, column_name) # :nodoc:
881
+ _schema, table_name = extract_schema_qualified_name(table_name)
882
+ super
883
+ end
884
+
885
+ def add_index_options(table_name, column_name, **options) # :nodoc:
886
+ if (where = options[:where]) && table_exists?(table_name) && column_exists?(table_name, where)
887
+ options[:where] = quote_column_name(where)
655
888
  end
889
+ super
890
+ end
656
891
 
892
+ def quoted_include_columns_for_index(column_names) # :nodoc:
893
+ return quote_column_name(column_names) if column_names.is_a?(Symbol)
894
+
895
+ quoted_columns = column_names.each_with_object({}) do |name, result|
896
+ result[name.to_sym] = quote_column_name(name).dup
897
+ end
898
+ add_options_for_index_columns(quoted_columns).values.join(", ")
899
+ end
900
+
901
+ def schema_creation # :nodoc:
902
+ PostgreSQL::SchemaCreation.new(self)
903
+ end
904
+
905
+ private
657
906
  def create_table_definition(name, **options)
658
907
  PostgreSQL::TableDefinition.new(self, name, **options)
659
908
  end
@@ -662,7 +911,7 @@ module ActiveRecord
662
911
  PostgreSQL::AlterTable.new create_table_definition(name)
663
912
  end
664
913
 
665
- def new_column_from_field(table_name, field)
914
+ def new_column_from_field(table_name, field, _definitions)
666
915
  column_name, type, default, notnull, oid, fmod, collation, comment, attgenerated = field
667
916
  type_metadata = fetch_type_metadata(column_name, type, oid.to_i, fmod.to_i)
668
917
  default_value = extract_value_from_default(default)
@@ -726,8 +975,19 @@ module ActiveRecord
726
975
  end
727
976
  end
728
977
 
729
- def extract_foreign_key_deferrable(deferrable, deferred)
730
- deferrable && (deferred ? :deferred : true)
978
+ def assert_valid_deferrable(deferrable)
979
+ return if !deferrable || %i(immediate deferred).include?(deferrable)
980
+
981
+ raise ArgumentError, "deferrable must be `:immediate` or `:deferred`, got: `#{deferrable.inspect}`"
982
+ end
983
+
984
+ def extract_constraint_deferrable(deferrable, deferred)
985
+ deferrable && (deferred ? :deferred : :immediate)
986
+ end
987
+
988
+ def reference_name_for_table(table_name)
989
+ _schema, table_name = extract_schema_qualified_name(table_name.to_s)
990
+ table_name.singularize
731
991
  end
732
992
 
733
993
  def add_column_for_alter(table_name, column_name, type, **options)
@@ -736,32 +996,20 @@ module ActiveRecord
736
996
  end
737
997
 
738
998
  def change_column_for_alter(table_name, column_name, type, **options)
739
- td = create_table_definition(table_name)
740
- cd = td.new_column_definition(column_name, type, **options)
741
- sqls = [schema_creation.accept(ChangeColumnDefinition.new(cd, column_name))]
999
+ change_col_def = build_change_column_definition(table_name, column_name, type, **options)
1000
+ sqls = [schema_creation.accept(change_col_def)]
742
1001
  sqls << Proc.new { change_column_comment(table_name, column_name, options[:comment]) } if options.key?(:comment)
743
1002
  sqls
744
1003
  end
745
1004
 
746
- def change_column_default_for_alter(table_name, column_name, default_or_changes)
747
- column = column_for(table_name, column_name)
748
- return unless column
749
-
750
- default = extract_new_default_value(default_or_changes)
751
- alter_column_query = "ALTER COLUMN #{quote_column_name(column_name)} %s"
1005
+ def change_column_null_for_alter(table_name, column_name, null, default = nil)
752
1006
  if default.nil?
753
- # <tt>DEFAULT NULL</tt> results in the same behavior as <tt>DROP DEFAULT</tt>. However, PostgreSQL will
754
- # cast the default to the columns type, which leaves us with a default like "default NULL::character varying".
755
- alter_column_query % "DROP DEFAULT"
1007
+ "ALTER COLUMN #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL"
756
1008
  else
757
- alter_column_query % "SET DEFAULT #{quote_default_expression(default, column)}"
1009
+ Proc.new { change_column_null(table_name, column_name, null, default) }
758
1010
  end
759
1011
  end
760
1012
 
761
- def change_column_null_for_alter(table_name, column_name, null, default = nil)
762
- "ALTER COLUMN #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL"
763
- end
764
-
765
1013
  def add_index_opclass(quoted_columns, **options)
766
1014
  opclasses = options_for_index_columns(options[:opclass])
767
1015
  quoted_columns.each do |name, column|
@@ -774,6 +1022,46 @@ module ActiveRecord
774
1022
  super
775
1023
  end
776
1024
 
1025
+ def exclusion_constraint_name(table_name, **options)
1026
+ options.fetch(:name) do
1027
+ expression = options.fetch(:expression)
1028
+ identifier = "#{table_name}_#{expression}_excl"
1029
+ hashed_identifier = Digest::SHA256.hexdigest(identifier).first(10)
1030
+
1031
+ "excl_rails_#{hashed_identifier}"
1032
+ end
1033
+ end
1034
+
1035
+ def exclusion_constraint_for(table_name, **options)
1036
+ excl_name = exclusion_constraint_name(table_name, **options)
1037
+ exclusion_constraints(table_name).detect { |excl| excl.name == excl_name }
1038
+ end
1039
+
1040
+ def exclusion_constraint_for!(table_name, expression: nil, **options)
1041
+ exclusion_constraint_for(table_name, expression: expression, **options) ||
1042
+ raise(ArgumentError, "Table '#{table_name}' has no exclusion constraint for #{expression || options}")
1043
+ end
1044
+
1045
+ def unique_key_name(table_name, **options)
1046
+ options.fetch(:name) do
1047
+ column_or_index = Array(options[:column] || options[:using_index]).map(&:to_s)
1048
+ identifier = "#{table_name}_#{column_or_index * '_and_'}_unique"
1049
+ hashed_identifier = Digest::SHA256.hexdigest(identifier).first(10)
1050
+
1051
+ "uniq_rails_#{hashed_identifier}"
1052
+ end
1053
+ end
1054
+
1055
+ def unique_key_for(table_name, **options)
1056
+ name = unique_key_name(table_name, **options) unless options.key?(:column)
1057
+ unique_keys(table_name).detect { |unique_key| unique_key.defined_for?(name: name, **options) }
1058
+ end
1059
+
1060
+ def unique_key_for!(table_name, column: nil, **options)
1061
+ unique_key_for(table_name, column: column, **options) ||
1062
+ raise(ArgumentError, "Table '#{table_name}' has no unique constraint for #{column || options}")
1063
+ end
1064
+
777
1065
  def data_source_sql(name = nil, type: nil)
778
1066
  scope = quoted_scope(name, type: type)
779
1067
  scope[:type] ||= "'r','v','m','p','f'" # (r)elation/table, (v)iew, (m)aterialized view, (p)artitioned table, (f)oreign table
@@ -807,6 +1095,15 @@ module ActiveRecord
807
1095
  name = Utils.extract_schema_qualified_name(string.to_s)
808
1096
  [name.schema, name.identifier]
809
1097
  end
1098
+
1099
+ def column_names_from_column_numbers(table_oid, column_numbers)
1100
+ Hash[query(<<~SQL, "SCHEMA")].values_at(*column_numbers).compact
1101
+ SELECT a.attnum, a.attname
1102
+ FROM pg_attribute a
1103
+ WHERE a.attrelid = #{table_oid}
1104
+ AND a.attnum IN (#{column_numbers.join(", ")})
1105
+ SQL
1106
+ end
810
1107
  end
811
1108
  end
812
1109
  end