-- Phase3D.1: private deterministic investigations; immutable catalogues and append-only state events. SET ANSI_NULLS ON; SET QUOTED_IDENTIFIER ON; GO IF OBJECT_ID('dbo.WriterInvestigationCatalogues') IS NULL CREATE TABLE dbo.WriterInvestigationCatalogues(Version varchar(80) NOT NULL PRIMARY KEY,Json nvarchar(max) NOT NULL CHECK(ISJSON(Json)=1),PublishedUtc datetime2 NOT NULL DEFAULT SYSUTCDATETIME(),Actor nvarchar(256) NOT NULL); IF OBJECT_ID('dbo.WriterInvestigations') IS NULL CREATE TABLE dbo.WriterInvestigations(ID uniqueidentifier NOT NULL PRIMARY KEY,UserID int NOT NULL REFERENCES dbo.AppUser(UserID) ON DELETE CASCADE,ProjectID int NOT NULL,BookID int NOT NULL REFERENCES dbo.Books(BookID) ON DELETE CASCADE,CatalogueVersion varchar(80) NOT NULL REFERENCES dbo.WriterInvestigationCatalogues(Version),Version int NOT NULL,Json nvarchar(max) NOT NULL CHECK(ISJSON(Json)=1),CreatedUtc datetime2 NOT NULL DEFAULT SYSUTCDATETIME(),UpdatedUtc datetime2 NOT NULL DEFAULT SYSUTCDATETIME()); IF OBJECT_ID('dbo.WriterInvestigationEvents') IS NULL CREATE TABLE dbo.WriterInvestigationEvents(InvestigationID uniqueidentifier NOT NULL REFERENCES dbo.WriterInvestigations(ID) ON DELETE CASCADE,Version int NOT NULL,Event nvarchar(max) NOT NULL CHECK(ISJSON(Event)=1),CreatedUtc datetime2 NOT NULL DEFAULT SYSUTCDATETIME(),PRIMARY KEY(InvestigationID,Version)); IF NOT EXISTS(SELECT 1 FROM sys.indexes WHERE object_id=OBJECT_ID('dbo.WriterInvestigations') AND name='IX_WriterInvestigations_AuthorBook') CREATE INDEX IX_WriterInvestigations_AuthorBook ON dbo.WriterInvestigations(UserID,BookID,UpdatedUtc DESC); GO CREATE OR ALTER PROCEDURE dbo.WriterInvestigationCatalogue @Action varchar(12),@Version varchar(80)=NULL,@Json nvarchar(max)=NULL,@Actor nvarchar(256)=NULL AS BEGIN SET NOCOUNT ON;SET XACT_ABORT ON; IF @Action='read' BEGIN SELECT TOP(1) Json FROM dbo.WriterInvestigationCatalogues WHERE @Version IS NULL OR Version=@Version ORDER BY PublishedUtc DESC,Version DESC;RETURN;END; IF @Action<>'publish' OR @Version IS NULL OR ISJSON(@Json)<>1 OR ISNULL(JSON_VALUE(@Json,'$.Version'),'')<>@Version OR ISNULL(JSON_VALUE(@Json,'$.Synthetic'),'')<>'true' THROW 51000,'Invalid catalogue',1; IF EXISTS(SELECT 1 FROM dbo.WriterInvestigationCatalogues WHERE Version=@Version) THROW 51002,'Catalogue version is immutable; publish a new version',1; INSERT dbo.WriterInvestigationCatalogues(Version,Json,Actor) VALUES(@Version,@Json,@Actor); END; GO CREATE OR ALTER PROCEDURE dbo.WriterInvestigation @Action varchar(12),@UserID int,@BookID int,@ProjectID int=NULL,@ID uniqueidentifier=NULL,@CatalogueVersion varchar(80)=NULL,@ExpectedVersion int=NULL,@Json nvarchar(max)=NULL,@Event nvarchar(max)=NULL AS BEGIN SET NOCOUNT ON;SET XACT_ABORT ON; DECLARE @ActualProject int; SELECT @ActualProject=b.ProjectID FROM dbo.Books b JOIN dbo.Projects p ON p.ProjectID=b.ProjectID AND p.IsArchived=0 JOIN dbo.ProjectUserAccess a ON a.ProjectID=b.ProjectID AND a.UserID=@UserID AND a.IsActive=1 WHERE b.BookID=@BookID AND b.IsArchived=0; IF @ActualProject IS NULL OR (@ProjectID IS NOT NULL AND @ProjectID<>@ActualProject) THROW 51000,'Investigation scope unavailable',1; IF @Action='list' BEGIN SELECT TOP(100) Json FROM dbo.WriterInvestigations WHERE UserID=@UserID AND BookID=@BookID ORDER BY UpdatedUtc DESC,ID;RETURN;END; IF @Action IN('read','events') BEGIN IF NOT EXISTS(SELECT 1 FROM dbo.WriterInvestigations WHERE ID=@ID AND UserID=@UserID AND BookID=@BookID) THROW 51000,'Investigation unavailable',1; IF @Action='read' SELECT Json FROM dbo.WriterInvestigations WHERE ID=@ID AND UserID=@UserID AND BookID=@BookID; ELSE SELECT e.Event FROM dbo.WriterInvestigationEvents e JOIN dbo.WriterInvestigations i ON i.ID=e.InvestigationID WHERE i.ID=@ID AND i.UserID=@UserID AND i.BookID=@BookID ORDER BY e.Version; RETURN; END; IF @Action<>'save' OR @ID IS NULL OR @ExpectedVersion<0 OR @ExpectedVersion>1000 OR ISJSON(@Json)<>1 OR ISJSON(@Event)<>1 OR DATALENGTH(@Json)>1000000 OR DATALENGTH(@Event)>2000000 THROW 51000,'Invalid investigation save',1; IF ISNULL(TRY_CONVERT(int,JSON_VALUE(@Json,'$.UserID')),-1)<>@UserID OR ISNULL(TRY_CONVERT(int,JSON_VALUE(@Json,'$.BookID')),-1)<>@BookID OR ISNULL(TRY_CONVERT(int,JSON_VALUE(@Json,'$.ProjectID')),-1)<>@ActualProject OR ISNULL(TRY_CONVERT(uniqueidentifier,JSON_VALUE(@Json,'$.ID')),'00000000-0000-0000-0000-000000000000')<>@ID OR ISNULL(JSON_VALUE(@Json,'$.CatalogueVersion'),'')<>@CatalogueVersion OR ISNULL(TRY_CONVERT(int,JSON_VALUE(@Json,'$.Version')),-1)<>@ExpectedVersion+1 THROW 51000,'Investigation identity mismatch',1; BEGIN TRANSACTION; IF @ExpectedVersion=0 BEGIN IF EXISTS(SELECT 1 FROM dbo.WriterInvestigations WITH(UPDLOCK,HOLDLOCK) WHERE ID=@ID) THROW 51002,'Investigation already exists; resume it',1; INSERT dbo.WriterInvestigations(ID,UserID,ProjectID,BookID,CatalogueVersion,Version,Json) VALUES(@ID,@UserID,@ActualProject,@BookID,@CatalogueVersion,1,@Json); END ELSE BEGIN UPDATE dbo.WriterInvestigations SET Json=@Json,Version=Version+1,UpdatedUtc=SYSUTCDATETIME() WHERE ID=@ID AND UserID=@UserID AND BookID=@BookID AND CatalogueVersion=@CatalogueVersion AND Version=@ExpectedVersion; IF @@ROWCOUNT<>1 THROW 51002,'Another window changed this investigation. Your submitted answer has not replaced it.',1; END; INSERT dbo.WriterInvestigationEvents(InvestigationID,Version,Event) VALUES(@ID,@ExpectedVersion+1,@Event); COMMIT; END; GO