Skip to content

Include cast type in column #1292

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

Merged
merged 2 commits into from
Feb 7, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def visit_CreateIndexDefinition(o)
end

def add_column_options!(sql, options)
sql << " DEFAULT #{quote_default_expression(options[:default], options[:column])}" if options_include_default?(options)
sql << " DEFAULT #{quote_default_expression_for_column_definition(options[:default], options[:column])}" if options_include_default?(options)
if options[:collation].present?
sql << " COLLATE #{options[:collation]}"
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ def columns(table_name)
column_definitions(table_name).map do |ci|
sqlserver_options = ci.slice :ordinal_position, :is_primary, :is_identity, :table_name
sql_type_metadata = fetch_type_metadata ci[:type], sqlserver_options

new_column(
ci[:name],
lookup_cast_type(ci[:type]),
ci[:default_value],
sql_type_metadata,
ci[:null],
Expand All @@ -83,9 +85,10 @@ def columns(table_name)
end
end

def new_column(name, default, sql_type_metadata, null, default_function = nil, collation = nil, comment = nil, sqlserver_options = {})
def new_column(name, cast_type, default, sql_type_metadata, null, default_function = nil, collation = nil, comment = nil, sqlserver_options = {})
SQLServer::Column.new(
name,
cast_type,
default,
sql_type_metadata,
null,
Expand Down
17 changes: 11 additions & 6 deletions test/cases/coerced_tests.rb
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,8 @@ def test_create_table_with_defaults_coerce
five = columns.detect { |c| c.name == "five" }

assert_equal "hello", one.default
assert_equal true, connection.lookup_cast_type_from_column(two).deserialize(two.default)
assert_equal false, connection.lookup_cast_type_from_column(three).deserialize(three.default)
assert_equal true, two.fetch_cast_type(connection).deserialize(two.default)
assert_equal false, three.fetch_cast_type(connection).deserialize(three.default)
assert_equal 1, four.default
assert_equal "hello", five.default
end
Expand Down Expand Up @@ -1971,12 +1971,17 @@ def with_marshable_time_defaults
# Revert changes
@connection.change_column_default(:sst_datatypes, :datetime, current_default) if current_default.present?
end

# We need to give the full path for this to work.
undef_method :schema_dump_path
def schema_dump_path
# We need to give the full paths for this to work.
undef_method :schema_dump_5_1_path
def schema_dump_5_1_path
File.join(ARTest::SQLServer.root_activerecord, "test/assets/schema_dump_5_1.yml")
end

undef_method :schema_dump_8_0_path
def schema_dump_8_0_path
File.join(ARTest::SQLServer.root_activerecord, "test/assets/schema_dump_8_0.yml")
end
end
end
end
Expand Down
Loading
Loading