Browse Source

Cleans 'all files also on disk' migration

Adrien Panay 7 years ago
parent
commit
ac77def488
1 changed files with 21 additions and 100 deletions
  1. 21 100
      tracim/migration/versions/913efdf409e5_all_files_also_on_disk.py

+ 21 - 100
tracim/migration/versions/913efdf409e5_all_files_also_on_disk.py View File

@@ -14,17 +14,12 @@ from depot.fields.upload import UploadedFile
14 14
 from depot.io.utils import FileIntent
15 15
 from depot.manager import DepotManager
16 16
 import sqlalchemy as sa
17
-# from sqlalchemy.ext.declarative import declarative_base
18
-# from sqlalchemy.orm import sessionmaker
19 17
 
20 18
 # revision identifiers, used by Alembic.
21 19
 revision = '913efdf409e5'
22 20
 down_revision = '69fb10c3d6f0'
23 21
 
24 22
 
25
-# Session = sessionmaker()
26
-# DeclarativeBase = declarative_base()
27
-
28 23
 revision_helper = sa.Table(
29 24
     'content_revisions',
30 25
     sa.MetaData(),
@@ -38,100 +33,32 @@ revision_helper = sa.Table(
38 33
 )
39 34
 
40 35
 
41
-# class RevisionForMigration(DeclarativeBase):
42
-#     """Revision of Content for this migration."""
43
-
44
-#     __tablename__ = 'content_revisions'
45
-
46
-#     revision_id = Column(Integer, primary_key=True)
47
-
48
-#     label = Column(Unicode(1024), unique=False, nullable=False)
49
-#     description = Column(Text(), unique=False, nullable=False, default='')
50
-#     file_extension = Column(
51
-#         Unicode(255),
52
-#         unique=False,
53
-#         nullable=False,
54
-#         server_default='',
55
-#     )
56
-#     file_mimetype = Column(Unicode(255),  unique=False, nullable=False, default='')
57
-#     file_content = deferred(Column(LargeBinary(), unique=False, nullable=True))
58
-#     depot_file = Column(UploadedFileField, unique=False, nullable=True)
59
-#     properties = Column('properties', Text(), unique=False, nullable=False, default='')
60
-#     type = Column(Unicode(32), unique=False, nullable=False)
61
-#     status = Column(Unicode(32), unique=False, nullable=False, default=ContentStatus.OPEN)
62
-#     created = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow)
63
-#     updated = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow)
64
-#     is_deleted = Column(Boolean, unique=False, nullable=False, default=False)
65
-#     is_archived = Column(Boolean, unique=False, nullable=False, default=False)
66
-#     is_temporary = Column(Boolean, unique=False, nullable=False, default=False)
67
-#     revision_type = Column(Unicode(32), unique=False, nullable=False, default='')
68
-
69
-#     def file_name(self) -> str:
70
-#         return '{0}{1}'.format(
71
-#             self.revision.label,
72
-#             self.revision.file_extension,
73
-#         )
74
-
75
-
76
-# def fill_depot_file_fields()
77
-#     """fills depot file fields when they are null."""
78
-#     connection = op.get_bind()
79
-#     session = Session(bind=connection)
80
-
81
-#     all_files_wo_depot = session.query(RevisionForMigration) \
82
-#         .filter_by(type='file', depot_file=None) \
83
-#         .all()
84
-#     for one_file_wo_depot in all_files_wo_depot:
85
-#         # with tempfile.TemporaryFile() as tmp_file:
86
-#         #     tmp_file.write(one_file_wo_depot.content)
87
-#         #     one_file_wo_depot.depot_file = tmp_file
88
-#         one_file_wo_depot.depot_file = (
89
-#             one_file_wo_depot.content,
90
-#             one_file_wo_depot.file_name(),
91
-#             one_file_wo_depot.file_mimetype,
92
-#         )
93
-#     session.commit()
94
-
95
-
96
-def upgrade():
97
-    """Sets depot file for file typed revisions."""
98
-    import pudb; pu.db
99
-
100
-    # INFO - A.P - 2017-07-21 - put all files also on disk
101
-    # Until now, files are both in database and, for the newly created
102
-    # ones, on disk. In order to simplify the migration, this procedure
103
-    # will:
104
-    # - delete the few files on disk,
105
-    # - create all files on disk from database.
106
-
107
-    connection = op.get_bind()
36
+def delete_files_on_disk(connection: sa.engine.Connection):
37
+    """Deletes files from disk and their references in database."""
108 38
     delete_query = revision_helper.update() \
109 39
         .where(revision_helper.c.type == 'file') \
110 40
         .where(revision_helper.c.depot_file.isnot(None)) \
111 41
         .values(depot_file=None)
112
-    delete_result = connection.execute(delete_query)
42
+    connection.execute(delete_query)
113 43
     shutil.rmtree('depot/', ignore_errors=True)
114 44
 
115
-    # Creates files depot used in this migration:
116
-    # - 'default': depot used until now,
45
+
46
+def upgrade():
47
+    """
48
+    Sets all depot files for file typed revisions.
49
+
50
+    Until now, files are both in database and, for the newly created
51
+    ones, on disk. In order to simplify the migration, this procedure
52
+    will:
53
+    - delete the few files on disk,
54
+    - create all files on disk from database.
55
+    """
56
+    # Creates files depot used in this migration
117 57
     DepotManager.configure(
118 58
         'tracim', {'depot.storage_path': 'depot/'},
119 59
     )
120
-    # depot = DepotManager.get('default')
121
-
122
-    # create_query = revision_helper.update() \
123
-    #     .where(revision_helper.c.type == 'file') \
124
-    #     .values(depot_file=
125
-    #         UploadedFile(
126
-    #             FileIntent(
127
-    #                 file_content,
128
-    #                 '{0}{1}'.format(label, file_extension),
129
-    #                 file_mimetype,
130
-    #             )
131
-    #         )
132
-    #     )
133
-    # create_result = connection.execute(create_query)
134
-
60
+    connection = op.get_bind()
61
+    delete_files_on_disk(connection=connection)
135 62
     select_query = revision_helper.select() \
136 63
         .where(revision_helper.c.type == 'file') \
137 64
         .where(revision_helper.c.depot_file.is_(None))
@@ -146,21 +73,15 @@ def upgrade():
146 73
             one_file_filename,
147 74
             one_file.file_mimetype,
148 75
         )
149
-        depot_file_field = UploadedFile(depot_file_intent, 'default')
150
-        one_file_update = revision_helper.update() \
151
-            .where(revision_helper.c.type == 'file') \
76
+        depot_file_field = UploadedFile(depot_file_intent, 'tracim')
77
+        update_query = revision_helper.update() \
152 78
             .where(revision_helper.c.revision_id == one_file.revision_id) \
153 79
             .values(depot_file=depot_file_field) \
154 80
             .return_defaults()
155
-        create_result = connection.execute(one_file_update)
81
+        connection.execute(update_query)
156 82
 
157 83
 
158 84
 def downgrade():
159 85
     """Resets depot file for file typed revisions."""
160 86
     connection = op.get_bind()
161
-    file_revision_query = revision_helper.update() \
162
-        .where(revision_helper.c.type == 'file') \
163
-        .where(revision_helper.c.depot_file.isnot(None)) \
164
-        .values(depot_file=None)
165
-    result = connection.execute(file_revision_query)
166
-    shutil.rmtree('depot/', ignore_errors=True)
87
+    delete_files_on_disk(connection=connection)