E1: le copie della cartella generale tornano con l'account, un rifiuto per ogni CHECK (#58)
La seconda revisione ha trovato che le copie della cartella generale di un account rimosso e aggiunto di nuovo restavano orfane per sempre: una «Tolto» o «Superato» senza file da adottare si sarebbe ricopiata da sola, contro §4.10. - export_copy tiene identificativo Instagram e nome dell'account rimosso, solo sulle copie orfane (CHECK, ix_export_copy_orphan); AccountStore.DeleteAsync li scrive. - RelinkOrphanCopiesAsync ridà all'account le sue copie della cartella generale, per identificativo o, se la copia non lo ha, per nome; AddAsync, UpdateAsync e RenameAsync lo chiamano nella loro transazione. La cartella propria resta ad AttachDestinationAsync. - Ogni CHECK della 0003 ha una prova di rifiuto su una riga valida per tutti gli altri; ambito, variante e formato sconosciuti si controllano anche sullo schema. - SPECIFICA §4.10 e §8.3, decisioni.md e stato.md aggiornati. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ea4666d49b
commit
980212fb50
10 files changed
+470
-32
No files matched your search
@@ -30,11 +30,17 @@ public interface IAccountStore
|
||||
|
||||
Task<AccountStats> GetStatsAsync(long accountId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Adds an account and returns it with its key.</summary>
|
||||
/// <summary>
|
||||
/// Adds an account and returns it with its key. In the same transaction the account gets back its orphan copies of
|
||||
/// the general export folder, if it was removed before (<see cref="IExportStore.RelinkOrphanCopiesAsync"/>).
|
||||
/// </summary>
|
||||
/// <exception cref="DuplicateRecordException">Another account has the same Instagram identifier, or is already «Tu».</exception>
|
||||
Task<Account> AddAsync(Account account, CancellationToken ct = default);
|
||||
|
||||
/// <summary>Replaces every field of an existing account (settings screen).</summary>
|
||||
/// <summary>
|
||||
/// Replaces every field of an existing account (settings screen); with its Instagram id and user name written, it
|
||||
/// gets back its orphan copies of the general export folder as in <see cref="AddAsync"/>.
|
||||
/// </summary>
|
||||
Task UpdateAsync(Account account, CancellationToken ct = default);
|
||||
|
||||
Task SetStateAsync(long accountId, AccountState state, DateTimeOffset at, UnreachableReason? reason = null, CancellationToken ct = default);
|
||||
@@ -47,7 +53,8 @@ public interface IAccountStore
|
||||
|
||||
/// <summary>
|
||||
/// Records a rename seen in the data (spec §4.2): updates the user name, keeps the folder (a default folder is
|
||||
/// pinned to the old name) and raises the «Renamed» notice.
|
||||
/// pinned to the old name) and raises the «Renamed» notice; with the new name, the account gets back its orphan copies
|
||||
/// of the general export folder as in <see cref="AddAsync"/>.
|
||||
/// </summary>
|
||||
Task RenameAsync(long accountId, string newUsername, DateTimeOffset at, CancellationToken ct = default);
|
||||
|
||||
@@ -61,7 +68,8 @@ public interface IAccountStore
|
||||
/// Removes the account record and, by cascade, its entries, files records and highlights. Files on disk are not
|
||||
/// touched: removing an account with its files is a library operation, and the default is to keep them and make
|
||||
/// the account «Solo archivio» (spec §4.2). Its export destinations stay, without account and with the user name;
|
||||
/// its «Esporta questo account» choice goes with it (spec §4.10).
|
||||
/// its export copies stay, without account and with its Instagram id and user name, so they come back if the account
|
||||
/// is added again; its «Esporta questo account» choice goes with it (spec §4.10).
|
||||
/// </summary>
|
||||
Task DeleteAsync(long accountId, CancellationToken ct = default);
|
||||
}
|
||||
|
||||
@@ -99,12 +99,26 @@ public interface IExportStore
|
||||
Task<IReadOnlyDictionary<ExportCopyState, int>> CountCopiesAsync(long destinationId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Re-links an orphan row (account removed) to a re-added account, when its file is adopted again.
|
||||
/// Re-links an orphan row (account removed) to a re-added account, when its file is adopted again, and forgets the
|
||||
/// removed account kept on it.
|
||||
/// <see cref="KeyNotFoundException"/> for a missing id; <see cref="InvalidOperationException"/> if the row is not an orphan;
|
||||
/// <see cref="DuplicateRecordException"/> if the account already has a current copy of that identity.
|
||||
/// </summary>
|
||||
Task<ExportCopy> RelinkOrphanCopyAsync(long copyId, long accountId, long? mediaId, DateTimeOffset at, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// Gives an account its orphan copies of the general folder (retired rows included), in every state, so a Removed or
|
||||
/// Superseded copy is never copied again by itself after the account was removed and added again (spec §4.10). A row
|
||||
/// matches by the Instagram id of the removed account when the row has one (only an account with that id), else by
|
||||
/// its user name, ignoring ASCII case. A current (Copied or Failed) row comes back only as the newest of its identity
|
||||
/// and only if the account has no current row of that identity: the others stay orphans. Copies of an own folder
|
||||
/// come back only with <see cref="AttachDestinationAsync"/>. Uses the account's current Instagram id and user name;
|
||||
/// <c>IAccountStore</c> already runs it when an account is added or its id or name is written, so callers need it
|
||||
/// only after writing those columns some other way. Returns the rows re-linked; <see cref="KeyNotFoundException"/>
|
||||
/// for a missing account.
|
||||
/// </summary>
|
||||
Task<int> RelinkOrphanCopiesAsync(long accountId, CancellationToken ct = default);
|
||||
|
||||
/// <summary>
|
||||
/// The library file of an account changed from <paramref name="oldSha256"/> to <paramref name="newSha256"/>: its
|
||||
/// Copied rows of the old file become Superseded, its Removed rows of the old file get the new SHA-256, and its rows
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
-- Febe, schema version 3: export (tappa E1 of 0.3.0, issue #58; spec §4.10, §8.3).
|
||||
-- Run by the migration runner inside a transaction; PRAGMA user_version is set by the runner.
|
||||
-- Only new tables and indexes: no column or index of the existing tables changes. The only existing path that touches
|
||||
-- the new tables is AccountStore.DeleteAsync, which disables the account's destinations and keeps its user name on them
|
||||
-- before deleting the account (nothing to do while there are no destinations); nothing else reads or writes them yet
|
||||
-- (tappa E1: no behaviour change). Times are UTC Unix milliseconds; enumerations are their English names,
|
||||
-- Only new tables and indexes: no column or index of the existing tables changes. The only existing paths that touch
|
||||
-- the new tables are in AccountStore: DeleteAsync disables the account's destinations and keeps its user name on them,
|
||||
-- and keeps its Instagram id and user name on its copies, before deleting the account; AddAsync, UpdateAsync and
|
||||
-- RenameAsync give the account back its orphan copies of the general folder (nothing to do while there are no
|
||||
-- destinations and no copies); nothing else reads or writes them yet (tappa E1: no behaviour change). Times are UTC Unix milliseconds; enumerations are their English names,
|
||||
-- checked here. Paths in a destination are relative to its root, '/' separated, NFC; paths of converted library files
|
||||
-- are relative to the account folder, like media.relative_path. Every foreign key has an index on its child column.
|
||||
|
||||
@@ -82,9 +83,18 @@ CREATE TABLE export_copy (
|
||||
id INTEGER PRIMARY KEY,
|
||||
destination_id INTEGER NOT NULL REFERENCES export_destination (id) ON DELETE RESTRICT,
|
||||
-- Effective account: the account segment in the general folder, or the account of its own folder. NULL only after
|
||||
-- the account was removed («orphan»): the store never writes NULL, and re-links an orphan row when its file is
|
||||
-- adopted again or its destination is attached to the account again.
|
||||
-- the account was removed («orphan»): only AccountStore.DeleteAsync writes NULL, with the two columns below, in the
|
||||
-- transaction that deletes the account (ON DELETE SET NULL then finds nothing left to clear).
|
||||
account_id INTEGER REFERENCES account (id) ON DELETE SET NULL,
|
||||
-- Instagram id and user name of the account when it was removed, on orphan rows only, so the account added again
|
||||
-- (a new account id) finds its copies again: those of the general folder come back by themselves, any state, when
|
||||
-- the account is added or its id or name written (by Instagram id when both have one, else by user name, only for a
|
||||
-- row without one); those of its own folder when the folder is attached to it again; any row when its file is
|
||||
-- adopted again by path. Both NULL on an orphan whose account is unknown (a raw delete): it comes back only by
|
||||
-- adoption. Without them a Removed or Superseded row of the general folder, which has no file to adopt, would be
|
||||
-- lost to the account added again and its file copied again by itself (spec §4.10).
|
||||
removed_account_instagram_id TEXT CHECK (removed_account_instagram_id IS NULL OR length(removed_account_instagram_id) > 0),
|
||||
removed_account_username TEXT COLLATE NOCASE CHECK (removed_account_username IS NULL OR length(removed_account_username) > 0),
|
||||
media_id INTEGER REFERENCES media (id) ON DELETE SET NULL,
|
||||
source_sha256 TEXT NOT NULL CHECK (length(source_sha256) = 64),
|
||||
variant TEXT NOT NULL CHECK (variant IN ('Whole', 'Converted', 'Frame')),
|
||||
@@ -115,7 +125,9 @@ CREATE TABLE export_copy (
|
||||
CHECK (copy_sha256 IS NULL OR (variant = 'Whole') = (copy_sha256 = source_sha256)),
|
||||
CHECK (state <> 'Superseded' OR superseded_by_sha256 IS NOT NULL),
|
||||
CHECK (superseded_by_sha256 IS NULL OR state IN ('Superseded', 'Removed')),
|
||||
CHECK (superseded_by_sha256 IS NULL OR superseded_by_sha256 <> source_sha256)
|
||||
CHECK (superseded_by_sha256 IS NULL OR superseded_by_sha256 <> source_sha256),
|
||||
-- The removed account is remembered only while the row has no account: re-linking clears it.
|
||||
CHECK (account_id IS NULL OR (removed_account_instagram_id IS NULL AND removed_account_username IS NULL))
|
||||
);
|
||||
|
||||
-- The copy identity: one current row (Copied or Failed) per identity. NULL account_id (orphan) is distinct.
|
||||
@@ -141,6 +153,9 @@ CREATE INDEX ix_export_copy_superseded_by ON export_copy (superseded_by_sha256,
|
||||
-- Foreign keys with SET NULL.
|
||||
CREATE INDEX ix_export_copy_media ON export_copy (media_id) WHERE media_id IS NOT NULL;
|
||||
CREATE INDEX ix_export_copy_account ON export_copy (account_id) WHERE account_id IS NOT NULL;
|
||||
-- The orphan copies of a removed account, by its Instagram id, or by its user name for rows without one (re-linking).
|
||||
CREATE INDEX ix_export_copy_orphan ON export_copy (removed_account_instagram_id, removed_account_username)
|
||||
WHERE account_id IS NULL;
|
||||
|
||||
-- An export job (on the model of import_run): «Copia in…», the automatic export, «Riconosci le copie già presenti».
|
||||
-- Interrupted (Febe closed) and Paused (destination not reachable) runs resume from their pending items.
|
||||
|
||||
@@ -301,7 +301,12 @@ internal sealed class AccountStore(FebeDatabase db) : IAccountStore
|
||||
ex);
|
||||
}
|
||||
|
||||
return account with { Id = w.LastInsertRowId(), InstagramId = Normalize(account.InstagramId), Username = NormalizeUsername(account.Username) };
|
||||
var added = account with { Id = w.LastInsertRowId(), InstagramId = Normalize(account.InstagramId), Username = NormalizeUsername(account.Username) };
|
||||
|
||||
// An account removed before and added again (a new id) gets back its copies of the general folder, so
|
||||
// its Removed and Superseded copies are never copied again by themselves (spec §4.10).
|
||||
ExportStore.RelinkOrphanCopies(w, added.Id, added.InstagramId, added.Username);
|
||||
return added;
|
||||
},
|
||||
ct);
|
||||
}
|
||||
@@ -337,6 +342,9 @@ internal sealed class AccountStore(FebeDatabase db) : IAccountStore
|
||||
{
|
||||
throw new DuplicateRecordException("Esiste già un account con lo stesso identificativo Instagram, oppure il profilo «Tu» è già presente.", ex);
|
||||
}
|
||||
|
||||
// The Instagram id may be known only now (an imported account): its copies come back as in AddAsync.
|
||||
ExportStore.RelinkOrphanCopies(w, account.Id, Normalize(account.InstagramId), NormalizeUsername(account.Username));
|
||||
},
|
||||
ct);
|
||||
}
|
||||
@@ -407,7 +415,8 @@ internal sealed class AccountStore(FebeDatabase db) : IAccountStore
|
||||
w =>
|
||||
{
|
||||
string? oldName;
|
||||
using (var r = w.Command("SELECT username, folder FROM account WHERE id = @id").With("@id", accountId).ExecuteReader())
|
||||
string? instagramId;
|
||||
using (var r = w.Command("SELECT username, folder, instagram_id FROM account WHERE id = @id").With("@id", accountId).ExecuteReader())
|
||||
{
|
||||
if (!r.Read())
|
||||
{
|
||||
@@ -415,6 +424,7 @@ internal sealed class AccountStore(FebeDatabase db) : IAccountStore
|
||||
}
|
||||
|
||||
oldName = r.GetString(0);
|
||||
instagramId = r.GetTextOrNull(2);
|
||||
}
|
||||
|
||||
if (string.Equals(oldName, name, StringComparison.Ordinal))
|
||||
@@ -434,6 +444,7 @@ internal sealed class AccountStore(FebeDatabase db) : IAccountStore
|
||||
.With("@renamed", (long)AccountNotices.Renamed)
|
||||
.With("@id", accountId)
|
||||
.ExecuteNonQuery();
|
||||
ExportStore.RelinkOrphanCopies(w, accountId, instagramId, name);
|
||||
},
|
||||
ct);
|
||||
}
|
||||
@@ -478,6 +489,18 @@ internal sealed class AccountStore(FebeDatabase db) : IAccountStore
|
||||
automatic = 0, automatic_since = NULL
|
||||
WHERE account_id = @id
|
||||
""").With("@id", accountId).ExecuteNonQuery();
|
||||
|
||||
// Its copies stay, in every destination and state, without account but with its Instagram id and user
|
||||
// name, so the account added again finds them (ExportStore.RelinkOrphanCopies): a Removed or Superseded
|
||||
// copy of the general folder has no file to adopt again. A CHECK allows them only without account, so
|
||||
// the link is cleared here, not by ON DELETE SET NULL. Job items keep only the SET NULL: they are not
|
||||
// what «già copiato» reads.
|
||||
w.Command("""
|
||||
UPDATE export_copy SET account_id = NULL,
|
||||
removed_account_instagram_id = (SELECT instagram_id FROM account WHERE id = @id),
|
||||
removed_account_username = (SELECT username FROM account WHERE id = @id)
|
||||
WHERE account_id = @id
|
||||
""").With("@id", accountId).ExecuteNonQuery();
|
||||
w.Command("DELETE FROM account WHERE id = @id").With("@id", accountId).ExecuteNonQuery();
|
||||
},
|
||||
ct);
|
||||
|
||||
@@ -379,7 +379,10 @@ internal sealed class ExportStore(FebeDatabase db) : IExportStore
|
||||
.With("@a", accountId).With("@id", id).ExecuteNonQuery();
|
||||
// «Automatica» stays off (the removal of the account turned it off): turning it on again asks «Solo i nuovi».
|
||||
Unique(
|
||||
w.Command("UPDATE export_copy SET account_id = @a WHERE destination_id = @id AND account_id IS NULL")
|
||||
w.Command("""
|
||||
UPDATE export_copy SET account_id = @a, removed_account_instagram_id = NULL, removed_account_username = NULL
|
||||
WHERE destination_id = @id AND account_id IS NULL
|
||||
""")
|
||||
.With("@a", accountId).With("@id", id).ExecuteNonQuery,
|
||||
$"L'account {accountId} ha già una copia corrente uguale a una copia della destinazione {id}.");
|
||||
},
|
||||
@@ -553,13 +556,92 @@ internal sealed class ExportStore(FebeDatabase db) : IExportStore
|
||||
}
|
||||
|
||||
Unique(
|
||||
w.Command("UPDATE export_copy SET account_id = @a, media_id = coalesce(@m, media_id), state_changed_at = @at WHERE id = @id")
|
||||
w.Command("""
|
||||
UPDATE export_copy SET account_id = @a, removed_account_instagram_id = NULL, removed_account_username = NULL,
|
||||
media_id = coalesce(@m, media_id), state_changed_at = @at
|
||||
WHERE id = @id
|
||||
""")
|
||||
.With("@a", accountId).With("@m", Sql.Int(mediaId)).With("@at", Sql.Time(at)).With("@id", copyId).ExecuteNonQuery,
|
||||
$"L'account {accountId} ha già una copia corrente uguale alla copia {copyId}.");
|
||||
return ReadFirst(w.Command(select).With("@id", copyId), ReadCopy)!;
|
||||
},
|
||||
ct);
|
||||
|
||||
public Task<int> RelinkOrphanCopiesAsync(long accountId, CancellationToken ct = default) =>
|
||||
db.WriteAsync(
|
||||
w =>
|
||||
{
|
||||
string? instagramId;
|
||||
string username;
|
||||
using (var r = w.Command("SELECT instagram_id, username FROM account WHERE id = @a").With("@a", accountId).ExecuteReader())
|
||||
{
|
||||
if (!r.Read())
|
||||
{
|
||||
throw new KeyNotFoundException($"Account {accountId} non trovato.");
|
||||
}
|
||||
|
||||
instagramId = r.GetTextOrNull(0);
|
||||
username = r.GetString(1);
|
||||
}
|
||||
|
||||
return RelinkOrphanCopies(w, accountId, instagramId, username);
|
||||
},
|
||||
ct);
|
||||
|
||||
/// <summary>
|
||||
/// The orphan copies of the general folder (retired rows included) whose removed account is this one (@iid, @u): by
|
||||
/// Instagram id when the row has one, else by user name (NOCASE). Two branches over ix_export_copy_orphan, so the
|
||||
/// lookup never walks the copies of the general folder (an OR of the two would).
|
||||
/// </summary>
|
||||
private const string OrphansOfAccount = """
|
||||
o.id IN (
|
||||
SELECT id FROM export_copy WHERE account_id IS NULL AND removed_account_instagram_id = @iid
|
||||
UNION ALL
|
||||
SELECT id FROM export_copy WHERE account_id IS NULL AND removed_account_instagram_id IS NULL AND removed_account_username = @u)
|
||||
AND o.destination_id IN (SELECT id FROM export_destination WHERE scope = 'General')
|
||||
""";
|
||||
|
||||
/// <summary>
|
||||
/// Current (Copied or Failed) orphans come back only as the newest of their identity, and only when the account has
|
||||
/// no current row of that identity: the others stay orphans, as history, so ux_export_copy_identity is never broken
|
||||
/// (a user name can match an account that already has copies, and two current orphans of one identity can exist).
|
||||
/// </summary>
|
||||
internal const string RelinkCurrentOrphansSql = $"""
|
||||
UPDATE export_copy SET account_id = @a, removed_account_instagram_id = NULL, removed_account_username = NULL
|
||||
WHERE id IN (
|
||||
SELECT max(o.id) FROM export_copy o
|
||||
WHERE {OrphansOfAccount} AND o.state IN ('Copied', 'Failed')
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM export_copy c
|
||||
WHERE c.account_id = @a AND c.destination_id = o.destination_id AND c.source_sha256 = o.source_sha256
|
||||
AND c.variant = o.variant AND c.format = o.format AND ifnull(c.frame_at_ms, -1) = ifnull(o.frame_at_ms, -1)
|
||||
AND c.state IN ('Copied', 'Failed'))
|
||||
GROUP BY o.destination_id, o.source_sha256, o.variant, o.format, ifnull(o.frame_at_ms, -1))
|
||||
""";
|
||||
|
||||
/// <summary>
|
||||
/// Removed and Superseded orphans always come back: no unique index holds them by identity, and the path does not
|
||||
/// change, so ux_export_copy_path cannot be broken either.
|
||||
/// </summary>
|
||||
internal const string RelinkHistoryOrphansSql = $"""
|
||||
UPDATE export_copy AS o SET account_id = @a, removed_account_instagram_id = NULL, removed_account_username = NULL
|
||||
WHERE {OrphansOfAccount} AND o.state IN ('Removed', 'Superseded')
|
||||
""";
|
||||
|
||||
/// <summary>
|
||||
/// Gives an account its orphan copies of the general folder (see the two statements above). Run inside the
|
||||
/// transaction of the caller: AccountStore.AddAsync, UpdateAsync, RenameAsync and <see cref="RelinkOrphanCopiesAsync"/>.
|
||||
/// Copies of an own folder are left to <see cref="AttachDestinationAsync"/>, a choice of Alberto.
|
||||
/// </summary>
|
||||
internal static int RelinkOrphanCopies(WriteContext w, long accountId, string? instagramId, string username)
|
||||
{
|
||||
var current = w.Command(RelinkCurrentOrphansSql)
|
||||
.With("@a", accountId).With("@iid", Sql.Text(instagramId)).With("@u", username).ExecuteNonQuery();
|
||||
var history = w.Command(RelinkHistoryOrphansSql)
|
||||
.With("@a", accountId).With("@iid", Sql.Text(instagramId)).With("@u", username).ExecuteNonQuery();
|
||||
return current + history;
|
||||
}
|
||||
|
||||
public Task<int> SupersedeCopiesAsync(long accountId, string oldSha256, string newSha256, DateTimeOffset at, CancellationToken ct = default)
|
||||
{
|
||||
var old = ExportValues.Sha(oldSha256, nameof(oldSha256));
|
||||
|
||||
Reference in new issue
Block a user