From 0871ed87aa19b95a9b2d23e082088b965aaf893a Mon Sep 17 00:00:00 2001 From: Aidan Haran Date: Thu, 12 Oct 2023 10:42:15 +0100 Subject: [PATCH] Fix issue with default view value not being found because of case sensitivity (#1113) --- CHANGELOG.md | 6 ++++ .../sqlserver/schema_statements.rb | 4 +-- test/cases/view_test_sqlserver.rb | 36 +++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 test/cases/view_test_sqlserver.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 1325fcb07..4826f4f8a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/active_record/connection_adapters/sqlserver/schema_statements.rb b/lib/active_record/connection_adapters/sqlserver/schema_statements.rb index 87713cd5a..276a2f2ed 100644 --- a/lib/active_record/connection_adapters/sqlserver/schema_statements.rb +++ b/lib/active_record/connection_adapters/sqlserver/schema_statements.rb @@ -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", [] @@ -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 diff --git a/test/cases/view_test_sqlserver.rb b/test/cases/view_test_sqlserver.rb new file mode 100644 index 000000000..82985f37e --- /dev/null +++ b/test/cases/view_test_sqlserver.rb @@ -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