Home > Software engineering >  Exec tSQLt.Faketable crashing our original table constraint and data
Exec tSQLt.Faketable crashing our original table constraint and data

Time:04-23

During debugging the tsqlt code, I have directly run the below statement without wrapping it as sp and my original table constraint got deleted and some data missing from the original table.

Exec tSQLt.FakeTable @TableName = N'DBO.Employee', @Identity=1; Exec tSQLt.FakeTable @TableName = N'DBO.Salary', @Identity=1;

How do I prevent running faketable statement in tsqlt is impacting the original table?

CodePudding user response:

Ugh, been there... You can't prevent it, short of contributing to the project and putting a pull request in to add the functionality.

FakeTable creates a backup of your original table so you should be able to get the original table back. These backup table names start with tSQLt.tempobject and end in an identifier. You can delete the new "fake" table (which now has the name of your original table) and rename the tempobject table if/when you find it.

Something I've done in the past is to query for a column that I know is in the table to find the name of the tSQLt table:

SELECT t.name
FROM sys.columns c
INNER JOIN sys.tables t ON t.object_id = c.object_id
WHERE c.name = 'SomeCol';

CodePudding user response:

There is no way to prevent executing tSQLt.FakeTable outside of the framework. There are also good reasons to not prevent that, so I do not think that adding that functionality is the right approach.

However, if you’re using the newest version of tSQLt, you can use tSQLt.UndoTestDoubles to get the original object(s) back.

  • Related