activerecord 7.1.0 → 7.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +95 -0
  3. data/README.rdoc +1 -0
  4. data/lib/active_record/associations.rb +14 -14
  5. data/lib/active_record/attribute_methods/dirty.rb +13 -9
  6. data/lib/active_record/callbacks.rb +2 -2
  7. data/lib/active_record/connection_adapters/abstract/database_statements.rb +5 -3
  8. data/lib/active_record/connection_adapters/abstract/schema_statements.rb +1 -3
  9. data/lib/active_record/connection_adapters/abstract_adapter.rb +13 -4
  10. data/lib/active_record/connection_adapters/mysql2/database_statements.rb +3 -0
  11. data/lib/active_record/connection_adapters/postgresql/column.rb +14 -2
  12. data/lib/active_record/connection_adapters/postgresql/database_statements.rb +4 -1
  13. data/lib/active_record/connection_adapters/postgresql/oid/money.rb +3 -2
  14. data/lib/active_record/connection_adapters/postgresql/oid/timestamp_with_time_zone.rb +1 -1
  15. data/lib/active_record/connection_adapters/postgresql/schema_statements.rb +14 -6
  16. data/lib/active_record/connection_adapters/postgresql_adapter.rb +32 -32
  17. data/lib/active_record/connection_adapters/sqlite3/database_statements.rb +10 -3
  18. data/lib/active_record/connection_adapters/trilogy/database_statements.rb +1 -0
  19. data/lib/active_record/core.rb +25 -24
  20. data/lib/active_record/encryption/extended_deterministic_queries.rb +0 -15
  21. data/lib/active_record/enum.rb +6 -9
  22. data/lib/active_record/gem_version.rb +1 -1
  23. data/lib/active_record/insert_all.rb +3 -3
  24. data/lib/active_record/internal_metadata.rb +3 -1
  25. data/lib/active_record/migration/command_recorder.rb +4 -1
  26. data/lib/active_record/migration/pending_migration_connection.rb +21 -0
  27. data/lib/active_record/migration.rb +6 -3
  28. data/lib/active_record/model_schema.rb +5 -5
  29. data/lib/active_record/nested_attributes.rb +7 -9
  30. data/lib/active_record/normalization.rb +8 -0
  31. data/lib/active_record/promise.rb +1 -1
  32. data/lib/active_record/railtie.rb +1 -1
  33. data/lib/active_record/railties/databases.rake +5 -5
  34. data/lib/active_record/reflection.rb +12 -0
  35. data/lib/active_record/relation/calculations.rb +16 -8
  36. data/lib/active_record/relation/query_methods.rb +1 -1
  37. data/lib/active_record/schema_migration.rb +1 -1
  38. data/lib/active_record/timestamp.rb +1 -1
  39. data/lib/arel/nodes/homogeneous_in.rb +1 -1
  40. metadata +10 -9
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c1c4576a55f62b03c5c65f85726c7a054dcf2265c0596ab2b836366f70de537
4
- data.tar.gz: 8e1a2827cfc53bef1deaba5901134b589bac138711e37ac34fcf95254372b875
3
+ metadata.gz: dba30f1c9ab82b5a3e78337bcdb364d613e04a20e815c4b1f6952e1360d8e602
4
+ data.tar.gz: dc7470a1bc6823f31ec4c3b678df945ac4a32a2a1a642943909f47318af205f6
5
5
  SHA512:
6
- metadata.gz: 13a36c3bbd80622c3a266fd79883baa35dec93e3624d92718cc0fa56f6f4c23dfab83e6fd702e4a8e35b6e4a6f2e81fcc24f8c9e7a71c8d9e791f71bccb9536e
7
- data.tar.gz: 0b132373d4dda124cc61707c5cd9e08e5cabb68815ed8650de2250becc5d0856610b3622a0d18434bd5182d84cf31fb6b20117d5b0d8ef28ef7698fdcfcc28b4
6
+ metadata.gz: b85f866d527785cad076c222228e1b6ee39122b383663cc1fe81dcad6eb152c52dfb46ad86dee471f01525c5e074a382fbe5360fd99e78e7886d7169c2c60091
7
+ data.tar.gz: 5291b47487b7bdb83885687b2c6ca90fe5295ed0c017317585bbb712c125c28f66c4b3d5c7cceb0e041bd25be069854c365222179bad6dc360b9156e5f3753ae
data/CHANGELOG.md CHANGED
@@ -1,3 +1,94 @@
1
+ ## Rails 7.1.2 (November 10, 2023) ##
2
+
3
+ * Fix renaming primary key index when renaming a table with a UUID primary key
4
+ in PostgreSQL.
5
+
6
+ *fatkodima*
7
+
8
+ * Fix `where(field: values)` queries when `field` is a serialized attribute
9
+ (for example, when `field` uses `ActiveRecord::Base.serialize` or is a JSON
10
+ column).
11
+
12
+ *João Alves*
13
+
14
+ * Prevent marking broken connections as verified.
15
+
16
+ *Daniel Colson*
17
+
18
+ * Don't mark Float::INFINITY as changed when reassigning it
19
+
20
+ When saving a record with a float infinite value, it shouldn't mark as changed
21
+
22
+ *Maicol Bentancor*
23
+
24
+ * `ActiveRecord::Base.table_name` now returns `nil` instead of raising
25
+ "undefined method `abstract_class?` for Object:Class".
26
+
27
+ *a5-stable*
28
+
29
+ * Fix upserting for custom `:on_duplicate` and `:unique_by` consisting of all
30
+ inserts keys.
31
+
32
+ *fatkodima*
33
+
34
+ * Fixed an [issue](https://github.com/rails/rails/issues/49809) where saving a
35
+ record could innappropriately `dup` its attributes.
36
+
37
+ *Jonathan Hefner*
38
+
39
+ * Dump schema only for a specific db for rollback/up/down tasks for multiple dbs.
40
+
41
+ *fatkodima*
42
+
43
+ * Fix `NoMethodError` when casting a PostgreSQL `money` value that uses a
44
+ comma as its radix point and has no leading currency symbol. For example,
45
+ when casting `"3,50"`.
46
+
47
+ *Andreas Reischuck* and *Jonathan Hefner*
48
+
49
+ * Re-enable support for using `enum` with non-column-backed attributes.
50
+ Non-column-backed attributes must be previously declared with an explicit
51
+ type. For example:
52
+
53
+ ```ruby
54
+ class Post < ActiveRecord::Base
55
+ attribute :topic, :string
56
+ enum topic: %i[science tech engineering math]
57
+ end
58
+ ```
59
+
60
+ *Jonathan Hefner*
61
+
62
+ * Raise on `foreign_key:` being passed as an array in associations
63
+
64
+ *Nikita Vasilevsky*
65
+
66
+ * Return back maximum allowed PostgreSQL table name to 63 characters.
67
+
68
+ *fatkodima*
69
+
70
+ * Fix detecting `IDENTITY` columns for PostgreSQL < 10.
71
+
72
+ *fatkodima*
73
+
74
+
75
+ ## Rails 7.1.1 (October 11, 2023) ##
76
+
77
+ * Fix auto populating IDENTITY columns for PostgreSQL.
78
+
79
+ *fatkodima*
80
+
81
+ * Fix "ArgumentError: wrong number of arguments (given 3, expected 2)" when
82
+ down migrating `rename_table` in older migrations.
83
+
84
+ *fatkodima*
85
+
86
+ * Do not require the Action Text, Active Storage and Action Mailbox tables
87
+ to be present when running when running test on CI.
88
+
89
+ *Rafael Mendonça França*
90
+
91
+
1
92
  ## Rails 7.1.0 (October 05, 2023) ##
2
93
 
3
94
  * No changes.
@@ -148,6 +239,10 @@
148
239
 
149
240
  *Adam Hess*
150
241
 
242
+ * Deprecate aliasing non-attributes with `alias_attribute`.
243
+
244
+ *Ian Candy*
245
+
151
246
  * Fix unscope is not working in specific case
152
247
 
153
248
  Before:
data/README.rdoc CHANGED
@@ -166,6 +166,7 @@ Active Record is an implementation of the object-relational mapping (ORM)
166
166
  pattern[https://www.martinfowler.com/eaaCatalog/activeRecord.html] by the same
167
167
  name described by Martin Fowler:
168
168
 
169
+ >>>
169
170
  "An object that wraps a row in a database table or view,
170
171
  encapsulates the database access, and adds domain logic on that data."
171
172
 
@@ -1339,7 +1339,7 @@ module ActiveRecord
1339
1339
  # Returns a Relation of all of the associated objects, forcing a database read.
1340
1340
  # An empty Relation is returned if none are found.
1341
1341
  #
1342
- # === Example
1342
+ # ==== Example
1343
1343
  #
1344
1344
  # class Firm < ActiveRecord::Base
1345
1345
  # has_many :clients
@@ -1369,7 +1369,7 @@ module ActiveRecord
1369
1369
  #
1370
1370
  # The declaration can also include an +options+ hash to specialize the behavior of the association.
1371
1371
  #
1372
- # === Scopes
1372
+ # ==== Scopes
1373
1373
  #
1374
1374
  # You can pass a second argument +scope+ as a callable (i.e. proc or
1375
1375
  # lambda) to retrieve a specific set of records or customize the generated
@@ -1380,7 +1380,7 @@ module ActiveRecord
1380
1380
  # has_many :employees, -> { joins(:address) }
1381
1381
  # has_many :posts, ->(blog) { where("max_post_length > ?", blog.max_post_length) }
1382
1382
  #
1383
- # === Extensions
1383
+ # ==== Extensions
1384
1384
  #
1385
1385
  # The +extension+ argument allows you to pass a block into a has_many
1386
1386
  # association. This is useful for adding new finders, creators, and other
@@ -1394,7 +1394,7 @@ module ActiveRecord
1394
1394
  # end
1395
1395
  # end
1396
1396
  #
1397
- # === Options
1397
+ # ==== Options
1398
1398
  # [+:class_name+]
1399
1399
  # Specify the class name of the association. Use it only if that name can't be inferred
1400
1400
  # from the association name. So <tt>has_many :products</tt> will by default be linked
@@ -1556,7 +1556,7 @@ module ActiveRecord
1556
1556
  # [<tt>reset_association</tt>]
1557
1557
  # Unloads the associated object. The next access will query it from the database.
1558
1558
  #
1559
- # === Example
1559
+ # ==== Example
1560
1560
  #
1561
1561
  # class Account < ActiveRecord::Base
1562
1562
  # has_one :beneficiary
@@ -1575,7 +1575,7 @@ module ActiveRecord
1575
1575
  # account.reload_beneficiary
1576
1576
  # account.reset_beneficiary
1577
1577
  #
1578
- # === Scopes
1578
+ # ==== Scopes
1579
1579
  #
1580
1580
  # You can pass a second argument +scope+ as a callable (i.e. proc or
1581
1581
  # lambda) to retrieve a specific record or customize the generated query
@@ -1586,7 +1586,7 @@ module ActiveRecord
1586
1586
  # has_one :employer, -> { joins(:company) }
1587
1587
  # has_one :latest_post, ->(blog) { where("created_at > ?", blog.enabled_at) }
1588
1588
  #
1589
- # === Options
1589
+ # ==== Options
1590
1590
  #
1591
1591
  # The declaration can also include an +options+ hash to specialize the behavior of the association.
1592
1592
  #
@@ -1745,7 +1745,7 @@ module ActiveRecord
1745
1745
  # [<tt>association_previously_changed?</tt>]
1746
1746
  # Returns true if the previous save updated the association to reference a new associate object.
1747
1747
  #
1748
- # === Example
1748
+ # ==== Example
1749
1749
  #
1750
1750
  # class Post < ActiveRecord::Base
1751
1751
  # belongs_to :author
@@ -1766,7 +1766,7 @@ module ActiveRecord
1766
1766
  # post.author_changed?
1767
1767
  # post.author_previously_changed?
1768
1768
  #
1769
- # === Scopes
1769
+ # ==== Scopes
1770
1770
  #
1771
1771
  # You can pass a second argument +scope+ as a callable (i.e. proc or
1772
1772
  # lambda) to retrieve a specific record or customize the generated query
@@ -1777,7 +1777,7 @@ module ActiveRecord
1777
1777
  # belongs_to :user, -> { joins(:friends) }
1778
1778
  # belongs_to :level, ->(game) { where("game_level > ?", game.current_level) }
1779
1779
  #
1780
- # === Options
1780
+ # ==== Options
1781
1781
  #
1782
1782
  # The declaration can also include an +options+ hash to specialize the behavior of the association.
1783
1783
  #
@@ -1964,7 +1964,7 @@ module ActiveRecord
1964
1964
  # Returns a Relation of all of the associated objects, forcing a database read.
1965
1965
  # An empty Relation is returned if none are found.
1966
1966
  #
1967
- # === Example
1967
+ # ==== Example
1968
1968
  #
1969
1969
  # class Developer < ActiveRecord::Base
1970
1970
  # has_and_belongs_to_many :projects
@@ -1993,7 +1993,7 @@ module ActiveRecord
1993
1993
  #
1994
1994
  # The declaration may include an +options+ hash to specialize the behavior of the association.
1995
1995
  #
1996
- # === Scopes
1996
+ # ==== Scopes
1997
1997
  #
1998
1998
  # You can pass a second argument +scope+ as a callable (i.e. proc or
1999
1999
  # lambda) to retrieve a specific set of records or customize the generated
@@ -2005,7 +2005,7 @@ module ActiveRecord
2005
2005
  # where("default_category = ?", post.default_category)
2006
2006
  # }
2007
2007
  #
2008
- # === Extensions
2008
+ # ==== Extensions
2009
2009
  #
2010
2010
  # The +extension+ argument allows you to pass a block into a
2011
2011
  # has_and_belongs_to_many association. This is useful for adding new
@@ -2020,7 +2020,7 @@ module ActiveRecord
2020
2020
  # end
2021
2021
  # end
2022
2022
  #
2023
- # === Options
2023
+ # ==== Options
2024
2024
  #
2025
2025
  # [+:class_name+]
2026
2026
  # Specify the class name of the association. Use it only if that name can't be inferred
@@ -7,7 +7,7 @@ module ActiveRecord
7
7
  # = Active Record Attribute Methods \Dirty
8
8
  #
9
9
  # Provides a way to track changes in your Active Record models. It adds all
10
- # methods from ActiveModel::Dirty and adds database specific methods.
10
+ # methods from ActiveModel::Dirty and adds database-specific methods.
11
11
  #
12
12
  # A newly created +Person+ object is unchanged:
13
13
  #
@@ -76,11 +76,13 @@ module ActiveRecord
76
76
  #
77
77
  # ==== Options
78
78
  #
79
- # +from+ When passed, this method will return false unless the original
80
- # value is equal to the given option
79
+ # [+from+]
80
+ # When specified, this method will return false unless the original
81
+ # value is equal to the given value.
81
82
  #
82
- # +to+ When passed, this method will return false unless the value was
83
- # changed to the given value
83
+ # [+to+]
84
+ # When specified, this method will return false unless the value will be
85
+ # changed to the given value.
84
86
  def saved_change_to_attribute?(attr_name, **options)
85
87
  mutations_before_last_save.changed?(attr_name.to_s, **options)
86
88
  end
@@ -126,11 +128,13 @@ module ActiveRecord
126
128
  #
127
129
  # ==== Options
128
130
  #
129
- # +from+ When passed, this method will return false unless the original
130
- # value is equal to the given option
131
+ # [+from+]
132
+ # When specified, this method will return false unless the original
133
+ # value is equal to the given value.
131
134
  #
132
- # +to+ When passed, this method will return false unless the value will be
133
- # changed to the given value
135
+ # [+to+]
136
+ # When specified, this method will return false unless the value will be
137
+ # changed to the given value.
134
138
  def will_save_change_to_attribute?(attr_name, **options)
135
139
  mutations_from_database.changed?(attr_name.to_s, **options)
136
140
  end
@@ -241,8 +241,8 @@ module ActiveRecord
241
241
  #
242
242
  # config.active_record.run_after_transaction_callbacks_in_order_defined = false
243
243
  #
244
- # If +true+ (the default from \Rails 7.1), callbacks are executed in the order they
245
- # are defined, just like the example above. If +false+, the order is reversed, so
244
+ # When set to +true+ (the default from \Rails 7.1), callbacks are executed in the order they
245
+ # are defined, just like the example above. When set to +false+, the order is reversed, so
246
246
  # +do_something_else+ is executed before +log_children+.
247
247
  #
248
248
  # == \Transactions
@@ -189,8 +189,10 @@ module ActiveRecord
189
189
  def insert(arel, name = nil, pk = nil, id_value = nil, sequence_name = nil, binds = [], returning: nil)
190
190
  sql, binds = to_sql_and_binds(arel, binds)
191
191
  value = exec_insert(sql, name, binds, pk, sequence_name, returning: returning)
192
- return id_value if id_value
193
- returning.nil? ? last_inserted_id(value) : returning_column_values(value)
192
+
193
+ return returning_column_values(value) unless returning.nil?
194
+
195
+ id_value || last_inserted_id(value)
194
196
  end
195
197
  alias create insert
196
198
 
@@ -673,7 +675,7 @@ module ActiveRecord
673
675
 
674
676
  def extract_table_ref_from_insert_sql(sql)
675
677
  if sql =~ /into\s("[A-Za-z0-9_."\[\]\s]+"|[A-Za-z0-9_."\[\]]+)\s*/im
676
- $1.strip
678
+ $1.delete('"').strip
677
679
  end
678
680
  end
679
681
  end
@@ -983,7 +983,6 @@ module ActiveRecord
983
983
  # Adds a reference. The reference column is a bigint by default,
984
984
  # the <tt>:type</tt> option can be used to specify a different type.
985
985
  # Optionally adds a +_type+ column, if <tt>:polymorphic</tt> option is provided.
986
- # #add_reference and #add_belongs_to are acceptable.
987
986
  #
988
987
  # The +options+ hash can include the following keys:
989
988
  # [<tt>:type</tt>]
@@ -1034,7 +1033,6 @@ module ActiveRecord
1034
1033
  alias :add_belongs_to :add_reference
1035
1034
 
1036
1035
  # Removes the reference(s). Also removes a +type+ column if one exists.
1037
- # #remove_reference and #remove_belongs_to are acceptable.
1038
1036
  #
1039
1037
  # ====== Remove the reference
1040
1038
  #
@@ -1400,7 +1398,7 @@ module ActiveRecord
1400
1398
 
1401
1399
  limited = relation.reselect(values).distinct!
1402
1400
  limited_ids = select_rows(limited.arel, "SQL").map do |results|
1403
- results.last(Array(relation.primary_key).length) # ignores order values for MySQL and Postgres
1401
+ results.last(Array(relation.primary_key).length) # ignores order values for MySQL and PostgreSQL
1404
1402
  end
1405
1403
 
1406
1404
  if limited_ids.empty?
@@ -970,7 +970,11 @@ module ActiveRecord
970
970
  # If +allow_retry+ is true, a connection-related exception will
971
971
  # cause an automatic reconnect and re-run of the block, up to
972
972
  # the connection's configured +connection_retries+ setting
973
- # and the configured +retry_deadline+ limit.
973
+ # and the configured +retry_deadline+ limit. (Note that when
974
+ # +allow_retry+ is true, it's possible to return without having marked
975
+ # the connection as verified. If the block is guaranteed to exercise the
976
+ # connection, consider calling `verified!` to avoid needless
977
+ # verification queries in subsequent calls.)
974
978
  #
975
979
  # If +materialize_transactions+ is false, the block will be run without
976
980
  # ensuring virtual transactions have been materialized in the DB
@@ -1021,9 +1025,7 @@ module ActiveRecord
1021
1025
  end
1022
1026
 
1023
1027
  begin
1024
- result = yield @raw_connection
1025
- @verified = true
1026
- result
1028
+ yield @raw_connection
1027
1029
  rescue => original_exception
1028
1030
  translated_exception = translate_exception_class(original_exception, nil, nil)
1029
1031
  invalidate_transaction(translated_exception)
@@ -1058,6 +1060,13 @@ module ActiveRecord
1058
1060
  end
1059
1061
  end
1060
1062
 
1063
+ # Mark the connection as verified. Call this inside a
1064
+ # `with_raw_connection` block only when the block is guaranteed to
1065
+ # exercise the raw connection.
1066
+ def verified!
1067
+ @verified = true
1068
+ end
1069
+
1061
1070
  def retryable_connection_error?(exception)
1062
1071
  exception.is_a?(ConnectionNotEstablished) || exception.is_a?(ConnectionFailed)
1063
1072
  end
@@ -98,6 +98,7 @@ module ActiveRecord
98
98
  with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
99
99
  sync_timezone_changes(conn)
100
100
  result = conn.query(sql)
101
+ verified!
101
102
  handle_warnings(sql)
102
103
  result
103
104
  end
@@ -126,6 +127,8 @@ module ActiveRecord
126
127
  result = ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
127
128
  stmt.execute(*type_casted_binds)
128
129
  end
130
+ verified!
131
+ result
129
132
  rescue ::Mysql2::Error => e
130
133
  if cache_stmt
131
134
  @statements.delete(sql)
@@ -6,16 +6,24 @@ module ActiveRecord
6
6
  class Column < ConnectionAdapters::Column # :nodoc:
7
7
  delegate :oid, :fmod, to: :sql_type_metadata
8
8
 
9
- def initialize(*, serial: nil, generated: nil, **)
9
+ def initialize(*, serial: nil, identity: nil, generated: nil, **)
10
10
  super
11
11
  @serial = serial
12
+ @identity = identity
12
13
  @generated = generated
13
14
  end
14
15
 
16
+ def identity?
17
+ @identity
18
+ end
19
+
15
20
  def serial?
16
21
  @serial
17
22
  end
18
- alias_method :auto_incremented_by_db?, :serial?
23
+
24
+ def auto_incremented_by_db?
25
+ serial? || identity?
26
+ end
19
27
 
20
28
  def virtual?
21
29
  # We assume every generated column is virtual, no matter the concrete type
@@ -41,12 +49,14 @@ module ActiveRecord
41
49
 
42
50
  def init_with(coder)
43
51
  @serial = coder["serial"]
52
+ @identity = coder["identity"]
44
53
  @generated = coder["generated"]
45
54
  super
46
55
  end
47
56
 
48
57
  def encode_with(coder)
49
58
  coder["serial"] = @serial
59
+ coder["identity"] = @identity
50
60
  coder["generated"] = @generated
51
61
  super
52
62
  end
@@ -54,6 +64,7 @@ module ActiveRecord
54
64
  def ==(other)
55
65
  other.is_a?(Column) &&
56
66
  super &&
67
+ identity? == other.identity? &&
57
68
  serial? == other.serial?
58
69
  end
59
70
  alias :eql? :==
@@ -61,6 +72,7 @@ module ActiveRecord
61
72
  def hash
62
73
  Column.hash ^
63
74
  super.hash ^
75
+ identity?.hash ^
64
76
  serial?.hash
65
77
  end
66
78
  end
@@ -16,7 +16,9 @@ module ActiveRecord
16
16
 
17
17
  log(sql, name) do
18
18
  with_raw_connection do |conn|
19
- conn.async_exec(sql).map_types!(@type_map_for_results).values
19
+ result = conn.async_exec(sql).map_types!(@type_map_for_results).values
20
+ verified!
21
+ result
20
22
  end
21
23
  end
22
24
  end
@@ -51,6 +53,7 @@ module ActiveRecord
51
53
  log(sql, name, async: async) do
52
54
  with_raw_connection(allow_retry: allow_retry, materialize_transactions: materialize_transactions) do |conn|
53
55
  result = conn.async_exec(sql)
56
+ verified!
54
57
  handle_warnings(result)
55
58
  result
56
59
  end
@@ -27,9 +27,10 @@ module ActiveRecord
27
27
  value = value.sub(/^\((.+)\)$/, '-\1') # (4)
28
28
  case value
29
29
  when /^-?\D*+[\d,]+\.\d{2}$/ # (1)
30
- value.gsub!(/[^-\d.]/, "")
30
+ value.delete!("^-0-9.")
31
31
  when /^-?\D*+[\d.]+,\d{2}$/ # (2)
32
- value.gsub!(/[^-\d,]/, "").sub!(/,/, ".")
32
+ value.delete!("^-0-9,")
33
+ value.tr!(",", ".")
33
34
  end
34
35
 
35
36
  super(value)
@@ -15,7 +15,7 @@ module ActiveRecord
15
15
  time = super
16
16
  return time if time.is_a?(ActiveSupport::TimeWithZone) || !time.acts_like?(:time)
17
17
 
18
- # While in UTC mode, the PG gem may not return times back in "UTC" even if they were provided to Postgres in UTC.
18
+ # While in UTC mode, the PG gem may not return times back in "UTC" even if they were provided to PostgreSQL in UTC.
19
19
  # We prefer times always in UTC, so here we convert back.
20
20
  if is_utc?
21
21
  time.getutc
@@ -341,7 +341,7 @@ module ActiveRecord
341
341
  JOIN pg_namespace nsp ON (t.relnamespace = nsp.oid)
342
342
  WHERE t.oid = #{quote(quote_table_name(table))}::regclass
343
343
  AND cons.contype = 'p'
344
- AND pg_get_expr(def.adbin, def.adrelid) ~* 'nextval|uuid_generate'
344
+ AND pg_get_expr(def.adbin, def.adrelid) ~* 'nextval|uuid_generate|gen_random_uuid'
345
345
  SQL
346
346
  end
347
347
 
@@ -385,11 +385,18 @@ module ActiveRecord
385
385
  execute "ALTER TABLE #{quote_table_name(table_name)} RENAME TO #{quote_table_name(new_name)}"
386
386
  pk, seq = pk_and_sequence_for(new_name)
387
387
  if pk
388
- idx = "#{table_name}_pkey"
389
- new_idx = "#{new_name}_pkey"
388
+ # PostgreSQL automatically creates an index for PRIMARY KEY with name consisting of
389
+ # truncated table name and "_pkey" suffix fitting into max_identifier_length number of characters.
390
+ max_pkey_prefix = max_identifier_length - "_pkey".size
391
+ idx = "#{table_name[0, max_pkey_prefix]}_pkey"
392
+ new_idx = "#{new_name[0, max_pkey_prefix]}_pkey"
390
393
  execute "ALTER INDEX #{quote_table_name(idx)} RENAME TO #{quote_table_name(new_idx)}"
391
- if seq && seq.identifier == "#{table_name}_#{pk}_seq"
392
- new_seq = "#{new_name}_#{pk}_seq"
394
+
395
+ # PostgreSQL automatically creates a sequence for PRIMARY KEY with name consisting of
396
+ # truncated table name and "#{primary_key}_seq" suffix fitting into max_identifier_length number of characters.
397
+ max_seq_prefix = max_identifier_length - "_#{pk}_seq".size
398
+ if seq && seq.identifier == "#{table_name[0, max_seq_prefix]}_#{pk}_seq"
399
+ new_seq = "#{new_name[0, max_seq_prefix]}_#{pk}_seq"
393
400
  execute "ALTER TABLE #{seq.quoted} RENAME TO #{quote_table_name(new_seq)}"
394
401
  end
395
402
  end
@@ -908,7 +915,7 @@ module ActiveRecord
908
915
  end
909
916
 
910
917
  def new_column_from_field(table_name, field, _definitions)
911
- column_name, type, default, notnull, oid, fmod, collation, comment, attgenerated = field
918
+ column_name, type, default, notnull, oid, fmod, collation, comment, identity, attgenerated = field
912
919
  type_metadata = fetch_type_metadata(column_name, type, oid.to_i, fmod.to_i)
913
920
  default_value = extract_value_from_default(default)
914
921
 
@@ -931,6 +938,7 @@ module ActiveRecord
931
938
  collation: collation,
932
939
  comment: comment.presence,
933
940
  serial: serial,
941
+ identity: identity.presence,
934
942
  generated: attgenerated
935
943
  )
936
944
  end