AppDbContext: Add ExecuteEmbeddedScript()
This commit is contained in:
@ -10,6 +10,7 @@ using Microsoft.Data.Sqlite;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Collections.Generic;
|
||||
using Elwig.Models.Dtos;
|
||||
using System.Reflection;
|
||||
|
||||
namespace Elwig.Helpers {
|
||||
|
||||
@ -106,6 +107,12 @@ namespace Elwig.Helpers {
|
||||
await (await cmd.ExecuteReaderAsync()).CloseAsync();
|
||||
}
|
||||
|
||||
public static async Task ExecuteEmbeddedScript(SqliteConnection cnx, Assembly asm, string name) {
|
||||
using var stream = asm.GetManifestResourceStream(name) ?? throw new FileNotFoundException("Unable to load embedded resource");
|
||||
using var reader = new StreamReader(stream);
|
||||
await ExecuteBatch(cnx, await reader.ReadToEndAsync());
|
||||
}
|
||||
|
||||
public static async Task<object?> ExecuteScalar(SqliteConnection cnx, string sql) {
|
||||
using var cmd = cnx.CreateCommand();
|
||||
cmd.CommandText = sql;
|
||||
|
@ -1,7 +1,6 @@
|
||||
using Microsoft.Data.Sqlite;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
@ -44,7 +43,7 @@ namespace Elwig.Helpers {
|
||||
|
||||
private static async Task UpdateDbSchema(SqliteConnection cnx, int fromVersion, int toVersion) {
|
||||
if (fromVersion == toVersion) {
|
||||
//return;
|
||||
return;
|
||||
} else if (fromVersion > toVersion) {
|
||||
throw new Exception("schema_version of database is too new");
|
||||
} else if (fromVersion <= 0) {
|
||||
@ -77,9 +76,7 @@ namespace Elwig.Helpers {
|
||||
BEGIN EXCLUSIVE;
|
||||
""");
|
||||
foreach (var script in toExecute) {
|
||||
using var stream = asm.GetManifestResourceStream(script) ?? throw new Exception("Unable to load embedded resource");
|
||||
using var reader = new StreamReader(stream);
|
||||
await AppDbContext.ExecuteBatch(cnx, await reader.ReadToEndAsync());
|
||||
await AppDbContext.ExecuteEmbeddedScript(cnx, asm, script);
|
||||
}
|
||||
await AppDbContext.ExecuteBatch(cnx, $"""
|
||||
PRAGMA foreign_key_check;
|
||||
|
Reference in New Issue
Block a user