Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable identity insert on view's base table #1231

Merged
merged 4 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
name: CI

on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
schedule:
- cron: '0 4 * * 2'
on: [push, pull_request]

jobs:
test:
name: Run test suite
runs-on: ubuntu-latest
runs-on: ubuntu-20.04 # TODO: Change back to 'ubuntu-latest' when https://github.com/microsoft/mssql-docker/issues/899 resolved.

env:
COMPOSE_FILE: docker-compose.ci.yml
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@
- [#1153](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1153) Only support Ruby v3.1+
- [#1196](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1196) Use default inspect for database adapter

#### Fixed

- [#1231](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/1231) Enable identity insert on view's base table

Please check [7-1-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/7-1-stable/CHANGELOG.md) for previous changes.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ def internal_exec_query(sql, name = "SQL", binds = [], prepare: false, async: fa
log(sql, name, binds, async: async) do |notification_payload|
with_raw_connection do |conn|
result = if id_insert_table_name = query_requires_identity_insert?(sql)
# If the table name is a view, we need to get the base table name for enabling identity insert.
id_insert_table_name = view_table_name(id_insert_table_name) if view_exists?(id_insert_table_name)

with_identity_insert_enabled(id_insert_table_name, conn) do
internal_exec_sql_query(sql, conn)
end
Expand Down
8 changes: 8 additions & 0 deletions test/cases/view_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,12 @@ class ViewTestSQLServer < ActiveRecord::TestCase
assert_equal 1, klass.count
end
end

describe 'identity insert' do
it "identity insert works with views" do
assert_difference("SSTestCustomersView.count", 1) do
SSTestCustomersView.create!(id: 5, name: "Bob")
end
end
end
end
Loading