Browse Source

Simplifies 'all files also on disk' migration

Adrien Panay 6 years ago
parent
commit
fb32708131

+ 53 - 38
tracim/migration/versions/913efdf409e5_all_files_also_on_disk.py View File

@@ -97,46 +97,62 @@ def upgrade():
97 97
     """Sets depot file for file typed revisions."""
98 98
     import pudb; pu.db
99 99
 
100
-    # fill_depot_file_fields()
101
-
102
-    # Creates files depot used in this migration:
103
-    # - 'default': depot used until now,
104
-    DepotManager.configure(
105
-        'default', {'depot.storage_path': 'depot/'},
106
-    )
107
-    depot = DepotManager.get('default')
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.
108 106
 
109 107
     connection = op.get_bind()
108
+    delete_query = revision_helper.update() \
109
+        .where(revision_helper.c.type == 'file') \
110
+        .where(revision_helper.c.depot_file.isnot(None)) \
111
+        .values(depot_file=None)
112
+    delete_result = connection.execute(delete_query)
113
+    shutil.rmtree('depot/', ignore_errors=True)
110 114
 
111
-    file_revision_query = revision_helper.select() \
112
-        .where(revision_helper.c.type == 'file')
113
-    all_file_revisions = connection.execute(file_revision_query)
114
-    for one_revision in all_file_revisions:
115
-        one_revision_filename = '{0}{1}'.format(
116
-            one_revision.label,
117
-            one_revision.file_extension,
115
+    # Creates files depot used in this migration:
116
+    # - 'default': depot used until now,
117
+    DepotManager.configure(
118
+        'tracim', {'depot.storage_path': 'depot/'},
119
+    )
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
+
135
+    select_query = revision_helper.select() \
136
+        .where(revision_helper.c.type == 'file') \
137
+        .where(revision_helper.c.depot_file.is_(None))
138
+    all_files = connection.execute(select_query).fetchall()
139
+    for one_file in all_files:
140
+        one_file_filename = '{0}{1}'.format(
141
+            one_file.label,
142
+            one_file.file_extension,
143
+        )
144
+        depot_file_intent = FileIntent(
145
+            one_file.file_content,
146
+            one_file_filename,
147
+            one_file.file_mimetype,
118 148
         )
119
-        if not one_revision.depot_file:
120
-            depot_file_intent = FileIntent(
121
-                one_revision.file_content,
122
-                one_revision_filename,
123
-                one_revision.file_mimetype,
124
-            )
125
-            depot_file_field = UploadedFile(depot_file_intent)
126
-            one_revision_update = revision_helper.update() \
127
-                .where(revision_helper.c.type == 'file') \
128
-                .where(revision_helper.c.revision_id == one_revision.revision_id) \
129
-                .values(depot_file=depot_file_field) \
130
-                .return_defaults()
131
-            result = connection.execute(one_revision_update)
132
-        else:
133
-            # TODO - A.P - makes the field in DB to be updated as well
134
-            depot_file_uid = depot.replace(
135
-                one_revision.depot_file.file_id,
136
-                one_revision.file_content,
137
-                one_revision_filename,
138
-                one_revision.file_mimetype,
139
-            )
149
+        depot_file_field = UploadedFile(depot_file_intent, 'default')
150
+        one_file_update = revision_helper.update() \
151
+            .where(revision_helper.c.type == 'file') \
152
+            .where(revision_helper.c.revision_id == one_file.revision_id) \
153
+            .values(depot_file=depot_file_field) \
154
+            .return_defaults()
155
+        create_result = connection.execute(one_file_update)
140 156
 
141 157
 
142 158
 def downgrade():
@@ -145,7 +161,6 @@ def downgrade():
145 161
     file_revision_query = revision_helper.update() \
146 162
         .where(revision_helper.c.type == 'file') \
147 163
         .where(revision_helper.c.depot_file.isnot(None)) \
148
-        .values(depot_file=None) \
149
-        .return_defaults()
164
+        .values(depot_file=None)
150 165
     result = connection.execute(file_revision_query)
151 166
     shutil.rmtree('depot/', ignore_errors=True)

+ 1 - 1
tracim/tracim/config/app_cfg.py View File

@@ -129,7 +129,7 @@ def start_daemons(manager: DaemonsManager):
129 129
 
130 130
 def configure_depot():
131 131
     """Configure Depot."""
132
-    depot_storage_name = 'default'
132
+    depot_storage_name = 'tracim'
133 133
     depot_storage_path = CFG.get_instance().DEPOT_STORAGE_DIR
134 134
     depot_storage_settings = {'depot.storage_path': depot_storage_path}
135 135
     DepotManager.configure(