AppDbUpdater: Actually check foreign key violations after updating
This commit is contained in:
@ -11,6 +11,7 @@ using System.Text.RegularExpressions;
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Elwig.Models.Dtos;
|
using Elwig.Models.Dtos;
|
||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Data;
|
||||||
|
|
||||||
namespace Elwig.Helpers {
|
namespace Elwig.Helpers {
|
||||||
|
|
||||||
@ -123,6 +124,21 @@ namespace Elwig.Helpers {
|
|||||||
return await cmd.ExecuteScalarAsync();
|
return await cmd.ExecuteScalarAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static async Task<(string Table, long RowId, string Parent, long FkId)[]> ForeignKeyCheck(SqliteConnection cnx) {
|
||||||
|
using var cmd = cnx.CreateCommand();
|
||||||
|
cmd.CommandText = "PRAGMA foreign_key_check";
|
||||||
|
using var reader = await cmd.ExecuteReaderAsync();
|
||||||
|
var list = new List<(string, long, string, long)>();
|
||||||
|
while (await reader.ReadAsync()) {
|
||||||
|
var table = reader.GetString(0);
|
||||||
|
var rowid = reader.GetInt64(1);
|
||||||
|
var parent = reader.GetString(2);
|
||||||
|
var fkid = reader.GetInt64(3);
|
||||||
|
list.Add((table, rowid, parent, fkid));
|
||||||
|
}
|
||||||
|
return [.. list];
|
||||||
|
}
|
||||||
|
|
||||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
|
||||||
optionsBuilder.UseSqlite(ConnectionString);
|
optionsBuilder.UseSqlite(ConnectionString);
|
||||||
optionsBuilder.UseLazyLoadingProxies();
|
optionsBuilder.UseLazyLoadingProxies();
|
||||||
|
@ -79,8 +79,13 @@ namespace Elwig.Helpers {
|
|||||||
foreach (var script in toExecute) {
|
foreach (var script in toExecute) {
|
||||||
await AppDbContext.ExecuteEmbeddedScript(cnx, asm, script);
|
await AppDbContext.ExecuteEmbeddedScript(cnx, asm, script);
|
||||||
}
|
}
|
||||||
|
var violations = await AppDbContext.ForeignKeyCheck(cnx);
|
||||||
|
if (violations.Length > 0) {
|
||||||
|
throw new Exception($"Foreign key violations ({violations.Length}):\n" + string.Join("\n", violations
|
||||||
|
.Select(v => $"{v.Table} - {v.RowId} - {v.Parent} - {v.FkId}")));
|
||||||
|
}
|
||||||
|
|
||||||
await AppDbContext.ExecuteBatch(cnx, $"""
|
await AppDbContext.ExecuteBatch(cnx, $"""
|
||||||
PRAGMA foreign_key_check;
|
|
||||||
COMMIT;
|
COMMIT;
|
||||||
PRAGMA foreign_keys = ON;
|
PRAGMA foreign_keys = ON;
|
||||||
VACUUM;
|
VACUUM;
|
||||||
|
@ -84,6 +84,7 @@ UPDATE area_commitment SET cultid = NULL WHERE cultid = 'N';
|
|||||||
DELETE FROM wine_cultivation WHERE cultid = 'N';
|
DELETE FROM wine_cultivation WHERE cultid = 'N';
|
||||||
UPDATE wine_cultivation SET cultid = 'B', name = 'Bio', description = 'AT-BIO-302' WHERE cultid = 'BIO';
|
UPDATE wine_cultivation SET cultid = 'B', name = 'Bio', description = 'AT-BIO-302' WHERE cultid = 'BIO';
|
||||||
UPDATE wine_cultivation SET description = 'Kontrollierte Integrierte Produktion' WHERE cultid = 'KIP';
|
UPDATE wine_cultivation SET description = 'Kontrollierte Integrierte Produktion' WHERE cultid = 'KIP';
|
||||||
|
UPDATE area_commitment SET cultid = 'B' WHERE cultid = 'BIO';
|
||||||
|
|
||||||
UPDATE area_commitment SET cultid = 'B', vtrgid = SUBSTR(vtrgid, 1, 2) WHERE vtrgid LIKE '__B';
|
UPDATE area_commitment SET cultid = 'B', vtrgid = SUBSTR(vtrgid, 1, 2) WHERE vtrgid LIKE '__B';
|
||||||
UPDATE area_commitment SET cultid = 'B' WHERE vtrgid LIKE '__HU';
|
UPDATE area_commitment SET cultid = 'B' WHERE vtrgid LIKE '__HU';
|
||||||
|
Reference in New Issue
Block a user