Skip to content

Conversation

@LordSimal
Copy link
Contributor

@LordSimal LordSimal commented Mar 17, 2025

as can be seen by the added test there seems to be a problem currently present with setting the IDENTITY_INSERT flag on a table in MS SQL.

CakePHP: 5.1.5
Migrations: 4.6.1

@LordSimal LordSimal force-pushed the 4.x-identity-insert branch from 7d5ad23 to 644edd0 Compare March 17, 2025 18:47
@LordSimal LordSimal changed the title testing bug with IDENTITY_INSERT Mar 17, 2025
@markstory
Copy link
Member

What is the bug? It seems like this is how sqlserver is documented to behave.

@LordSimal
Copy link
Contributor Author

according to the user in slack, it works if the following change is being done in the PDOAdapter

            if($this->getConnection()->getDriver() instanceof \Cake\Database\Driver\Sqlserver){
               $sql = 'SET IDENTITY_INSERT '.$this->quoteTableName($table->getName()). ' ON; '. $sql;
               $sql = $sql.' SET IDENTITY_INSERT '.$this->quoteTableName($table->getName()). ' OFF; ';
            }
            $this->getConnection()->execute($sql, $vals);

i think its just weird, that it works if its executed as 1 SQL command but does not, if its spread up on multiple commands.

The user reporting the issue explained it as "not being the same session/context" but as far as i can tell migrations doesnt recreate the connection object, therefore using the same PDO instance to do the queries.

@markstory
Copy link
Member

It could be related to implicit transactions. I wonder if this would work if run within a transaction 🤔

@LordSimal
Copy link
Contributor Author

As can be seen by the last commit and CI run even manually starting a transaction doesn't work.

@markstory
Copy link
Member

markstory commented Mar 20, 2025

Well that's unfortunate. SQLserver is a strange beast. I'm not in love with the type check in the middle of an abstract class.

We could duplicate the insert method into SqlserverAdapter and add the required changes. That would avoid the leaky implementation and lets us accommodate any other peculiarities that may arise.

@jhandel
Copy link
Contributor

jhandel commented Mar 24, 2025

nd add the required changes. That would avoid the leaky implementation and lets us accommodate any other peculiarities that may arise.

As the one who submitted the bug I am ALSO not a fan.. I did that as a Proof of Concept that the issue was with the Identity Insert not being "in context" if its executed in a seporate call even if its the same connection.... If there was a best practice as a user to override the adapters myself I would..

Or maybe add an "setIdentityInsert(bool:$enabled)" function and then for SQL that adds the identity statements to the inserts, and for other adapters they just ignore the new property? But then you have to update PDOAdapter, and pass the property around so they all know to look for it.. it might add a lot of noise to the public API just because SQL Server is being a jerk..

If you guys are ok with it, I could write it up and send you guys a PR of what I am suggesting above.

* Proposed solution with some refactoring in AbstractAdapter so we don't have to copy too much code to SqlServerAdapter. (could be dryer if we wanted)
* Fix tests & Adapter comments
@markstory markstory marked this pull request as ready for review March 27, 2025 15:44
@jhandel
Copy link
Contributor

jhandel commented Mar 27, 2025

Thank you @markstory for merging the propsed fix into this PR..

@markstory
Copy link
Member

Thank you @markstory for merging the propsed fix into this PR..

You're welcome, thank you for the pull request 👍

@markstory markstory merged commit 21456eb into 4.x Mar 28, 2025
19 of 20 checks passed
@markstory markstory deleted the 4.x-identity-insert branch March 28, 2025 14:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants