Repository :
http://git.fedorahosted.org/cgit/copr.git
On branch : master
---------------------------------------------------------------
commit 07fd65b8cca7fcf48367aa480b7483979c6325ec
Author: Bohuslav Kabrda <bkabrda(a)redhat.com>
Date: Tue Jan 15 10:22:13 2013 +0100
Improve the migration to actually migrate data, support migrating down
---------------------------------------------------------------
.../versions/2fa80e062525_add_mock_chroots.py | 37 ++++++++++++++++++-
1 files changed, 35 insertions(+), 2 deletions(-)
diff --git a/coprs_frontend/alembic/versions/2fa80e062525_add_mock_chroots.py
b/coprs_frontend/alembic/versions/2fa80e062525_add_mock_chroots.py
index 7d77a35..fdd88b3 100644
--- a/coprs_frontend/alembic/versions/2fa80e062525_add_mock_chroots.py
+++ b/coprs_frontend/alembic/versions/2fa80e062525_add_mock_chroots.py
@@ -13,7 +13,6 @@ down_revision = '2e30169e58ce'
from alembic import op
import sqlalchemy as sa
-
def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.create_table('mock_chroot',
@@ -31,6 +30,40 @@ def upgrade():
sa.ForeignKeyConstraint(['mock_chroot_id'], ['mock_chroot.id'], ),
sa.PrimaryKeyConstraint('mock_chroot_id', 'copr_id')
)
+
+
+ # transfer the data - we can't assume how the code looks like when
+ # running the migration, so do everything from scratch
+ session = sa.orm.sessionmaker(bind=op.get_bind())()
+ metadata = sa.MetaData()
+ # just what we need of copr table
+ coprs_table = sa.Table('copr', metadata, sa.Column('chroots',
sa.Text()), sa.Column('id', sa.Integer()))
+ # get chroots
+ chroots = set()
+ for cs in op.get_bind().execute(sa.select([coprs_table.c.chroots])):
+ chroots.update(set(cs[0].split(' ')))
+ chroots = list(chroots)
+
+ mc_table = sa.Table('mock_chroot', metadata,
+ sa.Column('id', sa.Integer(), nullable=False),
+ sa.Column('os_release', sa.String(length=50), nullable=False),
+ sa.Column('os_version', sa.String(length=50), nullable=False),
+ sa.Column('arch', sa.String(length=50), nullable=False),
+ sa.Column('is_active', sa.Boolean(), nullable=False),
+ )
+ cc_table = sa.Table('copr_chroot', metadata,
+ sa.Column('mock_chroot_id', sa.Integer(), nullable=False),
+ sa.Column('copr_id', sa.Integer(), nullable=False),
+ )
+ for i, c in enumerate(chroots): # each mock_chroot now has id of value i + 1 (not to
include 0)
+ sc = c.split('-')
+ op.bulk_insert(mc_table, [{'id': i + 1, 'os_release': sc[0],
'os_version': sc[1], 'arch': sc[2], 'is_active': True}])
+ # insert proper copr_chroots for every copr
+ for row in op.get_bind().execute(sa.select([coprs_table.c.id,
coprs_table.c.chroots])):
+ for c in row[1].split(' '):
+ op.bulk_insert(cc_table, [{'mock_chroot_id': chroots.index(c) + 1,
'copr_id': row[0]}])
+
+
if op.get_bind().dialect.name == 'sqlite':
op.rename_table('copr', 'copr_1')
op.create_table('copr',
@@ -52,7 +85,7 @@ def upgrade():
def downgrade():
### commands auto generated by Alembic - please adjust! ###
- op.add_column('copr', sa.Column(u'chroots', sa.TEXT(),
nullable=False))
+ op.add_column('copr', sa.Column(u'chroots', sa.TEXT(),
nullable=False, server_default='fedora-rawhide-x86_64'))
op.drop_table('copr_chroot')
op.drop_table('mock_chroot')
### end Alembic commands ###