Development

This commit is contained in:
Matthew Grotke 2026-06-03 16:04:51 -04:00
parent 8ab88094b3
commit 6ebf7027fd
4 changed files with 65 additions and 23 deletions

View file

@ -357,7 +357,8 @@ def _db():
parent_path TEXT NOT NULL,
item_key TEXT,
item_value TEXT,
reverts_group TEXT
reverts_group TEXT,
reverted INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS changes (
group_id TEXT NOT NULL REFERENCES groups(uuid),
@ -510,8 +511,9 @@ def load_all_groups():
conn.close()
def revert_group(group_uuid):
"""Revert a change group. Returns (flash_message, success_bool)."""
def revert_group(group_uuid, force=False):
"""Revert a change group. Returns (flash_message, success_bool).
force=True skips the revert-of-revert guard, used by revert_group_chain."""
conn = _db()
try:
g = conn.execute('SELECT * FROM groups WHERE uuid=?', (group_uuid,)).fetchone()
@ -524,7 +526,7 @@ def revert_group(group_uuid):
finally:
conn.close()
if g['reverts_group']:
if g['reverts_group'] and not force:
return 'Cannot revert a revert.', False
cfg = load_config()
@ -556,9 +558,45 @@ def revert_group(group_uuid):
inv = [(c['field'], c['after'], c['before'], c['value_type']) for c in changes]
msg = record_group(cfg, parent_path, item_key, item_value, inv,
g['cmd'], reverts_group=group_uuid)
conn = _db()
try:
conn.execute('UPDATE groups SET reverted=1 WHERE uuid=?', (group_uuid,))
conn.commit()
finally:
conn.close()
return msg, True
def revert_group_chain(group_uuid):
"""Revert group_uuid and all subsequent groups touching the same item
(same parent_path + item_key + item_value), newest first.
Returns (error_messages, succeeded_count, failed_count)."""
conn = _db()
try:
g = conn.execute('SELECT * FROM groups WHERE uuid=?', (group_uuid,)).fetchone()
if not g:
return [f'Snapshot not found for {group_uuid[:8]}.'], 0, 1
g = dict(g)
chain = [dict(r) for r in conn.execute(
'SELECT * FROM groups '
'WHERE parent_path=? AND item_key IS ? AND item_value IS ? AND ts >= ? AND reverted=0 '
'ORDER BY ts DESC',
(g['parent_path'], g['item_key'], g['item_value'], g['ts'])
).fetchall()]
finally:
conn.close()
errors, succeeded, failed = [], 0, 0
for grp in chain:
msg, ok = revert_group(grp['uuid'], force=True)
if ok:
succeeded += 1
else:
errors.append(msg)
failed += 1
return errors, succeeded, failed
# Misc ==============================================================
def run_apply():