Skip to content

Commit 9f0fdbc

Browse files
committed
Updated version to use SqlDateTime
The `DateTime` data type in SQL has a lower precision than the `DateTime` data type in C#. As a result, when storing the `version` in `topic.VersionHistory`, a different version was getting saved than in the database. In practice, this resulted in an inability to roll back versions that had been created since the last application reset. (Previous versions worked, as they were loaded with the lower-precision value from the database.) This is mitigated by instead initializing the `version` variable as a `SqlDateTime` object, which uses the same precision as the database. While I was at it, I move the `VersionHistory` insert into the `try/catch` block so that a `version` won't be prepended if there was an error during the `Save()` operation. (In practice, this is unlikely, and we haven't had any reports of this being a concern, but it's an easy fix to make while modifying the code.)
1 parent ab19b9d commit 9f0fdbc

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

OnTopic.Data.Sql/SqlTopicRepository.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
\=============================================================================================================================*/
66
using System;
77
using System.Data;
8+
using System.Data.SqlTypes;
89
using System.Diagnostics.CodeAnalysis;
910
using System.Globalization;
1011
using System.Linq;
@@ -333,7 +334,7 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
333334
var command = new SqlCommand(procedureName, connection) {
334335
CommandType = CommandType.StoredProcedure
335336
};
336-
var version = DateTime.Now;
337+
var version = new SqlDateTime(DateTime.Now);
337338

338339
/*------------------------------------------------------------------------------------------------------------------------
339340
| Establish query parameters
@@ -344,11 +345,11 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
344345
else if (topic.Parent != null) {
345346
command.AddParameter("ParentID", topic.Parent.Id);
346347
}
347-
command.AddParameter("Version", version);
348348
command.Parameters.AddWithValue("@Attributes", attributes);
349349
if (!isNew) {
350350
command.AddParameter("DeleteRelationships", true);
351351
}
352+
command.AddParameter("Version", version.Value);
352353
command.AddParameter("ExtendedAttributes", extendedAttributes);
353354
command.AddOutputParameter();
354355

@@ -369,6 +370,8 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
369370

370371
PersistRelations(topic, connection, true);
371372

373+
topic.VersionHistory.Insert(0, version.Value);
374+
372375
}
373376

374377
/*------------------------------------------------------------------------------------------------------------------------
@@ -390,11 +393,6 @@ public override int Save([NotNull]Topic topic, bool isRecursive = false, bool is
390393
attributes.Dispose();
391394
}
392395

393-
/*------------------------------------------------------------------------------------------------------------------------
394-
| Add version to version history
395-
\-----------------------------------------------------------------------------------------------------------------------*/
396-
topic.VersionHistory.Insert(0, version);
397-
398396
/*------------------------------------------------------------------------------------------------------------------------
399397
| Recurse
400398
\-----------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)