Repository: dougthor42/pynuget

Issue:
Deleting the last version of a package does not delete the entry in the "package" table.
Deleting the last version of a package does not delete the entry in the "package" table.

Steps to reproduce:

1. Upload `foo.0.0.1.nupkg`
2. Delete `foo.0.0.2.nupkg`
3. Attempt to upload `foo.0.0.1.nupkg` again
4. See that you get a 'package already exists' error.
5. View the sqlite file. table package contains 'foo' while table version does not.

Comments:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 464e237..4caff42 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,9 @@
 + Optionally run coverage when running in localhost development.
 + Fixed an issue where files with dependencies would break the "List" nuget
   command (#39)
++ Fixed an issue where deleting the last remaining version of a package
+  would not delete the row in the Package table, thus causing subsequet pushes
+  of the same version to fail. (#38)
 
 
 ## 0.2.1 (2018-07-25)
diff --git a/src/pynuget/db.py b/src/pynuget/db.py
index cb7caf7..668c652 100644
--- a/src/pynuget/db.py
+++ b/src/pynuget/db.py
@@ -73,7 +73,7 @@ class Version(Base):
     package = relationship("Package", backref="versions")
 
     def __repr__(self):
-        return "<Version({}, {})>".format(self.package.name, self.version)
+        return "<Version({}, {}, {})>".format(self.version_id, self.package.name, self.version)
 
     @hybrid_property
     def thing(self):
@@ -330,18 +330,19 @@ def delete_version(session, package_name, version):
            .filter(Package.name == package_name)
            .filter(Version.version == version)
            )
-    package = sql.one().package
-    logger.debug(package)
+    version = sql.one()
+    pkg = version.package
 
-    session.delete(sql.one())
-    session.commit()
+    session.delete(version)
 
     # update the Package.latest_version value, or delete the Package
     versions = (session.query(Version)
+                .join(Package)
                 .filter(Package.name == package_name)
                 ).all()
     if len(versions) > 0:
-        package.latest_version = max(v.version for v in versions)
+        pkg.latest_version = max(v.version for v in versions)
     else:
-        session.delete(package)
+        logger.info("No more versions exist. Deleting package %s" % pkg)
+        session.delete(pkg)
     session.commit()


Test patch:
diff --git a/tests/test_db.py b/tests/test_db.py
index 9f31f43..e2f4fb5 100644
--- a/tests/test_db.py
+++ b/tests/test_db.py
@@ -150,22 +150,46 @@ def test_insert_version(session):
 
 
 def test_delete_version(session):
+    # Add additional dummy data.
+    pkg = db.Package(name="Foo", latest_version="0.9.6")
+    session.add(pkg)
+    session.commit()
+
+    session.add(db.Version(package_id=pkg.package_id, version="0.9.6"))
+    session.add(db.Version(package_id=pkg.package_id, version="0.9.7"))
+    session.commit()
+
+    # The package we're interested in.
+    pkg_id = 'dummy'
+
     # get our initial counts
-    version_count = session.query(sa.func.count(db.Version.version_id))
+    version_count = (session.query(sa.func.count(db.Version.version_id))
+                     .join(db.Package)
+                     .filter(db.Package.name == pkg_id))
     package_count = session.query(sa.func.count(db.Package.package_id))
     initial_version_count = version_count.scalar()
     initial_package_count = package_count.scalar()
 
-    # Delete a version
-    pkg_id = 'dummy'
     db.delete_version(session, pkg_id, '0.0.2')
 
+    # The number of versions for our package should have decreased by 1.
     assert initial_version_count - 1 == version_count.scalar()
+
+    # We should still have 2 packages
+    assert initial_package_count == 2
     assert initial_package_count == package_count.scalar()
+
+    # The deleted version should not show up at all. (Note this particular
+    # test only works because our dummy data only has 1 instance of "0.0.2")
     assert '0.0.2' not in session.query(db.Version.version).all()
 
-    # twice more, the 2nd of which should delete the package
+    # Deleting the highest version should change the `latest_version` value
+    qry = session.query(db.Package).filter(db.Package.name == 'dummy')
     db.delete_version(session, pkg_id, '0.0.3')
+    assert qry.one().latest_version == '0.0.1'
+
+    # Deleting the last version of a package should remove the row from the
+    # Packages table.
     db.delete_version(session, pkg_id, '0.0.1')
     assert version_count.scalar() == 0
-    assert package_count.scalar() == 0
+    assert package_count.scalar() == 1