|
|
@@ -8,12 +8,14 @@ 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
|
|
14
|
15
|
from depot.io.utils import FileIntent
|
|
15
|
16
|
from depot.manager import DepotManager
|
|
16
|
17
|
import sqlalchemy as sa
|
|
|
18
|
+from sqlalchemy.sql.expression import func
|
|
17
|
19
|
|
|
18
|
20
|
# revision identifiers, used by Alembic.
|
|
19
|
21
|
revision = '913efdf409e5'
|
|
|
@@ -33,6 +35,17 @@ revision_helper = sa.Table(
|
|
33
|
35
|
)
|
|
34
|
36
|
|
|
35
|
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
|
+
|
|
36
|
49
|
def delete_files_on_disk(connection: sa.engine.Connection):
|
|
37
|
50
|
"""Deletes files from disk and their references in database."""
|
|
38
|
51
|
delete_query = revision_helper.update() \
|
|
|
@@ -40,7 +53,8 @@ def delete_files_on_disk(connection: sa.engine.Connection):
|
|
40
|
53
|
.where(revision_helper.c.depot_file.isnot(None)) \
|
|
41
|
54
|
.values(depot_file=None)
|
|
42
|
55
|
connection.execute(delete_query)
|
|
43
|
|
- 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)
|
|
44
|
58
|
|
|
45
|
59
|
|
|
46
|
60
|
def upgrade():
|
|
|
@@ -54,14 +68,13 @@ def upgrade():
|
|
54
|
68
|
- create all files on disk from database.
|
|
55
|
69
|
"""
|
|
56
|
70
|
# Creates files depot used in this migration
|
|
57
|
|
- DepotManager.configure(
|
|
58
|
|
- 'tracim', {'depot.storage_path': 'depot/'},
|
|
59
|
|
- )
|
|
|
71
|
+ configure_depot()
|
|
60
|
72
|
connection = op.get_bind()
|
|
61
|
73
|
delete_files_on_disk(connection=connection)
|
|
62
|
74
|
select_query = revision_helper.select() \
|
|
63
|
75
|
.where(revision_helper.c.type == 'file') \
|
|
64
|
|
- .where(revision_helper.c.depot_file.is_(None))
|
|
|
76
|
+ .where(revision_helper.c.depot_file.is_(None)) \
|
|
|
77
|
+ .where(func.length(revision_helper.c.file_content) > 0)
|
|
65
|
78
|
files = connection.execute(select_query).fetchall()
|
|
66
|
79
|
for file in files:
|
|
67
|
80
|
file_filename = '{0}{1}'.format(
|
|
|
@@ -76,8 +89,7 @@ def upgrade():
|
|
76
|
89
|
depot_file_field = UploadedFile(depot_file_intent, 'tracim')
|
|
77
|
90
|
update_query = revision_helper.update() \
|
|
78
|
91
|
.where(revision_helper.c.revision_id == file.revision_id) \
|
|
79
|
|
- .values(depot_file=depot_file_field) \
|
|
80
|
|
- .return_defaults()
|
|
|
92
|
+ .values(depot_file=depot_file_field)
|
|
81
|
93
|
connection.execute(update_query)
|
|
82
|
94
|
|
|
83
|
95
|
|