diff --git a/Docs/Context/Threads-154-Contract.md b/Docs/Context/Threads-154-Contract.md index eb1a01fd..3b946394 100644 --- a/Docs/Context/Threads-154-Contract.md +++ b/Docs/Context/Threads-154-Contract.md @@ -110,3 +110,17 @@ Owner decisions for the implementation stage are the prerequisite and mixed-scope ordering rules, legacy link editing at cutover, and approval of the backfill mapping after the inventory. This audit proposes those rules without applying a migration or selecting placements for existing records. + +## Development inventory query + +`Tools/ThreadPlanningInventory/development.sql` is an operator-run query for +the read-only inventory in step 2. An authorized operator can run it in a fresh +read-only session against `PlotDirector_Development` during a quiet period, +or use snapshot isolation if it is already available. It only writes to +session-local temporary tables. Keep the report private. It includes internal +record IDs and event provenance. + +The result sets show aggregate counts, counts by thread, raw scene associations +and event provenance, additional event target links, and counts of existing +notes, checklists and files by their chapter or scene owner. Aggregate categories +can overlap. Review the raw associations before proposing a mapping. diff --git a/Tools/ThreadPlanningInventory/development.sql b/Tools/ThreadPlanningInventory/development.sql new file mode 100644 index 00000000..7e5fabc8 --- /dev/null +++ b/Tools/ThreadPlanningInventory/development.sql @@ -0,0 +1,112 @@ +-- Run through an authorized read-only Development connection. Store results privately. +SET NOCOUNT ON; + +IF DB_NAME() <> N'PlotDirector_Development' + THROW 51000, 'This inventory requires PlotDirector_Development.', 1; + +SELECT t.PlotThreadID, t.PlotLineID, l.ProjectID, l.BookID AS ScopeBookID, + t.IsArchived AS ThreadArchived, l.IsArchived AS PlotLineArchived, + p.IsArchived AS ProjectArchived, t.IntroducedSceneID, + t.PlannedResolutionSceneID, t.ActualResolutionSceneID +INTO #Threads +FROM dbo.PlotThreads AS t +JOIN dbo.PlotLines AS l ON l.PlotLineID = t.PlotLineID +JOIN dbo.Projects AS p ON p.ProjectID = l.ProjectID; + +WITH RawAssociations AS +( + SELECT PlotThreadID, SceneID, 'Direct' AS Origin, CAST(NULL AS int) AS ThreadEventID + FROM dbo.SceneThreads + UNION ALL + SELECT PlotThreadID, SceneID, 'Event', ThreadEventID + FROM dbo.ThreadEvents + UNION ALL + SELECT t.PlotThreadID, m.SceneID, m.Origin, NULL + FROM #Threads AS t + CROSS APPLY (VALUES + (t.IntroducedSceneID, 'Introduced'), + (t.PlannedResolutionSceneID, 'Planned resolution'), + (t.ActualResolutionSceneID, 'Actual resolution') + ) AS m(SceneID, Origin) + WHERE m.SceneID IS NOT NULL +) +SELECT a.PlotThreadID, a.Origin, a.ThreadEventID, a.SceneID, + s.ChapterID, c.BookID, b.ProjectID AS TargetProjectID, + s.IsArchived AS SceneArchived, c.IsArchived AS ChapterArchived, + b.IsArchived AS BookArchived, p.IsArchived AS TargetProjectArchived, + CONVERT(bit, CASE WHEN b.BookID IS NULL OR p.ProjectID IS NULL + OR b.ProjectID <> t.ProjectID + OR (t.ScopeBookID IS NOT NULL AND b.BookID <> t.ScopeBookID) + THEN 1 ELSE 0 END) AS InvalidScope +INTO #Associations +FROM RawAssociations AS a +JOIN #Threads AS t ON t.PlotThreadID = a.PlotThreadID +LEFT JOIN dbo.Scenes AS s ON s.SceneID = a.SceneID +LEFT JOIN dbo.Chapters AS c ON c.ChapterID = s.ChapterID +LEFT JOIN dbo.Books AS b ON b.BookID = c.BookID +LEFT JOIN dbo.Projects AS p ON p.ProjectID = b.ProjectID; + +SELECT t.PlotThreadID, t.PlotLineID, t.ProjectID, t.ScopeBookID, + t.ThreadArchived, t.PlotLineArchived, t.ProjectArchived, + COUNT_BIG(a.SceneID) AS RawCount, + COUNT(DISTINCT a.SceneID) AS DistinctSceneCount, + COUNT(DISTINCT a.BookID) AS DistinctBookCount, + COUNT_BIG(CASE WHEN a.Origin = 'Direct' THEN 1 END) AS DirectCount, + COUNT_BIG(CASE WHEN a.Origin = 'Event' THEN 1 END) AS EventCount, + COUNT_BIG(CASE WHEN a.Origin IN ('Introduced', 'Planned resolution', 'Actual resolution') + THEN 1 END) AS MilestoneCount, + COUNT_BIG(CASE WHEN a.InvalidScope = 1 THEN 1 END) AS InvalidScopeCount, + COUNT_BIG(CASE WHEN a.SceneArchived = 1 OR a.ChapterArchived = 1 + OR a.BookArchived = 1 OR a.TargetProjectArchived = 1 + THEN 1 END) AS ArchivedTargetCount +INTO #ThreadCounts +FROM #Threads AS t +LEFT JOIN #Associations AS a ON a.PlotThreadID = t.PlotThreadID +GROUP BY t.PlotThreadID, t.PlotLineID, t.ProjectID, t.ScopeBookID, + t.ThreadArchived, t.PlotLineArchived, t.ProjectArchived; + +SELECT COUNT_BIG(*) AS TotalThreads, + COUNT_BIG(CASE WHEN RawCount = 0 THEN 1 END) AS NoAssociations, + COUNT_BIG(CASE WHEN DirectCount = 1 THEN 1 END) AS SingleDirect, + COUNT_BIG(CASE WHEN DirectCount > 1 THEN 1 END) AS RecurringDirect, + COUNT_BIG(CASE WHEN DistinctSceneCount > 1 THEN 1 END) AS MultipleScenes, + COUNT_BIG(CASE WHEN DistinctBookCount > 1 THEN 1 END) AS CrossBook, + COUNT_BIG(CASE WHEN InvalidScopeCount > 0 THEN 1 END) AS InvalidScope, + COUNT_BIG(CASE WHEN ArchivedTargetCount > 0 THEN 1 END) AS ArchivedTarget, + COUNT_BIG(CASE WHEN DirectCount = 0 AND EventCount > 0 AND MilestoneCount = 0 + THEN 1 END) AS EventOnly, + COUNT_BIG(CASE WHEN DirectCount = 0 AND EventCount = 0 AND MilestoneCount > 0 + THEN 1 END) AS MilestoneOnly, + COUNT_BIG(CASE WHEN (DirectCount > 0 AND (EventCount > 0 OR MilestoneCount > 0)) + OR (EventCount > 0 AND MilestoneCount > 0) + THEN 1 END) AS MixedOrigin +FROM #ThreadCounts; + +SELECT * FROM #ThreadCounts ORDER BY ProjectID, PlotLineID, PlotThreadID; + +SELECT a.*, e.EventTypeID, e.PlotEventTypeID, e.Source, e.SourceReferenceID, + e.TargetPlotLineID +FROM #Associations AS a +LEFT JOIN dbo.ThreadEvents AS e ON e.ThreadEventID = a.ThreadEventID +ORDER BY a.PlotThreadID, a.SceneID, a.Origin, a.ThreadEventID; + +SELECT x.PlotEventID AS ThreadEventID, x.PlotLineID AS TargetPlotLineID +FROM dbo.PlotEventTargetPlotLines AS x +JOIN #Associations AS a ON a.ThreadEventID = x.PlotEventID +ORDER BY x.PlotEventID, x.PlotLineID; + +WITH OwnedContent AS +( + SELECT 'Note' AS ContentType, SceneID, CAST(NULL AS int) AS ChapterID FROM dbo.SceneNotes + UNION ALL + SELECT 'Checklist', SceneID, ChapterID FROM dbo.SceneChecklistItems + UNION ALL + SELECT 'Attachment', SceneID, NULL FROM dbo.SceneAttachments +) +SELECT b.ProjectID, c.BookID, c.ChapterID, x.SceneID, x.ContentType, COUNT_BIG(*) AS ItemCount +FROM OwnedContent AS x +LEFT JOIN dbo.Scenes AS s ON s.SceneID = x.SceneID +LEFT JOIN dbo.Chapters AS c ON c.ChapterID = COALESCE(x.ChapterID, s.ChapterID) +LEFT JOIN dbo.Books AS b ON b.BookID = c.BookID +GROUP BY b.ProjectID, c.BookID, c.ChapterID, x.SceneID, x.ContentType +ORDER BY b.ProjectID, c.BookID, c.ChapterID, x.SceneID, x.ContentType;