From be1c0b097bda697227aac1674a37cbcfd8b51ac3 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Wed, 12 Aug 2026 22:31:46 -0700 Subject: [PATCH] Preserve snapshot completion from 0.8 betas --- .../core/migrations/0023_upgrade_to_0_9_0.py | 34 +++++++++++-------- archivebox/tests/test_migrations_08_to_09.py | 8 +++-- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/archivebox/core/migrations/0023_upgrade_to_0_9_0.py b/archivebox/core/migrations/0023_upgrade_to_0_9_0.py index 0bcd6b19..2bc9101a 100644 --- a/archivebox/core/migrations/0023_upgrade_to_0_9_0.py +++ b/archivebox/core/migrations/0023_upgrade_to_0_9_0.py @@ -318,6 +318,22 @@ def upgrade_core_tables(apps, schema_editor): snapshot_cols = get_table_columns("core_snapshot") has_added = "added" in snapshot_cols has_bookmarked_at = "bookmarked_at" in snapshot_cols + inferred_status_sql = """ + CASE + WHEN EXISTS ( + SELECT 1 FROM core_archiveresult + WHERE core_archiveresult.snapshot_id = core_snapshot.id + AND core_archiveresult.status IN ('queued', 'started', 'backoff') + ) + THEN 'queued' + WHEN EXISTS ( + SELECT 1 FROM core_archiveresult + WHERE core_archiveresult.snapshot_id = core_snapshot.id + ) + THEN 'sealed' + ELSE 'queued' + END + """ if has_added and not has_bookmarked_at: # Migrating from v0.7.2 (has added/updated fields) @@ -343,20 +359,7 @@ def upgrade_core_tables(apps, schema_editor): COALESCE(added, CURRENT_TIMESTAMP) as created_at, COALESCE(updated, added, CURRENT_TIMESTAMP) as modified_at, updated as downloaded_at, - CASE - WHEN EXISTS ( - SELECT 1 FROM core_archiveresult - WHERE core_archiveresult.snapshot_id = core_snapshot.id - AND core_archiveresult.status IN ('queued', 'started', 'backoff') - ) - THEN 'queued' - WHEN EXISTS ( - SELECT 1 FROM core_archiveresult - WHERE core_archiveresult.snapshot_id = core_snapshot.id - ) - THEN 'sealed' - ELSE 'queued' - END as status + {inferred_status_sql} as status FROM core_snapshot; """) print(f" copied {cursor.rowcount} Snapshots") @@ -398,6 +401,9 @@ def upgrade_core_tables(apps, schema_editor): END """, ) + else: + insert_cols.append("status") + select_cols.append(inferred_status_sql) if has_retry_at: insert_cols.append("retry_at") select_cols.append("retry_at") diff --git a/archivebox/tests/test_migrations_08_to_09.py b/archivebox/tests/test_migrations_08_to_09.py index 375e99b3..02bcbd0d 100644 --- a/archivebox/tests/test_migrations_08_to_09.py +++ b/archivebox/tests/test_migrations_08_to_09.py @@ -408,7 +408,8 @@ def test_migration_preserves_foreign_keys(migration_08_data): assert ok, msg -def test_migration_preserves_08_timestamp_meanings(migration_08_data): +@pytest.mark.parametrize("has_snapshot_status", (True, False), ids=("with-status", "without-status")) +def test_migration_preserves_08_timestamp_meanings(migration_08_data, has_snapshot_status): """0.8.x already has separated timestamp/bookmarked_at/created_at/downloaded_at fields.""" work_dir, db_path, original_data = migration_08_data snapshot = original_data["snapshots"][0] @@ -420,6 +421,8 @@ def test_migration_preserves_08_timestamp_meanings(migration_08_data): conn = sqlite3.connect(str(db_path)) cursor = conn.cursor() + if not has_snapshot_status: + cursor.execute("ALTER TABLE core_snapshot DROP COLUMN status") cursor.execute( """ UPDATE core_snapshot @@ -437,7 +440,7 @@ def test_migration_preserves_08_timestamp_meanings(migration_08_data): conn = sqlite3.connect(str(db_path)) cursor = conn.cursor() cursor.execute( - "SELECT timestamp, bookmarked_at, created_at, modified_at, downloaded_at FROM core_snapshot WHERE id = ?", + "SELECT timestamp, bookmarked_at, created_at, modified_at, downloaded_at, status FROM core_snapshot WHERE id = ?", (snapshot["id"],), ) migrated = cursor.fetchone() @@ -448,6 +451,7 @@ def test_migration_preserves_08_timestamp_meanings(migration_08_data): assert migrated[2].startswith("2024-08-28"), migrated[2] assert migrated[3].startswith("2024-08-29"), migrated[3] assert migrated[4].startswith("2024-08-30"), migrated[4] + assert migrated[5] == "sealed" def test_hyphenated_crawl_ids_are_normalized_before_snapshot_saves(migration_08_data):