350efb3f6baf_add_namespace_to_project.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. """Add namespace to project
  2. Revision ID: 350efb3f6baf
  3. Revises: 1640c7d75e5f
  4. Create Date: 2016-08-30 22:02:07.645138
  5. """
  6. # revision identifiers, used by Alembic.
  7. revision = '350efb3f6baf'
  8. down_revision = '1640c7d75e5f'
  9. from alembic import op
  10. import sqlalchemy as sa
  11. try:
  12. from pagure.lib import model
  13. except ImportError:
  14. import sys
  15. sys.path.insert(0, '.')
  16. from pagure.lib import model
  17. def upgrade():
  18. ''' Add the column namespace to the table projects.
  19. '''
  20. op.add_column(
  21. 'projects',
  22. sa.Column('namespace', sa.String(255), nullable=True, index=True)
  23. )
  24. # Update all the existing projects
  25. engine = op.get_bind()
  26. Session = sa.orm.scoped_session(sa.orm.sessionmaker())
  27. Session.configure(bind=engine)
  28. session = Session()
  29. for project in session.query(model.Project).filter(
  30. model.Project.name.ilike('%/%')).all():
  31. nspace, name = project.name.split('/', 1)
  32. project.name = name
  33. project.namespace = nspace
  34. session.add(project)
  35. session.commit()
  36. def downgrade():
  37. ''' Remove the column namespace from the table projects.
  38. '''
  39. # Update all the existing projects
  40. engine = op.get_bind()
  41. Session = sa.orm.scoped_session(sa.orm.sessionmaker())
  42. Session.configure(bind=engine)
  43. session = Session()
  44. for project in session.query(model.Project).filter(
  45. model.Project.namespace != None).all():
  46. if project.namespace.strip():
  47. project.name = '%s/%s' % (project.namespace, project.name)
  48. session.add(project)
  49. session.commit()
  50. op.drop_column('projects', 'namespace')