From 6bc5c78241b0593d5d8efd6be8a3649c4cb8a764 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Tue, 3 Nov 2015 09:51:26 -0500 Subject: [PATCH] Later migration changed one of the tables, so make local copies --- util/migrate/backfill_v1_metadata.py | 49 ++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/util/migrate/backfill_v1_metadata.py b/util/migrate/backfill_v1_metadata.py index f0455758c..28dda6827 100644 --- a/util/migrate/backfill_v1_metadata.py +++ b/util/migrate/backfill_v1_metadata.py @@ -2,8 +2,10 @@ import logging from peewee import JOIN_LEFT_OUTER -from data.database import (Image, ImageStorage, ImageStoragePlacement, ImageStorageLocation, db, - db_for_update) +from peewee import (CharField, BigIntegerField, BooleanField, ForeignKeyField, DateTimeField, + TextField) + +from data.database import BaseModel, db, db_for_update from app import app, storage from data import model @@ -11,6 +13,48 @@ from data import model logger = logging.getLogger(__name__) +class Repository(BaseModel): + pass + + +# Vendor the information from tables we will be writing to at the time of this migration +class ImageStorage(BaseModel): + uuid = CharField(index=True, unique=True) + checksum = CharField(null=True) + image_size = BigIntegerField(null=True) + uncompressed_size = BigIntegerField(null=True) + uploading = BooleanField(default=True, null=True) + + +class Image(BaseModel): + # This class is intentionally denormalized. Even though images are supposed + # to be globally unique we can't treat them as such for permissions and + # security reasons. So rather than Repository <-> Image being many to many + # each image now belongs to exactly one repository. + docker_image_id = CharField(index=True) + repository = ForeignKeyField(Repository) + + # '/' separated list of ancestory ids, e.g. /1/2/6/7/10/ + ancestors = CharField(index=True, default='/', max_length=64535, null=True) + + storage = ForeignKeyField(ImageStorage, index=True, null=True) + + created = DateTimeField(null=True) + comment = TextField(null=True) + command = TextField(null=True) + aggregate_size = BigIntegerField(null=True) + v1_json_metadata = TextField(null=True) + + +class ImageStorageLocation(BaseModel): + name = CharField(unique=True, index=True) + + +class ImageStoragePlacement(BaseModel): + storage = ForeignKeyField(ImageStorage) + location = ForeignKeyField(ImageStorageLocation) + + def image_json_path(storage_uuid): base_path = storage.image_path(storage_uuid) return '{0}json'.format(base_path) @@ -19,6 +63,7 @@ def image_json_path(storage_uuid): def backfill_v1_metadata(): """ Copies metadata from image storages to their images. """ logger.debug('Image v1 metadata backfill: Began execution') + while True: batch_image_ids = list(Image .select(Image.id)