Commits

Jun Omae  committed aae5292

Attachments migration simplifies with `os.renames`.

  • Participants
  • Parent commits b770204
  • Branches t10313-hashed-path

Comments (0)

Files changed (1)

File trac/attachment.py

     def environment_needs_upgrade(self, db):
         path = self.env.path
         if path:
-            old_dir = os.path.join(path, 'attachments')
-            new_dir = os.path.join(path, 'files', 'attachments')
-            return os.path.exists(old_dir) and not os.path.exists(new_dir)
+            return os.path.exists(os.path.join(path, 'attachments'))
 
     def upgrade_environment(self, db):
         """Migrate attachments from old-style directory to new-style
         directory.
         """
         path = self.env.path
-        os.makedirs(os.path.join(path, 'files', 'attachments'))
+        attachment_dir = os.path.join(path, 'files', 'attachments')
+        if not os.path.exists(attachment_dir):
+            os.makedirs(attachment_dir)
 
         cursor = db.cursor()
         cursor.execute("SELECT type, id, filename, description, size, time, "
         for row in cursor:
             attachment = Attachment(self.env, row[0], row[1])
             attachment._from_database(*row[2:])
-            self._copy_attachment_file(attachment)
+            self._move_attachment_file(attachment)
 
         shutil.rmtree(os.path.join(path, 'attachments'))
 
-    def _copy_attachment_file(self, attachment):
+    def _move_attachment_file(self, attachment):
         old_path = attachment._get_path_old(attachment.parent_realm,
                                             attachment.parent_id,
                                             attachment.filename)
         if os.path.isfile(old_path):
-            path = attachment.path
-            dir = os.path.dirname(path)
-            if not os.path.exists(dir):
-                os.makedirs(dir)
-            shutil.copyfile(old_path, path)
-            shutil.copystat(old_path, path)
+            os.renames(old_path, attachment.path)
 
 
 class AttachmentModule(Component):