Skip to content

Commit 5950ec1

Browse files
committed
Merge branch 'bugfix/SQL-migration' into develop
Previously, when running schema comparison, the auto-generated script would fail since `[dbo].[Topics]` contained values. That's because it was attempting to update the new columns from nullable to non-nullable. Any attempt to modify an existing column will throw an error during schema comparison. We create these columns during the migration script as nullable, since they don't have logical defaults, but then immediately populate them with the subsequent script. Given that, this update immediately follows that up with a query to make the columns non-nullable, thus preventing the schema comparison script from needing to modify them. In addition, I also extended the range of possible values picked up for the `BaseTopic` migration. This was originally set to update an attribute named `InheritedTopic` to `BaseTopic`. But, due to historical reasons, that key for that attribute could also be `TopicID` or `DerivedTopic`. (Due to how OnTopic Editor 4.0.0 was written, it didn't actually matter what the key was, and thus the range of possible values.) To ensure that these works against that range, the update statement was modified to accept `TopicID`, `DerivedTopic`, OR `InheritedTopic`. Finally, I added some additional messaging to the upgrade script to make it easier to read the output.
2 parents c68040a + 9f7b544 commit 5950ec1

1 file changed

Lines changed: 32 additions & 6 deletions

File tree

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

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
-- migrations.
1414
--------------------------------------------------------------------------------------------------------------------------------
1515

16+
PRINT('Dropping legacy columns...');
17+
1618
ALTER
1719
TABLE Topics
1820
DROP
@@ -38,6 +40,8 @@ TABLE ExtendedAttributes
3840
DROP
3941
COLUMN DateModified;
4042

43+
PRINT('Dropped legacy columns');
44+
4145
--------------------------------------------------------------------------------------------------------------------------------
4246
-- MIGRATE CORE ATTRIBUTES
4347
--------------------------------------------------------------------------------------------------------------------------------
@@ -46,10 +50,12 @@ COLUMN DateModified;
4650
-- in a way that's inconsistent with other attributes. By moving them to Topic, we better acknowledge their unique status.
4751
--------------------------------------------------------------------------------------------------------------------------------
4852

49-
ALTER TABLE [dbo].[Topics]
50-
ADD [TopicKey] VARCHAR (128) NULL,
51-
[ContentType] VARCHAR (128) NULL,
52-
[ParentID] INT NULL;
53+
PRINT('Migrating core attributes...');
54+
55+
ALTER TABLE [dbo].[Topics]
56+
ADD [TopicKey] VARCHAR(128) NULL,
57+
[ContentType] VARCHAR(128) NULL,
58+
[ParentID] INT NULL;
5359

5460
WITH KeyAttributes AS (
5561
SELECT TopicID,
@@ -82,6 +88,12 @@ PIVOT ( MIN(AttributeValue)
8288
WHERE RowNumber = 1
8389
AND Topics.TopicID = Pvt.TopicID
8490

91+
ALTER TABLE [dbo].[Topics]
92+
ALTER COLUMN TopicKey VARCHAR(128) NOT NULL;
93+
94+
ALTER TABLE [dbo].[Topics]
95+
ALTER COLUMN ContentType VARCHAR(128) NOT NULL;
96+
8597
DELETE
8698
FROM Attributes
8799
WHERE AttributeKey
@@ -90,6 +102,8 @@ IN ( 'Key',
90102
'ParentID'
91103
)
92104

105+
PRINT('Migrated core attributes');
106+
93107
--------------------------------------------------------------------------------------------------------------------------------
94108
-- MIGRATE TOPIC REFERENCES
95109
--------------------------------------------------------------------------------------------------------------------------------
@@ -99,6 +113,8 @@ IN ( 'Key',
99113
-- service to infer which attributes represent relationships in order to translate their values from `TopicID` to `UniqueKey`.
100114
--------------------------------------------------------------------------------------------------------------------------------
101115

116+
PRINT('Migrating topic references...');
117+
102118
CREATE
103119
TABLE [dbo].[TopicReferences] (
104120
[Source_TopicID] INT NOT NULL,
@@ -118,6 +134,8 @@ WHERE AttributeKey LIKE '%ID'
118134
AND ISNUMERIC(AttributeValue) = 1
119135
AND Topics.TopicID IS NOT NULL
120136

137+
PRINT('Migrated core attributes');
138+
121139
--------------------------------------------------------------------------------------------------------------------------------
122140
-- MIGRATE DERIVED TOPICS
123141
--------------------------------------------------------------------------------------------------------------------------------
@@ -127,15 +145,19 @@ WHERE AttributeKey LIKE '%ID'
127145
-- and how its 'ReferenceKey'.
128146
--------------------------------------------------------------------------------------------------------------------------------
129147

148+
PRINT('Migrating base topics...');
149+
130150
UPDATE TopicReferences
131151
SET ReferenceKey = 'BaseTopic'
132152
WHERE ReferenceKey = 'Topic'
133153

134154
UPDATE Topics
135155
SET TopicKey = 'BaseTopic'
136-
WHERE TopicKey = 'InheritedTopic'
156+
WHERE TopicKey IN ('TopicID', 'InheritedTopic', 'DerivedTopic')
137157
AND ContentType = 'TopicReferenceAttribute'
138158

159+
PRINT('Migrated base topics');
160+
139161
--------------------------------------------------------------------------------------------------------------------------------
140162
-- MIGRATE ATTRIBUTE KEYS
141163
--------------------------------------------------------------------------------------------------------------------------------
@@ -144,11 +166,15 @@ AND ContentType = 'TopicReferenceAttribute'
144166
-- conflict with .NET's own "*Attribute" convention (which is usually reserved for actual attributes).
145167
--------------------------------------------------------------------------------------------------------------------------------
146168

169+
PRINT('Migrating attribute descriptors...');
170+
147171
UPDATE Topics
148172
SET TopicKey = TopicKey + 'Descriptor'
149173
WHERE TopicKey LIKE '%Attribute'
150174
AND ContentType = 'ContentTypeDescriptor'
151175

152176
UPDATE Topics
153177
SET ContentType = ContentType + 'Descriptor'
154-
WHERE ContentType LIKE '%Attribute'
178+
WHERE ContentType LIKE '%Attribute'
179+
180+
PRINT('Migrated attribute descriptors');

0 commit comments

Comments
 (0)