Blame alembic/versions/350efb3f6baf_add_namespace_to_project.py

Pierre-Yves Chibon 6572f4
"""Add namespace to project
Pierre-Yves Chibon 6572f4
Pierre-Yves Chibon 6572f4
Revision ID: 350efb3f6baf
Pierre-Yves Chibon ea386a
Revises: 1640c7d75e5f
Pierre-Yves Chibon 6572f4
Create Date: 2016-08-30 22:02:07.645138
Pierre-Yves Chibon 6572f4
Pierre-Yves Chibon 6572f4
"""
Pierre-Yves Chibon 6572f4
Pierre-Yves Chibon 6572f4
# revision identifiers, used by Alembic.
Pierre-Yves Chibon 6572f4
revision = '350efb3f6baf'
Pierre-Yves Chibon ea386a
down_revision = '1640c7d75e5f'
Pierre-Yves Chibon 6572f4
Pierre-Yves Chibon 6572f4
from alembic import op
Pierre-Yves Chibon 6572f4
import sqlalchemy as sa
Pierre-Yves Chibon 6572f4
Pierre-Yves Chibon 6572f4
try:
Pierre-Yves Chibon 6572f4
    from pagure.lib import model
Pierre-Yves Chibon 6572f4
except ImportError:
Pierre-Yves Chibon 6572f4
    import sys
Pierre-Yves Chibon 6572f4
    sys.path.insert(0, '.')
Pierre-Yves Chibon 6572f4
    from pagure.lib import model
Pierre-Yves Chibon 6572f4
Pierre-Yves Chibon 6572f4
Pierre-Yves Chibon 6572f4
def upgrade():
Pierre-Yves Chibon 6572f4
    ''' Add the column namespace to the table projects.
Pierre-Yves Chibon 6572f4
    '''
Pierre-Yves Chibon 6572f4
    op.add_column(
Pierre-Yves Chibon 6572f4
        'projects',
Pierre-Yves Chibon 6572f4
        sa.Column('namespace', sa.String(255), nullable=True, index=True)
Pierre-Yves Chibon 6572f4
    )
Pierre-Yves Chibon 6572f4
Pierre-Yves Chibon 6572f4
    # Update all the existing projects
Pierre-Yves Chibon 6572f4
    engine = op.get_bind()
Pierre-Yves Chibon 6572f4
    Session = sa.orm.scoped_session(sa.orm.sessionmaker())
Pierre-Yves Chibon 6572f4
    Session.configure(bind=engine)
Pierre-Yves Chibon 6572f4
    session = Session()
Pierre-Yves Chibon 6572f4
    for project in session.query(model.Project).filter(
Pierre-Yves Chibon 6572f4
            model.Project.name.ilike('%/%')).all():
Pierre-Yves Chibon 6572f4
        nspace, name = project.name.split('/', 1)
Pierre-Yves Chibon 6572f4
        project.name = name
Pierre-Yves Chibon 6572f4
        project.namespace = nspace
Pierre-Yves Chibon 6572f4
        session.add(project)
Pierre-Yves Chibon 6572f4
    session.commit()
Pierre-Yves Chibon 6572f4
Pierre-Yves Chibon 6572f4
Pierre-Yves Chibon 6572f4
def downgrade():
Pierre-Yves Chibon 6572f4
    ''' Remove the column namespace from the table projects.
Pierre-Yves Chibon 6572f4
    '''
Pierre-Yves Chibon 6572f4
    # Update all the existing projects
Pierre-Yves Chibon 6572f4
    engine = op.get_bind()
Pierre-Yves Chibon 6572f4
    Session = sa.orm.scoped_session(sa.orm.sessionmaker())
Pierre-Yves Chibon 6572f4
    Session.configure(bind=engine)
Pierre-Yves Chibon 6572f4
    session = Session()
Pierre-Yves Chibon 6572f4
    for project in session.query(model.Project).filter(
Pierre-Yves Chibon 6572f4
            model.Project.namespace != None).all():
Pierre-Yves Chibon 6572f4
        if project.namespace.strip():
Pierre-Yves Chibon 6572f4
            project.name = '%s/%s' % (project.namespace, project.name)
Pierre-Yves Chibon 6572f4
            session.add(project)
Pierre-Yves Chibon 6572f4
    session.commit()
Pierre-Yves Chibon 6572f4
Pierre-Yves Chibon 6572f4
    op.drop_column('projects', 'namespace')