Skip to content

Example of Migration to MsSqlStreamStoreV3

Michael Freidgeim edited this page Dec 6, 2018 · 1 revision

New version 1.2.0 (not available as NuGet package yet) includes Optimization - Lifts the core metadata, MaxAge and MaxCount, to the streams table. #140. Depends on your workflow it may have 10-times performance improvements.

Below is example of the code, how you can include migration. Partially it based on SqlStreamStore.MsSql.V3.Tests\MigrationTests.cs

    private async Task<StreamStoreBase> GetMsSqlStreamStore()
    {
        var settings = new MsSqlStreamStoreSettings(ConnectionString)
        {
            Schema = _schema,
        };
        var store = new MsSqlStreamStore(settings);

        var checkSchemaResult = await store.CheckSchema();
        _logger.LogInformation($"checkSchemaResult before Migrate IsMatch {checkSchemaResult.IsMatch()} CurrentVersion {checkSchemaResult.CurrentVersion } ExpectedVersion {checkSchemaResult.ExpectedVersion } ");
        if (checkSchemaResult.CurrentVersion == 2)
        {
              Task.Factory.StartNew(() => MigrateToV3());
        }
        else
        {
            var v3store=CreateMsSqlStreamStoreV3();
            return v3store;
        }
      
        return store;
    }

    /// <summary>
    /// From SQLStreamStore\src\SqlStreamStore.MsSql.V3.Tests\MigrationTests.cs
    /// </summary>
    /// <returns></returns>
    public async Task<StreamStoreBase> MigrateToV3()
    {
        var v3Store = CreateMsSqlStreamStoreV3();

         var checkSchemaResult = await v3Store.CheckSchema();
        _logger.LogInformation($"Before Migrate IsMatch {checkSchemaResult.IsMatch()} CurrentVersion {checkSchemaResult.CurrentVersion} ExpectedVersion {checkSchemaResult.ExpectedVersion} ");

        var progress = new Progress<MigrateProgress>();
        progress.ProgressChanged += (_, migrateProgress) =>
               _logger.LogInformation($"Migration stage complete: {migrateProgress.Stage}");

        await v3Store.Migrate(progress, CancellationToken.None);
        
        checkSchemaResult = await v3Store.CheckSchema();
        _logger.LogInformation($"After Migrate IsMatch {checkSchemaResult.IsMatch()} CurrentVersion {checkSchemaResult.CurrentVersion} ExpectedVersion {checkSchemaResult.ExpectedVersion} ");
        return v3Store;
    }
	
    private MsSqlStreamStoreV3 CreateMsSqlStreamStoreV3()
    {
        var settings = new MsSqlStreamStoreV3Settings(ConnectionString)
        {
            Schema = _schema,
            DisableDeletionTracking = true //we currently do not care about deletion , consider to change if needed 
        };

        var v3Store = new MsSqlStreamStoreV3(settings);
        return v3Store;
    }
Clone this wiki locally