Initial changes to move repositories from using a namespace string to referencing a user object. Also stores the user id in the cookie rather than the username, to allow users to be renamed. This commit must not be used unmodified because the database migration is too aggressive for live migration.

This commit is contained in:
Jake Moshenko 2014-09-18 14:52:29 -04:00
parent 8c00eabedd
commit 8626d1cd70
7 changed files with 87 additions and 28 deletions

View file

@ -0,0 +1,54 @@
"""Migrate registry namespaces to reference a user.
Revision ID: 13da56878560
Revises: 51d04d0e7e6f
Create Date: 2014-09-18 13:56:45.130455
"""
# revision identifiers, used by Alembic.
revision = '13da56878560'
down_revision = '51d04d0e7e6f'
from alembic import op
import sqlalchemy as sa
from data.database import Repository, User
def upgrade(tables):
# Add the namespace_user column, allowing it to be nullable
op.add_column('repository', sa.Column('namespace_user', sa.Integer(), sa.ForeignKey('user.id')))
# backfill the namespace_user column
namespace_to_user = {}
for user in User.select(User.name, User.id).where(User.robot == False):
namespace_to_user[user.username] = user
for repo in Repository.select():
repo.namespace_user = namespace_to_user[repo.namespace]
repo.save()
# Ensure the backfill was a success, then remove the namespace column
op.alter_column('repository', 'namespace_user', nullable=False)
op.create_index('repository_namespace_user_name', 'repository', ['namespace_user', 'name'],
unique=True)
op.drop_index('repository_namespace_user', table_name='repository')
op.drop_column('repository', 'namespace')
def downgrade(tables):
# Add the namespace column, allowing it to be nullable
op.add_column('repository', sa.Column('namespace', sa.String(length=255)))
# backfill the namespace column
for repo in Repository.select(Repository, User.username).join(User):
repo.namespace = repo.namespace_user.username
repo.save()
# Ensure the backfill was a success, then remove the namespace column
op.alter_column('repository', 'namespace', nullable=False)
op.create_index('repository_namespace_name', 'repository', ['namespace', 'name'], unique=True)
op.drop_index('repository_namespace_user', table_name='repository')
op.drop_column('repository', 'namespace_user')