Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Error migrating instances using document_exporter with split-manifest
### Description

I'm trying to create a fresh instance of my exisitng paperless-ngx to test v2.
To do that, I created an export using the `document_exporter` with `1.17.4`. Then I spin up another `1.17.4` instance with identical settings (despite the URL). The import seems to work fine. But when I try to login I run into an server error.




### Steps to reproduce

1. Create export: `docker exec -i -t paperless-webserver-1 document_exporter -z -sm ../export/migate`
2. Spin up new instance with same settings. The service URL will be the only change.
3. Copy export zip to new instance and unzip
4. Import th export: `docker exec -it paperless-webserver-1 document_importer ../export`
5. Access new paperless instance -> Server error happens

![grafik](https://github.com/paperless-ngx/paperless-ngx/assets/7643434/96da7930-ce82-434b-bcfc-9c7bce3fc9f3)


### Webserver logs

```bash
paperless-webserver-1  | -- ******* ---- Linux-6.5.0-1006-raspi-aarch64-with-glibc2.36 2023-11-17 18:25:44
paperless-webserver-1  | - *** --- * ---
paperless-webserver-1  | - ** ---------- [config]
paperless-webserver-1  | - ** ---------- .> app:         paperless:0xffff958dd520
paperless-webserver-1  | - ** ---------- .> transport:   redis://broker:6379//
paperless-webserver-1  | - ** ---------- .> results:
paperless-webserver-1  | - *** --- * --- .> concurrency: 1 (prefork)
paperless-webserver-1  | -- ******* ---- .> task events: ON
paperless-webserver-1  | --- ***** -----
paperless-webserver-1  |  -------------- [queues]
paperless-webserver-1  |                 .> celery           exchange=celery(direct) key=celery
paperless-webserver-1  |
paperless-webserver-1  |
paperless-webserver-1  | [tasks]
paperless-webserver-1  |   . documents.tasks.bulk_update_documents
paperless-webserver-1  |   . documents.tasks.consume_file
paperless-webserver-1  |   . documents.tasks.index_optimize
paperless-webserver-1  |   . documents.tasks.sanity_check
paperless-webserver-1  |   . documents.tasks.train_classifier
paperless-webserver-1  |   . documents.tasks.update_document_archive_file
paperless-webserver-1  |   . paperless_mail.mail.apply_mail_action
paperless-webserver-1  |   . paperless_mail.mail.error_callback
paperless-webserver-1  |   . paperless_mail.tasks.process_mail_accounts
paperless-webserver-1  |
paperless-webserver-1  | [2023-11-17 18:25:44,448] [INFO] [celery.worker.consumer.connection] Connected to redis://broker:6379//
paperless-webserver-1  | [2023-11-17 18:25:44,474] [INFO] [celery.apps.worker] celery@a7071dc79846 ready.
paperless-webserver-1  | [2023-11-17 18:28:49,462] [ERROR] [django.request] Internal Server Error: /api/documents/
paperless-webserver-1  | Traceback (most recent call last):
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 534, in thread_handler
paperless-webserver-1  |     raise exc_info[1]
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 43, in inner
paperless-webserver-1  |     response = await get_response(request)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 253, in _get_response_async
paperless-webserver-1  |     response = await wrapped_callback(
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 479, in __call__
paperless-webserver-1  |     ret: _R = await loop.run_in_executor(
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/current_thread_executor.py", line 40, in run
paperless-webserver-1  |     result = self.fn(*self.args, **self.kwargs)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 538, in thread_handler
paperless-webserver-1  |     return func(*args, **kwargs)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 55, in wrapped_view
paperless-webserver-1  |     return view_func(*args, **kwargs)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework/viewsets.py", line 125, in view
paperless-webserver-1  |     return self.dispatch(request, *args, **kwargs)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch
paperless-webserver-1  |     response = self.handle_exception(exc)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 469, in handle_exception
paperless-webserver-1  |     self.raise_uncaught_exception(exc)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
paperless-webserver-1  |     raise exc
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch
paperless-webserver-1  |     response = handler(request, *args, **kwargs)
paperless-webserver-1  |   File "/usr/src/paperless/src/documents/views.py", line 650, in list
paperless-webserver-1  |     return super().list(request)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework/mixins.py", line 38, in list
paperless-webserver-1  |     queryset = self.filter_queryset(self.get_queryset())
paperless-webserver-1  |   File "/usr/src/paperless/src/documents/views.py", line 632, in filter_queryset
paperless-webserver-1  |     return super().filter_queryset(queryset)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework/generics.py", line 150, in filter_queryset
paperless-webserver-1  |     queryset = backend().filter_queryset(self.request, queryset, self)
paperless-webserver-1  |   File "/usr/src/paperless/src/documents/filters.py", line 160, in filter_queryset
paperless-webserver-1  |     objects_with_perms = super().filter_queryset(request, queryset, view)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework_guardian/filters.py", line 28, in filter_queryset
paperless-webserver-1  |     return get_objects_for_user(
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/guardian/shortcuts.py", line 515, in get_objects_for_user
paperless-webserver-1  |     new_ctype = ContentType.objects.get(app_label=app_label,
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
paperless-webserver-1  |     return getattr(self.get_queryset(), name)(*args, **kwargs)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 650, in get
paperless-webserver-1  |     raise self.model.DoesNotExist(
paperless-webserver-1  | django.contrib.contenttypes.models.ContentType.DoesNotExist: ContentType matching query does not exist.
paperless-webserver-1  | [2023-11-17 18:28:49,492] [ERROR] [django.request] Internal Server Error: /api/tags/
paperless-webserver-1  | Traceback (most recent call last):
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 534, in thread_handler
paperless-webserver-1  |     raise exc_info[1]
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/exception.py", line 43, in inner
paperless-webserver-1  |     response = await get_response(request)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/django/core/handlers/base.py", line 253, in _get_response_async
paperless-webserver-1  |     response = await wrapped_callback(
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 479, in __call__
paperless-webserver-1  |     ret: _R = await loop.run_in_executor(
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/current_thread_executor.py", line 40, in run
paperless-webserver-1  |     result = self.fn(*self.args, **self.kwargs)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/asgiref/sync.py", line 538, in thread_handler
paperless-webserver-1  |     return func(*args, **kwargs)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/django/views/decorators/csrf.py", line 55, in wrapped_view
paperless-webserver-1  |     return view_func(*args, **kwargs)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework/viewsets.py", line 125, in view
paperless-webserver-1  |     return self.dispatch(request, *args, **kwargs)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 509, in dispatch
paperless-webserver-1  |     response = self.handle_exception(exc)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 469, in handle_exception
paperless-webserver-1  |     self.raise_uncaught_exception(exc)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
paperless-webserver-1  |     raise exc
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework/views.py", line 506, in dispatch
paperless-webserver-1  |     response = handler(request, *args, **kwargs)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework/mixins.py", line 38, in list
paperless-webserver-1  |     queryset = self.filter_queryset(self.get_queryset())
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework/generics.py", line 150, in filter_queryset
paperless-webserver-1  |     queryset = backend().filter_queryset(self.request, queryset, self)
paperless-webserver-1  |   File "/usr/src/paperless/src/documents/filters.py", line 160, in filter_queryset
paperless-webserver-1  |     objects_with_perms = super().filter_queryset(request, queryset, view)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/rest_framework_guardian/filters.py", line 28, in filter_queryset
paperless-webserver-1  |     return get_objects_for_user(
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/guardian/shortcuts.py", line 515, in get_objects_for_user
paperless-webserver-1  |     new_ctype = ContentType.objects.get(app_label=app_label,
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/django/db/models/manager.py", line 85, in manager_method
paperless-webserver-1  |     return getattr(self.get_queryset(), name)(*args, **kwargs)
paperless-webserver-1  |   File "/usr/local/lib/python3.9/site-packages/django/db/models/query.py", line 650, in get
paperless-webserver-1  |     raise self.model.DoesNotExist(
paperless-webserver-1  | django.contrib.contenttypes.models.ContentType.DoesNotExist: ContentType matching query does not exist.
```


### Browser logs

_No response_

### Paperless-ngx version

1.17.4

### Host OS

Raspberry Pi5, ARM64, Ubuntu 23.10

### Installation method

Docker - official image

### Browser

Firefox

### Configuration changes

https://github.com/BWibo/paperless-ngx-compose/blob/main/docker-compose.env

### Other

_No response_


Discussion:
Just tested with my export from before updating to the beta (so 1.17.4).  An import into a fresh 1.17.4 worked fine.

Honestly, even if there is some bug here, there's not going to be another release to fix anything.
Ah, don't use the split manifest.  I'll fix it for the v2 beta, but that's going to be the solution for 1.17.4

Resolving patch:
diff --git a/src/documents/management/commands/document_importer.py b/src/documents/management/commands/document_importer.py
--- a/src/documents/management/commands/document_importer.py
+++ b/src/documents/management/commands/document_importer.py
@@ -118,10 +118,10 @@ def handle(self, *args, **options):
             # Fill up the database with whatever is in the manifest
             try:
                 with transaction.atomic():
+                    # delete these since pk can change, re-created from import
+                    ContentType.objects.all().delete()
+                    Permission.objects.all().delete()
                     for manifest_path in manifest_paths:
-                        # delete these since pk can change, re-created from import
-                        ContentType.objects.all().delete()
-                        Permission.objects.all().delete()
                         call_command("loaddata", manifest_path)
             except (FieldDoesNotExist, DeserializationError, IntegrityError) as e:
                 self.stdout.write(self.style.ERROR("Database import failed"))


Test patch: