From d064a3eeffa0a6cb52462ce1f2edb0d6be8f753a Mon Sep 17 00:00:00 2001
From: Cristian <cristian@swapps.com>
Date: Wed, 4 Nov 2020 15:02:54 -0500
Subject: [PATCH] fix: Handle case when update tries to re-add a link that is
 not in the sql index

---
 archivebox/extractors/__init__.py | 6 +++++-
 tests/test_update.py              | 3 ++-
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/archivebox/extractors/__init__.py b/archivebox/extractors/__init__.py
index 23a4f5ef..e27b9d80 100644
--- a/archivebox/extractors/__init__.py
+++ b/archivebox/extractors/__init__.py
@@ -8,6 +8,7 @@ from datetime import datetime
 from django.db.models import QuerySet
 
 from ..index.schema import Link
+from ..index.sql import write_link_to_sql_index
 from ..index import (
     load_link_details,
     write_link_details,
@@ -68,7 +69,10 @@ def archive_link(link: Link, overwrite: bool=False, methods: Optional[Iterable[s
     # TODO: Remove when the input is changed to be a snapshot. Suboptimal approach.
     if not skip_index:
         from core.models import Snapshot, ArchiveResult
-        snapshot = Snapshot.objects.get(url=link.url)
+        try:
+            snapshot = Snapshot.objects.get(url=link.url) # TODO: This will be unnecessary once everything is a snapshot
+        except Snapshot.DoesNotExist:
+            write_link_to_sql_index(link)
 
     ARCHIVE_METHODS = get_default_archive_methods()
     
diff --git a/tests/test_update.py b/tests/test_update.py
index 238a92d9..29db0174 100644
--- a/tests/test_update.py
+++ b/tests/test_update.py
@@ -6,7 +6,7 @@ def test_update_status_invalid(tmp_path, process, disable_extractors_dict):
     subprocess.run(['archivebox', 'add', 'http://127.0.0.1:8080/static/example.com.html'], capture_output=True, env=disable_extractors_dict)
     assert list((tmp_path / "archive").iterdir()) != []
 
-    subprocess.run(['archivebox', 'remove', 'http://127.0.0.1:8080/static/example.com.html', '--yes'], capture_output=True)
+    a_process = subprocess.run(['archivebox', 'remove', 'http://127.0.0.1:8080/static/example.com.html', '--yes'], capture_output=True)
 
     conn = sqlite3.connect(str(tmp_path / "index.sqlite3"))
     c = conn.cursor()
@@ -17,6 +17,7 @@ def test_update_status_invalid(tmp_path, process, disable_extractors_dict):
     assert link is None
 
     update_process = subprocess.run(['archivebox', 'update', '--status=invalid'], capture_output=True, env=disable_extractors_dict)
+    #breakpoint()
 
     conn = sqlite3.connect(str(tmp_path / "index.sqlite3"))
     c = conn.cursor()