junction/src/Junction.Persistence/SqliteSchema.cs
dtrentin 507753f82e init: scaffold Junction multi-protocol machine monitor (M1 walking skeleton)
Greenfield C# app reading industrial machine data over pluggable protocols.
M1 walking skeleton: MTConnect plugin polls a machine, Avalonia dashboard
shows live last datum. Build 0 warn/0 err on net48 + net8.0 (Linux),
115 unit tests + 2 docker-gated integration tests.

Projects:
- Junction.Domain (netstandard2.0): Result<T>, models, IProtocolDriver,
  IMachineRepository, plugin manifest. Zero package deps.
- Junction.Core (netstandard2.0): PollingEngine, PluginLoader
  (Assembly.LoadFrom), MachineMonitor, DI extensions.
- Junction.Persistence (netstandard2.0): SqliteMachineRepository (Dapper),
  schema, connection factory. Provider-swap seam to SQL Server.
- Junction.Protocols.MTConnect (netstandard2.0): HTTP driver + namespace-
  version-agnostic parser (MTConnect 1.7 + 2.0). Runtime plugin.
- Junction.App (net48;net8.0): Avalonia MVVM, live dashboard, NLog, DI root.
- Junction.Tests (net8.0): xUnit + Moq, 117 tests.
- mock/: docker-compose MTConnect agent (ladder99/agent).

Target net48 for Windows 7/8 fleet compatibility. Avalonia pinned 11.3.x.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-21 23:32:56 +02:00

104 lines
3.8 KiB
C#

using System;
using System.Data;
using Junction.Domain;
namespace Junction.Persistence
{
/// <summary>
/// Owns the SQLite schema (DDL). Idempotent: safe to run on every startup.
/// Only-latest policy — one snapshot row and one item-set per machine — but the
/// shape leaves room to add history tables later without breaking these tables.
/// </summary>
public static class SqliteSchema
{
// machines: one row per configured machine. ConnectionConfig bag stored as JSON text.
private const string CreateMachines =
@"CREATE TABLE IF NOT EXISTS machines (
Id TEXT NOT NULL PRIMARY KEY,
Name TEXT NOT NULL,
ProtocolId TEXT NOT NULL,
PollIntervalTicks INTEGER NOT NULL,
ConnectionConfigJson TEXT NOT NULL
);";
// latest_snapshots: exactly one row per machine (PK = MachineId). Upsert overwrites.
private const string CreateLatestSnapshots =
@"CREATE TABLE IF NOT EXISTS latest_snapshots (
MachineId TEXT NOT NULL PRIMARY KEY,
CapturedAt TEXT NOT NULL,
ConnectionState INTEGER NOT NULL
);";
// snapshot_items: datums of the latest snapshot. Replaced on each SaveSnapshot.
private const string CreateSnapshotItems =
@"CREATE TABLE IF NOT EXISTS snapshot_items (
MachineId TEXT NOT NULL,
ItemId TEXT NOT NULL,
Name TEXT NOT NULL,
Value TEXT NOT NULL,
Category TEXT NOT NULL,
Timestamp TEXT NOT NULL,
PRIMARY KEY (MachineId, ItemId)
);";
/// <summary>
/// Creates all tables if they do not already exist. Idempotent.
/// Returns a failed <see cref="Result"/> instead of throwing on store errors.
/// </summary>
public static Result EnsureCreated(IDbConnection connection)
{
if (connection == null)
{
return Result.Fail(OperationError.Of("SqliteSchema", "Connection is null."));
}
try
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
Execute(connection, CreateMachines);
Execute(connection, CreateLatestSnapshots);
Execute(connection, CreateSnapshotItems);
return Result.Ok();
}
catch (Exception ex)
{
return Result.Fail(OperationError.Of("SqliteSchema", "Schema init failed: " + ex.Message));
}
}
/// <summary>Convenience overload: opens a connection from the factory and runs <see cref="EnsureCreated(IDbConnection)"/>.</summary>
public static Result EnsureCreated(IConnectionFactory connectionFactory)
{
if (connectionFactory == null)
{
return Result.Fail(OperationError.Of("SqliteSchema", "Connection factory is null."));
}
try
{
using (var connection = connectionFactory.CreateOpenConnection())
{
return EnsureCreated(connection);
}
}
catch (Exception ex)
{
return Result.Fail(OperationError.Of("SqliteSchema", "Schema init failed: " + ex.Message));
}
}
private static void Execute(IDbConnection connection, string sql)
{
using (var command = connection.CreateCommand())
{
command.CommandText = sql;
command.ExecuteNonQuery();
}
}
}
}