This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/database.py

74 lines
1.6 KiB
Python

import string
from random import SystemRandom
from peewee import *
from peewee import create_model_tables
from datetime import datetime
db = SqliteDatabase('test.db', threadlocals=True)
class BaseModel(Model):
class Meta:
database = db
class User(BaseModel):
username = CharField(primary_key=True)
password_hash = CharField()
email = CharField()
verified = BooleanField(default=False)
class Repository(BaseModel):
repository_id = PrimaryKeyField()
namespace = CharField()
name = CharField()
class Meta:
database = db
indexes = (
# create a unique index on namespace and name
(('namespace', 'name'), True),
)
def random_string_generator(length=16):
def random_string():
random = SystemRandom()
return ''.join([random.choice(string.ascii_uppercase + string.digits)
for x in range(length)])
return random_string
class AccessToken(BaseModel):
token_id = PrimaryKeyField()
code = CharField(default=random_string_generator())
repository = ForeignKeyField(Repository)
created = DateTimeField(default=datetime.now)
class Image(BaseModel):
image_id = CharField(primary_key=True)
checksum = CharField(null=True)
class RepositoryImage(BaseModel):
repository = ForeignKeyField(Repository)
image = ForeignKeyField(Image)
tag = CharField()
class Meta:
database = db
indexes = (
# we don't really want duplicates
(('repository', 'image', 'tag'), True),
)
def create_tables():
create_model_tables([User, Repository, Image, RepositoryImage, AccessToken])
if __name__ == '__main__':
create_tables()