The new strategy is to do a three phase migration. This is the first phase: getting the namespace user in the db and written for all new repositories.
This commit is contained in:
parent
8626d1cd70
commit
3259cda000
7 changed files with 62 additions and 74 deletions
|
@ -19,36 +19,6 @@ 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')
|
||||
|
|
Reference in a new issue