Przeglądaj źródła

Downgrades the 'all files only on disk' migration

Adrien Panay 7 lat temu
rodzic
commit
002bc11583

+ 43 - 0
tracim/migration/versions/f3852e1349c4_all_files_only_on_disk.py Wyświetl plik

@@ -6,14 +6,42 @@ Create Date: 2017-07-24 17:15:54.278141
6 6
 
7 7
 """
8 8
 
9
+from alembic import context
9 10
 from alembic import op
11
+from depot.fields.sqlalchemy import UploadedFileField
12
+from depot.manager import DepotManager
10 13
 import sqlalchemy as sa
14
+from sqlalchemy.sql.expression import func
11 15
 
12 16
 # revision identifiers, used by Alembic.
13 17
 revision = 'f3852e1349c4'
14 18
 down_revision = '913efdf409e5'
15 19
 
16 20
 
21
+def configure_depot():
22
+    """Configure Depot."""
23
+    depot_storage_name = context.config.get_main_option('depot_storage_name')
24
+    depot_storage_path = context.config.get_main_option('depot_storage_dir')
25
+    depot_storage_settings = {'depot.storage_path': depot_storage_path}
26
+    DepotManager.configure(
27
+        depot_storage_name,
28
+        depot_storage_settings,
29
+    )
30
+
31
+
32
+revision_helper = sa.Table(
33
+    'content_revisions',
34
+    sa.MetaData(),
35
+    sa.Column('revision_id', sa.Integer, primary_key=True),
36
+    sa.Column('label', sa.String(1024), nullable=False),
37
+    sa.Column('file_extension', sa.String(255), nullable=False),
38
+    sa.Column('file_mimetype', sa.String(255), nullable=False),
39
+    sa.Column('file_content', sa.LargeBinary),
40
+    sa.Column('depot_file', UploadedFileField, nullable=True),
41
+    sa.Column('type', sa.String(32), nullable=False),
42
+)
43
+
44
+
17 45
 def upgrade():
18 46
     """Drops the file content from revision."""
19 47
     with op.batch_alter_table('content_revisions') as batch_op:
@@ -24,3 +52,18 @@ def downgrade():
24 52
     """Adds the file content in revision."""
25 53
     with op.batch_alter_table('content_revisions') as batch_op:
26 54
         batch_op.add_column(sa.Column('file_content', sa.LargeBinary))
55
+
56
+    configure_depot()
57
+    depot = DepotManager.get()
58
+    connection = op.get_bind()
59
+    select_query = revision_helper.select() \
60
+        .where(revision_helper.c.type == 'file') \
61
+        .where(revision_helper.c.depot_file.isnot(None)) \
62
+        .where(func.length(revision_helper.c.depot_file) > 0)
63
+    files = connection.execute(select_query).fetchall()
64
+    for file in files:
65
+        depot_file_content = depot.get(file.depot_file)
66
+        update_query = revision_helper.update() \
67
+            .where(revision_helper.c.revision_id == file.revision_id) \
68
+            .values(file_content=depot_file_content)
69
+        connection.execute(update_query)