Skip to content

Commit 8999323

Browse files
Mike Blairclaude
andcommitted
feat: update MsSqlDataStore schema hash and timestamp on sync
After sync, compare the new schema hash with the stored one. If they differ, update SchemaHash, LastSchemaImport, and clear NeedsResync on the MsSqlDataStoreConfiguration. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 9acf5e3 commit 8999323

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

src/FractalDataWorks.Data.DataStores.SqlServer/MsSqlSchemaImportPersister.cs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public sealed class MsSqlSchemaImportPersister : ISchemaImportPersister
3333
private readonly IOptionsMonitor<List<DataContainerConfiguration>> _containerOptions;
3434
private readonly IOptionsMonitor<List<DataContainerFieldConfiguration>> _fieldOptions;
3535
private readonly IOptionsMonitor<List<Services.Connections.MsSql.MsSqlConnectionConfiguration>> _connectionOptions;
36+
private readonly IOptionsMonitor<List<Services.Connections.MsSql.MsSqlDataStoreConfiguration>> _msSqlDataStoreOptions;
3637
private readonly ILogger<MsSqlSchemaImportPersister> _logger;
3738

3839
/// <summary>
@@ -43,20 +44,23 @@ public sealed class MsSqlSchemaImportPersister : ISchemaImportPersister
4344
/// <param name="containerOptions">Options monitor for container configurations.</param>
4445
/// <param name="fieldOptions">Options monitor for field configurations.</param>
4546
/// <param name="connectionOptions">Options monitor for MsSql connection configurations.</param>
47+
/// <param name="msSqlDataStoreOptions">Options monitor for MsSql data store configurations.</param>
4648
/// <param name="logger">Logger instance.</param>
4749
public MsSqlSchemaImportPersister(
4850
IConfigurationWriterFactory writerFactory,
4951
IOptionsMonitor<List<DataPathConfiguration>> dataPathOptions,
5052
IOptionsMonitor<List<DataContainerConfiguration>> containerOptions,
5153
IOptionsMonitor<List<DataContainerFieldConfiguration>> fieldOptions,
5254
IOptionsMonitor<List<Services.Connections.MsSql.MsSqlConnectionConfiguration>> connectionOptions,
55+
IOptionsMonitor<List<Services.Connections.MsSql.MsSqlDataStoreConfiguration>> msSqlDataStoreOptions,
5356
ILogger<MsSqlSchemaImportPersister> logger)
5457
{
5558
_writerFactory = writerFactory ?? throw new ArgumentNullException(nameof(writerFactory));
5659
_dataPathOptions = dataPathOptions ?? throw new ArgumentNullException(nameof(dataPathOptions));
5760
_containerOptions = containerOptions ?? throw new ArgumentNullException(nameof(containerOptions));
5861
_fieldOptions = fieldOptions ?? throw new ArgumentNullException(nameof(fieldOptions));
5962
_connectionOptions = connectionOptions ?? throw new ArgumentNullException(nameof(connectionOptions));
63+
_msSqlDataStoreOptions = msSqlDataStoreOptions ?? throw new ArgumentNullException(nameof(msSqlDataStoreOptions));
6064
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
6165
}
6266

@@ -142,7 +146,7 @@ public async Task<IGenericResult<Guid>> Persist(
142146
}
143147

144148
/// <inheritdoc />
145-
[ConventionOverride(MaxMethodLines = 70)]
149+
[ConventionOverride(MaxMethodLines = 75)]
146150
public async Task<IGenericResult<SchemaImportSyncResult>> Sync(
147151
Guid existingDataStoreId,
148152
IDataStore dataStore,
@@ -232,6 +236,9 @@ await DeleteRemovedPaths(
232236

233237
var newSchemaHash = ComputeSchemaHash(schemaBuilder.ToString());
234238

239+
// Update MsSqlDataStore hash and timestamp if schema changed
240+
await UpdateDataStoreSchemaHash(existingDataStoreId, newSchemaHash, cancellationToken).ConfigureAwait(false);
241+
235242
var syncResult = new SchemaImportSyncResult
236243
{
237244
DataStoreId = existingDataStoreId,
@@ -809,6 +816,33 @@ private static async Task DeleteRemovedFields(
809816
}
810817
}
811818

819+
private async Task UpdateDataStoreSchemaHash(Guid dataStoreId, string newSchemaHash, CancellationToken cancellationToken)
820+
{
821+
var dataStores = _msSqlDataStoreOptions.CurrentValue ?? [];
822+
var msSqlDataStore = dataStores.Find(ds => ds.DataStoreId == dataStoreId);
823+
if (msSqlDataStore is null)
824+
{
825+
return;
826+
}
827+
828+
if (string.Equals(msSqlDataStore.SchemaHash, newSchemaHash, StringComparison.Ordinal))
829+
{
830+
return;
831+
}
832+
833+
var writerResult = _writerFactory.Create<Services.Connections.MsSql.MsSqlDataStoreConfiguration>();
834+
if (!writerResult.IsSuccess)
835+
{
836+
return;
837+
}
838+
839+
msSqlDataStore.SchemaHash = newSchemaHash;
840+
msSqlDataStore.LastSchemaImport = DateTimeOffset.UtcNow;
841+
msSqlDataStore.NeedsResync = false;
842+
843+
await writerResult.Value!.Save(msSqlDataStore, cancellationToken).ConfigureAwait(false);
844+
}
845+
812846
private async Task UpdateConnectionAssociation(Guid connectionId, Guid dataStoreId)
813847
{
814848
var connectionWriterResult = _writerFactory.Create<Services.Connections.MsSql.MsSqlConnectionConfiguration>();

0 commit comments

Comments
 (0)