Browse Source

Avoid deleting versions unless they have gone missing for >1day, fixes #610

Jordi Boggiano 7 years ago
parent
commit
c22b770171

+ 25 - 0
src/Packagist/WebBundle/Entity/Version.php

@@ -182,6 +182,11 @@ class Version
      */
     private $createdAt;
 
+    /**
+     * @ORM\Column(type="datetime", nullable=true)
+     */
+    private $softDeletedAt;
+
     /**
      * @ORM\Column(type="datetime")
      */
@@ -657,6 +662,26 @@ class Version
         return $this->updatedAt;
     }
 
+    /**
+     * Set softDeletedAt
+     *
+     * @param \DateTime|null $softDeletedAt
+     */
+    public function setSoftDeletedAt($softDeletedAt)
+    {
+        $this->softDeletedAt = $softDeletedAt;
+    }
+
+    /**
+     * Get softDeletedAt
+     *
+     * @return \DateTime|null $softDeletedAt
+     */
+    public function getSoftDeletedAt()
+    {
+        return $this->softDeletedAt;
+    }
+
     /**
      * Get authors
      *

+ 10 - 1
src/Packagist/WebBundle/Package/Updater.php

@@ -101,6 +101,8 @@ class Updater
         }
         $pruneDate = clone $start;
         $pruneDate->modify('-1min');
+        $deleteDate = clone $start;
+        $deleteDate->modify('-1day');
 
         $em = $this->doctrine->getManager();
         $apc = extension_loaded('apcu');
@@ -208,7 +210,13 @@ class Updater
         // remove outdated versions
         foreach ($package->getVersions() as $version) {
             if ($version->getUpdatedAt() < $pruneDate) {
-                $versionRepository->remove($version);
+                if (!is_null($version->getSoftDeletedAt()) && $version->getSoftDeletedAt() < $deleteDate) {
+                    $versionRepository->remove($version);
+                } else {
+                    // set it to be soft-deleted so next update that occurs after deleteDate (1day) if the
+                    // version is still missing it will be really removed
+                    $version->setSoftDeletedAt(new \DateTime);
+                }
             }
         }
 
@@ -242,6 +250,7 @@ class Updater
             } else {
                 // mark it updated to avoid it being pruned
                 $existingVersion->setUpdatedAt(new \DateTime);
+                $existingVersion->setSoftDeletedAt(null);
 
                 return false;
             }