|
@@ -8,6 +8,7 @@ Create Date: 2017-07-12 15:44:20.568447
|
8
|
8
|
|
9
|
9
|
import shutil
|
10
|
10
|
|
|
11
|
+from alembic import context
|
11
|
12
|
from alembic import op
|
12
|
13
|
from depot.fields.sqlalchemy import UploadedFileField
|
13
|
14
|
from depot.fields.upload import UploadedFile
|
|
@@ -34,6 +35,17 @@ revision_helper = sa.Table(
|
34
|
35
|
)
|
35
|
36
|
|
36
|
37
|
|
|
38
|
+def configure_depot():
|
|
39
|
+ """Configure Depot."""
|
|
40
|
+ depot_storage_name = context.config.get_main_option('depot_storage_name')
|
|
41
|
+ depot_storage_path = context.config.get_main_option('depot_storage_dir')
|
|
42
|
+ depot_storage_settings = {'depot.storage_path': depot_storage_path}
|
|
43
|
+ DepotManager.configure(
|
|
44
|
+ depot_storage_name,
|
|
45
|
+ depot_storage_settings,
|
|
46
|
+ )
|
|
47
|
+
|
|
48
|
+
|
37
|
49
|
def delete_files_on_disk(connection: sa.engine.Connection):
|
38
|
50
|
"""Deletes files from disk and their references in database."""
|
39
|
51
|
delete_query = revision_helper.update() \
|
|
@@ -41,7 +53,8 @@ def delete_files_on_disk(connection: sa.engine.Connection):
|
41
|
53
|
.where(revision_helper.c.depot_file.isnot(None)) \
|
42
|
54
|
.values(depot_file=None)
|
43
|
55
|
connection.execute(delete_query)
|
44
|
|
- shutil.rmtree('depot/', ignore_errors=True)
|
|
56
|
+ depot_storage_path = context.config.get_main_option('depot_storage_dir')
|
|
57
|
+ shutil.rmtree(depot_storage_path, ignore_errors=True)
|
45
|
58
|
|
46
|
59
|
|
47
|
60
|
def upgrade():
|
|
@@ -55,9 +68,7 @@ def upgrade():
|
55
|
68
|
- create all files on disk from database.
|
56
|
69
|
"""
|
57
|
70
|
# Creates files depot used in this migration
|
58
|
|
- DepotManager.configure(
|
59
|
|
- 'tracim', {'depot.storage_path': 'depot/'},
|
60
|
|
- )
|
|
71
|
+ configure_depot()
|
61
|
72
|
connection = op.get_bind()
|
62
|
73
|
delete_files_on_disk(connection=connection)
|
63
|
74
|
select_query = revision_helper.select() \
|
|
@@ -78,8 +89,7 @@ def upgrade():
|
78
|
89
|
depot_file_field = UploadedFile(depot_file_intent, 'tracim')
|
79
|
90
|
update_query = revision_helper.update() \
|
80
|
91
|
.where(revision_helper.c.revision_id == file.revision_id) \
|
81
|
|
- .values(depot_file=depot_file_field) \
|
82
|
|
- .return_defaults()
|
|
92
|
+ .values(depot_file=depot_file_field)
|
83
|
93
|
connection.execute(update_query)
|
84
|
94
|
|
85
|
95
|
|