Fix bug in deletion of repos with OCI-style linked tags

MySQL does not allow rows in the same table referencing other rows to be deleted in a single statement. We now do a two-pass deletion, and add a test to make sure.

Fixes https://jira.prod.coreos.systems/browse/QS-18
This commit is contained in:
Joseph Schorr 2017-10-18 17:03:27 -04:00
parent 6b217b497a
commit 9f804de23d
2 changed files with 14 additions and 2 deletions

View file

@ -10,7 +10,7 @@ from cachetools import ttl_cache
from data.model import (
config, DataModelException, tag, db_transaction, storage, permission, _basequery)
from data.database import (
Repository, Namespace, RepositoryTag, Star, Image, ImageStorage, User, Visibility,
Repository, Namespace, RepositoryTag, Star, Image, ImageStorage, User, Visibility, Tag,
RepositoryPermission, RepositoryActionCount, Role, RepositoryAuthorizedEmail, TagManifest,
DerivedStorageForImage, Label, TagManifestLabel, db_for_update, get_epoch_timestamp,
db_random_func, db_concat_func, RepositorySearchScore)
@ -81,6 +81,12 @@ def purge_repository(namespace_name, repository_name):
except Repository.DoesNotExist:
return False
# Delete the repository of all OCI-referenced entries.
# Note that new-model Tag's must be deleted in *two* passes, as they can reference parent tags,
# and MySQL is... particular... about such relationships when deleting.
Tag.delete().where(Tag.repository == repo, ~(Tag.linked_tag >> None)).execute()
Tag.delete().where(Tag.repository == repo).execute()
# Delete all tags to allow gc to reclaim storage
previously_referenced = tag.purge_all_tags(repo)
unreferenced_image_q = Image.select(Image.id).where(Image.repository == repo)