Blame progit/model.py

Pierre-Yves Chibon ae4136
#-*- coding: utf-8 -*-
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
"""
Pierre-Yves Chibon ae4136
 (c) 2014 - Copyright Red Hat Inc
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
 Authors:
Pierre-Yves Chibon ae4136
   Pierre-Yves Chibon <pingou@pingoured.fr></pingou@pingoured.fr>
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
"""
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ee9b42
__requires__ = ['SQLAlchemy >= 0.8', 'jinja2 >= 2.4']
Pierre-Yves Chibon ae4136
import pkg_resources
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
import datetime
Pierre-Yves Chibon ae4136
import logging
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
import sqlalchemy as sa
Pierre-Yves Chibon ae4136
from sqlalchemy import create_engine
Pierre-Yves Chibon caf4e8
from sqlalchemy.exc import SQLAlchemyError
Pierre-Yves Chibon ae4136
from sqlalchemy.ext.declarative import declarative_base
Pierre-Yves Chibon ae4136
from sqlalchemy.orm import sessionmaker
Pierre-Yves Chibon ae4136
from sqlalchemy.orm import scoped_session
Pierre-Yves Chibon ae4136
from sqlalchemy.orm import relation
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
BASE = declarative_base()
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
ERROR_LOG = logging.getLogger('progit.model')
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
def create_tables(db_url, alembic_ini=None, debug=False):
Pierre-Yves Chibon ae4136
    """ Create the tables in the database using the information from the
Pierre-Yves Chibon ae4136
    url obtained.
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    :arg db_url, URL used to connect to the database. The URL contains
Pierre-Yves Chibon ae4136
        information with regards to the database engine, the host to
Pierre-Yves Chibon ae4136
        connect to, the user and password and the database name.
Pierre-Yves Chibon ae4136
          ie: <engine>://<user>:<password>@<host>/<dbname></dbname></host></password></user></engine>
Pierre-Yves Chibon ae4136
    :kwarg alembic_ini, path to the alembic ini file. This is necessary
Pierre-Yves Chibon ae4136
        to be able to use alembic correctly, but not for the unit-tests.
Pierre-Yves Chibon ae4136
    :kwarg debug, a boolean specifying wether we should have the verbose
Pierre-Yves Chibon ae4136
        output of sqlalchemy or not.
Pierre-Yves Chibon ae4136
    :return a session that can be used to query the database.
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    """
Pierre-Yves Chibon ae4136
    engine = create_engine(db_url, echo=debug)
Pierre-Yves Chibon c46cda
    from progit.plugins import get_plugin_tables
Pierre-Yves Chibon c46cda
    get_plugin_tables()
Pierre-Yves Chibon ae4136
    BASE.metadata.create_all(engine)
Pierre-Yves Chibon ae4136
    #engine.execute(collection_package_create_view(driver=engine.driver))
Pierre-Yves Chibon ae4136
    if db_url.startswith('sqlite:'):
Pierre-Yves Chibon ae4136
        ## Ignore the warning about con_record
Pierre-Yves Chibon ae4136
        # pylint: disable=W0613
Pierre-Yves Chibon ae4136
        def _fk_pragma_on_connect(dbapi_con, con_record):
Pierre-Yves Chibon ae4136
            ''' Tries to enforce referential constraints on sqlite. '''
Pierre-Yves Chibon ae4136
            dbapi_con.execute('pragma foreign_keys=ON')
Pierre-Yves Chibon ae4136
        sa.event.listen(engine, 'connect', _fk_pragma_on_connect)
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    if alembic_ini is not None:  # pragma: no cover
Pierre-Yves Chibon ae4136
        # then, load the Alembic configuration and generate the
Pierre-Yves Chibon ae4136
        # version table, "stamping" it with the most recent rev:
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
        ## Ignore the warning missing alembic
Pierre-Yves Chibon ae4136
        # pylint: disable=F0401
Pierre-Yves Chibon ae4136
        from alembic.config import Config
Pierre-Yves Chibon ae4136
        from alembic import command
Pierre-Yves Chibon ae4136
        alembic_cfg = Config(alembic_ini)
Pierre-Yves Chibon ae4136
        command.stamp(alembic_cfg, "head")
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    scopedsession = scoped_session(sessionmaker(bind=engine))
Pierre-Yves Chibon caf4e8
    # Insert the default data into the db
Pierre-Yves Chibon c46cda
    try:
Pierre-Yves Chibon c46cda
        create_default_status(scopedsession)
Pierre-Yves Chibon c46cda
    except SQLAlchemyError:
Pierre-Yves Chibon c46cda
        pass
Pierre-Yves Chibon ae4136
    return scopedsession
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon caf4e8
def create_default_status(session):
Pierre-Yves Chibon caf4e8
    """ Insert the defaults status in the status tables.
Pierre-Yves Chibon caf4e8
    """
Pierre-Yves Chibon caf4e8
Pierre-Yves Chibon caf4e8
    for status in ['Open', 'Invalid', 'Insufficient data', 'Fixed']:
Pierre-Yves Chibon caf4e8
        ticket_stat = StatusIssue(status=status)
Pierre-Yves Chibon caf4e8
        session.add(ticket_stat)
Pierre-Yves Chibon caf4e8
        try:
Pierre-Yves Chibon caf4e8
            session.flush()
Pierre-Yves Chibon caf4e8
        except SQLAlchemyError, err:
Pierre-Yves Chibon caf4e8
            ERROR_LOG.debug('Status %s could not be added', ticket_stat)
Pierre-Yves Chibon caf4e8
Pierre-Yves Chibon caf4e8
    session.commit()
Pierre-Yves Chibon caf4e8
Pierre-Yves Chibon caf4e8
Pierre-Yves Chibon cc324e
class StatusIssue(BASE):
Pierre-Yves Chibon cc324e
    """ Stores the status a ticket can have.
Pierre-Yves Chibon cc324e
Pierre-Yves Chibon cc324e
    Table -- status_issue
Pierre-Yves Chibon cc324e
    """
Pierre-Yves Chibon cc324e
    __tablename__ = 'status_issue'
Pierre-Yves Chibon cc324e
Pierre-Yves Chibon cc324e
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon cc324e
    status = sa.Column(sa.Text, nullable=False, unique=True)
Pierre-Yves Chibon cc324e
Pierre-Yves Chibon cc324e
Pierre-Yves Chibon acc99e
class User(BASE):
Pierre-Yves Chibon acc99e
    """ Stores information about users.
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
    Table -- users
Pierre-Yves Chibon acc99e
    """
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
    __tablename__ = 'users'
Pierre-Yves Chibon acc99e
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon acc99e
    user = sa.Column(sa.String(32), nullable=False, unique=True, index=True)
Pierre-Yves Chibon acc99e
    fullname = sa.Column(sa.Text, nullable=False, index=True)
Pierre-Yves Chibon baeff8
    public_ssh_key = sa.Column(sa.Text, nullable=True)
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
class UserEmail(BASE):
Pierre-Yves Chibon acc99e
    """ Stores email information about the users.
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
    Table -- user_emails
Pierre-Yves Chibon acc99e
    """
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
    __tablename__ = 'user_emails'
Pierre-Yves Chibon acc99e
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 1a9d17
    user_id = sa.Column(
Pierre-Yves Chibon 1a9d17
        sa.Integer,
Pierre-Yves Chibon acc99e
        sa.ForeignKey('users.id', onupdate='CASCADE'),
Pierre-Yves Chibon acc99e
        nullable=False,
Pierre-Yves Chibon acc99e
        index=True)
Pierre-Yves Chibon acc99e
    email = sa.Column(sa.Text, nullable=False, unique=True)
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
    user = relation('User', foreign_keys=[user_id],
Pierre-Yves Chibon acc99e
                    remote_side=[User.id], backref='emails')
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon acc99e
Pierre-Yves Chibon ae4136
class Project(BASE):
Pierre-Yves Chibon ae4136
    """ Stores the projects.
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    Table -- projects
Pierre-Yves Chibon ae4136
    """
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    __tablename__ = 'projects'
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 1a9d17
    user_id = sa.Column(
Pierre-Yves Chibon 1a9d17
        sa.Integer,
Pierre-Yves Chibon acc99e
        sa.ForeignKey('users.id', onupdate='CASCADE'),
Pierre-Yves Chibon acc99e
        nullable=False,
Pierre-Yves Chibon acc99e
        index=True)
Pierre-Yves Chibon ae4136
    name = sa.Column(sa.String(32), nullable=False, index=True)
Pierre-Yves Chibon d688cf
    description = sa.Column(sa.Text, nullable=True)
Pierre-Yves Chibon ae4136
    parent_id = sa.Column(
Pierre-Yves Chibon ae4136
        sa.Integer,
Pierre-Yves Chibon ae4136
        sa.ForeignKey('projects.id', onupdate='CASCADE'),
Pierre-Yves Chibon ae4136
        nullable=True)
Pierre-Yves Chibon 20b945
    issue_tracker = sa.Column(sa.Boolean, nullable=False, default=True)
Pierre-Yves Chibon 4d5b45
    project_docs = sa.Column(sa.Boolean, nullable=False, default=True)
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    date_created = sa.Column(sa.DateTime, nullable=False,
Pierre-Yves Chibon ae4136
                             default=datetime.datetime.utcnow)
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon f3f2fe
    parent = relation('Project', remote_side=[id], backref='forks')
Pierre-Yves Chibon acc99e
    user = relation('User', foreign_keys=[user_id],
Pierre-Yves Chibon acc99e
                    remote_side=[User.id], backref='projects')
Pierre-Yves Chibon 334ada
Mathieu Bridon b0b97e
    @property
Mathieu Bridon b0b97e
    def path(self):
Pierre-Yves Chibon 0c862c
        ''' Return the name of the git repo on the filesystem. '''
Pierre-Yves Chibon ebf29e
        if self.parent_id:
Pierre-Yves Chibon 51f6a6
            path = '%s/%s.git' % (self.user.user, self.name)
Pierre-Yves Chibon ebf29e
        else:
Pierre-Yves Chibon ebf29e
            path = '%s.git' % (self.name)
Pierre-Yves Chibon ebf29e
        return path
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon b393e8
    @property
Pierre-Yves Chibon e2c16f
    def is_fork(self):
Pierre-Yves Chibon 49feff
        ''' Return a boolean specifying if the project is a fork or not '''
Pierre-Yves Chibon 3a95ee
        return self.parent_id is not None
Pierre-Yves Chibon 49feff
Pierre-Yves Chibon 49feff
    @property
Pierre-Yves Chibon b393e8
    def fullname(self):
Pierre-Yves Chibon b393e8
        ''' Return the name of the git repo as user/project if it is a
Pierre-Yves Chibon b393e8
        project forked, otherwise it returns the project name.
Pierre-Yves Chibon b393e8
        '''
Pierre-Yves Chibon b393e8
        str_name = self.name
Pierre-Yves Chibon 3a95ee
        if self.parent_id:
Pierre-Yves Chibon 51f6a6
            str_name = "%s/%s" % (self.user.user, str_name)
Pierre-Yves Chibon b393e8
        return str_name
Pierre-Yves Chibon b393e8
Pierre-Yves Chibon 2f8732
Pierre-Yves Chibon 96fa1e
class ProjectUser(BASE):
Pierre-Yves Chibon 96fa1e
    """ Stores the user of a projects.
Pierre-Yves Chibon 96fa1e
Pierre-Yves Chibon 96fa1e
    Table -- user_projects
Pierre-Yves Chibon 96fa1e
    """
Pierre-Yves Chibon 96fa1e
Pierre-Yves Chibon 96fa1e
    __tablename__ = 'user_projects'
Pierre-Yves Chibon 96fa1e
    __table_args__ = (
Pierre-Yves Chibon acc99e
        sa.UniqueConstraint('project_id', 'user_id'),
Pierre-Yves Chibon 96fa1e
    )
Pierre-Yves Chibon 96fa1e
Pierre-Yves Chibon 96fa1e
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 96fa1e
    project_id = sa.Column(
Pierre-Yves Chibon 96fa1e
        sa.Integer,
Pierre-Yves Chibon 96fa1e
        sa.ForeignKey('projects.id', onupdate='CASCADE'),
Pierre-Yves Chibon 96fa1e
        nullable=False)
Pierre-Yves Chibon 1a9d17
    user_id = sa.Column(
Pierre-Yves Chibon 1a9d17
        sa.Integer,
Pierre-Yves Chibon acc99e
        sa.ForeignKey('users.id', onupdate='CASCADE'),
Pierre-Yves Chibon acc99e
        nullable=False,
Pierre-Yves Chibon acc99e
        index=True)
Pierre-Yves Chibon 96fa1e
Pierre-Yves Chibon 96fa1e
    project = relation(
Pierre-Yves Chibon 96fa1e
        'Project', foreign_keys=[project_id], remote_side=[Project.id],
Pierre-Yves Chibon 96fa1e
        backref='users')
Pierre-Yves Chibon acc99e
    user = relation('User', foreign_keys=[user_id],
Pierre-Yves Chibon acc99e
                    remote_side=[User.id], backref='co_projects')
Pierre-Yves Chibon 96fa1e
Pierre-Yves Chibon 96fa1e
Pierre-Yves Chibon ae4136
class Issue(BASE):
Pierre-Yves Chibon ae4136
    """ Stores the issues reported on a project.
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    Table -- issues
Pierre-Yves Chibon ae4136
    """
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    __tablename__ = 'issues'
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon de6d71
    uid = sa.Column(sa.Text(32), unique=True)
Pierre-Yves Chibon ae4136
    project_id = sa.Column(
Pierre-Yves Chibon ae4136
        sa.Integer,
Pierre-Yves Chibon ae4136
        sa.ForeignKey(
Pierre-Yves Chibon ae4136
            'projects.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon ae4136
        nullable=False)
Pierre-Yves Chibon ae4136
    title = sa.Column(
Pierre-Yves Chibon ae4136
        sa.Text,
Pierre-Yves Chibon ae4136
        nullable=False)
Pierre-Yves Chibon ae4136
    content = sa.Column(
Pierre-Yves Chibon ae4136
        sa.Text(),
Pierre-Yves Chibon ae4136
        nullable=False)
Pierre-Yves Chibon 1a9d17
    user_id = sa.Column(
Pierre-Yves Chibon 1a9d17
        sa.Integer,
Pierre-Yves Chibon acc99e
        sa.ForeignKey('users.id', onupdate='CASCADE'),
Pierre-Yves Chibon acc99e
        nullable=False,
Pierre-Yves Chibon acc99e
        index=True)
Pierre-Yves Chibon caf4e8
    status = sa.Column(
Pierre-Yves Chibon caf4e8
        sa.Text,
Pierre-Yves Chibon caf4e8
        sa.ForeignKey(
Pierre-Yves Chibon caf4e8
            'status_issue.status', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon caf4e8
        default='Open',
Pierre-Yves Chibon caf4e8
        nullable=False)
Pierre-Yves Chibon ae4136
Pierre-Yves Chibon ae4136
    date_created = sa.Column(sa.DateTime, nullable=False,
Pierre-Yves Chibon ae4136
                             default=datetime.datetime.utcnow)
Pierre-Yves Chibon 7b8c4b
Pierre-Yves Chibon 0ef501
    project = relation(
Pierre-Yves Chibon 0ef501
        'Project', foreign_keys=[project_id], remote_side=[Project.id],
Pierre-Yves Chibon 0ef501
        backref='issues')
Pierre-Yves Chibon acc99e
    user = relation('User', foreign_keys=[user_id],
Pierre-Yves Chibon acc99e
                    remote_side=[User.id], backref='issues')
Pierre-Yves Chibon 0ef501
Pierre-Yves Chibon 2a0134
    def __repr__(self):
Pierre-Yves Chibon 2a0134
        return 'Issue(%s, project:%s, user:%s, title:%s)' % (
Pierre-Yves Chibon 2a0134
            self.id, self.project.name, self.user.user, self.title
Pierre-Yves Chibon 2a0134
        )
Pierre-Yves Chibon 2a0134
Pierre-Yves Chibon 11c5e9
    @property
Pierre-Yves Chibon 11c5e9
    def mail_id(self):
Pierre-Yves Chibon 11c5e9
        ''' Return a unique reprensetation of the issue as string that
Pierre-Yves Chibon 11c5e9
        can be used when sending emails.
Pierre-Yves Chibon 11c5e9
        '''
Pierre-Yves Chibon 11c5e9
        return '%s-ticket-%s@progit' % (self.project.name, self.id)
Pierre-Yves Chibon 11c5e9
Pierre-Yves Chibon 7b8c4b
Pierre-Yves Chibon bb9f71
class IssueComment(BASE):
Pierre-Yves Chibon bb9f71
    """ Stores the comments made on a commit/file.
Pierre-Yves Chibon bb9f71
Pierre-Yves Chibon bb9f71
    Table -- issue_comments
Pierre-Yves Chibon bb9f71
    """
Pierre-Yves Chibon bb9f71
Pierre-Yves Chibon bb9f71
    __tablename__ = 'issue_comments'
Pierre-Yves Chibon bb9f71
Pierre-Yves Chibon bb9f71
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon bb9f71
    issue_id = sa.Column(
Pierre-Yves Chibon bb9f71
        sa.Integer,
Pierre-Yves Chibon bb9f71
        sa.ForeignKey(
Pierre-Yves Chibon bb9f71
            'issues.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon bb9f71
        index=True)
Pierre-Yves Chibon bb9f71
    comment = sa.Column(
Pierre-Yves Chibon bb9f71
        sa.Text(),
Pierre-Yves Chibon bb9f71
        nullable=False)
Pierre-Yves Chibon bb9f71
    parent_id = sa.Column(
Pierre-Yves Chibon bb9f71
        sa.Integer,
Pierre-Yves Chibon 36edcd
        sa.ForeignKey('issue_comments.id', onupdate='CASCADE'),
Pierre-Yves Chibon bb9f71
        nullable=True)
Pierre-Yves Chibon 1a9d17
    user_id = sa.Column(
Pierre-Yves Chibon 1a9d17
        sa.Integer,
Pierre-Yves Chibon acc99e
        sa.ForeignKey('users.id', onupdate='CASCADE'),
Pierre-Yves Chibon acc99e
        nullable=False,
Pierre-Yves Chibon acc99e
        index=True)
Pierre-Yves Chibon bb9f71
Pierre-Yves Chibon bb9f71
    date_created = sa.Column(sa.DateTime, nullable=False,
Pierre-Yves Chibon bb9f71
                             default=datetime.datetime.utcnow)
Pierre-Yves Chibon bb9f71
Pierre-Yves Chibon bb9f71
    issue = relation(
Pierre-Yves Chibon bb9f71
        'Issue', foreign_keys=[issue_id], remote_side=[Issue.id],
Pierre-Yves Chibon bb9f71
        backref='comments')
Pierre-Yves Chibon acc99e
    user = relation('User', foreign_keys=[user_id],
Pierre-Yves Chibon acc99e
                    remote_side=[User.id], backref='comment_issues')
Pierre-Yves Chibon bb9f71
Pierre-Yves Chibon bb9f71
Pierre-Yves Chibon 7b8c4b
class PullRequest(BASE):
Pierre-Yves Chibon 7b8c4b
    """ Stores the pull requests created on a project.
Pierre-Yves Chibon 7b8c4b
Pierre-Yves Chibon 7b8c4b
    Table -- pull_requests
Pierre-Yves Chibon 7b8c4b
    """
Pierre-Yves Chibon 7b8c4b
Pierre-Yves Chibon 7b8c4b
    __tablename__ = 'pull_requests'
Pierre-Yves Chibon 7b8c4b
Pierre-Yves Chibon 7b8c4b
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 7b8c4b
    project_id = sa.Column(
Pierre-Yves Chibon 7b8c4b
        sa.Integer,
Pierre-Yves Chibon 7b8c4b
        sa.ForeignKey(
Pierre-Yves Chibon 7b8c4b
            'projects.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon 7b8c4b
        nullable=False)
Pierre-Yves Chibon 85ebac
    project_id_from = sa.Column(
Pierre-Yves Chibon 85ebac
        sa.Integer,
Pierre-Yves Chibon 85ebac
        sa.ForeignKey(
Pierre-Yves Chibon 85ebac
            'projects.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon 85ebac
        nullable=False)
Pierre-Yves Chibon 7b8c4b
    title = sa.Column(
Pierre-Yves Chibon 7b8c4b
        sa.Text,
Pierre-Yves Chibon 7b8c4b
        nullable=False)
Pierre-Yves Chibon 9e21cc
    branch = sa.Column(
Pierre-Yves Chibon 9e21cc
        sa.Text(),
Pierre-Yves Chibon 9e21cc
        nullable=False)
Pierre-Yves Chibon 7b8c4b
    start_id = sa.Column(
Pierre-Yves Chibon 7b8c4b
        sa.String(40),
Pierre-Yves Chibon 04a4f4
        nullable=True)
Pierre-Yves Chibon 7b8c4b
    stop_id = sa.Column(
Pierre-Yves Chibon 7b8c4b
        sa.String(40),
Pierre-Yves Chibon 7b8c4b
        nullable=False)
Pierre-Yves Chibon 1a9d17
    user_id = sa.Column(
Pierre-Yves Chibon 1a9d17
        sa.Integer,
Pierre-Yves Chibon acc99e
        sa.ForeignKey('users.id', onupdate='CASCADE'),
Pierre-Yves Chibon acc99e
        nullable=False,
Pierre-Yves Chibon acc99e
        index=True)
Pierre-Yves Chibon 7f6e93
    status = sa.Column(sa.Boolean, nullable=False, default=True)
Pierre-Yves Chibon 7b8c4b
Pierre-Yves Chibon 7b8c4b
    date_created = sa.Column(sa.DateTime, nullable=False,
Pierre-Yves Chibon 7b8c4b
                             default=datetime.datetime.utcnow)
Pierre-Yves Chibon aa3b6b
Pierre-Yves Chibon aa3b6b
    repo = relation(
Pierre-Yves Chibon 58e9f5
        'Project', foreign_keys=[project_id], remote_side=[Project.id],
Pierre-Yves Chibon 58e9f5
        backref='requests')
Pierre-Yves Chibon aa3b6b
    repo_from = relation(
Pierre-Yves Chibon aa3b6b
        'Project', foreign_keys=[project_id_from], remote_side=[Project.id])
Pierre-Yves Chibon acc99e
    user = relation('User', foreign_keys=[user_id],
Pierre-Yves Chibon acc99e
                    remote_side=[User.id], backref='pull_requests')
Pierre-Yves Chibon 2a35bc
Pierre-Yves Chibon 2a35bc
    def __repr__(self):
Pierre-Yves Chibon 2a35bc
        return 'PullRequest(%s, project:%s, user:%s, title:%s)' % (
Pierre-Yves Chibon 2a35bc
            self.id, self.repo.name, self.user.user, self.title
Pierre-Yves Chibon 2a35bc
        )
Pierre-Yves Chibon 4e1ed8
Pierre-Yves Chibon 4e1ed8
Pierre-Yves Chibon 26f048
class PullRequestComment(BASE):
Pierre-Yves Chibon 26f048
    """ Stores the comments made on a pull-request.
Pierre-Yves Chibon 26f048
Pierre-Yves Chibon 26f048
    Table -- pull_request_comments
Pierre-Yves Chibon 26f048
    """
Pierre-Yves Chibon 26f048
Pierre-Yves Chibon 26f048
    __tablename__ = 'pull_request_comments'
Pierre-Yves Chibon 26f048
Pierre-Yves Chibon 26f048
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 26f048
    pull_request_id = sa.Column(
Pierre-Yves Chibon 26f048
        sa.Integer,
Pierre-Yves Chibon 26f048
        sa.ForeignKey(
Pierre-Yves Chibon 26f048
            'pull_requests.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon 26f048
        nullable=False)
Pierre-Yves Chibon 26f048
    commit_id = sa.Column(
Pierre-Yves Chibon 26f048
        sa.String(40),
Pierre-Yves Chibon 26f048
        nullable=False,
Pierre-Yves Chibon 26f048
        index=True)
Pierre-Yves Chibon 26f048
    user_id = sa.Column(
Pierre-Yves Chibon 26f048
        sa.Integer,
Pierre-Yves Chibon 26f048
        sa.ForeignKey('users.id', onupdate='CASCADE'),
Pierre-Yves Chibon 26f048
        nullable=False,
Pierre-Yves Chibon 26f048
        index=True)
Pierre-Yves Chibon 26f048
    line = sa.Column(
Pierre-Yves Chibon 26f048
        sa.Integer,
Pierre-Yves Chibon 26f048
        nullable=True)
Pierre-Yves Chibon 26f048
    comment = sa.Column(
Pierre-Yves Chibon 26f048
        sa.Text(),
Pierre-Yves Chibon 26f048
        nullable=False)
Pierre-Yves Chibon 26f048
    parent_id = sa.Column(
Pierre-Yves Chibon 26f048
        sa.Integer,
Pierre-Yves Chibon 26f048
        sa.ForeignKey('pull_request_comments.id', onupdate='CASCADE'),
Pierre-Yves Chibon 26f048
        nullable=True)
Pierre-Yves Chibon 26f048
Pierre-Yves Chibon 26f048
    date_created = sa.Column(sa.DateTime, nullable=False,
Pierre-Yves Chibon 26f048
                             default=datetime.datetime.utcnow)
Pierre-Yves Chibon 26f048
Pierre-Yves Chibon 26f048
    user = relation('User', foreign_keys=[user_id],
Pierre-Yves Chibon 26f048
                    remote_side=[User.id], backref='pull_request_comments')
Pierre-Yves Chibon 26f048
    pull_request = relation(
Pierre-Yves Chibon 26f048
        'PullRequest', foreign_keys=[pull_request_id], remote_side=[PullRequest.id],
Pierre-Yves Chibon 26f048
        backref='comments')
Pierre-Yves Chibon 26f048
Pierre-Yves Chibon 26f048
Pierre-Yves Chibon 4e1ed8
class GlobalId(BASE):
Pierre-Yves Chibon 4e1ed8
    """ Store the mapping of the project with their issue and pull-request
Pierre-Yves Chibon 4e1ed8
    and provides us with a way to get global identifier per project
Pierre-Yves Chibon 4e1ed8
Pierre-Yves Chibon 4e1ed8
    Table -- global_id
Pierre-Yves Chibon 4e1ed8
    """
Pierre-Yves Chibon 4e1ed8
Pierre-Yves Chibon 4e1ed8
    __tablename__ = 'global_id'
Pierre-Yves Chibon 4e1ed8
Pierre-Yves Chibon 4e1ed8
    id = sa.Column(sa.Integer, primary_key=True)
Pierre-Yves Chibon 4e1ed8
    project_id = sa.Column(
Pierre-Yves Chibon 4e1ed8
        sa.Integer,
Pierre-Yves Chibon 4e1ed8
        sa.ForeignKey(
Pierre-Yves Chibon 4e1ed8
            'projects.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon 4e1ed8
        nullable=False)
Pierre-Yves Chibon 4e1ed8
    issue_id = sa.Column(
Pierre-Yves Chibon 4e1ed8
        sa.Integer,
Pierre-Yves Chibon 4e1ed8
        sa.ForeignKey(
Pierre-Yves Chibon 4e1ed8
            'issues.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon 4e1ed8
        nullable=True)
Pierre-Yves Chibon 4e1ed8
    request_id = sa.Column(
Pierre-Yves Chibon 4e1ed8
        sa.Integer,
Pierre-Yves Chibon 4e1ed8
        sa.ForeignKey(
Pierre-Yves Chibon 4e1ed8
            'pull_requests.id', ondelete='CASCADE', onupdate='CASCADE'),
Pierre-Yves Chibon 4e1ed8
        nullable=True)
Pierre-Yves Chibon 4e1ed8
Pierre-Yves Chibon 4e1ed8
    __table_args__ = (
Pierre-Yves Chibon 4e1ed8
        # Both fields should not be NULL
Pierre-Yves Chibon 4e1ed8
        sa.CheckConstraint('NOT(request_id IS NULL AND issue_id IS NULL)'),
Pierre-Yves Chibon 4e1ed8
        # Both fields should not be not NULL (ie: only one of them should)
Pierre-Yves Chibon 4e1ed8
        sa.CheckConstraint('NOT(request_id IS NOT NULL AND issue_id IS NOT NULL)'),
Pierre-Yves Chibon 4e1ed8
    )