[Phase 2] IDENTITY_INSERT handling for inserts on identity tables
Part of the SQL Server Phase 2 epic #150.
The driver isn't on main. Code lives on feat/sql-server and isn't in any release. Check that branch out, branch off it, target your PR at it — not at main. The whole thing squashes into main when Phase 2 closes (#149).
SQL Server refuses any INSERT that provides a value for an IDENTITY column unless the session has SET IDENTITY_INSERT [schema].[table] ON. Phase 2 needs to detect this situation and wrap the insert in the right boilerplate.
Task
In src-tauri/src/drivers/sqlserver/mod.rs::insert_record (currently a Phase 1 read-only stub that returns an error), replace the stub with a real implementation:
1. Detect the identity column via a metadata round-trip:
SELECT c.name
FROM sys.columns c
WHERE c.object_id = OBJECT_ID(@P1) AND c.is_identity = 1
(Or reuse the existing Q_GET_COLUMNS + filter on is_identity.)
2. Two branches. If the caller-provided data: HashMap<String, serde_json::Value> contains a value for the identity column, wrap the INSERT in an explicit transaction:
BEGIN TRAN;
SET IDENTITY_INSERT [schema].[table] ON;
INSERT INTO [schema].[table] ([c1], [c2], ...) VALUES (@P1, @P2, ...);
SET IDENTITY_INSERT [schema].[table] OFF;
COMMIT;
Otherwise plain INSERT.
3. Error safety. If the transaction fails, an implicit rollback leaves IDENTITY_INSERT OFF — but add an integration test that asserts this, so regressions are caught.
Rules
- Integration test via
testcontainers + mcr.microsoft.com/mssql/server:2022-latest, gated #[ignore] so CI without Docker skips it
- Test matrix: insert without identity value / insert with identity value / failed insert (e.g. NULL violation on NOT NULL column) / verify
IDENTITY_INSERT state is OFF at the end of each
Reference
[Phase 2]
IDENTITY_INSERThandling for inserts on identity tablesPart of the SQL Server Phase 2 epic #150.
The driver isn't on
main. Code lives onfeat/sql-serverand isn't in any release. Check that branch out, branch off it, target your PR at it — not atmain. The whole thing squashes intomainwhen Phase 2 closes (#149).SQL Server refuses any
INSERTthat provides a value for anIDENTITYcolumn unless the session hasSET IDENTITY_INSERT [schema].[table] ON. Phase 2 needs to detect this situation and wrap the insert in the right boilerplate.Task
In
src-tauri/src/drivers/sqlserver/mod.rs::insert_record(currently a Phase 1 read-only stub that returns an error), replace the stub with a real implementation:1. Detect the identity column via a metadata round-trip:
(Or reuse the existing
Q_GET_COLUMNS+ filter onis_identity.)2. Two branches. If the caller-provided
data: HashMap<String, serde_json::Value>contains a value for the identity column, wrap theINSERTin an explicit transaction:Otherwise plain
INSERT.3. Error safety. If the transaction fails, an implicit rollback leaves
IDENTITY_INSERTOFF — but add an integration test that asserts this, so regressions are caught.Rules
testcontainers+mcr.microsoft.com/mssql/server:2022-latest, gated#[ignore]so CI without Docker skips itIDENTITY_INSERTstate is OFF at the end of eachReference
docs/sql-server-implementation-plan.md§ Phase 2 — 2.4 (INSERT / IDENTITY handling)SET IDENTITY_INSERTdocs: https://learn.microsoft.com/en-us/sql/t-sql/statements/set-identity-insert-transact-sql