class Flag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) active = db.Column(db.Boolean, nullable=False, default=True) __table_args__ = (db.UniqueConstraint('id', 'name'), ) @classmethod def from_json(cls, name): return cls(name=name) def to_json(self): return self.name
class Dependency(db.Model): """A dependency (e.g. gomod dependency) associated with the request.""" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) type = db.Column(db.String, nullable=False) version = db.Column(db.String, nullable=False) __table_args__ = (db.UniqueConstraint('name', 'type', 'version'), ) def __repr__(self): return ('<Dependency id={0!r}, name={1!r} type={2!r} version={3!r}>'. format(self.id, self.name, self.type, self.version)) @staticmethod def validate_json(dependency): """ Validate the JSON representation of a dependency. :param any dependency: the JSON representation of a dependency :raise ValidationError: if the JSON does not match the required schema """ if not isinstance( dependency, dict) or dependency.keys() != {'name', 'type', 'version'}: raise ValidationError( 'A dependency must be a JSON object with the keys name, type, and version' ) for key in ('name', 'type', 'version'): if not isinstance(dependency[key], str): raise ValidationError( 'The "{}" key of the dependency must be a string'.format( key)) @classmethod def from_json(cls, dependency): cls.validate_json(dependency) return cls(**dependency) def to_json(self): return { 'name': self.name, 'type': self.type, 'version': self.version, }
class EnvironmentVariable(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) value = db.Column(db.String, nullable=False) __table_args__ = (db.UniqueConstraint('name', 'value'), ) @classmethod def validate_json(cls, name, value): if not isinstance(value, str): raise ValidationError( 'The value of environment variables must be a string') @classmethod def from_json(cls, name, value): cls.validate_json(name, value) return cls(name=name, value=value) def to_json(self): return self.name, self.value
class RequestPackage(db.Model): """An association table between requests and the packages they contain.""" # A primary key is required by SQLAlchemy when using declaritive style tables, so a composite # primary key is used on the two required columns request_id = db.Column( db.Integer, db.ForeignKey('request.id'), autoincrement=False, index=True, primary_key=True, ) package_id = db.Column( db.Integer, db.ForeignKey('package.id'), autoincrement=False, index=True, primary_key=True, ) __table_args__ = (db.UniqueConstraint('request_id', 'package_id'), )
class Package(db.Model): """A package associated with the request.""" # Statically set the table name so that the inherited classes uses this value instead of one # derived from the class name __tablename__ = 'package' id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, index=True, nullable=False) type = db.Column(db.String, index=True, nullable=False) version = db.Column(db.String, index=True, nullable=False) __table_args__ = (db.UniqueConstraint('name', 'type', 'version'), ) def __repr__(self): return ( f'<{self.__class__.__name__} id={self.id}, name={self.name} type={self.type} ' f'version={self.version}>') @classmethod def validate_json(cls, package): """ Validate the JSON representation of a package. :param dict package: the JSON representation of a package :raise ValidationError: if the JSON does not match the required schema """ required = {'name', 'type', 'version'} if not isinstance(package, dict) or package.keys() != required: raise ValidationError( 'A package must be a JSON object with the following ' f'keys: {", ".join(sorted(required))}.') for key in package.keys(): if not isinstance(package[key], str): raise ValidationError( 'The "{}" key of the package must be a string'.format(key)) @classmethod def from_json(cls, package): cls.validate_json(package) return cls(**package) def to_json(self): """ Generate the JSON representation of the package. :return: the JSON form of the Package object :rtype: dict """ return { 'name': self.name, 'type': self.type, 'version': self.version, } @classmethod def get_or_create(cls, package): """ Get the package from the database and create it if it doesn't exist. :param dict package: the JSON representation of a package :return: an object based on the input dictionary; the object will be added to the database session, but not committed, if it was created :rtype: Package """ package_object = cls.query.filter_by(**package).first() if not package_object: package_object = cls.from_json(package) db.session.add(package_object) return package_object
request_pkg_manager_table = db.Table( 'request_pkg_manager', db.Column('request_id', db.Integer, db.ForeignKey('request.id'), index=True, nullable=False), db.Column( 'pkg_manager_id', db.Integer, db.ForeignKey('package_manager.id'), index=True, nullable=False, ), db.UniqueConstraint('request_id', 'pkg_manager_id'), ) request_environment_variable_table = db.Table( 'request_environment_variable', db.Column('request_id', db.Integer, db.ForeignKey('request.id'), index=True, nullable=False), db.Column( 'env_var_id', db.Integer, db.ForeignKey('environment_variable.id'), index=True, nullable=False,
class EnvironmentVariable(db.Model): """An environment variable that the consumer of the request should set.""" VALID_KINDS = ("path", "literal") id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) value = db.Column(db.String, nullable=False) kind = db.Column(db.String, nullable=False) __table_args__ = (db.UniqueConstraint("name", "value", "kind"), ) @classmethod def validate_json(cls, name, info): """ Validate the input environment variable. :param str name: the name of the environment variable :param dict info: the description of the environment variable. Must include "value" and "kind" attributes :raises ValidationError: if the environment variable is invalid """ if not isinstance(name, str): raise ValidationError( "The name of environment variables must be a string") if not isinstance(info, dict): raise ValidationError( "The info of environment variables must be an object") required_keys = {"value", "kind"} missing_keys = required_keys - info.keys() if missing_keys: raise ValidationError( "The following keys must be set in the info of the environment variables: " f"{', '.join(sorted(missing_keys))}") invalid_keys = info.keys() - required_keys if invalid_keys: raise ValidationError( "The following keys are not allowed in the info of the environment " f"variables: {', '.join(sorted(invalid_keys))}") if not isinstance(info["value"], str): raise ValidationError( "The value of environment variables must be a string") kind = info.get("kind") if not isinstance(kind, str): raise ValidationError( "The kind of environment variables must be a string") if kind not in cls.VALID_KINDS: raise ValidationError( f"The environment variable kind, {kind}, is not supported") @classmethod def from_json(cls, name, info): """ Create an EnvironmentVariable object from JSON. :param str name: the name of the environment variable :param dict info: the description of the environment variable :return: the EnvironmentVariable object :rtype: EnvironmentVariable """ cls.validate_json(name, info) return cls(name=name, **info)
request_pkg_manager_table = db.Table( "request_pkg_manager", db.Column("request_id", db.Integer, db.ForeignKey("request.id"), index=True, nullable=False), db.Column( "pkg_manager_id", db.Integer, db.ForeignKey("package_manager.id"), index=True, nullable=False, ), db.UniqueConstraint("request_id", "pkg_manager_id"), ) request_environment_variable_table = db.Table( "request_environment_variable", db.Column("request_id", db.Integer, db.ForeignKey("request.id"), index=True, nullable=False), db.Column( "env_var_id", db.Integer, db.ForeignKey("environment_variable.id"), index=True, nullable=False,
import sqlalchemy from cachito.errors import ValidationError from cachito.web import db request_pkg_manager_table = db.Table( 'request_pkg_manager', db.Column('request_id', db.Integer, db.ForeignKey('request.id'), nullable=False), db.Column('pkg_manager_id', db.Integer, db.ForeignKey('package_manager.id'), nullable=False), db.UniqueConstraint('request_id', 'pkg_manager_id'), ) request_dependency_table = db.Table( 'request_dependency', db.Column('request_id', db.Integer, db.ForeignKey('request.id'), nullable=False), db.Column('dependency_id', db.Integer, db.ForeignKey('dependency.id'), nullable=False), db.UniqueConstraint('request_id', 'dependency_id'), )