Skip to content

Commit

Permalink
Fix: #34, Added PostgreNpgsql6Dialect dialect
Browse files Browse the repository at this point in the history
  • Loading branch information
AGiorgetti committed Dec 2, 2021
1 parent cea14f3 commit fae5208
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 17 deletions.
13 changes: 1 addition & 12 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
# NEventStore.Persistence.Sql

## PostgreSQL Warning

If you upgrade Npgsql to version 6.0 and up you must take into account the breaking changes
made about the timezones handling [Timestamp rationalization and improvements](https://www.npgsql.org/efcore/release-notes/6.0.html#timestamp-rationalization-and-improvements).

Possible solutions:
- manually migrate the Table schema and update the "CommitStamp" column type from "timestamp" to "timestamptz".
- disable the new behavior by calling:
```
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
```

## 9.0.0

- Updated NEventStore core library to 9.0.0.
- Added support for net6.0.
- Added a new PostgreSQL dialect (`PostgreNpgsql6Dialect`) to deal with Npgsql version 6 timestamp breaking changes. If you update the driver you might need to update the table schema manually [#34](https://github.com/NEventStore/NEventStore.Persistence.SQL/issues/34).

## 8.0.0

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ NEventStore.Persistence.Sql currently supports:
- .net framework 4.6.1
- .net standard 2.0
- .net 5.0
- .net 6.0
- MsSql
- SqlLite
- MySql
Expand All @@ -22,6 +23,19 @@ Branches:
- master [![Build status](https://ci.appveyor.com/api/projects/status/5difan7hap8vwhwe/branch/master?svg=true)](https://ci.appveyor.com/project/AGiorgetti/neventstore-persistence-sql/branch/master)
- develop [![Build status](https://ci.appveyor.com/api/projects/status/5difan7hap8vwhwe/branch/develop?svg=true)](https://ci.appveyor.com/project/AGiorgetti/neventstore-persistence-sql/branch/develop)

## PostgreSQL Warning

If you upgrade Npgsql to version 6.0 and up you must take into account the breaking changes
made about the timezones handling [Timestamp rationalization and improvements](https://www.npgsql.org/efcore/release-notes/6.0.html#timestamp-rationalization-and-improvements).

Possible solutions:
- manually migrate the Table schema and update the "CommitStamp" column type from "timestamp" to "timestamptz".
- disable the new behavior by calling:
```
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
```
- use the new `PostgreNpgsql6Dialect`.

## How to Build (locally)

- Clone the repository with:
Expand Down
2 changes: 1 addition & 1 deletion dependencies/NEventStore
Submodule NEventStore updated 39 files
+2 −0 Changelog.md
+2 −1 readme.markdown
+0 −27 src/NEventStore.Benchmark/Benchmarks/Md5VsSha256.cs
+6 −5 src/NEventStore.Benchmark/Benchmarks/PersistenceBenchmarks.cs
+5 −5 src/NEventStore.Persistence.AcceptanceTests/ExtensionMethods.cs
+1 −1 src/NEventStore.Persistence.AcceptanceTests/NEventStore.Persistence.AcceptanceTests.Core.csproj
+9 −14 src/NEventStore.Persistence.AcceptanceTests/PersistenceTests.cs
+1 −1 src/NEventStore.Serialization.Binary.Tests/NEventStore.Serialization.Binary.Core.Tests.csproj
+4 −2 src/NEventStore.Serialization.Binary.Tests/SerializerFixture.cs
+1 −1 src/NEventStore.Serialization.Bson.Tests/NEventStore.Serialization.Bson.Core.Tests.csproj
+1 −1 src/NEventStore.Serialization.Gzip.Tests/NEventStore.Serialization.GZip.Core.Tests.csproj
+2 −0 src/NEventStore.Serialization.Gzip.Tests/SerializerFixture.cs
+1 −1 src/NEventStore.Serialization.Json.Tests/NEventStore.Serialization.Json.Core.Tests.csproj
+1 −1 src/NEventStore.Serialization.Json.Tests/SerializerFixture.cs
+19 −3 src/NEventStore.Serialization.Json/JsonSerializationWireupExtension.cs
+28 −14 src/NEventStore.Serialization.Json/JsonSerializer.cs
+1 −1 src/NEventStore.Serialization.Rijndael.Tests/NEventStore.Serialization.Rijndael.Core.Tests.csproj
+4 −2 src/NEventStore.Serialization.Rijndael.Tests/SerializerFixture.cs
+4 −4 src/NEventStore.Tests/NEventStore.Core.Tests.csproj
+1 −1 src/NEventStore.Tests/OptimisticEventStoreTests.cs
+1 −1 src/NEventStore.Tests/OptimisticEventStreamTests.cs
+1 −1 src/NEventStore.Tests/OptimisticPipelineHookTests.cs
+7 −7 src/NEventStore.Tests/Persistence/InMemory/InMemoryPersistenceTests.cs
+1 −1 src/NEventStore.Tests/PipelineHooksAwarePersistanceDecoratorTests.cs
+126 −128 src/NEventStore/CommitAttempt.cs
+0 −22 src/NEventStore/CommitAttemptExtensions.cs
+1 −1 src/NEventStore/Conversion/EventUpconverterPipelineHook.cs
+1 −9 src/NEventStore/Helpers/DisposableAction.cs
+1 −1 src/NEventStore/ICommit.cs
+1 −1 src/NEventStore/IEventStream.cs
+1 −1 src/NEventStore/OptimisticEventStore.cs
+2 −2 src/NEventStore/OptimisticEventStream.cs
+228 −228 src/NEventStore/OptimisticPipelineHook.cs
+55 −94 src/NEventStore/Persistence/Commit.cs
+2 −2 src/NEventStore/Persistence/IPersistStreams.cs
+1 −1 src/NEventStore/Persistence/InMemory/InMemoryPersistenceEngine.cs
+75 −76 src/NEventStore/Persistence/StreamHead.cs
+5 −0 src/NEventStore/Serialization/BinarySerializer.cs
+2 −0 src/NEventStore/SerializationWireupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<Reference Include="System.Transactions" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Npgsql" Version="4.1.6" />
<PackageReference Include="Npgsql" Version="6.0.0" />
<PackageReference Include="FluentAssertions" Version="6.2.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.0.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@ namespace NEventStore.Persistence.AcceptanceTests
using NEventStore.Persistence.Sql;
using NEventStore.Persistence.Sql.SqlDialects;
using NEventStore.Serialization;
using System;

public partial class PersistenceEngineFixture
{
public PersistenceEngineFixture()
{
// It will be done when creating the PostgreNpgsql6Dialect dialect
// AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);

#if NET461
_createPersistence = pageSize =>
new SqlPersistenceFactory(
new EnviromentConnectionFactory("PostgreSql", "Npgsql"),
new BinarySerializer(),
new PostgreSqlDialect(),
new PostgreNpgsql6Dialect(npgsql6timestamp: true),
pageSize: pageSize).Build();
#else
_createPersistence = pageSize =>
new SqlPersistenceFactory(
new EnviromentConnectionFactory("PostgreSql", Npgsql.NpgsqlFactory.Instance),
new BinarySerializer(),
new PostgreSqlDialect(),
new PostgreNpgsql6Dialect(npgsql6timestamp: true),
pageSize: pageSize).Build();
#endif
}
Expand Down
58 changes: 58 additions & 0 deletions src/NEventStore.Persistence.Sql/SqlDialects/PostgreSqlDialect.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace NEventStore.Persistence.Sql.SqlDialects
{
using System;
using System.Data;

public class PostgreSqlDialect : CommonSqlDialect
{
Expand All @@ -20,4 +21,61 @@ public override bool IsDuplicate(Exception exception)
return message.Contains("23505") || message.Contains("IX_COMMITS_COMMITSEQUENCE");
}
}

public class PostgreNpgsql6Dialect : CommonSqlDialect
{
private readonly bool _npgsql6Timestamp;

/// <summary>
/// Create an instance of the PostgreSQL dialect
/// </summary>
/// <param name="npgsql6timestamp">
/// There's a breaking change in Npgsql Version 6.x that changes the way timestamps should be persisted.
/// - false: to disable the new behavior for the whole application (see the driver release notes) this should be done before using the database.
/// - true: enable the new behavior, you might need to migrate the data manually (see Npgsql release notes).
/// </param>
public PostgreNpgsql6Dialect(
bool npgsql6timestamp = true
)
{
_npgsql6Timestamp = npgsql6timestamp;
if (!_npgsql6Timestamp)
{
AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true);
}
}

public override string InitializeStorage
{
get
{
var initStorage = PostgreSqlStatements.InitializeStorage;
if (_npgsql6Timestamp)
{
initStorage = initStorage.Replace("timestamp", "timestamptz");
}
return initStorage;
}
}

public override string PersistCommit
{
get { return PostgreSqlStatements.PersistCommits; }
}

public override bool IsDuplicate(Exception exception)
{
string message = exception.Message.ToUpperInvariant();
return message.Contains("23505") || message.Contains("IX_COMMITS_COMMITSEQUENCE");
}

public override DbType GetDateTimeDbType()
{
if (_npgsql6Timestamp)
{
return DbType.DateTimeOffset;
}
return base.GetDateTimeDbType();
}
}
}
2 changes: 1 addition & 1 deletion src/NEventStore.Persistence.Sql/SqlPersistenceEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace NEventStore.Persistence.Sql
public class SqlPersistenceEngine : IPersistStreams
{
private static readonly ILogger Logger = LogFactory.BuildLogger(typeof(SqlPersistenceEngine));
private static readonly DateTime EpochTime = new DateTime(1970, 1, 1);
private static readonly DateTime EpochTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private readonly IConnectionFactory _connectionFactory;
private readonly ISqlDialect _dialect;
private readonly int _pageSize;
Expand Down

0 comments on commit fae5208

Please sign in to comment.