44using System . Text ;
55using System . Threading ;
66using System . Threading . Tasks ;
7+ using MySql . Data . MySqlClient ;
78using Npgsql ;
89using Serilog ;
910using SqlStreamStore . Infrastructure ;
@@ -21,14 +22,16 @@ private delegate Task<IStreamStore> CreateStreamStore(
2122
2223 private const string postgres = nameof ( postgres ) ;
2324 private const string mssql = nameof ( mssql ) ;
25+ private const string mysql = nameof ( mysql ) ;
2426 private const string inmemory = nameof ( inmemory ) ;
2527
2628 private static readonly IDictionary < string , CreateStreamStore > s_factories
2729 = new Dictionary < string , CreateStreamStore >
2830 {
2931 [ inmemory ] = CreateInMemoryStreamStore ,
3032 [ postgres ] = CreatePostgresStreamStore ,
31- [ mssql ] = CreateMssqlStreamStore
33+ [ mssql ] = CreateMssqlStreamStore ,
34+ [ mysql ] = CreateMySqlStreamStore
3235 } ;
3336
3437 public SqlStreamStoreFactory ( SqlStreamStoreServerConfiguration configuration )
@@ -110,6 +113,44 @@ CREATE DATABASE [{connectionStringBuilder.InitialCatalog}]
110113 return streamStore ;
111114 }
112115
116+ private static async Task < IStreamStore > CreateMySqlStreamStore (
117+ string connectionString ,
118+ string _ ,
119+ CancellationToken cancellationToken )
120+ {
121+ var connectionStringBuilder = new MySqlConnectionStringBuilder ( connectionString ) ;
122+ var settings = new MySqlStreamStoreSettings ( connectionString ) ;
123+
124+ var streamStore = new MySqlStreamStore ( settings ) ;
125+
126+ try
127+ {
128+ using ( var connection = new MySqlConnection ( new MySqlConnectionStringBuilder ( connectionString )
129+ {
130+ Database = null
131+ } . ConnectionString ) )
132+ {
133+ await connection . OpenAsync ( cancellationToken ) . NotOnCapturedContext ( ) ;
134+
135+ using ( var command = new MySqlCommand (
136+ $ "CREATE DATABASE IF NOT EXISTS `{ connectionStringBuilder . Database } `",
137+ connection ) )
138+ {
139+ await command . ExecuteNonQueryAsync ( cancellationToken ) . NotOnCapturedContext ( ) ;
140+ }
141+ }
142+
143+ await streamStore . CreateSchemaIfNotExists ( cancellationToken ) ;
144+ }
145+ catch ( SqlException ex )
146+ {
147+ SchemaCreationFailed ( streamStore . GetSchemaCreationScript , ex ) ;
148+ throw ;
149+ }
150+
151+ return streamStore ;
152+ }
153+
113154 private static async Task < IStreamStore > CreatePostgresStreamStore (
114155 string connectionString ,
115156 string schema ,
0 commit comments