select_deleted
select_deleted finds objects that matched your query in a past state but no longer do in a more recent state. It's perfect for tracking which items were "removed" from a specific view or collection between two points in time. 🗑️
An object is considered deleted if it no longer satisfies the query conditions. This could happen if you remove a tag, change an attribute's value, or if the object itself is garbage-collected.
db0.select_deleted(query, pre_snapshot, last_snapshot)Parameters
query (dbzero.Query)
The query that defines the set of objects to check in both snapshots.
pre_snapshot (dbzero.Snapshot)
A snapshot representing the state before any changes. This is your baseline.
last_snapshot (dbzero.Snapshot)
A snapshot representing the state after the changes. This is what you'll compare against the baseline.
Returns
A list of objects that matched the query in pre_snapshot but are missing from the query results in last_snapshot. If there's no initial state (pre_snapshot is None), it just returns an empty list.
Example
Here's how to find a Task object that was untagged between two commits, effectively "deleting" it from the "active" view.
import dbzero as db0
@db0.memo
class Task:
def __init__(self, name):
self.name = name
# --- State 1: Setup ---
# Create two tasks and tag them as 'active'
task1 = Task("Write docs")
db0.tags(task1).add("active")
task2 = Task("Review code")
db0.tags(task2).add("active")
db0.commit()
# Capture the "before" snapshot
snap_before = db0.snapshot()
# --- State 2: Make a Change ---
# "Delete" task1 from the 'active' view by removing its tag
db0.tags(task1).remove("active")
db0.commit()
# Capture the "after" snapshot
snap_after = db0.snapshot()
# --- Analysis: Find What Was Deleted ---
# Define the query for active tasks
active_tasks_query = db0.find(Task, "active")
# Find which tasks were removed from the query results
deleted_tasks = db0.select_deleted(
active_tasks_query,
snap_before,
snap_after
)
assert len(deleted_tasks) == 1
assert deleted_tasks[0].name == "Write docs"
print(f"Deleted task: {deleted_tasks[0].name}")
# Expected output: Deleted task: Write docs