Skip to content

[Phase 2] IDENTITY_INSERT handling for inserts on identity tables #147

@debba

Description

@debba

[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

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is neededsql-serverSQL Server driver

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions