Browse Source

Fixes 'all files only on disk' migration downgrade

Adrien Panay 7 years ago
parent
commit
3f3937c039
1 changed files with 7 additions and 10 deletions
  1. 7 10
      tracim/migration/versions/f3852e1349c4_all_files_only_on_disk.py

+ 7 - 10
tracim/migration/versions/f3852e1349c4_all_files_only_on_disk.py View File

1
-"""all files only on disk
1
+"""all files only on disk.
2
 
2
 
3
 Revision ID: f3852e1349c4
3
 Revision ID: f3852e1349c4
4
 Revises: 913efdf409e5
4
 Revises: 913efdf409e5
8
 
8
 
9
 from alembic import context
9
 from alembic import context
10
 from alembic import op
10
 from alembic import op
11
+
11
 from depot.fields.sqlalchemy import UploadedFileField
12
 from depot.fields.sqlalchemy import UploadedFileField
12
 from depot.manager import DepotManager
13
 from depot.manager import DepotManager
14
+
13
 import sqlalchemy as sa
15
 import sqlalchemy as sa
14
-from sqlalchemy.sql.expression import func
15
 
16
 
16
 # revision identifiers, used by Alembic.
17
 # revision identifiers, used by Alembic.
17
 revision = 'f3852e1349c4'
18
 revision = 'f3852e1349c4'
33
     'content_revisions',
34
     'content_revisions',
34
     sa.MetaData(),
35
     sa.MetaData(),
35
     sa.Column('revision_id', sa.Integer, primary_key=True),
36
     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),
37
     sa.Column('file_content', sa.LargeBinary),
40
     sa.Column('depot_file', UploadedFileField, nullable=True),
38
     sa.Column('depot_file', UploadedFileField, nullable=True),
41
     sa.Column('type', sa.String(32), nullable=False),
39
     sa.Column('type', sa.String(32), nullable=False),
43
 
41
 
44
 
42
 
45
 def upgrade():
43
 def upgrade():
46
-    """Drops the file content from revision."""
44
+    """Drop the file content from revision."""
47
     with op.batch_alter_table('content_revisions') as batch_op:
45
     with op.batch_alter_table('content_revisions') as batch_op:
48
         batch_op.drop_column('file_content')
46
         batch_op.drop_column('file_content')
49
 
47
 
50
 
48
 
51
 def downgrade():
49
 def downgrade():
52
-    """Adds the file content in revision."""
50
+    """Add the file content in revision."""
53
     with op.batch_alter_table('content_revisions') as batch_op:
51
     with op.batch_alter_table('content_revisions') as batch_op:
54
         batch_op.add_column(sa.Column('file_content', sa.LargeBinary))
52
         batch_op.add_column(sa.Column('file_content', sa.LargeBinary))
55
 
53
 
58
     connection = op.get_bind()
56
     connection = op.get_bind()
59
     select_query = revision_helper.select() \
57
     select_query = revision_helper.select() \
60
         .where(revision_helper.c.type == 'file') \
58
         .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)
59
+        .where(revision_helper.c.depot_file.isnot(None))
63
     files = connection.execute(select_query).fetchall()
60
     files = connection.execute(select_query).fetchall()
64
     for file in files:
61
     for file in files:
65
-        depot_file_content = depot.get(file.depot_file)
62
+        depot_file_content = depot.get(file.depot_file).read()
66
         update_query = revision_helper.update() \
63
         update_query = revision_helper.update() \
67
             .where(revision_helper.c.revision_id == file.revision_id) \
64
             .where(revision_helper.c.revision_id == file.revision_id) \
68
             .values(file_content=depot_file_content)
65
             .values(file_content=depot_file_content)