Skip to content

Commit c2964ce

Browse files
authored
Enable identity insert on view's base table for fixtures (#1334)
1 parent 7904bf6 commit c2964ce

File tree

4 files changed

+27
-9
lines changed

4 files changed

+27
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## Unreleased
2+
3+
#### Fixed
4+
5+
- [#1334](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1334) Enable identity insert on view's base table for fixtures.
6+
17
## v8.0.6
28

39
#### Fixed

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ def write_query?(sql) # :nodoc:
1515

1616
def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notification_payload:, batch:)
1717
result = if id_insert_table_name = query_requires_identity_insert?(sql)
18-
# If the table name is a view, we need to get the base table name for enabling identity insert.
19-
id_insert_table_name = view_table_name(id_insert_table_name) if view_exists?(id_insert_table_name)
20-
2118
with_identity_insert_enabled(id_insert_table_name, raw_connection) do
2219
internal_exec_sql_query(sql, raw_connection)
2320
end
@@ -225,11 +222,14 @@ def execute_procedure(proc_name, *variables)
225222
end
226223

227224
def with_identity_insert_enabled(table_name, conn)
228-
table_name = quote_table_name(table_name)
229-
set_identity_insert(table_name, conn, true)
225+
# If the table name is a view, we need to get the base table name for enabling identity insert.
226+
table_name = view_table_name(table_name) if view_exists?(table_name)
227+
quoted_table_name = quote_table_name(table_name)
228+
229+
set_identity_insert(quoted_table_name, conn, true)
230230
yield
231231
ensure
232-
set_identity_insert(table_name, conn, false)
232+
set_identity_insert(quoted_table_name, conn, false)
233233
end
234234

235235
def use_database(database = nil)

test/cases/view_test_sqlserver.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,17 @@ class ViewTestSQLServer < ActiveRecord::TestCase
4848
end
4949
end
5050

51-
describe 'identity insert' do
52-
it "identity insert works with views" do
53-
assert_difference("SSTestCustomersView.count", 1) do
51+
describe "identity insert" do
52+
it "creates table record through a view" do
53+
assert_difference("SSTestCustomersView.count", 2) do
5454
SSTestCustomersView.create!(id: 5, name: "Bob")
55+
SSTestCustomersView.create!(id: 6, name: "Tim")
5556
end
5657
end
58+
59+
it "creates table records through a view using fixtures" do
60+
ActiveRecord::FixtureSet.create_fixtures(File.join(ARTest::SQLServer.test_root_sqlserver, "fixtures"), ["sst_customers_view"])
61+
assert_equal SSTestCustomersView.all.count, 2
62+
end
5763
end
5864
end

test/fixtures/sst_customers_view.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
david:
2+
name: "David"
3+
balance: 2,004
4+
aidan:
5+
name: "Aidan"
6+
balance: 10,191

0 commit comments

Comments
 (0)