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

Renamed project to Blumchen #19

Merged
merged 2 commits into from
Jun 20, 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
2 changes: 1 addition & 1 deletion .github/workflows/build.dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Start containers
run: docker-compose up -d

- name: Setup .NET Core
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: "8.0.x"
Expand Down
40 changes: 40 additions & 0 deletions .github/workflows/publish-nuget.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: NuGet Manual Publish

on: [workflow_dispatch]

env:
config: Release
DOTNET_CLI_TELEMETRY_OPTOUT: 1
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
dotnet_core_version: 8.0.x

jobs:
publish_job:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: "8.0.x"

- name: Restore NuGet packages
run: dotnet restore

- name: Build
run: dotnet build --configuration Release --no-restore

- name: Run tests
run: dotnet test --configuration Release --no-build --filter Category!=SkipCI

- name: Run tests
run: dotnet pack --configuration -o ./artifacts --configuration Release --no-build

- name: Publish to NuGet
run: |
find . -name '*.nupkg' -exec dotnet nuget push "{}" -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_DEPLOY_KEY }} --skip-duplicate \;
# find . -name '*.snupkg' -exec dotnet nuget push "{}" -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_DEPLOY_KEY }} \;
shell: bash
3 changes: 2 additions & 1 deletion PostgresOutboxPattern.sln → Blumchen.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docker", "docker", "{CD59A1
docker-compose.yml = docker-compose.yml
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PostgresOutbox", "src\PostgresOutbox\PostgresOutbox.csproj", "{E1AD98FC-42B1-4B7B-AF44-8057B7A61FDD}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blumchen", "src\Blumchen\Blumchen.csproj", "{E1AD98FC-42B1-4B7B-AF44-8057B7A61FDD}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "build", "build", "{D0AB0FF4-C8A1-4B4B-A682-64F353A2D248}"
ProjectSection(SolutionItems) = preProject
src\Directory.Build.props = src\Directory.Build.props
.github\workflows\build.dotnet.yml = .github\workflows\build.dotnet.yml
.github\workflows\publish-nuget.yml = .github\workflows\publish-nuget.yml
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{47B6EDAC-2EBC-4368-BAD6-2B8695E7D067}"
Expand Down
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
# Postgres Outbox Pattern with CDC and .NET
PoC of doing Outbox Pattern with CDC and .NET
# Blumchen - Push-based Outbox for PostgreSQL
Outbox Pattern with CDC and .NET based on [Postgres logical replication](https://www.postgresql.org/docs/current/logical-replication.html) with [Npgsql integration](https://www.npgsql.org/doc/replication.html).

Read more details in:
- [Push-based Outbox Pattern with Postgres Logical Replication](https://event-driven.io/en/push_based_outbox_pattern_with_postgres_logical_replication/?utm_source=github_outbox_cdc).
- [How to get all messages through Postgres logical replication](https://event-driven.io/en/how_to_get_all_messages_through_postgres_logical_replication/?utm_source=github_outbox_cdc).

## Features:

- Publication filter [is enabled](https://www.postgresql.org/docs/current/sql-createpublication.html#SQL-CREATEPUBLICATION-WITH) to trigger only INSERTed rows;
- AOT compliant compilation enforced by design

Uses [Postgres logical replication](https://www.postgresql.org/docs/current/logical-replication.html) with [Npgsql integration](https://www.npgsql.org/doc/replication.html).

Main logic is placed in [EventsSubscription](./PostgresOutbox/Subscriptions/Subscription.cs). Check [LogicalReplicationTest](./PostgresOutboxPatternWithCDC.NET.Tests/LogicalReplicationTest.cs) for sample usage.

Read more details in:
- [Push-based Outbox Pattern with Postgres Logical Replication](https://event-driven.io/en/push_based_outbox_pattern_with_postgres_logical_replication/?utm_source=github_outbox_cdc).
- [How to get all messages through Postgres logical replication](https://event-driven.io/en/how_to_get_all_messages_through_postgres_logical_replication/?utm_source=github_outbox_cdc).
Main logic is placed in [EventsSubscription](./src/Blumchen/Subscriptions/Subscription.cs).

## Running
## Running source code locally

1. Start Postgres with WAL enabled from Docker image.
```shell
Expand Down
35 changes: 35 additions & 0 deletions src/Blumchen/Blumchen.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<VersionPrefix>0.0.1</VersionPrefix>
<TargetFramework>net8.0</TargetFramework>
<GenerateAssemblyTitleAttribute>true</GenerateAssemblyTitleAttribute>
<GenerateAssemblyDescriptionAttribute>true</GenerateAssemblyDescriptionAttribute>
<GenerateAssemblyProductAttribute>true</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GenerateAssemblyVersionAttribute>true</GenerateAssemblyVersionAttribute>
<GenerateAssemblyFileVersionAttribute>true</GenerateAssemblyFileVersionAttribute>
<GenerateAssemblyInformationalVersionAttribute>true</GenerateAssemblyInformationalVersionAttribute>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>12.0</LangVersion>
<Authors>Oskar Dudycz</Authors>
<!-- <PackageIconUrl>https://github.com/event-driven-io/Blumchen/content/images/emblem.png</PackageIconUrl>-->
<PackageProjectUrl>https://github.com/event-driven-io/Blumchen</PackageProjectUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<RepositoryUrl>https://github.com/event-driven-io/Blumchen.git</RepositoryUrl>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<Product>Blumchen</Product>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<RootNamespace>Blumchen</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JetBrains.Annotations" Version="2023.3.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageReference Include="Npgsql" Version="8.0.3" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
using System.Data;
using System.Runtime.CompilerServices;
using Blumchen.Subscriptions.Replication;
using Blumchen.Subscriptions.ReplicationMessageHandlers;
using Npgsql;
using PostgresOutbox.Subscriptions.Replication;
using PostgresOutbox.Subscriptions.ReplicationMessageHandlers;

namespace PostgresOutbox.Database;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

namespace Blumchen.Database;

public static class Run
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace PostgresOutbox.Serialization;
namespace Blumchen.Serialization;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

public interface INamingPolicy
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;

namespace PostgresOutbox.Serialization;
namespace Blumchen.Serialization;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

public interface ITypeResolver
{
Expand All @@ -28,6 +29,6 @@ public TypeResolver WhiteList<T>() where T:class

public (string, JsonTypeInfo) Resolve(Type type) =>
(TypeDictionary.Single(kv => kv.Value == type).Key, TypeInfoDictionary[type]);

public Type Resolve(string type) => TypeDictionary[type];
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using PostgresOutbox.Streams;
using Blumchen.Streams;

namespace PostgresOutbox.Serialization;
namespace Blumchen.Serialization;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

public static class JsonSerialization
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Concurrent;

namespace PostgresOutbox.Serialization;
namespace Blumchen.Serialization;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface)]
public class MessageUrnAttribute:
Expand Down Expand Up @@ -38,7 +39,7 @@ public static class MessageUrn
public const string Prefix = "urn:message:";

private static readonly ConcurrentDictionary<Type, ICached> Cache = new();


public static string ForTypeString(Type type) =>
Cache.GetOrAdd(type,t =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace PostgresOutbox.Serialization;
namespace Blumchen.Serialization;

internal class SOHSkippingStream(Stream inner): Stream
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace PostgresOutbox.Streams
namespace Blumchen.Streams
{
internal class SohSkippingStream(Stream inner): Stream
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace PostgresOutbox.Streams
namespace Blumchen.Streams
{
internal static class StreamExtensions
{
Expand Down
9 changes: 9 additions & 0 deletions src/Blumchen/Subscriptions/IConsume.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Blumchen.Subscriptions;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

public interface IConsume;

public interface IConsumes<in T>: IConsume where T : class
{
Task Handle(T value);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Blumchen.Subscriptions.Replication;
using JetBrains.Annotations;
using PostgresOutbox.Subscriptions.Replication;
using static PostgresOutbox.Subscriptions.Management.PublicationManagement;
using static PostgresOutbox.Subscriptions.Management.ReplicationSlotManagement;
using static Blumchen.Subscriptions.Management.PublicationManagement;
using static Blumchen.Subscriptions.Management.ReplicationSlotManagement;

namespace PostgresOutbox.Subscriptions;
namespace Blumchen.Subscriptions;

internal interface ISubscriptionOptions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Blumchen.Database;
using Npgsql;
using PostgresOutbox.Database;

#pragma warning disable CA2208
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

namespace PostgresOutbox.Subscriptions.Management;
namespace Blumchen.Subscriptions.Management;

using static PublicationManagement.SetupPublicationResult;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using Blumchen.Database;
using Npgsql;
using Npgsql.Replication;
using NpgsqlTypes;
using PostgresOutbox.Database;

namespace PostgresOutbox.Subscriptions.Management;
namespace Blumchen.Subscriptions.Management;
using static ReplicationSlotManagement.CreateReplicationSlotResult;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

public static class ReplicationSlotManagement
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Blumchen.Subscriptions.ReplicationMessageHandlers;
using Npgsql;
using Npgsql.Replication.PgOutput.Messages;
using PostgresOutbox.Subscriptions.ReplicationMessageHandlers;

namespace PostgresOutbox.Subscriptions.Replication;
namespace Blumchen.Subscriptions.Replication;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

public interface IReplicationDataMapper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Npgsql;
using Npgsql.Replication.PgOutput.Messages;
using PostgresOutbox.Serialization;
using PostgresOutbox.Subscriptions.ReplicationMessageHandlers;
using System.Text.Json;
using Blumchen.Serialization;
using Blumchen.Subscriptions.ReplicationMessageHandlers;
using Npgsql;
using Npgsql.Replication.PgOutput;
using Npgsql.Replication.PgOutput.Messages;

namespace PostgresOutbox.Subscriptions.Replication;
namespace Blumchen.Subscriptions.Replication;

internal sealed class ReplicationDataMapper(ITypeResolver resolver): IReplicationDataMapper
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace PostgresOutbox.Subscriptions.ReplicationMessageHandlers;
namespace Blumchen.Subscriptions.ReplicationMessageHandlers;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

public interface IEnvelope;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using System.Runtime.CompilerServices;
using Blumchen.Database;
using Blumchen.Subscriptions.Replication;
using Blumchen.Subscriptions.ReplicationMessageHandlers;
using Npgsql;
using PostgresOutbox.Database;
using PostgresOutbox.Subscriptions.Replication;
using PostgresOutbox.Subscriptions.ReplicationMessageHandlers;

namespace PostgresOutbox.Subscriptions.SnapshotReader;
namespace Blumchen.Subscriptions.SnapshotReader;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

public static class SnapshotReader
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using Blumchen.Database;
using Blumchen.Subscriptions.Management;
using Blumchen.Subscriptions.ReplicationMessageHandlers;
using Blumchen.Subscriptions.SnapshotReader;
using Microsoft.Extensions.Logging;
using Npgsql;
using Npgsql.Replication;
using Npgsql.Replication.PgOutput;
using Npgsql.Replication.PgOutput.Messages;
using PostgresOutbox.Database;
using PostgresOutbox.Subscriptions.Management;
using PostgresOutbox.Subscriptions.ReplicationMessageHandlers;
using PostgresOutbox.Subscriptions.SnapshotReader;

namespace PostgresOutbox.Subscriptions;
namespace Blumchen.Subscriptions;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

using static PublicationManagement;
using static ReplicationSlotManagement;
Expand Down Expand Up @@ -117,7 +118,7 @@ IErrorProcessor errorProcessor
}

private static readonly Dictionary<Type, (IConsume consumer, MethodInfo methodInfo)> Cache = [];


private static (IConsume consumer, MethodInfo methodInfo) Memoize
(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Blumchen.Serialization;
using Blumchen.Subscriptions.Management;
using Blumchen.Subscriptions.Replication;
using JetBrains.Annotations;
using PostgresOutbox.Serialization;
using PostgresOutbox.Subscriptions.Management;
using PostgresOutbox.Subscriptions.Replication;

namespace PostgresOutbox.Subscriptions;
namespace Blumchen.Subscriptions;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

public sealed class SubscriptionOptionsBuilder
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Collections;
using Blumchen.Serialization;
using Npgsql;
using PostgresOutbox.Serialization;

namespace PostgresOutbox.Table;
namespace Blumchen.Table;
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member

public static class MessageAppender
{
Expand Down Expand Up @@ -51,7 +52,7 @@ string tableName
var batchCommand = batch.CreateBatchCommand();
var data = JsonSerialization.ToJson(input, jsonTypeInfo);


batchCommand.CommandText =
$"INSERT INTO {tableName}(message_type, data) values ('{typeName}', '{data}')";
batch.BatchCommands.Add(batchCommand);
Expand Down
2 changes: 1 addition & 1 deletion src/Commons/Commons.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\PostgresOutbox\PostgresOutbox.csproj" />
<ProjectReference Include="..\Blumchen\Blumchen.csproj" />
</ItemGroup>

</Project>
Loading
Loading