Skip to content

A community-supported supercharged document management system: scan, index and archive all your documents

making the migration reversible

Jonas Winkler321adb5df251
diff --git a/src/documents/migrations/1003_mime_types.py b/src/documents/migrations/1003_mime_types.py
index 4c73a4235..1038d57b3 100644
--- a/src/documents/migrations/1003_mime_types.py
+++ b/src/documents/migrations/1003_mime_types.py
@@ -1,4 +1,5 @@
# Generated by Django 3.1.3 on 2020-11-20 11:21
+import mimetypes
import os
import magic
@@ -29,6 +30,15 @@ def add_mime_types(apps, schema_editor):
d.save()
+def add_file_extensions(apps, schema_editor):
+ Document = apps.get_model("documents", "Document")
+ documents = Document.objects.all()
+
+ for d in documents:
+ d.file_type = os.path.splitext(d.filename)[1].strip('.')
+ d.save()
+
+
class Migration(migrations.Migration):
dependencies = [
@@ -42,7 +52,24 @@ class Migration(migrations.Migration):
field=models.CharField(default="-", editable=False, max_length=256),
preserve_default=False,
),
- migrations.RunPython(add_mime_types),
+ migrations.RunPython(add_mime_types, migrations.RunPython.noop),
+
+ # This operation is here so that we can revert the entire migration:
+ # By allowing this field to be blank and null, we can revert the
+ # remove operation further down and the database won't complain about
+ # NOT NULL violations.
+ migrations.AlterField(
+ model_name='document',
+ name='file_type',
+ field=models.CharField(
+ choices=[('pdf', 'PDF'), ('png', 'PNG'), ('jpg', 'JPG'), ('gif', 'GIF'), ('tiff', 'TIFF'), ('txt', 'TXT'), ('csv', 'CSV'), ('md', 'MD')],
+ editable=False,
+ max_length=4,
+ null=True,
+ blank=True
+ ),
+ ),
+ migrations.RunPython(migrations.RunPython.noop, add_file_extensions),
migrations.RemoveField(
model_name='document',
name='file_type',