Remove manual tagging
A tag could come from two places: rules.toml, or the TUI's t key, which wrote manual_tag with COALESCE(manual_tag, rule_tag) deciding the winner. That split paid for itself in the first invariant of the codebase, in ClearOverrides and the c key, in the * marker on the tag column, and in the one exception to a disposable index -- a tag set by hand was the only thing in index.db that the statements could not reproduce. Now rules.toml decides every tag. The index is derived entirely from the statements plus that file, so deleting it and re-importing gets back exactly what was there, and retag has nothing to be careful of. Tagging a one-off means writing a narrow rule on screen 4, which previews what the glob catches before it is saved. A manual tag in an existing index is dropped along with the column the first time this build opens it, and those rows read as whatever the rules say, or as untagged. TestManualTagSurvivesRetag guarded the invariant that has just been removed; TestRetagRewritesEveryTag replaces it with the one that took its place, and keeps Retag itself covered. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+9
-24
@@ -51,7 +51,6 @@ CREATE TABLE IF NOT EXISTS transactions (
|
||||
type TEXT NOT NULL DEFAULT '',
|
||||
balance_minor INTEGER,
|
||||
rule_tag TEXT,
|
||||
manual_tag TEXT,
|
||||
UNIQUE(account_id, fingerprint)
|
||||
);
|
||||
|
||||
@@ -69,7 +68,7 @@ var migrations = []string{
|
||||
// dropped are columns an older build created that this one no longer reads.
|
||||
// Nothing indexes or constrains them, so they can simply go; leaving them
|
||||
// would keep a NOT NULL column alive that no INSERT here ever names.
|
||||
var dropped = []string{"counterparty", "rule_transfer", "manual_transfer"}
|
||||
var dropped = []string{"counterparty", "rule_transfer", "manual_transfer", "manual_tag"}
|
||||
|
||||
// migrate brings an index created by an older build up to date, adding the
|
||||
// columns it lacks and removing the ones it should no longer have.
|
||||
@@ -225,8 +224,8 @@ func (d *DB) SourceFileSHA(accountID int64, path string) (string, bool, error) {
|
||||
// InsertTransaction adds a transaction unless its fingerprint already exists
|
||||
// for that account. It reports whether a new row was created.
|
||||
//
|
||||
// Existing rows are deliberately left untouched: re-importing an overlapping
|
||||
// statement must never clobber tags the user set by hand.
|
||||
// Existing rows are deliberately left untouched, so re-importing a statement
|
||||
// that overlaps one already imported adds nothing rather than duplicating it.
|
||||
func (d *DB) InsertTransaction(t model.Transaction) (bool, error) {
|
||||
var balance any
|
||||
if t.BalanceMinor != nil {
|
||||
@@ -265,7 +264,7 @@ func (d *DB) Transactions(f Filter) ([]model.Transaction, error) {
|
||||
SELECT t.id, t.account_id, a.slug, a.currency, a.minor_digits,
|
||||
t.fingerprint, t.date, t.description, t.amount_minor,
|
||||
COALESCE(s.path, ''), t.type, t.balance_minor,
|
||||
COALESCE(t.rule_tag, ''), COALESCE(t.manual_tag, '')
|
||||
COALESCE(t.rule_tag, '')
|
||||
FROM transactions t
|
||||
JOIN accounts a ON a.id = t.account_id
|
||||
LEFT JOIN source_files s ON s.id = t.source_file_id
|
||||
@@ -276,7 +275,7 @@ func (d *DB) Transactions(f Filter) ([]model.Transaction, error) {
|
||||
args = append(args, f.AccountSlug)
|
||||
}
|
||||
if f.Untagged {
|
||||
q += ` AND COALESCE(NULLIF(t.manual_tag, ''), NULLIF(t.rule_tag, '')) IS NULL`
|
||||
q += ` AND NULLIF(t.rule_tag, '') IS NULL`
|
||||
}
|
||||
if f.Month != "" {
|
||||
q += ` AND substr(t.date, 1, 7) = ?`
|
||||
@@ -301,8 +300,7 @@ func (d *DB) Transactions(f Filter) ([]model.Transaction, error) {
|
||||
)
|
||||
if err := rows.Scan(&t.ID, &t.AccountID, &t.AccountSlug, &t.Currency, &t.MinorDigits,
|
||||
&t.Fingerprint, &t.Date, &t.Description, &t.AmountMinor, &t.SourcePath,
|
||||
&t.Type, &balance,
|
||||
&t.RuleTag, &t.ManualTag); err != nil {
|
||||
&t.Type, &balance, &t.RuleTag); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if balance.Valid {
|
||||
@@ -320,19 +318,6 @@ func (d *DB) Transactions(f Filter) ([]model.Transaction, error) {
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// SetManualTag sets (or, with an empty tag, clears) the user's tag override.
|
||||
func (d *DB) SetManualTag(id int64, tag string) error {
|
||||
_, err := d.sql.Exec(`UPDATE transactions SET manual_tag = NULLIF(?, '') WHERE id = ?`, tag, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// ClearOverrides drops the manual tag for a transaction, so the rules decide
|
||||
// it again.
|
||||
func (d *DB) ClearOverrides(id int64) error {
|
||||
_, err := d.sql.Exec(`UPDATE transactions SET manual_tag = NULL WHERE id = ?`, id)
|
||||
return err
|
||||
}
|
||||
|
||||
// RuleAssignment is one row's recomputed rule verdict.
|
||||
type RuleAssignment struct {
|
||||
ID int64
|
||||
@@ -382,11 +367,11 @@ func (d *DB) Count(accountID int64) (int, error) {
|
||||
return n, err
|
||||
}
|
||||
|
||||
// Tags lists every effective tag in use, for completion in the TUI.
|
||||
// Tags lists every tag in use, for completion in the TUI.
|
||||
func (d *DB) Tags() ([]string, error) {
|
||||
rows, err := d.sql.Query(`
|
||||
SELECT DISTINCT COALESCE(NULLIF(manual_tag, ''), NULLIF(rule_tag, '')) AS tag
|
||||
FROM transactions WHERE tag IS NOT NULL ORDER BY tag`)
|
||||
SELECT DISTINCT rule_tag FROM transactions
|
||||
WHERE rule_tag IS NOT NULL AND rule_tag != '' ORDER BY rule_tag`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user