Skip to content

Commit cbba817

Browse files
committed
Merge branch 'feature/UpdateRelationships-synchronization' into develop
Updated `UpdateRelationships` to fully synchronize relationships Previously, in order to update existing relationships, callers had to trigger `UpdateTopic`'s `@DeleteRelationships` flag—which would delete all relationships for that topic—and _then_ call `UpdateRelationships` with the final relationships set. This was wasteful, as often times the relationships will have overlap, or even be identical. This issue is fixed with with the introduction of the `@DeleteUnmatched` parameter to the `UpdateRelationships` stored procedure (46ba436). By default, this parameter is set to `1` (enabled), though it can be optionally disabled if preferred, in which case `UpdateRelationships` will _add_ any missing relationships, but won't _delete_ any unmatched relationships. That might be useful for just appending new relationships, without worrying about passing current relationships. (I'd prefer the default to be `0` here, but that would break the public interface, so that change will need to wait until the next major version.) Given that, we are also able to remove the `@DeleteRelationships` functionality from `UpdateTopic`. That said, since `@DeleteRelationships` is part of the public interface, we don't want to remove it as that would require that the `SqlTopicRepository` version be aligned with the database version—something we restrict to major releases, as part of semantic versioning. As such, we're keeping the `@DeleteRelationships` parameter, but removing the functionality (9b58a4b). If the database is up-to-date with this release, that will yield the same results as before. If the database isn't, it will still accept the `@DeleteRelationships` parameter, as expected, and behave as it used to. Finally, the code from a previous update which avoided primary key constraint violations by deduplicating the insert was optimized to use a `LEFT JOIN` with the actual `Relationships` table instead of using `@Existing_TopicIDs` as a temporary storage location, as user-defined, table-valued types have proven to be quite inefficient (e725077).
2 parents fae0e8d + d3a7323 commit cbba817

4 files changed

Lines changed: 30 additions & 42 deletions

File tree

OnTopic.Data.Sql.Database/Stored Procedures/DeleteTopic.sql

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -87,23 +87,23 @@ WHERE RangeLeft
8787
--------------------------------------------------------------------------------------------------------------------------------
8888
DELETE Attributes
8989
FROM Attributes Attributes
90-
INNER JOIN @Topics Topics
91-
ON Topics.TopicId = Attributes.TopicID
90+
INNER JOIN @Topics Topics
91+
ON Topics.TopicId = Attributes.TopicID
9292

9393
DELETE ExtendedAttributes
9494
FROM ExtendedAttributes ExtendedAttributes
95-
INNER JOIN @Topics Topics
96-
ON Topics.TopicId = ExtendedAttributes.TopicID
95+
INNER JOIN @Topics Topics
96+
ON Topics.TopicId = ExtendedAttributes.TopicID
9797

9898
DELETE Relationships
9999
FROM Relationships Relationships
100-
INNER JOIN @Topics Topics
101-
ON Topics.TopicId = Relationships.Source_TopicID
100+
INNER JOIN @Topics Topics
101+
ON Topics.TopicId = Relationships.Source_TopicID
102102

103103
DELETE Relationships
104104
FROM Relationships Relationships
105-
INNER JOIN @Topics Topics
106-
ON Topics.TopicId = Relationships.Target_TopicID
105+
INNER JOIN @Topics Topics
106+
ON Topics.TopicId = Relationships.Target_TopicID
107107

108108
--------------------------------------------------------------------------------------------------------------------------------
109109
-- DELETE RANGE

OnTopic.Data.Sql.Database/Stored Procedures/UpdateRelationships.sql

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,10 @@
77
CREATE PROCEDURE [dbo].[UpdateRelationships]
88
@TopicID INT = -1,
99
@RelationshipKey VARCHAR(255) = 'related',
10-
@RelatedTopics TopicList READONLY
10+
@RelatedTopics TopicList READONLY,
11+
@DeleteUnmatched BIT = 1
1112
AS
1213

13-
--------------------------------------------------------------------------------------------------------------------------------
14-
-- DECLARE AND SET VARIABLES
15-
16-
DECLARE @Existing_TopicIDs TopicList
17-
18-
--------------------------------------------------------------------------------------------------------------------------------
19-
-- IDENTIFY EXISTING VALUES
20-
--------------------------------------------------------------------------------------------------------------------------------
21-
INSERT
22-
INTO @Existing_TopicIDs (
23-
TopicID
24-
)
25-
SELECT Target_TopicID
26-
FROM Relationships
27-
WHERE Source_TopicID = @TopicID
28-
AND RelationshipKey = @RelationshipKey
29-
3014
--------------------------------------------------------------------------------------------------------------------------------
3115
-- INSERT NOVEL VALUES
3216
--------------------------------------------------------------------------------------------------------------------------------
@@ -36,13 +20,27 @@ INTO Relationships (
3620
RelationshipKey,
3721
Target_TopicID
3822
)
39-
SELECT @TopicId,
23+
SELECT @TopicID,
4024
@RelationshipKey,
41-
Target.TopicID
25+
TopicID
4226
FROM @RelatedTopics Target
43-
FULL JOIN @Existing_TopicIDs Existing
44-
ON Existing.TopicID = Target.TopicID
45-
WHERE Existing.TopicID is null
27+
LEFT JOIN Relationships Existing
28+
ON Target_TopicID = TopicID
29+
AND Source_TopicID = @TopicID
30+
WHERE Target_TopicID IS NULL
31+
32+
--------------------------------------------------------------------------------------------------------------------------------
33+
-- DELETE UNMATCHED VALUES
34+
--------------------------------------------------------------------------------------------------------------------------------
35+
IF @DeleteUnmatched = 1
36+
BEGIN
37+
DELETE EXISTING
38+
FROM @RelatedTopics Relationships
39+
RIGHT JOIN Relationships Existing
40+
ON Target_TopicID = TopicID
41+
WHERE Source_TopicID = @TopicID
42+
AND ISNULL(TopicID, '') = ''
43+
END
4644

4745
--------------------------------------------------------------------------------------------------------------------------------
4846
-- RETURN TOPIC ID

OnTopic.Data.Sql.Database/Stored Procedures/UpdateTopic.sql

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -99,16 +99,6 @@ CROSS APPLY (
9999
WHERE IsNull(AttributeValue, '') = ''
100100
AND ExistingValue != ''
101101

102-
--------------------------------------------------------------------------------------------------------------------------------
103-
-- REMOVE EXISTING RELATIONS
104-
--------------------------------------------------------------------------------------------------------------------------------
105-
-- Relationships will be re-added by the Data Access Layer using Topics_PersistRelationships.
106-
--------------------------------------------------------------------------------------------------------------------------------
107-
DELETE
108-
FROM [dbo].[Relationships]
109-
WHERE Source_TopicID = @TopicID
110-
AND @DeleteRelationships = 1
111-
112102
--------------------------------------------------------------------------------------------------------------------------------
113103
-- RETURN TOPIC ID
114104
--------------------------------------------------------------------------------------------------------------------------------

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ BEGIN
118118
END; -- while
119119

120120
SELECT Stack_Top,
121-
TopicId,
121+
TopicID,
122122
RangeLeft,
123123
RangeRight
124124
FROM Topics

0 commit comments

Comments
 (0)