Skip to content

Commit

Permalink
Fix issue with default view value not being found because of case sen…
Browse files Browse the repository at this point in the history
…sitivity (#1113)
  • Loading branch information
aidanharan authored Oct 12, 2023
1 parent 5b720b6 commit 0871ed8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## Unreleased

#### Fixed

- [#1113](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1113) Fix issue with default view value not being found because of case sensitivity

## v7.0.4.0

#### Changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ def column_definitions(table_name)

if view_exists
results = sp_executesql %{
SELECT c.COLUMN_NAME AS [name], c.COLUMN_DEFAULT AS [default]
SELECT LOWER(c.COLUMN_NAME) AS [name], c.COLUMN_DEFAULT AS [default]
FROM #{database}.INFORMATION_SCHEMA.COLUMNS c
WHERE c.TABLE_NAME = #{quote(view_tblnm)}
}.squish, "SCHEMA", []
Expand Down Expand Up @@ -426,7 +426,7 @@ def column_definitions(table_name)
ci[:default_function] = begin
default = ci[:default_value]
if default.nil? && view_exists
view_column = views_real_column_name(table_name, ci[:name])
view_column = views_real_column_name(table_name, ci[:name]).downcase
default = default_functions[view_column] if view_column.present?
end
case default
Expand Down
36 changes: 36 additions & 0 deletions test/cases/view_test_sqlserver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require "cases/helper_sqlserver"

class ViewTestSQLServer < ActiveRecord::TestCase
let(:connection) { ActiveRecord::Base.connection }

describe 'view with default values' do
before do
connection.drop_table :view_casing_table rescue nil
connection.create_table :view_casing_table, force: true do |t|
t.boolean :Default_Falsey, null: false, default: false
t.boolean :Default_Truthy, null: false, default: true
end

connection.execute("DROP VIEW IF EXISTS view_casing_table_view;")
connection.execute("CREATE VIEW view_casing_table_view AS SELECT id AS id, default_falsey AS falsey, default_truthy AS truthy FROM view_casing_table")
end

it "default values are correct when column casing used in tables and views are different" do
klass = Class.new(ActiveRecord::Base) do
self.table_name = "view_casing_table_view"
end

obj = klass.new
assert_equal false, obj.falsey
assert_equal true, obj.truthy
assert_equal 0, klass.count

obj.save!
assert_equal false, obj.falsey
assert_equal true, obj.truthy
assert_equal 1, klass.count
end
end
end

0 comments on commit 0871ed8

Please sign in to comment.