Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Virtual column schema dump - default_function - Bring in recent postgressql adaptor change #383

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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