Skip to content

Commit 6893233

Browse files
authored
Add missing migration parameters to Akka.Hosting extension method (#225)
1 parent 35eca99 commit 6893233

File tree

4 files changed

+137
-11
lines changed

4 files changed

+137
-11
lines changed

src/Akka.Persistence.Sql.Hosting/DatabaseMapping.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public static string Name(this DatabaseMapping map)
3030
_ => throw new Exception($"Unknown DatabaseMapping: {map}")
3131
};
3232

33-
public static JournalDatabaseOptions DatabaseOption(DatabaseMapping map)
33+
public static JournalDatabaseOptions JournalOption(this DatabaseMapping map)
3434
=> map switch
3535
{
3636
DatabaseMapping.Default => JournalDatabaseOptions.Default,
@@ -40,5 +40,16 @@ public static JournalDatabaseOptions DatabaseOption(DatabaseMapping map)
4040
DatabaseMapping.MySql => JournalDatabaseOptions.MySql,
4141
_ => throw new Exception($"Unknown DatabaseMapping: {map}")
4242
};
43+
44+
public static SnapshotDatabaseOptions SnapshotOption(this DatabaseMapping map)
45+
=> map switch
46+
{
47+
DatabaseMapping.Default => SnapshotDatabaseOptions.Default,
48+
DatabaseMapping.SqlServer => SnapshotDatabaseOptions.SqlServer,
49+
DatabaseMapping.Sqlite => SnapshotDatabaseOptions.Sqlite,
50+
DatabaseMapping.PostgreSql => SnapshotDatabaseOptions.PostgreSql,
51+
DatabaseMapping.MySql => SnapshotDatabaseOptions.MySql,
52+
_ => throw new Exception($"Unknown DatabaseMapping: {map}")
53+
};
4354
}
4455
}

src/Akka.Persistence.Sql.Hosting/HostingExtensions.cs

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using Akka.Actor;
99
using Akka.Hosting;
1010
using Akka.Persistence.Hosting;
11+
using Akka.Persistence.Sql.Config;
12+
using Akka.Persistence.Sql.Hosting;
1113

1214
namespace Akka.Persistence.Sql.Hosting
1315
{
@@ -65,11 +67,70 @@ public static class HostingExtensions
6567
/// </para>
6668
/// <b>Default</b>: <c>true</c>
6769
/// </param>
70+
/// <param name="journalDatabaseMapping">
71+
/// <para>
72+
/// The database options to modify database table column mapping for this journal.
73+
/// </para>
74+
/// <b>NOTE</b>: This is used primarily for backward compatibility,
75+
/// you leave this empty for greenfield projects.
76+
/// </param>
77+
/// <param name="databaseMapping">
78+
/// <para>
79+
/// The database options to modify database table column mapping for this journal.
80+
/// </para>
81+
/// <b>NOTE</b>: This is used primarily for backward compatibility,
82+
/// you leave this empty for greenfield projects.
83+
/// </param>
84+
/// <param name="tagStorageMode">
85+
/// <para>
86+
/// Describe how tags are being stored inside the database. Setting this to
87+
/// <see cref="TagMode.Csv"/> will store the tags as a comma delimited value
88+
/// in a column named <c>tags</c> inside the event journal. Setting this to
89+
/// <see cref="TagMode.TagTable"/> will store the tags inside a separate
90+
/// tag table instead.
91+
/// </para>
92+
/// <b>NOTE</b>: This is used primarily for backward compatibility,
93+
/// you leave this empty for greenfield projects.
94+
/// </param>
95+
/// <param name="deleteCompatibilityMode">
96+
/// <para>
97+
/// If true, journal_metadata is created and used for deletes
98+
/// and max sequence number queries.
99+
/// </para>
100+
/// <b>NOTE</b>: This is used primarily for backward compatibility,
101+
/// you leave this empty for greenfield projects.
102+
/// </param>
103+
/// <param name="useWriterUuidColumn">
104+
/// <para>
105+
/// A flag to indicate if the writer_uuid column should be generated and be populated in run-time.
106+
/// </para>
107+
/// <b>Notes:</b>
108+
/// <list type="number">
109+
/// <item>
110+
/// The column will only be generated if auto-initialize is set to true.
111+
/// </item>
112+
/// <item>
113+
/// This feature is Akka.Persistence.Sql specific, setting this to true will break
114+
/// backward compatibility with databases generated by other Akka.Persistence plugins.
115+
/// </item>
116+
/// <item>
117+
/// <para>
118+
/// To make this feature work with legacy plugins, you will have to alter the old
119+
/// journal table:
120+
/// </para>
121+
/// <c>ALTER TABLE [journal_table_name] ADD [writer_uuid_column_name] VARCHAR(128);</c>
122+
/// </item>
123+
/// <item>
124+
/// If set to true, the code will not check for backward compatibility. It will expect
125+
/// that the `writer-uuid` column to be present inside the journal table.
126+
/// </item>
127+
/// </list>
128+
/// </param>
68129
/// <returns>
69130
/// The same <see cref="AkkaConfigurationBuilder"/> instance originally passed in.
70131
/// </returns>
71132
/// <exception cref="ArgumentOutOfRangeException">
72-
/// Thrown when <see cref="journalBuilder"/> is set and <see cref="mode"/> is set to
133+
/// Thrown when <paramref name="journalBuilder"/> is set and <paramref name="mode"/> is set to
73134
/// <see cref="PersistenceMode.SnapshotStore"/>
74135
/// </exception>
75136
public static AkkaConfigurationBuilder WithSqlPersistence(
@@ -81,7 +142,11 @@ public static AkkaConfigurationBuilder WithSqlPersistence(
81142
Action<AkkaPersistenceJournalBuilder>? journalBuilder = null,
82143
bool autoInitialize = true,
83144
string pluginIdentifier = "sql",
84-
bool isDefaultPlugin = true)
145+
bool isDefaultPlugin = true,
146+
DatabaseMapping? databaseMapping = null,
147+
TagMode? tagStorageMode = null,
148+
bool? deleteCompatibilityMode = null,
149+
bool? useWriterUuidColumn = null)
85150
{
86151
if (mode == PersistenceMode.SnapshotStore && journalBuilder is { })
87152
throw new Exception($"{nameof(journalBuilder)} can only be set when {nameof(mode)} is set to either {PersistenceMode.Both} or {PersistenceMode.Journal}");
@@ -97,14 +162,24 @@ public static AkkaConfigurationBuilder WithSqlPersistence(
97162
ConnectionString = connectionString,
98163
ProviderName = providerName,
99164
AutoInitialize = autoInitialize,
165+
TagStorageMode = tagStorageMode,
166+
DeleteCompatibilityMode = deleteCompatibilityMode,
100167
};
101168

169+
if (databaseMapping is { })
170+
journalOpt.DatabaseOptions = databaseMapping.Value.JournalOption();
171+
102172
if (schemaName is { })
103173
{
104-
journalOpt.DatabaseOptions = new JournalDatabaseOptions(DatabaseMapping.Default)
105-
{
106-
SchemaName = schemaName
107-
};
174+
journalOpt.DatabaseOptions ??= JournalDatabaseOptions.Default;
175+
journalOpt.DatabaseOptions.SchemaName = schemaName;
176+
}
177+
178+
if (useWriterUuidColumn is { })
179+
{
180+
journalOpt.DatabaseOptions ??= JournalDatabaseOptions.Default;
181+
journalOpt.DatabaseOptions.JournalTable ??= JournalTableOptions.Default;
182+
journalOpt.DatabaseOptions.JournalTable.UseWriterUuidColumn = useWriterUuidColumn;
108183
}
109184

110185
var adapters = new AkkaPersistenceJournalBuilder(journalOpt.Identifier, builder);
@@ -118,12 +193,13 @@ public static AkkaConfigurationBuilder WithSqlPersistence(
118193
AutoInitialize = autoInitialize,
119194
};
120195

196+
if (databaseMapping is { })
197+
snapshotOpt.DatabaseOptions = databaseMapping.Value.SnapshotOption();
198+
121199
if (schemaName is { })
122200
{
123-
snapshotOpt.DatabaseOptions = new SnapshotDatabaseOptions(DatabaseMapping.Default)
124-
{
125-
SchemaName = schemaName
126-
};
201+
snapshotOpt.DatabaseOptions ??= new SnapshotDatabaseOptions(DatabaseMapping.Default);
202+
snapshotOpt.DatabaseOptions.SchemaName = schemaName;
127203
}
128204

129205
return mode switch

src/Akka.Persistence.Sql.Hosting/JournalTableOptions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,32 @@ public sealed class JournalTableOptions
9292
ManifestColumnName = "manifest",
9393
};
9494

95+
/// <summary>
96+
/// <para>
97+
/// A flag to indicate if the writer_uuid column should be generated and be populated in run-time.
98+
/// </para>
99+
/// <b>Notes:</b>
100+
/// <list type="number">
101+
/// <item>
102+
/// The column will only be generated if auto-initialize is set to true.
103+
/// </item>
104+
/// <item>
105+
/// This feature is Akka.Persistence.Sql specific, setting this to true will break
106+
/// backward compatibility with databases generated by other Akka.Persistence plugins.
107+
/// </item>
108+
/// <item>
109+
/// <para>
110+
/// To make this feature work with legacy plugins, you will have to alter the old
111+
/// journal table:
112+
/// </para>
113+
/// <c>ALTER TABLE [journal_table_name] ADD [writer_uuid_column_name] VARCHAR(128);</c>
114+
/// </item>
115+
/// <item>
116+
/// If set to true, the code will not check for backward compatibility. It will expect
117+
/// that the `writer-uuid` column to be present inside the journal table.
118+
/// </item>
119+
/// </list>
120+
/// </summary>
95121
public bool? UseWriterUuidColumn { get; set; }
96122
public string? TableName { get; set; }
97123

src/Akka.Persistence.Sql.Hosting/SqlJournalOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public SqlJournalOptions(bool isDefaultPlugin, string identifier = "sql") : base
8181
/// </summary>
8282
public TagMode? TagStorageMode { get; set; }
8383

84+
/// <summary>
85+
/// <para>
86+
/// If true, journal_metadata is created and used for deletes
87+
/// and max sequence number queries.
88+
/// </para>
89+
/// <b>NOTE</b>: This is used primarily for backward compatibility,
90+
/// you leave this empty for greenfield projects.
91+
/// </summary>
92+
public bool? DeleteCompatibilityMode { get; set; }
93+
8494
protected override Configuration.Config InternalDefaultConfig => Default;
8595

8696
protected override StringBuilder Build(StringBuilder sb)
@@ -94,6 +104,9 @@ protected override StringBuilder Build(StringBuilder sb)
94104
sb.AppendLine($"connection-string = {ConnectionString.ToHocon()}");
95105
sb.AppendLine($"provider-name = {ProviderName.ToHocon()}");
96106

107+
if (DeleteCompatibilityMode is { })
108+
sb.AppendLine($"delete-compatibility-mode = {DeleteCompatibilityMode.ToHocon()}");
109+
97110
if (TagStorageMode is { })
98111
sb.AppendLine($"tag-write-mode = {TagStorageMode.ToString().ToHocon()}");
99112

0 commit comments

Comments
 (0)