Skip to content

Commit 78dd6cd

Browse files
committed
Prevent duplicates for indexed attributes
Currently, we rely on the caller to eliminate duplicate values. This works fine. But it'd be nice to prevent that at the database level just to be safe. This adds a _small_ amount of overhead, but it's relatively trivial compared to the benefit. With this `OUTER APPLY` added, attribute values will _only_ be added if they are different from the last value saved for that key. This ensures we don't introduce unnecessary consecutive duplicates to the attributes table. This is handy for casual scripting, as we can call `UpdateTopic` with whatever values we want, and ensure they are added if appropriate. If they are already the current value, they will be skipped. This also adds utility for cases where we are importing data, but don't know the current state of the database, as it prevents the need to first query the database, then deduplicate the records, and finally the delta.
1 parent 4035bcc commit 78dd6cd

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,18 @@ SELECT @TopicID,
3232
AttributeKey,
3333
AttributeValue,
3434
@Version
35-
FROM @Attributes
35+
FROM @Attributes New
36+
OUTER APPLY (
37+
SELECT TOP 1
38+
AttributeValue AS ExistingValue
39+
FROM Attributes
40+
WHERE TopicID = @TopicID
41+
AND AttributeKey = New.AttributeKey
42+
ORDER BY Version DESC
43+
) Existing
3644
WHERE AttributeKey != 'ParentId'
37-
AND IsNull(AttributeValue, '') != ''
45+
AND ISNULL(AttributeValue, '') != ''
46+
AND ISNULL(ExistingValue, '') != AttributeValue
3847

3948
--------------------------------------------------------------------------------------------------------------------------------
4049
-- PULL PREVIOUS EXTENDED ATTRIBUTES

0 commit comments

Comments
 (0)