PlotDirector/PlotLine/Sql/337_LocationMutationVersionIntegrity.sql

62 lines
3.7 KiB
Transact-SQL

SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
-- All location writers must advance the existing optimistic-concurrency timestamp,
-- including children moved by a merge. No new version field or existing data rewrite.
SET XACT_ABORT ON;
BEGIN TRY
BEGIN TRANSACTION;
DECLARE @merge nvarchar(max)=OBJECT_DEFINITION(OBJECT_ID(N'dbo.Location_Merge'));
IF @merge NOT LIKE N'%LocationsMergeVersions337%'
BEGIN
IF CHARINDEX(N'UPDATE dbo.Locations SET ParentLocationID = @TargetID WHERE ParentLocationID = @SourceID;',@merge)=0
THROW 51012,'Review the location merge child-update contract.',1;
SET @merge=REPLACE(@merge,N'UPDATE dbo.Locations SET ParentLocationID = @TargetID WHERE ParentLocationID = @SourceID;',
N'UPDATE dbo.Locations SET ParentLocationID = @TargetID,UpdatedDate=@Now,ModifiedDateUTC=@Now,ModifiedByUserID=@UserID WHERE ParentLocationID = @SourceID;');
SET @merge=REPLACE(@merge,N'@SourceDescription=@TargetDescription',
N'(@SourceDescription COLLATE Latin1_General_100_BIN2=@TargetDescription COLLATE Latin1_General_100_BIN2 AND DATALENGTH(@SourceDescription)=DATALENGTH(@TargetDescription))');
SET @merge=REPLACE(@merge,N'UpdatedDate=@Now',N'UpdatedDate=CASE WHEN @Now<=UpdatedDate THEN DATEADD(NANOSECOND,100,UpdatedDate) ELSE @Now END');
SET @merge=REPLACE(@merge,N'UpdatedDate = @Now',N'UpdatedDate = CASE WHEN @Now<=UpdatedDate THEN DATEADD(NANOSECOND,100,UpdatedDate) ELSE @Now END');
SET @merge=STUFF(@merge,1,CHARINDEX(N'PROCEDURE',@merge)-1,N'CREATE OR ALTER ')+NCHAR(10)+N'-- LocationsMergeVersions337';
EXEC sys.sp_executesql @merge;
END;
DECLARE @name sysname,@reference nvarchar(100),@definition nvarchar(max),@replacement nvarchar(500);
DECLARE writers CURSOR LOCAL FAST_FORWARD FOR SELECT Name,Reference FROM(VALUES
(N'Location_WorkspaceAssign',N'l.UpdatedDate'),(N'Location_Save',N'UpdatedDate'),
(N'Location_Reparent',N'UpdatedDate'),(N'Location_SetPreferredName',N'UpdatedDate'))v(Name,Reference);
OPEN writers;FETCH NEXT FROM writers INTO @name,@reference;
WHILE @@FETCH_STATUS=0
BEGIN
SET @definition=OBJECT_DEFINITION(OBJECT_ID(N'dbo.'+@name));
IF @definition IS NULL THROW 51012,'A required location writer is unavailable.',1;
IF @definition NOT LIKE N'%LocationsWriterVersion337%'
BEGIN
IF @definition NOT LIKE N'%UpdatedDate%SYSUTCDATETIME()%' AND @definition NOT LIKE N'%UpdatedDate%@Now%'
THROW 51012,'Review the location writer version contract.',1;
SET @replacement=N'UpdatedDate=CASE WHEN SYSUTCDATETIME()<='+@reference+N' THEN DATEADD(NANOSECOND,100,'+@reference+N') ELSE SYSUTCDATETIME() END';
SET @definition=REPLACE(@definition,N'UpdatedDate=SYSUTCDATETIME()',@replacement);
SET @definition=REPLACE(@definition,N'UpdatedDate = SYSUTCDATETIME()',@replacement);
SET @replacement=N'UpdatedDate=CASE WHEN @Now<='+@reference+N' THEN DATEADD(NANOSECOND,100,'+@reference+N') ELSE @Now END';
SET @definition=REPLACE(@definition,N'UpdatedDate=@Now',@replacement);
SET @definition=REPLACE(@definition,N'UpdatedDate = @Now',@replacement);
IF @name=N'Location_WorkspaceAssign'
BEGIN
IF CHARINDEX(N'END) COLLATE Latin1_General_100_BIN2;',@definition)=0 THROW 51012,'Review the location assignment text comparison.',1;
SET @definition=REPLACE(@definition,N'END) COLLATE Latin1_General_100_BIN2;',
N'END) COLLATE Latin1_General_100_BIN2 OR (@Attribute=N''description'' AND DATALENGTH(i.Value)<>DATALENGTH(ISNULL(l.Description,N'''')));');
END;
SET @definition=STUFF(@definition,1,CHARINDEX(N'PROCEDURE',@definition)-1,N'CREATE OR ALTER ')+NCHAR(10)+N'-- LocationsWriterVersion337';
EXEC sys.sp_executesql @definition;
END;
FETCH NEXT FROM writers INTO @name,@reference;
END;
CLOSE writers;DEALLOCATE writers;
COMMIT;
END TRY
BEGIN CATCH
IF XACT_STATE()<>0 ROLLBACK;
THROW;
END CATCH;
GO