@@ -12,30 +12,34 @@ public static async Task CleanupWriteTableAsync(string connectionString)
1212 await connection . OpenAsync ( ) ;
1313 using var cmd = new SqliteCommand ( "DROP TABLE IF EXISTS order_items" , connection ) ;
1414 await cmd . ExecuteNonQueryAsync ( ) ;
15- var schemaPath = Path . Combine ( AppContext . BaseDirectory , "schema.sql" ) ;
16- if ( File . Exists ( schemaPath ) )
17- {
18- await CreateSchemaAsync ( connectionString , schemaPath ) ;
19- }
15+ await InitializeDatabaseAsync ( connectionString ) ;
2016 }
2117
2218 public static async Task InitializeDatabaseAsync ( string connectionString )
2319 {
2420 var schemaPath = Path . Combine ( AppContext . BaseDirectory , "schema.sql" ) ;
25- if ( ! File . Exists ( schemaPath ) )
26- {
27- throw new FileNotFoundException ( $ "Schema file not found at { schemaPath } . Make sure schema.sql is copied to the output directory.") ;
28- }
29- await CreateSchemaAsync ( connectionString , schemaPath ) ;
21+ if ( File . Exists ( schemaPath ) )
22+ await CreateSchemaAsync ( connectionString , schemaPath ) ;
3023 }
3124
3225 private static async Task CreateSchemaAsync ( string connectionString , string schemaPath )
3326 {
3427 var schemaSql = await File . ReadAllTextAsync ( schemaPath ) ;
3528 using var connection = new SqliteConnection ( connectionString ) ;
3629 await connection . OpenAsync ( ) ;
37- using var command = new SqliteCommand ( schemaSql , connection ) ;
38- await command . ExecuteNonQueryAsync ( ) ;
30+
31+ // Split the SQL by semicolons and execute each statement separately
32+ // SQLite doesn't handle multiple statements in a single ExecuteNonQueryAsync call
33+ var statements = schemaSql
34+ . Split ( ';' , StringSplitOptions . RemoveEmptyEntries | StringSplitOptions . TrimEntries )
35+ . Where ( s => ! string . IsNullOrWhiteSpace ( s ) )
36+ . ToArray ( ) ;
37+
38+ foreach ( var statement in statements )
39+ {
40+ using var command = new SqliteCommand ( statement , connection ) ;
41+ await command . ExecuteNonQueryAsync ( ) ;
42+ }
3943 }
4044
4145 public static void CleanupDatabase ( string connectionString )
0 commit comments