Skip to content

Commit

Permalink
Merge pull request #44319 from fatkodima/fix-postgres-virtual-column
Browse files Browse the repository at this point in the history
Fix parsing expression for PostgreSQL generated column
  • Loading branch information
kamipo committed Feb 3, 2022
1 parent a938fe8 commit 350cfc4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
Expand Up @@ -663,7 +663,12 @@ def new_column_from_field(table_name, field)
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]
Expand Down
Expand Up @@ -19,6 +19,8 @@ def setup
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
Expand Down Expand Up @@ -78,6 +80,7 @@ def test_schema_dumping
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
Expand Down

0 comments on commit 350cfc4

Please sign in to comment.