feat(data): add migrations, repositories and TitanoDatabase facade
Embedded per-dialect migrations applied in one transaction and recorded in schema_migrations (advisory lock on PostgreSQL, newer schemas refused). Schema for every BRIEF section 5 entity plus import history and uploads. Hand-written repositories for archives, sequences (filters, keyset paging, Archive statistics), frames (bulk insert and updates), renders, jobs (exclusive claiming, checkpoints, crash recovery), media cards, users, tokens, render nodes, settings, tags, audit and uploads. TitanoDatabase opens, migrates and exposes them, also bound to a transaction. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
This commit is contained in:
18 files changed
+2334
No files matched your search
@@ -0,0 +1,90 @@
|
||||
using System.Globalization;
|
||||
using Titano.Core.Diagnostics;
|
||||
|
||||
namespace Titano.Data.Migrations;
|
||||
|
||||
/// <summary>A numbered schema change shipped as <c>Migrations/<dialect>/NNNN_name.sql</c>.</summary>
|
||||
public sealed record Migration(int Version, string Name, string Sql);
|
||||
|
||||
/// <summary>
|
||||
/// Applies the embedded migrations of a dialect at startup (BRIEF §6.1): all pending ones in a single
|
||||
/// transaction, each recorded in <c>schema_migrations(version, name, applied_at)</c>. A database that already
|
||||
/// holds a version this build does not know is refused with <see cref="SchemaTooNewException"/>.
|
||||
/// </summary>
|
||||
public static class MigrationRunner
|
||||
{
|
||||
private const string ResourcePrefix = "Titano.Data.Migrations.";
|
||||
|
||||
/// <summary>Serialises concurrent migrators on one PostgreSQL database (several server instances starting).</summary>
|
||||
private const long AdvisoryLockKey = 0x5469_7461_6E6F;
|
||||
|
||||
/// <summary>The embedded migrations of <paramref name="dialect"/>, by ascending version.</summary>
|
||||
public static IReadOnlyList<Migration> Load(Dialect dialect)
|
||||
{
|
||||
var folder = ResourcePrefix + (dialect == Dialect.Postgres ? "postgres" : "sqlite");
|
||||
var assembly = typeof(MigrationRunner).Assembly;
|
||||
var migrations = new List<Migration>();
|
||||
foreach (var resource in assembly.GetManifestResourceNames())
|
||||
{
|
||||
// The logical name keeps the folder separator of the build host ('/' or '\').
|
||||
if (resource.Length <= folder.Length + 1 || !resource.StartsWith(folder, StringComparison.Ordinal)
|
||||
|| resource[folder.Length] is not ('/' or '\\'))
|
||||
continue;
|
||||
var file = resource[(folder.Length + 1)..];
|
||||
var underscore = file.IndexOf('_', StringComparison.Ordinal);
|
||||
if (underscore <= 0 || !file.EndsWith(".sql", StringComparison.Ordinal)
|
||||
|| !int.TryParse(file.AsSpan(0, underscore), NumberStyles.None, CultureInfo.InvariantCulture, out var version))
|
||||
throw new InvalidOperationException($"Migration resource '{resource}' is not named NNNN_name.sql.");
|
||||
using var reader = new StreamReader(assembly.GetManifestResourceStream(resource)!);
|
||||
migrations.Add(new Migration(version, file[(underscore + 1)..^4], reader.ReadToEnd()));
|
||||
}
|
||||
migrations.Sort((a, b) => a.Version.CompareTo(b.Version));
|
||||
for (var i = 1; i < migrations.Count; i++)
|
||||
{
|
||||
if (migrations[i].Version == migrations[i - 1].Version)
|
||||
throw new InvalidOperationException($"Two migrations share version {migrations[i].Version}.");
|
||||
}
|
||||
return migrations;
|
||||
}
|
||||
|
||||
/// <summary>Migrates <paramref name="database"/> to the newest embedded version and returns that version.</summary>
|
||||
public static Task<int> MigrateAsync(IDatabase database, CancellationToken cancellationToken = default) =>
|
||||
MigrateAsync(database, Load(database.Dialect), cancellationToken);
|
||||
|
||||
/// <summary>Applies every migration of <paramref name="migrations"/> not yet recorded; returns the resulting version.</summary>
|
||||
public static async Task<int> MigrateAsync(IDatabase database, IReadOnlyList<Migration> migrations, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var postgres = database.Dialect == Dialect.Postgres;
|
||||
var session = await database.OpenAsync(cancellationToken).ConfigureAwait(false);
|
||||
await using (session.ConfigureAwait(false))
|
||||
{
|
||||
var transaction = await session.BeginAsync(cancellationToken).ConfigureAwait(false);
|
||||
await using (transaction.ConfigureAwait(false))
|
||||
{
|
||||
if (postgres) await session.ExecuteAsync("SELECT pg_advisory_xact_lock($1)", [AdvisoryLockKey], cancellationToken).ConfigureAwait(false);
|
||||
await session.ExecuteScriptAsync(postgres
|
||||
? "CREATE TABLE IF NOT EXISTS schema_migrations (version integer PRIMARY KEY, name text NOT NULL, applied_at timestamptz NOT NULL)"
|
||||
: "CREATE TABLE IF NOT EXISTS schema_migrations (version INTEGER PRIMARY KEY, name TEXT NOT NULL, applied_at TEXT NOT NULL)",
|
||||
cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var applied = new HashSet<int>(await session.QueryListAsync(
|
||||
"SELECT version FROM schema_migrations", [], r => r.GetInt32(0), cancellationToken).ConfigureAwait(false));
|
||||
var supported = migrations.Count == 0 ? 0 : migrations[^1].Version;
|
||||
var current = applied.Count == 0 ? 0 : applied.Max();
|
||||
if (current > supported) throw new SchemaTooNewException(current, supported);
|
||||
|
||||
foreach (var migration in migrations)
|
||||
{
|
||||
if (applied.Contains(migration.Version)) continue;
|
||||
await session.ExecuteScriptAsync(migration.Sql, cancellationToken).ConfigureAwait(false);
|
||||
await session.ExecuteAsync(
|
||||
"INSERT INTO schema_migrations (version, name, applied_at) VALUES ($1, $2, $3)",
|
||||
[migration.Version, migration.Name, DateTimeOffset.UtcNow], cancellationToken).ConfigureAwait(false);
|
||||
Log.Info("Data", $"Applied {database.Dialect} migration {migration.Version:0000} {migration.Name}");
|
||||
}
|
||||
await transaction.CommitAsync(cancellationToken).ConfigureAwait(false);
|
||||
return supported;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,250 @@
|
||||
-- Titano schema v1 (PostgreSQL). Instants are timestamptz, camera wall-clock times timestamp, JSON jsonb.
|
||||
-- Sort keys use the "C" collation so ordering equals SQLite's binary UTF-8 order. Mirrors sqlite/0001_initial.sql.
|
||||
|
||||
CREATE TABLE archives (
|
||||
id uuid NOT NULL PRIMARY KEY,
|
||||
name text NOT NULL,
|
||||
root_path text NOT NULL,
|
||||
folder_template text NOT NULL,
|
||||
file_template text NOT NULL,
|
||||
schema_version integer NOT NULL DEFAULT 1,
|
||||
created timestamptz NOT NULL,
|
||||
proxy_long_edge integer NOT NULL DEFAULT 1920
|
||||
);
|
||||
|
||||
CREATE TABLE users (
|
||||
id uuid NOT NULL PRIMARY KEY,
|
||||
username text NOT NULL,
|
||||
username_key text NOT NULL UNIQUE,
|
||||
display_name text,
|
||||
password_hash text NOT NULL,
|
||||
role integer NOT NULL,
|
||||
permissions bigint NOT NULL,
|
||||
quota_bytes bigint NOT NULL DEFAULT 0,
|
||||
used_bytes bigint NOT NULL DEFAULT 0,
|
||||
disabled boolean NOT NULL DEFAULT FALSE,
|
||||
must_change_password boolean NOT NULL DEFAULT FALSE,
|
||||
created timestamptz NOT NULL,
|
||||
last_login timestamptz
|
||||
);
|
||||
|
||||
CREATE TABLE sequences (
|
||||
id uuid NOT NULL PRIMARY KEY,
|
||||
archive_id uuid NOT NULL REFERENCES archives (id) ON DELETE CASCADE,
|
||||
title text,
|
||||
title_key text COLLATE "C" NOT NULL,
|
||||
search_text text NOT NULL,
|
||||
folder text NOT NULL,
|
||||
camera_make text,
|
||||
camera_model text,
|
||||
camera_serial text,
|
||||
lens text,
|
||||
first_capture timestamp,
|
||||
last_capture timestamp,
|
||||
frame_count integer NOT NULL DEFAULT 0,
|
||||
median_interval double precision,
|
||||
total_bytes bigint NOT NULL DEFAULT 0,
|
||||
status integer NOT NULL DEFAULT 0,
|
||||
rendered_count integer NOT NULL DEFAULT 0,
|
||||
verified_at timestamptz,
|
||||
hero_frame_id uuid,
|
||||
rating integer NOT NULL DEFAULT 0,
|
||||
notes text,
|
||||
settings_json jsonb,
|
||||
analysis_json jsonb,
|
||||
origin integer NOT NULL DEFAULT 0,
|
||||
sync_state integer NOT NULL DEFAULT 0,
|
||||
settings_version integer NOT NULL DEFAULT 0,
|
||||
frames_version integer NOT NULL DEFAULT 0,
|
||||
owner_user_id uuid,
|
||||
warning_count integer NOT NULL DEFAULT 0,
|
||||
updated_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX ix_sequences_archive_capture ON sequences (archive_id, first_capture, id);
|
||||
CREATE INDEX ix_sequences_capture ON sequences (first_capture, id);
|
||||
CREATE INDEX ix_sequences_camera ON sequences (camera_model);
|
||||
|
||||
CREATE TABLE frames (
|
||||
id uuid NOT NULL PRIMARY KEY,
|
||||
sequence_id uuid NOT NULL REFERENCES sequences (id) ON DELETE CASCADE,
|
||||
idx integer NOT NULL,
|
||||
relative_path text NOT NULL,
|
||||
file_name text NOT NULL,
|
||||
original_file_name text,
|
||||
size bigint NOT NULL DEFAULT 0,
|
||||
sha256 text,
|
||||
captured_at timestamp,
|
||||
utc_offset_minutes integer,
|
||||
timestamp_source integer NOT NULL DEFAULT 0,
|
||||
exposure_time double precision,
|
||||
f_number double precision,
|
||||
iso integer,
|
||||
focal_length double precision,
|
||||
exposure_bias double precision,
|
||||
white_balance integer,
|
||||
orientation integer NOT NULL DEFAULT 1,
|
||||
width integer NOT NULL DEFAULT 0,
|
||||
height integer NOT NULL DEFAULT 0,
|
||||
format integer NOT NULL DEFAULT 0,
|
||||
preview_offset bigint,
|
||||
preview_length bigint,
|
||||
mean_log_luminance double precision,
|
||||
clip_low double precision,
|
||||
clip_high double precision,
|
||||
flags integer NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE UNIQUE INDEX ux_frames_sequence_idx ON frames (sequence_id, idx);
|
||||
CREATE INDEX ix_frames_sha256 ON frames (sha256);
|
||||
CREATE INDEX ix_frames_captured_at ON frames (captured_at);
|
||||
|
||||
CREATE TABLE renders (
|
||||
id uuid NOT NULL PRIMARY KEY,
|
||||
sequence_id uuid NOT NULL REFERENCES sequences (id) ON DELETE CASCADE,
|
||||
preset_name text NOT NULL,
|
||||
settings_json jsonb,
|
||||
output_path text NOT NULL,
|
||||
codec text,
|
||||
width integer NOT NULL DEFAULT 0,
|
||||
height integer NOT NULL DEFAULT 0,
|
||||
fps double precision NOT NULL DEFAULT 0,
|
||||
duration_seconds double precision NOT NULL DEFAULT 0,
|
||||
bytes bigint NOT NULL DEFAULT 0,
|
||||
created timestamptz NOT NULL,
|
||||
produced_by text
|
||||
);
|
||||
CREATE INDEX ix_renders_sequence ON renders (sequence_id, created);
|
||||
|
||||
CREATE TABLE jobs (
|
||||
id uuid NOT NULL PRIMARY KEY,
|
||||
kind integer NOT NULL,
|
||||
target_ids text NOT NULL DEFAULT '',
|
||||
target_label text,
|
||||
parameters_json jsonb,
|
||||
state integer NOT NULL,
|
||||
priority integer NOT NULL,
|
||||
progress_done bigint NOT NULL DEFAULT 0,
|
||||
progress_total bigint NOT NULL DEFAULT 0,
|
||||
items_per_second double precision,
|
||||
eta_seconds double precision,
|
||||
checkpoint text,
|
||||
log_tail text,
|
||||
created_by uuid,
|
||||
node_id text,
|
||||
attempts integer NOT NULL DEFAULT 0,
|
||||
error text,
|
||||
created timestamptz NOT NULL,
|
||||
started timestamptz,
|
||||
finished timestamptz
|
||||
);
|
||||
CREATE INDEX ix_jobs_queue ON jobs (state, priority DESC, created, id);
|
||||
CREATE INDEX ix_jobs_state_created ON jobs (state, created, id);
|
||||
CREATE INDEX ix_jobs_finished ON jobs (finished);
|
||||
|
||||
CREATE TABLE media_cards (
|
||||
id uuid NOT NULL PRIMARY KEY,
|
||||
identity text NOT NULL UNIQUE,
|
||||
volume_serial bigint NOT NULL,
|
||||
label text,
|
||||
capacity bigint NOT NULL DEFAULT 0,
|
||||
nickname text,
|
||||
camera_make text,
|
||||
camera_model text,
|
||||
camera_serial text,
|
||||
profile_json jsonb,
|
||||
auto_import boolean NOT NULL DEFAULT FALSE,
|
||||
first_seen timestamptz NOT NULL,
|
||||
last_seen timestamptz NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE import_history (
|
||||
card_id uuid NOT NULL REFERENCES media_cards (id) ON DELETE CASCADE,
|
||||
path text NOT NULL,
|
||||
size bigint NOT NULL,
|
||||
mtime timestamptz NOT NULL,
|
||||
sha256 text,
|
||||
frame_id uuid REFERENCES frames (id) ON DELETE SET NULL,
|
||||
job_id uuid,
|
||||
imported_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (card_id, path, size, mtime)
|
||||
);
|
||||
CREATE INDEX ix_import_history_frame ON import_history (frame_id);
|
||||
|
||||
CREATE TABLE tokens (
|
||||
token_hash text NOT NULL PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
client_name text,
|
||||
created timestamptz NOT NULL,
|
||||
expires timestamptz NOT NULL,
|
||||
last_used timestamptz NOT NULL,
|
||||
revoked boolean NOT NULL DEFAULT FALSE
|
||||
);
|
||||
CREATE INDEX ix_tokens_user ON tokens (user_id);
|
||||
CREATE INDEX ix_tokens_expires ON tokens (expires);
|
||||
|
||||
CREATE TABLE render_nodes (
|
||||
id uuid NOT NULL PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
client_name text NOT NULL,
|
||||
capabilities_json jsonb,
|
||||
last_heartbeat timestamptz NOT NULL,
|
||||
state text NOT NULL
|
||||
);
|
||||
CREATE INDEX ix_render_nodes_user ON render_nodes (user_id);
|
||||
CREATE INDEX ix_render_nodes_heartbeat ON render_nodes (last_heartbeat);
|
||||
|
||||
CREATE TABLE settings (
|
||||
scope text NOT NULL,
|
||||
scope_id text NOT NULL DEFAULT '',
|
||||
key text NOT NULL,
|
||||
value_json jsonb NOT NULL,
|
||||
updated_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (scope, scope_id, key)
|
||||
);
|
||||
|
||||
CREATE TABLE tags (
|
||||
id uuid NOT NULL PRIMARY KEY,
|
||||
name text NOT NULL,
|
||||
name_key text COLLATE "C" NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
CREATE TABLE sequence_tags (
|
||||
sequence_id uuid NOT NULL REFERENCES sequences (id) ON DELETE CASCADE,
|
||||
tag_id uuid NOT NULL REFERENCES tags (id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (sequence_id, tag_id)
|
||||
);
|
||||
CREATE INDEX ix_sequence_tags_tag ON sequence_tags (tag_id);
|
||||
|
||||
CREATE TABLE audit_log (
|
||||
id uuid NOT NULL PRIMARY KEY,
|
||||
at timestamptz NOT NULL,
|
||||
user_id uuid,
|
||||
username text,
|
||||
action text NOT NULL,
|
||||
target_kind text,
|
||||
target_id text,
|
||||
details text,
|
||||
remote_address text
|
||||
);
|
||||
CREATE INDEX ix_audit_log_at ON audit_log (at, id);
|
||||
CREATE INDEX ix_audit_log_user ON audit_log (user_id, at);
|
||||
|
||||
CREATE TABLE uploads (
|
||||
id uuid NOT NULL PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
sequence_id uuid REFERENCES sequences (id) ON DELETE SET NULL,
|
||||
file_name text NOT NULL,
|
||||
size bigint NOT NULL,
|
||||
sha256 text NOT NULL,
|
||||
captured_at timestamp,
|
||||
chunk_size integer NOT NULL,
|
||||
received bytea NOT NULL,
|
||||
received_count integer NOT NULL DEFAULT 0,
|
||||
state integer NOT NULL,
|
||||
frame_id uuid,
|
||||
created timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX ix_uploads_user ON uploads (user_id, created);
|
||||
CREATE INDEX ix_uploads_state ON uploads (state, updated_at);
|
||||
CREATE INDEX ix_uploads_sha256 ON uploads (sha256);
|
||||
CREATE INDEX ix_uploads_sequence ON uploads (sequence_id);
|
||||
@@ -0,0 +1,250 @@
|
||||
-- Titano schema v1 (SQLite). Ids are UUID text, instants ISO-8601 UTC text ('…Z'), camera wall-clock times
|
||||
-- ISO-8601 text without zone, JSON as text, booleans as 0/1. Mirrors postgres/0001_initial.sql.
|
||||
|
||||
CREATE TABLE archives (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
root_path TEXT NOT NULL,
|
||||
folder_template TEXT NOT NULL,
|
||||
file_template TEXT NOT NULL,
|
||||
schema_version INTEGER NOT NULL DEFAULT 1,
|
||||
created TEXT NOT NULL,
|
||||
proxy_long_edge INTEGER NOT NULL DEFAULT 1920
|
||||
);
|
||||
|
||||
CREATE TABLE users (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
username TEXT NOT NULL,
|
||||
username_key TEXT NOT NULL UNIQUE,
|
||||
display_name TEXT,
|
||||
password_hash TEXT NOT NULL,
|
||||
role INTEGER NOT NULL,
|
||||
permissions INTEGER NOT NULL,
|
||||
quota_bytes INTEGER NOT NULL DEFAULT 0,
|
||||
used_bytes INTEGER NOT NULL DEFAULT 0,
|
||||
disabled INTEGER NOT NULL DEFAULT 0,
|
||||
must_change_password INTEGER NOT NULL DEFAULT 0,
|
||||
created TEXT NOT NULL,
|
||||
last_login TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE sequences (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
archive_id TEXT NOT NULL REFERENCES archives (id) ON DELETE CASCADE,
|
||||
title TEXT,
|
||||
title_key TEXT NOT NULL,
|
||||
search_text TEXT NOT NULL,
|
||||
folder TEXT NOT NULL,
|
||||
camera_make TEXT,
|
||||
camera_model TEXT,
|
||||
camera_serial TEXT,
|
||||
lens TEXT,
|
||||
first_capture TEXT,
|
||||
last_capture TEXT,
|
||||
frame_count INTEGER NOT NULL DEFAULT 0,
|
||||
median_interval REAL,
|
||||
total_bytes INTEGER NOT NULL DEFAULT 0,
|
||||
status INTEGER NOT NULL DEFAULT 0,
|
||||
rendered_count INTEGER NOT NULL DEFAULT 0,
|
||||
verified_at TEXT,
|
||||
hero_frame_id TEXT,
|
||||
rating INTEGER NOT NULL DEFAULT 0,
|
||||
notes TEXT,
|
||||
settings_json TEXT,
|
||||
analysis_json TEXT,
|
||||
origin INTEGER NOT NULL DEFAULT 0,
|
||||
sync_state INTEGER NOT NULL DEFAULT 0,
|
||||
settings_version INTEGER NOT NULL DEFAULT 0,
|
||||
frames_version INTEGER NOT NULL DEFAULT 0,
|
||||
owner_user_id TEXT,
|
||||
warning_count INTEGER NOT NULL DEFAULT 0,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX ix_sequences_archive_capture ON sequences (archive_id, first_capture, id);
|
||||
CREATE INDEX ix_sequences_capture ON sequences (first_capture, id);
|
||||
CREATE INDEX ix_sequences_camera ON sequences (camera_model);
|
||||
|
||||
CREATE TABLE frames (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
sequence_id TEXT NOT NULL REFERENCES sequences (id) ON DELETE CASCADE,
|
||||
idx INTEGER NOT NULL,
|
||||
relative_path TEXT NOT NULL,
|
||||
file_name TEXT NOT NULL,
|
||||
original_file_name TEXT,
|
||||
size INTEGER NOT NULL DEFAULT 0,
|
||||
sha256 TEXT,
|
||||
captured_at TEXT,
|
||||
utc_offset_minutes INTEGER,
|
||||
timestamp_source INTEGER NOT NULL DEFAULT 0,
|
||||
exposure_time REAL,
|
||||
f_number REAL,
|
||||
iso INTEGER,
|
||||
focal_length REAL,
|
||||
exposure_bias REAL,
|
||||
white_balance INTEGER,
|
||||
orientation INTEGER NOT NULL DEFAULT 1,
|
||||
width INTEGER NOT NULL DEFAULT 0,
|
||||
height INTEGER NOT NULL DEFAULT 0,
|
||||
format INTEGER NOT NULL DEFAULT 0,
|
||||
preview_offset INTEGER,
|
||||
preview_length INTEGER,
|
||||
mean_log_luminance REAL,
|
||||
clip_low REAL,
|
||||
clip_high REAL,
|
||||
flags INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE UNIQUE INDEX ux_frames_sequence_idx ON frames (sequence_id, idx);
|
||||
CREATE INDEX ix_frames_sha256 ON frames (sha256);
|
||||
CREATE INDEX ix_frames_captured_at ON frames (captured_at);
|
||||
|
||||
CREATE TABLE renders (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
sequence_id TEXT NOT NULL REFERENCES sequences (id) ON DELETE CASCADE,
|
||||
preset_name TEXT NOT NULL,
|
||||
settings_json TEXT,
|
||||
output_path TEXT NOT NULL,
|
||||
codec TEXT,
|
||||
width INTEGER NOT NULL DEFAULT 0,
|
||||
height INTEGER NOT NULL DEFAULT 0,
|
||||
fps REAL NOT NULL DEFAULT 0,
|
||||
duration_seconds REAL NOT NULL DEFAULT 0,
|
||||
bytes INTEGER NOT NULL DEFAULT 0,
|
||||
created TEXT NOT NULL,
|
||||
produced_by TEXT
|
||||
);
|
||||
CREATE INDEX ix_renders_sequence ON renders (sequence_id, created);
|
||||
|
||||
CREATE TABLE jobs (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
kind INTEGER NOT NULL,
|
||||
target_ids TEXT NOT NULL DEFAULT '',
|
||||
target_label TEXT,
|
||||
parameters_json TEXT,
|
||||
state INTEGER NOT NULL,
|
||||
priority INTEGER NOT NULL,
|
||||
progress_done INTEGER NOT NULL DEFAULT 0,
|
||||
progress_total INTEGER NOT NULL DEFAULT 0,
|
||||
items_per_second REAL,
|
||||
eta_seconds REAL,
|
||||
checkpoint TEXT,
|
||||
log_tail TEXT,
|
||||
created_by TEXT,
|
||||
node_id TEXT,
|
||||
attempts INTEGER NOT NULL DEFAULT 0,
|
||||
error TEXT,
|
||||
created TEXT NOT NULL,
|
||||
started TEXT,
|
||||
finished TEXT
|
||||
);
|
||||
CREATE INDEX ix_jobs_queue ON jobs (state, priority DESC, created, id);
|
||||
CREATE INDEX ix_jobs_state_created ON jobs (state, created, id);
|
||||
CREATE INDEX ix_jobs_finished ON jobs (finished);
|
||||
|
||||
CREATE TABLE media_cards (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
identity TEXT NOT NULL UNIQUE,
|
||||
volume_serial INTEGER NOT NULL,
|
||||
label TEXT,
|
||||
capacity INTEGER NOT NULL DEFAULT 0,
|
||||
nickname TEXT,
|
||||
camera_make TEXT,
|
||||
camera_model TEXT,
|
||||
camera_serial TEXT,
|
||||
profile_json TEXT,
|
||||
auto_import INTEGER NOT NULL DEFAULT 0,
|
||||
first_seen TEXT NOT NULL,
|
||||
last_seen TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE import_history (
|
||||
card_id TEXT NOT NULL REFERENCES media_cards (id) ON DELETE CASCADE,
|
||||
path TEXT NOT NULL,
|
||||
size INTEGER NOT NULL,
|
||||
mtime TEXT NOT NULL,
|
||||
sha256 TEXT,
|
||||
frame_id TEXT REFERENCES frames (id) ON DELETE SET NULL,
|
||||
job_id TEXT,
|
||||
imported_at TEXT NOT NULL,
|
||||
PRIMARY KEY (card_id, path, size, mtime)
|
||||
);
|
||||
CREATE INDEX ix_import_history_frame ON import_history (frame_id);
|
||||
|
||||
CREATE TABLE tokens (
|
||||
token_hash TEXT NOT NULL PRIMARY KEY,
|
||||
user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
client_name TEXT,
|
||||
created TEXT NOT NULL,
|
||||
expires TEXT NOT NULL,
|
||||
last_used TEXT NOT NULL,
|
||||
revoked INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
CREATE INDEX ix_tokens_user ON tokens (user_id);
|
||||
CREATE INDEX ix_tokens_expires ON tokens (expires);
|
||||
|
||||
CREATE TABLE render_nodes (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
client_name TEXT NOT NULL,
|
||||
capabilities_json TEXT,
|
||||
last_heartbeat TEXT NOT NULL,
|
||||
state TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX ix_render_nodes_user ON render_nodes (user_id);
|
||||
CREATE INDEX ix_render_nodes_heartbeat ON render_nodes (last_heartbeat);
|
||||
|
||||
CREATE TABLE settings (
|
||||
scope TEXT NOT NULL,
|
||||
scope_id TEXT NOT NULL DEFAULT '',
|
||||
key TEXT NOT NULL,
|
||||
value_json TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL,
|
||||
PRIMARY KEY (scope, scope_id, key)
|
||||
);
|
||||
|
||||
CREATE TABLE tags (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
name_key TEXT NOT NULL UNIQUE
|
||||
);
|
||||
|
||||
CREATE TABLE sequence_tags (
|
||||
sequence_id TEXT NOT NULL REFERENCES sequences (id) ON DELETE CASCADE,
|
||||
tag_id TEXT NOT NULL REFERENCES tags (id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (sequence_id, tag_id)
|
||||
);
|
||||
CREATE INDEX ix_sequence_tags_tag ON sequence_tags (tag_id);
|
||||
|
||||
CREATE TABLE audit_log (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
at TEXT NOT NULL,
|
||||
user_id TEXT,
|
||||
username TEXT,
|
||||
action TEXT NOT NULL,
|
||||
target_kind TEXT,
|
||||
target_id TEXT,
|
||||
details TEXT,
|
||||
remote_address TEXT
|
||||
);
|
||||
CREATE INDEX ix_audit_log_at ON audit_log (at, id);
|
||||
CREATE INDEX ix_audit_log_user ON audit_log (user_id, at);
|
||||
|
||||
CREATE TABLE uploads (
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
user_id TEXT NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
sequence_id TEXT REFERENCES sequences (id) ON DELETE SET NULL,
|
||||
file_name TEXT NOT NULL,
|
||||
size INTEGER NOT NULL,
|
||||
sha256 TEXT NOT NULL,
|
||||
captured_at TEXT,
|
||||
chunk_size INTEGER NOT NULL,
|
||||
received BLOB NOT NULL,
|
||||
received_count INTEGER NOT NULL DEFAULT 0,
|
||||
state INTEGER NOT NULL,
|
||||
frame_id TEXT,
|
||||
created TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
CREATE INDEX ix_uploads_user ON uploads (user_id, created);
|
||||
CREATE INDEX ix_uploads_state ON uploads (state, updated_at);
|
||||
CREATE INDEX ix_uploads_sha256 ON uploads (sha256);
|
||||
CREATE INDEX ix_uploads_sequence ON uploads (sequence_id);
|
||||
Reference in new issue
Block a user