04e3969071
AUD-5: PurgeChannelOlderThanAsync binds @channel as varchar(32) and the model gains IX_AuditLog_Channel_Occurred (Category, OccurredAtUtc). The migration + partition-aligned SQL script land in the WP's final migration commit (AuditLogChannelOccurredIndex). sqlcmd -I, SQL_Latin1_General_CP1_CI_AS, scratch DB with 1M AuditLog rows (6 channels, clustered (OccurredAtUtc, EventId), ScriptTrace override 30 d), steady-state per-channel tick (nothing left to purge): before (nvarchar, old indexes): 5,858 logical reads, 124 ms after (varchar + new index): 6 logical reads, 0 ms new index but nvarchar param: 4,747 logical reads, 114 ms (binding matters) SEL-1: site_events idx_events_instance -> idx_events_instance_timestamp (instance_id, timestamp), dropped/rebuilt in place by SiteEventLogSchema.Apply like #123; the storage-cap size model now charges 4 x timestamp per row. sqlite3, 2.7M rows / 20 instances, instance-filtered 501-row page: before: SEARCH idx_events_instance + USE TEMP B-TREE FOR ORDER BY; 396 ms after: SEARCH idx_events_instance_timestamp (temp b-tree for id tiebreak only); 0.7 ms
140 lines
5.0 KiB
C#
140 lines
5.0 KiB
C#
using Microsoft.Data.Sqlite;
|
|
|
|
namespace ZB.MOM.WW.ScadaBridge.SiteEventLogging.Tests;
|
|
|
|
/// <summary>
|
|
/// The <c>site_events</c> DDL is extracted out of
|
|
/// <see cref="SiteEventLogger"/> so the Host's <c>AddZbLocalDb</c> onReady callback can
|
|
/// create it in the consolidated site database.
|
|
/// <para>
|
|
/// The extraction also carries the one deliberate schema CHANGE of Phase 1: the primary
|
|
/// key moves from <c>INTEGER PRIMARY KEY AUTOINCREMENT</c> to a TEXT GUID. Under the
|
|
/// library's last-writer-wins replication, two site nodes each minting autoincrement id 1
|
|
/// would be treated as the same row and silently overwrite one another. GUID keys make
|
|
/// the event log a pure union across the pair instead.
|
|
/// </para>
|
|
/// </summary>
|
|
public class SiteEventLogSchemaTests
|
|
{
|
|
private static SqliteConnection OpenConnection()
|
|
{
|
|
var conn = new SqliteConnection("Data Source=:memory:");
|
|
conn.Open();
|
|
return conn;
|
|
}
|
|
|
|
private static List<(string Name, string Type, bool NotNull, bool Pk)> TableInfo(
|
|
SqliteConnection conn, string table)
|
|
{
|
|
using var cmd = conn.CreateCommand();
|
|
cmd.CommandText = $"PRAGMA table_info('{table}')";
|
|
using var reader = cmd.ExecuteReader();
|
|
var cols = new List<(string, string, bool, bool)>();
|
|
while (reader.Read())
|
|
{
|
|
cols.Add((reader.GetString(1), reader.GetString(2), reader.GetInt32(3) == 1,
|
|
reader.GetInt32(5) > 0));
|
|
}
|
|
|
|
return cols;
|
|
}
|
|
|
|
/// <summary>Verifies that applying the schema creates the table with the expected columns.</summary>
|
|
[Fact]
|
|
public void Apply_CreatesTableWithExpectedColumns()
|
|
{
|
|
using var conn = OpenConnection();
|
|
|
|
SiteEventLogSchema.Apply(conn);
|
|
|
|
Assert.Equal(
|
|
["id", "timestamp", "event_type", "severity", "instance_id", "source", "message", "details"],
|
|
TableInfo(conn, "site_events").Select(c => c.Name));
|
|
}
|
|
|
|
/// <summary>Verifies that the id column is a text primary key rather than an autoincrement integer.</summary>
|
|
[Fact]
|
|
public void Apply_IdIsTextPrimaryKey_NotAutoincrement()
|
|
{
|
|
using var conn = OpenConnection();
|
|
|
|
SiteEventLogSchema.Apply(conn);
|
|
|
|
var pk = Assert.Single(TableInfo(conn, "site_events"), c => c.Pk);
|
|
Assert.Equal("id", pk.Name);
|
|
Assert.Equal("TEXT", pk.Type);
|
|
|
|
// AUTOINCREMENT creates the sqlite_sequence bookkeeping table. Its absence
|
|
// is the load-bearing assertion: ids must be application-minted GUIDs, not
|
|
// server-minted integers that collide across the replicating pair.
|
|
using var cmd = conn.CreateCommand();
|
|
cmd.CommandText =
|
|
"SELECT COUNT(*) FROM sqlite_master WHERE type = 'table' AND name = 'sqlite_sequence'";
|
|
Assert.Equal(0L, Convert.ToInt64(cmd.ExecuteScalar()));
|
|
}
|
|
|
|
/// <summary>Verifies that inserts that omit the text id are rejected.</summary>
|
|
[Fact]
|
|
public void Apply_RejectsIntegerIdInserts()
|
|
{
|
|
using var conn = OpenConnection();
|
|
|
|
SiteEventLogSchema.Apply(conn);
|
|
|
|
// A TEXT PK with no AUTOINCREMENT means an INSERT omitting id is an error,
|
|
// which is what forces the writer to mint one.
|
|
using var cmd = conn.CreateCommand();
|
|
cmd.CommandText = """
|
|
INSERT INTO site_events (timestamp, event_type, severity, source, message)
|
|
VALUES ('2026-07-19T00:00:00Z', 'Test', 'Info', 'test', 'no id supplied')
|
|
""";
|
|
Assert.Throws<SqliteException>(() => cmd.ExecuteNonQuery());
|
|
}
|
|
|
|
/// <summary>Verifies that the schema has no blob columns.</summary>
|
|
[Fact]
|
|
public void Apply_HasNoBlobColumns()
|
|
{
|
|
using var conn = OpenConnection();
|
|
|
|
SiteEventLogSchema.Apply(conn);
|
|
|
|
Assert.DoesNotContain(
|
|
TableInfo(conn, "site_events"),
|
|
c => c.Type.Contains("BLOB", StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>Verifies that applying the schema creates the expected indexes.</summary>
|
|
[Fact]
|
|
public void Apply_CreatesExpectedIndexes()
|
|
{
|
|
using var conn = OpenConnection();
|
|
|
|
SiteEventLogSchema.Apply(conn);
|
|
|
|
using var cmd = conn.CreateCommand();
|
|
cmd.CommandText =
|
|
"SELECT name FROM sqlite_master WHERE type = 'index' AND tbl_name = 'site_events' " +
|
|
"AND name NOT LIKE 'sqlite_%' ORDER BY name";
|
|
using var reader = cmd.ExecuteReader();
|
|
var indexes = new List<string>();
|
|
while (reader.Read()) indexes.Add(reader.GetString(0));
|
|
|
|
Assert.Equal(
|
|
["idx_events_instance_timestamp", "idx_events_severity_timestamp", "idx_events_timestamp", "idx_events_type_timestamp"],
|
|
indexes);
|
|
}
|
|
|
|
/// <summary>Verifies that applying the schema repeatedly is safe.</summary>
|
|
[Fact]
|
|
public void Apply_IsIdempotent()
|
|
{
|
|
using var conn = OpenConnection();
|
|
|
|
SiteEventLogSchema.Apply(conn);
|
|
SiteEventLogSchema.Apply(conn);
|
|
|
|
Assert.Equal(8, TableInfo(conn, "site_events").Count);
|
|
}
|
|
}
|