11using System ;
22using System . Collections . Generic ;
33using System . Data . SqlClient ;
4+ using System . Text ;
45using System . Threading ;
56using System . Threading . Tasks ;
67using Npgsql ;
@@ -32,10 +33,11 @@ private static readonly IDictionary<string, CreateStreamStore> s_factories
3233
3334 public SqlStreamStoreFactory ( SqlStreamStoreServerConfiguration configuration )
3435 {
35- if ( configuration == null )
36+ if ( configuration == null )
3637 {
3738 throw new ArgumentNullException ( nameof ( configuration ) ) ;
3839 }
40+
3941 _configuration = configuration ;
4042 }
4143
@@ -46,7 +48,7 @@ public Task<IStreamStore> Create(CancellationToken cancellationToken = default)
4648
4749 Log . Information ( $ "Creating stream store for provider '{ provider } '") ;
4850
49- if ( ! s_factories . TryGetValue ( provider , out var factory ) )
51+ if ( ! s_factories . TryGetValue ( provider , out var factory ) )
5052 {
5153 throw new InvalidOperationException ( $ "No provider factory for provider '{ provider } ' found.") ;
5254 }
@@ -69,14 +71,14 @@ private static async Task<IStreamStore> CreateMssqlStreamStore(
6971 CancellationToken cancellationToken )
7072 {
7173 var connectionStringBuilder = new SqlConnectionStringBuilder ( connectionString ) ;
72- using ( var connection = new SqlConnection ( new SqlConnectionStringBuilder ( connectionString )
74+ using ( var connection = new SqlConnection ( new SqlConnectionStringBuilder ( connectionString )
7375 {
7476 InitialCatalog = "master"
7577 } . ConnectionString ) )
7678 {
7779 await connection . OpenAsync ( cancellationToken ) . NotOnCapturedContext ( ) ;
7880
79- using ( var command = new SqlCommand (
81+ using ( var command = new SqlCommand (
8082 $@ "
8183IF NOT EXISTS (SELECT name FROM sys.databases WHERE name = N'{ connectionStringBuilder . InitialCatalog } ')
8284BEGIN
@@ -91,14 +93,21 @@ CREATE DATABASE [{connectionStringBuilder.InitialCatalog}]
9193
9294 var settings = new MsSqlStreamStoreV3Settings ( connectionString ) ;
9395
94- if ( schema != null )
96+ if ( schema != null )
9597 {
9698 settings . Schema = schema ;
9799 }
98100
99101 var streamStore = new MsSqlStreamStoreV3 ( settings ) ;
100102
101- await streamStore . CreateSchemaIfNotExists ( cancellationToken ) ;
103+ try
104+ {
105+ await streamStore . CreateSchemaIfNotExists ( cancellationToken ) ;
106+ }
107+ catch ( SqlException ex )
108+ {
109+ SchemaCreationFailed ( streamStore . GetSchemaCreationScript , ex ) ;
110+ }
102111
103112 return streamStore ;
104113 }
@@ -110,25 +119,25 @@ private static async Task<IStreamStore> CreatePostgresStreamStore(
110119 {
111120 var connectionStringBuilder = new NpgsqlConnectionStringBuilder ( connectionString ) ;
112121
113- using ( var connection = new NpgsqlConnection ( new NpgsqlConnectionStringBuilder ( connectionString )
122+ using ( var connection = new NpgsqlConnection ( new NpgsqlConnectionStringBuilder ( connectionString )
114123 {
115124 Database = null
116125 } . ConnectionString ) )
117126 {
118127 bool exists ;
119128 await connection . OpenAsync ( cancellationToken ) . NotOnCapturedContext ( ) ;
120129
121- using ( var command = new NpgsqlCommand (
130+ using ( var command = new NpgsqlCommand (
122131 $ "SELECT 1 FROM pg_database WHERE datname = '{ connectionStringBuilder . Database } '",
123132 connection ) )
124133 {
125134 exists = await command . ExecuteScalarAsync ( cancellationToken ) . NotOnCapturedContext ( )
126135 != null ;
127136 }
128137
129- if ( ! exists )
138+ if ( ! exists )
130139 {
131- using ( var command = new NpgsqlCommand (
140+ using ( var command = new NpgsqlCommand (
132141 $ "CREATE DATABASE { connectionStringBuilder . Database } ",
133142 connection ) )
134143 {
@@ -138,17 +147,36 @@ private static async Task<IStreamStore> CreatePostgresStreamStore(
138147
139148 var settings = new PostgresStreamStoreSettings ( connectionString ) ;
140149
141- if ( schema != null )
150+ if ( schema != null )
142151 {
143152 settings . Schema = schema ;
144153 }
145154
146155 var streamStore = new PostgresStreamStore ( settings ) ;
147156
148- await streamStore . CreateSchemaIfNotExists ( cancellationToken ) ;
157+ try
158+ {
159+ await streamStore . CreateSchemaIfNotExists ( cancellationToken ) ;
160+ }
161+ catch ( NpgsqlException ex )
162+ {
163+ SchemaCreationFailed ( streamStore . GetSchemaCreationScript , ex ) ;
164+ }
149165
150166 return streamStore ;
151167 }
152168 }
169+
170+ private static void SchemaCreationFailed ( Func < string > getSchemaCreationScript , Exception ex )
171+ => Log . Warning (
172+ new StringBuilder ( )
173+ . Append ( $ "Could not create schema: { ex . Message } ")
174+ . AppendLine ( )
175+ . Append (
176+ "Does your connection string have enough permissions? If not, run the following sql script as a privileged user:" )
177+ . AppendLine ( )
178+ . Append ( getSchemaCreationScript )
179+ . ToString ( ) ,
180+ ex ) ;
153181 }
154- }
182+ }
0 commit comments