Skip to content

Commit

Permalink
Merge pull request #383 from northwoodspd/virtual_properties
Browse files Browse the repository at this point in the history
Virtual column schema dump - default_function - Bring in recent postgressql adaptor change
  • Loading branch information
keithdoggett committed Oct 5, 2023
2 parents f2bb31b + e01c18e commit 1b7588b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
Expand Up @@ -11,7 +11,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
15 changes: 15 additions & 0 deletions test/cases/ddl_test.rb
Expand Up @@ -307,6 +307,20 @@ def test_column_defaults
assert_equal(-1, klass.new.sample_integer)
end

# Ensure virtual column default function works like the Postgres adapter.
def test_virtual_column_default_function
skip "Virtual Columns are not supported in this version of PostGIS" unless SpatialModel.connection.supports_virtual_columns?
klass.connection.create_table(:spatial_models, force: true) do |t|
t.integer :column1
t.virtual :column2, type: :integer, as: "(column1 + 1)", stored: true
end
klass.reset_column_information
col = klass.columns.last
assert_equal(:integer, col.type)
assert_equal("(column1 + 1)", col.default_function)
assert(col.virtual?)
end

def test_column_types
klass.connection.create_table(:spatial_models, force: true) do |t|
t.column "sample_integer", :integer
Expand Down Expand Up @@ -364,6 +378,7 @@ def test_generated_geometry_column
end
klass.reset_column_information
col = klass.columns.last
assert_equal("st_buffer(coordinates, (10)::double precision)", col.default_function)
assert_equal(:geometry, col.type)
assert(col.virtual?)
end
Expand Down

0 comments on commit 1b7588b

Please sign in to comment.