Merge pull request #44319 from fatkodima/fix-postgres-virtual-column

Fix parsing expression for PostgreSQL generated column
This commit is contained in:
Ryuta Kamizono 2022-02-03 16:36:37 +09:00 committed by GitHub
commit 11b125ae95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 1 deletions

View File

@ -663,7 +663,12 @@ module ActiveRecord
column_name, type, default, notnull, oid, fmod, collation, comment, attgenerated = field
type_metadata = fetch_type_metadata(column_name, type, oid.to_i, fmod.to_i)
default_value = extract_value_from_default(default)
default_function = extract_default_function(default_value, default)
if attgenerated.present?
default_function = default
else
default_function = extract_default_function(default_value, default)
end
if match = default_function&.match(/\Anextval\('"?(?<sequence_name>.+_(?<suffix>seq\d*))"?'::regclass\)\z/)
serial = sequence_name_from_parts(table_name, column_name, match[:suffix]) == match[:sequence_name]

View File

@ -19,6 +19,8 @@ if ActiveRecord::Base.connection.supports_virtual_columns?
t.virtual :upper_name, type: :string, as: "UPPER(name)", stored: true
t.virtual :name_length, type: :integer, as: "LENGTH(name)", stored: true
t.virtual :name_octet_length, type: :integer, as: "OCTET_LENGTH(name)", stored: true
t.integer :column1
t.virtual :column2, type: :integer, as: "column1 + 1", stored: true
end
VirtualColumn.create(name: "Rails")
end
@ -78,6 +80,7 @@ if ActiveRecord::Base.connection.supports_virtual_columns?
assert_match(/t\.virtual\s+"upper_name",\s+type: :string,\s+as: "upper\(\(name\)::text\)", stored: true$/i, output)
assert_match(/t\.virtual\s+"name_length",\s+type: :integer,\s+as: "length\(\(name\)::text\)", stored: true$/i, output)
assert_match(/t\.virtual\s+"name_octet_length",\s+type: :integer,\s+as: "octet_length\(\(name\)::text\)", stored: true$/i, output)
assert_match(/t\.virtual\s+"column2",\s+type: :integer,\s+as: "\(column1 \+ 1\)", stored: true$/i, output)
end
def test_build_fixture_sql