Execute SSDT project/dacpac deployment script in single transaction, including pre/post scripts

。_饼干妹妹 提交于 2020-05-09 06:47:05

问题


I am using SQLPackage.exe utility together with Database project's publish profiles to run changes to the SQL Server database.

From my understanding, this is the deployment process:

Pre-Deployment Script


Main Deployment


Post-Deployment Script


I have "Include transactional scripts" option checked in the publish profile advanced settings, however, that seems to apply to Main Deployment section only.

My questions is: If Main deployment fails, is Pre-Deployment script committed? As well as, if Post-Deployment script fails, does Pre-Deployment changes and Main Deployment changes are being committed?

Is it possible to make publish script "Atomic" - Everything succeeds or fails as a single unit?

Thank you.


回答1:


You should simply open the transaction in the pre script and commit it in the post script. It is very similar code like SSDT generates when you set this setting on.

Pre script:

IF (SELECT OBJECT_ID('tempdb..#tmpErrors2')) IS NOT NULL DROP TABLE #tmpErrors2
GO
CREATE TABLE #tmpErrors2 (Error int)
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
GO
BEGIN TRANSACTION
GO



--Your prescript part goes here



GO
IF @@ERROR <> 0
   AND @@TRANCOUNT > 0
    BEGIN
        ROLLBACK;
    END

IF @@TRANCOUNT = 0
    BEGIN
        INSERT  INTO #tmpErrors2 (Error)
        VALUES                 (1);
        BEGIN TRANSACTION;
    END

Post script:

GO
IF @@ERROR <> 0
   AND @@TRANCOUNT > 0
    BEGIN
        ROLLBACK;
    END

IF @@TRANCOUNT = 0
    BEGIN
        INSERT  INTO #tmpErrors2 (Error)
        VALUES                 (1);
        BEGIN TRANSACTION;
    END


GO

IF EXISTS (SELECT * FROM #tmpErrors2) ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT>0 BEGIN
PRINT N'Post/Pre script finished'
COMMIT TRANSACTION
END
ELSE PRINT N'Pre/Post scripts failed'
GO
DROP TABLE #tmpErrors2
GO


来源:https://stackoverflow.com/questions/51952689/execute-ssdt-project-dacpac-deployment-script-in-single-transaction-including-p

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!