0005_auto_20200322_0607.py 3.75 KB
Newer Older
1
2
3
import logging

from django.core.files.storage import Storage
Roberto Rosario's avatar
Roberto Rosario committed
4
5
6
from django.db import migrations
from django.utils.module_loading import import_string

7
8
9
10
STORAGE_PATH_UPDATE_MAP = {
    'mayan.apps.document_states.storages.storage_workflowimagecache': 'mayan.apps.document_states.storages.storage_workflow_image',
    'mayan.apps.documents.storages.storage_documentimagecache': 'mayan.apps.documents.storages.storage_document_image_cache'
}
11
12
13
14
15
16
17
18
19
logger = logging.getLogger(name=__name__)


class DummyStorage(Storage):
    def delete(self, name):
        """
        Do nothing. This dummy storage avoids an if in the
        `cache_partition_file` loop.
        """
20

Roberto Rosario's avatar
Roberto Rosario committed
21
22
23
24
25

def operation_purge_and_delete_caches(apps, schema_editor):
    Cache = apps.get_model(
        app_label='file_caching', model_name='Cache'
    )
26

27
28
    cursor_primary = schema_editor.connection.create_cursor(name='file_caching_partition_files')
    cursor_secondary = schema_editor.connection.cursor()
29
30
31
32
33
34
35
36
37
38
39
40

    query = '''
        SELECT
            "file_caching_cachepartition"."name",
            "file_caching_cachepartitionfile"."filename",
            "file_caching_cachepartition"."cache_id"
        FROM "file_caching_cachepartitionfile"
        INNER JOIN
            "file_caching_cachepartition" ON (
                "file_caching_cachepartitionfile"."partition_id" = "file_caching_cachepartition"."id"
            )
    '''
Roberto Rosario's avatar
Roberto Rosario committed
41
42

    cache_storages = {}
43
    for cache in Cache.objects.using(alias=schema_editor.connection.alias).all():
44
45
46
47
48
49
50
51
52
53
54
        try:
            cache_storages[cache.pk] = import_string(
                dotted_path=cache.storage_instance_path
            ).get_storage_instance()
        except ImportError as exception:
            logger.error(
                'Storage "%s" not found. Remove the files from this '
                'storage manually.; %s', cache.storage_instance_path,
                exception
            )
            cache_storages[cache.pk] = DummyStorage()
Roberto Rosario's avatar
Roberto Rosario committed
55

56
57
    cursor_primary.execute(query)
    for partition_name, filename, cache_id in cursor_primary.fetchall():
Roberto Rosario's avatar
Roberto Rosario committed
58
        cache_storages[
59
            cache_id
Roberto Rosario's avatar
Roberto Rosario committed
60
61
        ].delete(
            name='{}-{}'.format(
62
63
                partition_name,
                filename
Roberto Rosario's avatar
Roberto Rosario committed
64
65
66
            )
        )

67
68
69
    cursor_secondary.execute('DELETE FROM "file_caching_cachepartitionfile";')
    cursor_secondary.execute('DELETE FROM "file_caching_cachepartition";')
    cursor_secondary.execute('DELETE FROM "file_caching_cache";')
Roberto Rosario's avatar
Roberto Rosario committed
70
71


72
73
74
75
76
def operation_update_storage_paths(apps, schema_editor):
    Cache = apps.get_model(
        app_label='file_caching', model_name='Cache'
    )

77
    for cache in Cache.objects.using(alias=schema_editor.connection.alias).all():
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
        cache.storage_instance_path = STORAGE_PATH_UPDATE_MAP.get(
            cache.storage_instance_path, cache.storage_instance_path
        )
        cache.save()


def operation_update_storage_paths_reverse(apps, schema_editor):
    storage_path_update_map_inverted = {
        value: key for key, value in STORAGE_PATH_UPDATE_MAP.items()
    }

    Cache = apps.get_model(
        app_label='file_caching', model_name='Cache'
    )

93
    for cache in Cache.objects.using(alias=schema_editor.connection.alias).all():
94
95
96
97
98
99
        cache.storage_instance_path = storage_path_update_map_inverted.get(
            cache.storage_instance_path, cache.storage_instance_path
        )
        cache.save()


Roberto Rosario's avatar
Roberto Rosario committed
100
101
102
103
104
class Migration(migrations.Migration):
    dependencies = [
        ('file_caching', '0004_auto_20200309_0922'),
    ]
    operations = [
105
106
107
108
        migrations.RunPython(
            code=operation_update_storage_paths,
            reverse_code=operation_update_storage_paths_reverse
        ),
Roberto Rosario's avatar
Roberto Rosario committed
109
110
111
112
113
        migrations.RunPython(
            code=operation_purge_and_delete_caches,
            reverse_code=migrations.RunPython.noop
        ),
    ]