Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an actual test and switch to datetime2 #2

Merged
merged 3 commits into from
Oct 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.2.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
21 changes: 17 additions & 4 deletions src/DotJEM.Json.Storage2.Test/SqlServerStorageAreaFactoryTest.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Data.SqlClient;
using DotJEM.Json.Storage2.SqlServer;
using DotJEM.Json.Storage2.SqlServer;
using Newtonsoft.Json.Linq;
using NUnit.Framework;

Expand All @@ -13,6 +12,9 @@ public async Task EnsureLogTable_NoTableExists_ShouldCreateTable()
SqlServerStorageContext context = await SqlServerStorageContext.Create(TestSqlConnectionFactory.ConnectionString);
IStorageArea area = await context.AreaAsync("test");
await area.InsertAsync("na", new JObject());



}

[Test]
Expand Down Expand Up @@ -48,12 +50,23 @@ public async Task GetAsync_NoTableExists_ShouldCreateTables()
await area.InsertAsync("na", JObject.FromObject(new { track="T-01"}));
await area.InsertAsync("na", JObject.FromObject(new { track= "T-02" }));
await area.InsertAsync("na", JObject.FromObject(new { track= "T-03" }));


await foreach (StorageObject obj in area.GetAsync())
{
Console.WriteLine(obj);
}
}

[Test]
public async Task InsertAsync_Record_ShouldAddToTable()
{
SqlServerStorageContext context = await SqlServerStorageContext.Create(TestSqlConnectionFactory.ConnectionString, "fox");
IStorageArea area = await context.AreaAsync("test");

StorageObject obj = await area.InsertAsync("na", JObject.FromObject(new { track = "T-01" }));
StorageObject? obj2 = await area.GetAsync(obj.Id);

Assert.That(obj2, Is.Not.Null & Has.Property(nameof(StorageObject.UpdatedBy)).EqualTo(obj.UpdatedBy));

}

}
22 changes: 0 additions & 22 deletions src/DotJEM.Json.Storage2/IsExternalInit.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using System.Data;
using System.Data.SqlClient;

namespace DotJEM.Json.Storage2.SqlServer.Initialization;

public interface ISqlServerSchemaStateManager
{
string Schema { get; }

Task Ensure();
}

public class SqlServerSchemaStateManager : ISqlServerSchemaStateManager
{
private bool created;
private readonly SqlServerConnectionFactory connectionFactory;
private readonly SemaphoreSlim padlock = new(1, 1);

public string Schema { get; }

public SqlServerSchemaStateManager(SqlServerConnectionFactory connectionFactory, string schema, bool created)
{
this.connectionFactory = connectionFactory;
this.Schema = schema;
this.created = created;
}

public async Task Ensure()
{
if (created)
return;

using IDisposable locked = await padlock.ObtainLockAsync();
if (created)
return;

string commandText = SqlTemplates.CreateSchema(Schema);

await using SqlConnection connection = connectionFactory.Create();
await using SqlCommand command = new SqlCommand(commandText, connection);
await connection.OpenAsync().ConfigureAwait(false);

await using SqlTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted);
command.Connection = connection;
command.Transaction = transaction;
await command.ExecuteNonQueryAsync().ConfigureAwait(false);
await transaction.CommitAsync().ConfigureAwait(false);

created = true;
}
}

public static class SemaphoreSlimExt
{
public static async Task<IDisposable> ObtainLockAsync(this SemaphoreSlim semaphore)
{
await semaphore.WaitAsync();
return new ObtainedLock(semaphore);
}

private class ObtainedLock(SemaphoreSlim semaphore) : IDisposable
{
public void Dispose() => semaphore.Release();
}
}

public class SqlServerAreaStateManager : ISqlServerSchemaStateManager
{
private bool created;
private readonly ISqlServerConnectionFactory connectionFactory;
private readonly SemaphoreSlim padlock = new(1, 1);

public string AreaName { get; }
public bool Exists => created;
public string Schema { get; }

public SqlServerAreaStateManager(ISqlServerConnectionFactory connectionFactory, string schema, string area, bool created)
{
this.connectionFactory = connectionFactory;
this.Schema = schema;
this.AreaName = area;
this.created = created;
}


public async Task Ensure()
{
if (created)
return;

using IDisposable locked = await padlock.ObtainLockAsync();
if (created)
return;

string dataTableCommandText = SqlTemplates.CreateDataTable(Schema, AreaName);// SqlServerStatements.Load("CreateDataTable", map);
string logTableCommandText = SqlTemplates.CreateLogTable(Schema, AreaName); // SqlServerStatements.Load("CreateLogTable", map);
string schemaTableCommandText = SqlTemplates.CreateSchemasTable(Schema, AreaName); // SqlServerStatements.Load("CreateSchemasTable", map);

//await using SqlConnection connection = connectionFactory.Create();
await using SqlConnection connection = connectionFactory.Create();
await connection.OpenAsync().ConfigureAwait(false);

//await using SqlTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted);
await using SqlTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted);
await Execute(dataTableCommandText, connection, transaction);
await Execute(logTableCommandText, connection, transaction);
await Execute(schemaTableCommandText, connection, transaction);
//await transaction.CommitAsync().ConfigureAwait(false);
transaction.Commit();

created = true;
}

private async Task Execute(string commandText, SqlConnection connection, SqlTransaction transaction)
{
//await using SqlCommand command = new(commandText);
await using SqlCommand command = new(commandText);
command.Connection = connection;
command.Transaction = transaction;
await command.ExecuteNonQueryAsync().ConfigureAwait(false);
}

}
104 changes: 1 addition & 103 deletions src/DotJEM.Json.Storage2/SqlServer/Initialization/Temp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace DotJEM.Json.Storage2.SqlServer.Initialization;

//NOTE: Temp holds "in development" items, these should go into meaningful services under interfaces so they can be substituded e.g. for testing purposes.
public static class Temp
{
public static async Task<SqlServerStorageAreaFactory> Create(string schema, SqlServerConnectionFactory connectionFactory)
Expand Down Expand Up @@ -52,106 +53,3 @@ public static async Task<SqlServerStorageAreaFactory> Create(string schema, SqlS

}

public interface ISqlServerSchemaStateManager
{
string SchemaName { get; }

Task Ensure();
}

public class SqlServerSchemaStateManager : ISqlServerSchemaStateManager
{
private bool created;
private readonly SqlServerConnectionFactory connectionFactory;
private readonly SemaphoreSlim padlock = new(1, 1);
public string SchemaName { get; }

public SqlServerSchemaStateManager(SqlServerConnectionFactory connectionFactory, string schema, bool created)
{
this.connectionFactory = connectionFactory;
this.SchemaName = schema;
this.created = created;
}

public async Task Ensure()
{
if (created)
return;

await padlock.WaitAsync();

string commandText = SqlTemplates.CreateSchema(SchemaName);

await using SqlConnection connection = connectionFactory.Create();
await using SqlCommand command = new SqlCommand(commandText, connection);
await connection.OpenAsync().ConfigureAwait(false);

await using SqlTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted);
command.Connection = connection;
command.Transaction = transaction;
await command.ExecuteNonQueryAsync().ConfigureAwait(false);
await transaction.CommitAsync().ConfigureAwait(false);

created = true;
padlock.Release();
}
}


public class SqlServerAreaStateManager
{
private bool created;
private readonly ISqlServerConnectionFactory connectionFactory;
private readonly SemaphoreSlim padlock = new(1, 1);

public string Schema { get; }
public string AreaName { get; }
public bool Exists => created;

public SqlServerAreaStateManager(ISqlServerConnectionFactory connectionFactory, string schema, string area, bool created)
{
this.connectionFactory = connectionFactory;
this.Schema = schema;
this.AreaName = area;
this.created = created;
}

public async Task Ensure()
{
if (created)
return;

await padlock.WaitAsync();
if (created)
return;

string dataTableCommandText = SqlTemplates.CreateDataTable(Schema, AreaName);// SqlServerStatements.Load("CreateDataTable", map);
string logTableCommandText = SqlTemplates.CreateLogTable(Schema, AreaName); // SqlServerStatements.Load("CreateLogTable", map);
string schemaTableCommandText = SqlTemplates.CreateSchemasTable(Schema, AreaName); // SqlServerStatements.Load("CreateSchemasTable", map);

//await using SqlConnection connection = connectionFactory.Create();
using SqlConnection connection = connectionFactory.Create();
await connection.OpenAsync().ConfigureAwait(false);

//await using SqlTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted);
using SqlTransaction transaction = connection.BeginTransaction(IsolationLevel.ReadUncommitted);
await Execute(dataTableCommandText, connection, transaction);
await Execute(logTableCommandText, connection, transaction);
await Execute(schemaTableCommandText, connection, transaction);
//await transaction.CommitAsync().ConfigureAwait(false);
transaction.Commit();

created = true;
padlock.Release();
}

private async Task Execute(string commandText, SqlConnection connection, SqlTransaction transaction)
{
//await using SqlCommand command = new(commandText);
using SqlCommand command = new(commandText);
command.Connection = connection;
command.Transaction = transaction;
await command.ExecuteNonQueryAsync().ConfigureAwait(false);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task<SqlServerStorageArea> Create(string name, SqlServerStorageCont
return new(context, areaInfo.State);

await padlock.WaitAsync().ConfigureAwait(false);
AreaInfo area = new(name, new(context.ConnectionFactory, schema.SchemaName, name, false));
AreaInfo area = new(name, new(context.ConnectionFactory, schema.Schema, name, false));
areas.Add(name, area);
return new(context, area.State);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
[Id] [uniqueidentifier] NOT NULL,
[Version] [int] NOT NULL,
[ContentType] [varchar](64) NOT NULL,
[Created] [datetime] NOT NULL,
[Updated] [datetime] NOT NULL,
[Created] [datetime2] NOT NULL,
[Updated] [datetime2] NOT NULL,
[CreatedBy] [nvarchar](256) NULL,
[UpdatedBy] [nvarchar](256) NULL,
[Data] [nvarchar](max) NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[Revision] [bigint] IDENTITY(1,1) NOT NULL,
[Id] [uniqueidentifier] NOT NULL,
[Event] [varchar](1) NOT NULL,
[Time] [datetime] NOT NULL,
[Time] [datetime2] NOT NULL,
[User] [nvarchar](256) NULL,
[Version] [bigint] NOT NULL,
[Data] [nvarchar](max) NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
[Id] [uniqueidentifier] NOT NULL,
[Version] [int] NOT NULL,
[ContentType] [varchar](64) NOT NULL,
[Created] [datetime] NOT NULL,
[Updated] [datetime] NOT NULL,
[Created] [datetime2] NOT NULL,
[Updated] [datetime2] NOT NULL,
[Data] [nvarchar](max) NOT NULL,
[RV] [rowversion] NOT NULL,
CONSTRAINT [PK_@{area_name}.schemas] PRIMARY KEY CLUSTERED ( [Id] ASC )
Expand Down
Loading