Skip to content

Commit 193e7ef

Browse files
authored
Enable identity insert on view's base table (#1231)
1 parent 644b51e commit 193e7ef

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
name: CI
22

3-
on:
4-
push:
5-
branches: [ main ]
6-
pull_request:
7-
branches: [ main ]
8-
schedule:
9-
- cron: '0 4 * * 2'
3+
on: [push, pull_request]
104

115
jobs:
126
test:
137
name: Run test suite
14-
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.
159

1610
env:
1711
COMPOSE_FILE: docker-compose.ci.yml

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@
99
- [#1153](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1153) Only support Ruby v3.1+
1010
- [#1196](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1196) Use default inspect for database adapter
1111

12+
#### Fixed
13+
14+
- [#1231](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1231) Enable identity insert on view's base table
15+
1216
Please check [7-1-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/7-1-stable/CHANGELOG.md) for previous changes.

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: fa
4242
log(sql, name, binds, async: async) do |notification_payload|
4343
with_raw_connection do |conn|
4444
result = if id_insert_table_name = query_requires_identity_insert?(sql)
45+
# If the table name is a view, we need to get the base table name for enabling identity insert.
46+
id_insert_table_name = view_table_name(id_insert_table_name) if view_exists?(id_insert_table_name)
47+
4548
with_identity_insert_enabled(id_insert_table_name, conn) do
4649
internal_exec_sql_query(sql, conn)
4750
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)