Skip to content

Commit 3552bf6

Browse files
committed
Merge branch 'bugfix/association-versioning' into develop
There were a number of bugs in how association versioning—i.e., versioning of `TopicReferences` and `Relationships`—was addressed as part of the migration and utility scripts, which this update resolves. This includes: - Setting the `Version` as part of the `TopicReferences` migration (#70, d041eae) - Setting the `Version` as part of the `Relationships` migration (#74, 2ec30da) - Consolidating the `TopicReferences` as part of `ConsolidateVersions` (#75, 7e322c0) - Consolidating the `Relationships` as part of `ConsolidateVersions` (#75, b8c5d09)
2 parents ebfcaad + b8c5d09 commit 3552bf6

2 files changed

Lines changed: 91 additions & 7 deletions

File tree

OnTopic.Data.Sql.Database/Scripts/Upgrade from OnTopic 4 to OnTopic 5.sql

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,17 @@ CREATE
119119
TABLE [dbo].[TopicReferences] (
120120
[Source_TopicID] INT NOT NULL,
121121
[ReferenceKey] VARCHAR(128) NOT NULL,
122-
[Target_TopicID] INT NOT NULL
122+
[Target_TopicID] INT NOT NULL,
123+
[Version] DATETIME2(7) NULL
123124
);
124125

125126
INSERT
126127
INTO TopicReferences
127-
SELECT AttributeIndex.TopicID,
128+
SELECT Attributes.TopicID,
128129
SUBSTRING(AttributeKey, 0, LEN(AttributeKey)-1),
129-
AttributeValue
130-
FROM AttributeIndex
130+
AttributeValue,
131+
Version
132+
FROM Attributes
131133
JOIN Topics
132134
ON Topics.TopicID = CONVERT(INT, AttributeValue)
133135
WHERE AttributeKey LIKE '%ID'
@@ -136,6 +138,34 @@ WHERE AttributeKey LIKE '%ID'
136138

137139
PRINT('Migrated core attributes');
138140

141+
--------------------------------------------------------------------------------------------------------------------------------
142+
-- MIGRATE RELATIONSHIP VERSIONS
143+
--------------------------------------------------------------------------------------------------------------------------------
144+
-- In OnTopic 5, relationships are now versioned. This means that when a topic is rolled back, it will now remove relationships
145+
-- that were introduced after that version. Since relationships weren't previously versioned, that means that their version
146+
-- needs to be set to earliest version of the target topic, to prevent rollbacks from deleting relationships of an indeterminate
147+
-- date.
148+
--------------------------------------------------------------------------------------------------------------------------------
149+
150+
PRINT('Migrating relationship versions...');
151+
152+
ALTER TABLE [dbo].[Relationships]
153+
ADD [Version] DATETIME2(7) NULL
154+
155+
UPDATE Relationships
156+
SET Version = FirstVersion
157+
FROM Relationships
158+
CROSS APPLY (
159+
SELECT TOP 1
160+
Version AS FirstVersion
161+
FROM ExtendedAttributes
162+
WHERE TopicID = Relationships.Source_TopicID
163+
ORDER BY Version ASC
164+
) AS Attributes
165+
WHERE FirstVersion IS NOT NULL
166+
167+
PRINT('Migrated relationship versions...');
168+
139169
--------------------------------------------------------------------------------------------------------------------------------
140170
-- MIGRATE DERIVED TOPICS
141171
--------------------------------------------------------------------------------------------------------------------------------

OnTopic.Data.Sql.Database/Utilities/Stored Procedures/ConsolidateVersions.sql

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ IF (@StartDate IS NULL)
4141
END
4242

4343
--------------------------------------------------------------------------------------------------------------------------------
44-
-- SELECT TOPIC ATTRIBUTES
44+
-- CREATE CONSOLIDATED TOPIC ATTRIBUTE RECORD
4545
--------------------------------------------------------------------------------------------------------------------------------
4646
;WITH TopicAttributes
4747
AS (
@@ -68,7 +68,7 @@ WHERE Version > @StartDate
6868
AND Version < @EndDate
6969

7070
--------------------------------------------------------------------------------------------------------------------------------
71-
-- SELECT EXTENDED ATTRIBUTES
71+
-- CREATE CONSOLIDATED EXTENDED ATTRIBUTES RECORD
7272
--------------------------------------------------------------------------------------------------------------------------------
7373
;WITH TopicExtendedAttributes
7474
AS (
@@ -86,13 +86,67 @@ SET Version = @EndDate
8686
WHERE RowNumber = 1
8787

8888
--------------------------------------------------------------------------------------------------------------------------------
89-
-- DELETE CONSOLIDATED ATTRIBUTES
89+
-- DELETE CONSOLIDATED EXTENDED ATTRIBUTES
9090
--------------------------------------------------------------------------------------------------------------------------------
9191
DELETE
9292
FROM ExtendedAttributes
9393
WHERE Version > @StartDate
9494
AND Version < @EndDate
9595

96+
--------------------------------------------------------------------------------------------------------------------------------
97+
-- CREATE CONSOLIDATED RELATIONSHIPS RECORD
98+
--------------------------------------------------------------------------------------------------------------------------------
99+
;WITH TopicRelationships
100+
AS (
101+
SELECT Version,
102+
RowNumber = ROW_NUMBER() OVER (
103+
PARTITION BY Source_TopicID,
104+
RelationshipKey,
105+
Target_TopicID
106+
ORDER BY Version DESC
107+
)
108+
FROM Relationships
109+
WHERE Version > @StartDate
110+
AND Version <= @EndDate
111+
)
112+
UPDATE TopicRelationships
113+
SET Version = @EndDate
114+
WHERE RowNumber = 1
115+
116+
--------------------------------------------------------------------------------------------------------------------------------
117+
-- DELETE CONSOLIDATED RELATIONSHIPS
118+
--------------------------------------------------------------------------------------------------------------------------------
119+
DELETE
120+
FROM Relationships
121+
WHERE Version > @StartDate
122+
AND Version < @EndDate
123+
124+
--------------------------------------------------------------------------------------------------------------------------------
125+
-- CREATE CONSOLIDATED TOPIC REFERENCES RECORD
126+
--------------------------------------------------------------------------------------------------------------------------------
127+
;WITH TopicReferenceVersions
128+
AS (
129+
SELECT Version,
130+
RowNumber = ROW_NUMBER() OVER (
131+
PARTITION BY Source_TopicID
132+
ORDER BY Version DESC
133+
)
134+
FROM TopicReferences
135+
WHERE Version > @StartDate
136+
AND Version <= @EndDate
137+
)
138+
UPDATE TopicReferenceVersions
139+
SET Version = @EndDate
140+
WHERE RowNumber = 1
141+
142+
--------------------------------------------------------------------------------------------------------------------------------
143+
-- DELETE CONSOLIDATED TOPIC REFERENCES
144+
--------------------------------------------------------------------------------------------------------------------------------
145+
DELETE
146+
FROM TopicReferences
147+
WHERE Version > @StartDate
148+
AND Version < @EndDate
149+
96150
--------------------------------------------------------------------------------------------------------------------------------
97151
-- COMMIT TRANSACTION
98152
--------------------------------------------------------------------------------------------------------------------------------

0 commit comments

Comments
 (0)