private static void ConfigurePgSqlStreamStore(Config config) { var connectionString = "Host=localhost;Port=5432;User Id=postgres;Database=postgres"; var settings = new PostgresStreamStoreSettings(connectionString); var pgStreamStore = new PostgresStreamStore(settings); config.Container.Register <IStreamStore>(pgStreamStore); pgStreamStore.CreateSchemaIfNotExists().GetAwaiter().GetResult(); }
private void ConfigurePostgres() { _connection = new NpgsqlConnection(PgsqlConnectionString); var settings = new PostgresStreamStoreSettings(PgsqlConnectionString); settings.Schema = "randy"; SqlStreamStoreProvider.InitializePgSqlStreamStore(settings); _streamStore = new PostgresStreamStore(settings); }
public async Task <PostgresStreamStore> GetUninitializedPostgresStreamStore(bool scavengeAsynchronously = false) { await CreateDatabase(); var settings = new PostgresStreamStoreSettings(ConnectionString) { Schema = _schema, ScavengeAsynchronously = scavengeAsynchronously }; return(new PostgresStreamStore(settings)); }
public PostgresStreamStore CreatePostgresStreamStore() { var settings = new PostgresStreamStoreSettings(_configuration.ConnectionString) { DisableDeletionTracking = _configuration.DisableDeletionTracking }; if (_configuration.Schema != null) { settings.Schema = _configuration.Schema; } return(new PostgresStreamStore(settings)); }
private static async Task <IStreamStore> CreatePostgresStreamStore( string connectionString, string schema, CancellationToken cancellationToken) { var connectionStringBuilder = new NpgsqlConnectionStringBuilder(connectionString); using (var connection = new NpgsqlConnection(new NpgsqlConnectionStringBuilder(connectionString) { Database = null }.ConnectionString)) { bool exists; await connection.OpenAsync(cancellationToken).ConfigureAwait(false); using (var command = new NpgsqlCommand( $"SELECT 1 FROM pg_database WHERE datname = '{connectionStringBuilder.Database}'", connection)) { exists = await command.ExecuteScalarAsync(cancellationToken).ConfigureAwait(false) != null; } if (!exists) { using (var command = new NpgsqlCommand( $"CREATE DATABASE {connectionStringBuilder.Database}", connection)) { await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false); } } var settings = new PostgresStreamStoreSettings(connectionString); if (schema != null) { settings.Schema = schema; } var streamStore = new PostgresStreamStore(settings); await streamStore.CreateSchemaIfNotExists(cancellationToken); return(streamStore); } }
public static void AddEventStore(this IServiceCollection services, string connectionString) { if (services == null) { throw new ArgumentNullException(nameof(services)); } if (string.IsNullOrWhiteSpace(connectionString)) { throw new ArgumentException(nameof(connectionString)); } var settings = new PostgresStreamStoreSettings(connectionString); services.AddScoped <IDbConnection>(ctx => new NpgsqlConnection(connectionString)); services.AddSingleton <PostgresStreamStore>(ctx => new PostgresStreamStore(settings)); services.AddSingleton <IStreamStore>(ctx => ctx.GetService <PostgresStreamStore>()); }
/// <summary> /// Initialize a postgres database for use as a Memstate SqlStreamStore backend /// <remarks>You must use this method to initialize the database objects. /// The normal SqlStreamStore schema uses the JSONB datatype</remarks> /// </summary> /// <param name="settings">A settings object with a valid connection string</param> public static void InitializePgSqlStreamStore(PostgresStreamStoreSettings settings) { var store = new PostgresStreamStore(settings); var originalScript = store.GetSchemaCreationScript(); var sb = new StringBuilder("CREATE SCHEMA IF NOT EXISTS "); sb.AppendLine(settings.Schema + ";"); sb.Append(originalScript); var script = sb.ToString().Replace("JSONB", "JSON"); using (var connection = new NpgsqlConnection(settings.ConnectionString)) { connection.Open(); var cmd = connection.CreateCommand(); cmd.CommandText = script; cmd.ExecuteNonQuery(); connection.Close(); } }
private int OnExecute(CommandLineApplication app) { if (string.IsNullOrEmpty(SQLDialect) || string.IsNullOrEmpty(Output)) { app.ShowHelp(); return(0); } var exitCode = 0; switch (SQLDialect.ToLowerInvariant()) { case "mssqlv2": #pragma warning disable 618 var mssqlV2Settings = new MsSqlStreamStoreSettings(new SqlConnectionStringBuilder { DataSource = "tcp:0.0.0.0,1433" }.ConnectionString); if (!string.IsNullOrEmpty(Schema)) { mssqlV2Settings.Schema = Schema; } if (CreateSchema) { var script = string.Join( Environment.NewLine, $@"IF NOT EXISTS ( SELECT schema_name FROM information_schema.schemata WHERE schema_name = '{Schema}' ) BEGIN EXEC sp_executesql N'CREATE SCHEMA {Schema}' END", new MsSqlStreamStore(mssqlV2Settings).GetSchemaCreationScript()); File.WriteAllText(Output, script); } else { File.WriteAllText(Output, new MsSqlStreamStore(mssqlV2Settings).GetSchemaCreationScript()); } #pragma warning restore 618 break; case "mssqlv3": var mssqlV3Settings = new MsSqlStreamStoreV3Settings(new SqlConnectionStringBuilder { DataSource = "tcp:0.0.0.0,1433" }.ConnectionString); if (!string.IsNullOrEmpty(Schema)) { mssqlV3Settings.Schema = Schema; } if (CreateSchema) { var script = string.Join( Environment.NewLine, $@"IF NOT EXISTS ( SELECT schema_name FROM information_schema.schemata WHERE schema_name = '{Schema}' ) BEGIN EXEC sp_executesql N'CREATE SCHEMA {Schema}' END", new MsSqlStreamStoreV3(mssqlV3Settings).GetSchemaCreationScript()); File.WriteAllText(Output, script); } else { File.WriteAllText(Output, new MsSqlStreamStoreV3(mssqlV3Settings).GetSchemaCreationScript()); } break; case "mysql": var mysqlSettings = new MySqlStreamStoreSettings(new MySqlConnectionStringBuilder { Server = "0.0.0.0" }.ConnectionString); if (!string.IsNullOrEmpty(Schema)) { Log.Information("The optional database schema does not apply to the mysql dialect and can be omitted: {Schema}", Schema); } File.WriteAllText(Output, new MySqlStreamStore(mysqlSettings).GetSchemaCreationScript()); break; case "postgres": var postgresSettings = new PostgresStreamStoreSettings(new NpgsqlConnectionStringBuilder { Host = "0.0.0.0" }.ConnectionString); if (!string.IsNullOrEmpty(Schema)) { postgresSettings.Schema = Schema; } if (CreateSchema) { var script = string.Join( Environment.NewLine, $"CREATE SCHEMA IF NOT EXISTS {Schema};", new PostgresStreamStore(postgresSettings).GetSchemaCreationScript() ); File.WriteAllText(Output, script); } else { File.WriteAllText(Output, new PostgresStreamStore(postgresSettings).GetSchemaCreationScript()); } break; default: Log.Error("The SQL dialect was not recognized: {SQLDialect}", SQLDialect); exitCode = 1; break; } return(exitCode); }