Skip to content

Commit c25db90

Browse files
authored
Enable identity insert on view's base table (#1232)
1 parent a2f2290 commit c25db90

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on: [push, pull_request]
55
jobs:
66
test:
77
name: Run test suite
8-
runs-on: ubuntu-latest
8+
runs-on: ubuntu-20.04 # TODO: Change back to 'ubuntu-latest' when https://github.com/microsoft/mssql-docker/issues/899 resolved.
99

1010
env:
1111
COMPOSE_FILE: docker-compose.ci.yml

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+
- [#1232](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1232) Enable identity insert on view's base table
6+
17
## v7.1.7
28

39
#### Fixed

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: fa
4545
log(sql, name, binds, async: async) do
4646
with_raw_connection do |conn|
4747
if id_insert_table_name = query_requires_identity_insert?(sql)
48+
# If the table name is a view, we need to get the base table name for enabling identity insert.
49+
id_insert_table_name = view_table_name(id_insert_table_name) if view_exists?(id_insert_table_name)
50+
4851
with_identity_insert_enabled(id_insert_table_name, conn) do
4952
result = internal_exec_sql_query(sql, conn)
5053
end

test/cases/view_test_sqlserver.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,12 @@ class ViewTestSQLServer < ActiveRecord::TestCase
4747
assert_equal 1, klass.count
4848
end
4949
end
50+
51+
describe 'identity insert' do
52+
it "identity insert works with views" do
53+
assert_difference("SSTestCustomersView.count", 1) do
54+
SSTestCustomersView.create!(id: 5, name: "Bob")
55+
end
56+
end
57+
end
5058
end

0 commit comments

Comments
 (0)