Bläddra i källkod

Merge pull request #354 from tracim/fix/325/downgrade_all_files_only_on_disk

Damien Accorsi 7 år sedan
förälder
incheckning
b892def58a
1 ändrade filer med 43 tillägg och 3 borttagningar
  1. 43 3
      tracim/migration/versions/f3852e1349c4_all_files_only_on_disk.py

+ 43 - 3
tracim/migration/versions/f3852e1349c4_all_files_only_on_disk.py Visa fil

@@ -1,4 +1,4 @@
1
-"""all files only on disk
1
+"""all files only on disk.
2 2
 
3 3
 Revision ID: f3852e1349c4
4 4
 Revises: 913efdf409e5
@@ -6,7 +6,12 @@ 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
+
12
+from depot.fields.sqlalchemy import UploadedFileField
13
+from depot.manager import DepotManager
14
+
10 15
 import sqlalchemy as sa
11 16
 
12 17
 # revision identifiers, used by Alembic.
@@ -14,13 +19,48 @@ revision = 'f3852e1349c4'
14 19
 down_revision = '913efdf409e5'
15 20
 
16 21
 
22
+def configure_depot():
23
+    """Configure Depot."""
24
+    depot_storage_name = context.config.get_main_option('depot_storage_name')
25
+    depot_storage_path = context.config.get_main_option('depot_storage_dir')
26
+    depot_storage_settings = {'depot.storage_path': depot_storage_path}
27
+    DepotManager.configure(
28
+        depot_storage_name,
29
+        depot_storage_settings,
30
+    )
31
+
32
+
33
+revision_helper = sa.Table(
34
+    'content_revisions',
35
+    sa.MetaData(),
36
+    sa.Column('revision_id', sa.Integer, primary_key=True),
37
+    sa.Column('file_content', sa.LargeBinary),
38
+    sa.Column('depot_file', UploadedFileField, nullable=True),
39
+    sa.Column('type', sa.String(32), nullable=False),
40
+)
41
+
42
+
17 43
 def upgrade():
18
-    """Drops the file content from revision."""
44
+    """Drop the file content from revision."""
19 45
     with op.batch_alter_table('content_revisions') as batch_op:
20 46
         batch_op.drop_column('file_content')
21 47
 
22 48
 
23 49
 def downgrade():
24
-    """Adds the file content in revision."""
50
+    """Add the file content in revision."""
25 51
     with op.batch_alter_table('content_revisions') as batch_op:
26 52
         batch_op.add_column(sa.Column('file_content', sa.LargeBinary))
53
+
54
+    configure_depot()
55
+    depot = DepotManager.get()
56
+    connection = op.get_bind()
57
+    select_query = revision_helper.select() \
58
+        .where(revision_helper.c.type == 'file') \
59
+        .where(revision_helper.c.depot_file.isnot(None))
60
+    files = connection.execute(select_query).fetchall()
61
+    for file in files:
62
+        depot_file_content = depot.get(file.depot_file).read()
63
+        update_query = revision_helper.update() \
64
+            .where(revision_helper.c.revision_id == file.revision_id) \
65
+            .values(file_content=depot_file_content)
66
+        connection.execute(update_query)