mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 03:16:10 -06:00 
			
		
		
		
	fix the migration history and provide a script to update existing paperless instances to the new version
This commit is contained in:
		
							
								
								
									
										37
									
								
								src/documents/migrations/0023_document_current_filename.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								src/documents/migrations/0023_document_current_filename.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,37 @@
 | 
			
		||||
# Generated by Django 2.0.10 on 2019-04-26 18:57
 | 
			
		||||
 | 
			
		||||
from django.db import migrations, models
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def set_filename(apps, schema_editor):
 | 
			
		||||
    Document = apps.get_model("documents", "Document")
 | 
			
		||||
    for doc in Document.objects.all():
 | 
			
		||||
        file_name = "{:07}.{}".format(doc.pk, doc.file_type)
 | 
			
		||||
        if doc.storage_type == "gpg":
 | 
			
		||||
            file_name += ".gpg"
 | 
			
		||||
 | 
			
		||||
        # Set filename
 | 
			
		||||
        doc.filename = file_name
 | 
			
		||||
 | 
			
		||||
        # Save document
 | 
			
		||||
        doc.save()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Migration(migrations.Migration):
 | 
			
		||||
 | 
			
		||||
    dependencies = [
 | 
			
		||||
        ('documents', '0022_auto_20181007_1420'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    operations = [
 | 
			
		||||
        migrations.AddField(
 | 
			
		||||
            model_name='document',
 | 
			
		||||
            name='filename',
 | 
			
		||||
            field=models.FilePathField(default=None,
 | 
			
		||||
                                       null=True,
 | 
			
		||||
                                       editable=False,
 | 
			
		||||
                                       help_text='Current filename in storage',
 | 
			
		||||
                                       max_length=256),
 | 
			
		||||
        ),
 | 
			
		||||
        migrations.RunPython(set_filename, migrations.RunPython.noop)
 | 
			
		||||
    ]
 | 
			
		||||
							
								
								
									
										57
									
								
								src/documents/migrations/1000_update_paperless.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								src/documents/migrations/1000_update_paperless.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,57 @@
 | 
			
		||||
# Generated by Django 3.1.2 on 2020-10-29 14:29
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
from django.db import migrations
 | 
			
		||||
 | 
			
		||||
from django.conf import settings
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def make_index(apps, schema_editor):
 | 
			
		||||
    Document = apps.get_model("documents", "Document")
 | 
			
		||||
    documents = Document.objects.all()
 | 
			
		||||
    print()
 | 
			
		||||
    try:
 | 
			
		||||
        print("  --> Creating document index...")
 | 
			
		||||
        from whoosh.writing import AsyncWriter
 | 
			
		||||
        from documents import index
 | 
			
		||||
        ix = index.open_index(recreate=True)
 | 
			
		||||
        with AsyncWriter(ix) as writer:
 | 
			
		||||
            for document in documents:
 | 
			
		||||
                index.update_document(writer, document)
 | 
			
		||||
    except ImportError:
 | 
			
		||||
        # index may not be relevant anymore
 | 
			
		||||
        print("  --> Cannot create document index.")
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def restore_filenames(apps, schema_editor):
 | 
			
		||||
    Document = apps.get_model("documents", "Document")
 | 
			
		||||
    for doc in Document.objects.all():
 | 
			
		||||
        file_name = "{:07}.{}".format(doc.pk, doc.file_type)
 | 
			
		||||
        if doc.storage_type == "gpg":
 | 
			
		||||
            file_name += ".gpg"
 | 
			
		||||
 | 
			
		||||
        if not doc.filename == file_name:
 | 
			
		||||
            try:
 | 
			
		||||
                print("file was renamed, restoring {} to {}".format(doc.filename, file_name))
 | 
			
		||||
                os.rename(os.path.join(settings.ORIGINALS_DIR, doc.filename),
 | 
			
		||||
                          os.path.join(settings.ORIGINALS_DIR, file_name))
 | 
			
		||||
            except PermissionError:
 | 
			
		||||
                pass
 | 
			
		||||
            except FileNotFoundError:
 | 
			
		||||
                pass
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class Migration(migrations.Migration):
 | 
			
		||||
 | 
			
		||||
    dependencies = [
 | 
			
		||||
        ('documents', '0023_document_current_filename'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    operations = [
 | 
			
		||||
        migrations.RunPython(make_index, migrations.RunPython.noop),
 | 
			
		||||
        migrations.RunPython(restore_filenames),
 | 
			
		||||
        migrations.RemoveField(
 | 
			
		||||
            model_name='document',
 | 
			
		||||
            name='filename',
 | 
			
		||||
        ),
 | 
			
		||||
    ]
 | 
			
		||||
@@ -6,7 +6,7 @@ from django.db import migrations, models
 | 
			
		||||
class Migration(migrations.Migration):
 | 
			
		||||
 | 
			
		||||
    dependencies = [
 | 
			
		||||
        ('documents', '0022_auto_20181007_1420'),
 | 
			
		||||
        ('documents', '1000_update_paperless'),
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
    operations = [
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user