IF DB_NAME()<>N'PlotDirector_Development' THROW 51000,'Development only',1; DECLARE @Project int,@Book int,@Operation uniqueidentifier=NEWID(); BEGIN TRY BEGIN TRANSACTION; DECLARE @User int=(SELECT TOP(1) UserID FROM dbo.AppUser ORDER BY UserID),@Review uniqueidentifier=NEWID(),@Doc uniqueidentifier=NEWID(); INSERT dbo.Projects(ProjectName) VALUES(N'Native rollback isolated fixture');SET @Project=SCOPE_IDENTITY(); INSERT dbo.ProjectUserAccess(ProjectID,UserID,AccessRole) VALUES(@Project,@User,N'Owner'); INSERT dbo.Books(ProjectID,BookTitle,BookNumber,SortOrder) VALUES(@Project,N'Rollback isolated fixture',1,1);SET @Book=SCOPE_IDENTITY(); INSERT dbo.NativeChapterReviews(ReviewID,UserID,BookID,ManuscriptID,ChapterStart,ChapterEnd,ContentHash,ResultJson,EncryptedSource) VALUES(@Review,@User,@Book,@Doc,0,20,'rollback-hash',N'{"DocumentRevision":1,"Scenes":[{"Start":0,"End":20}]}',N'fixture'); DECLARE @Items nvarchar(max)=(SELECT @Review ReviewID,1 Version,'rollback-chapter' [Key],'rollback-hash' Hash,'Rollback chapter' Title,0 Start,20 [End],JSON_QUERY(N'[{"Key":"rollback-chapter:0","Title":"Rollback scene","Text":null,"StartParagraph":1,"EndParagraph":1,"Words":0}]') Scenes FOR JSON PATH); -- Deliberately force a canonical source-save constraint failure AFTER the core build emitted its maps. EXEC dbo.NativeReviewImport @Action='Commit',@UserID=@User,@BookID=@Book,@OperationID=@Operation,@DocumentGuid=@Doc,@ItemsJson=@Items; THROW 51000,'Expected source-save failure did not occur',1; END TRY BEGIN CATCH DECLARE @Error int=ERROR_NUMBER(); IF @@TRANCOUNT>0 ROLLBACK; IF @Error<>515 THROW; IF EXISTS(SELECT 1 FROM dbo.Projects WHERE ProjectID=@Project) OR EXISTS(SELECT 1 FROM dbo.NativeImportOperations WHERE OperationID=@Operation) OR EXISTS(SELECT 1 FROM dbo.Chapters WHERE BookID=@Book) THROW 51000,'Partial import survived source failure',1; SELECT 'PASS source-stage failure rolled back core chapters, scenes, receipt and fixture; review/canonical author data untouched'; END CATCH;