Script 52e53e7b413e_add_build_chroot_py
[hide private]
[frames] | no frames]

Source Code for Script script-52e53e7b413e_add_build_chroot_py

 1  """ Add BuildChroot table 
 2   
 3  Revision ID: 52e53e7b413e 
 4  Revises: 246fd2dbf398 
 5  Create Date: 2013-11-14 09:00:43.787717 
 6   
 7  """ 
 8   
 9  # revision identifiers, used by Alembic. 
10  revision = '52e53e7b413e' 
11  down_revision = '246fd2dbf398' 
12   
13  from alembic import op 
14  import sqlalchemy as sa 
15   
16   
17 -def upgrade():
18 ### commands auto generated by Alembic - please adjust! ### 19 op.create_table('build_chroot', 20 sa.Column('mock_chroot_id', sa.Integer(), nullable=False), 21 sa.Column('build_id', sa.Integer(), nullable=False), 22 sa.Column('status', sa.Integer(), nullable=True), 23 sa.ForeignKeyConstraint(['build_id'], ['build.id'], ), 24 sa.ForeignKeyConstraint(['mock_chroot_id'], ['mock_chroot.id'], ), 25 sa.PrimaryKeyConstraint('mock_chroot_id', 'build_id') 26 ) 27 28 #transfer data from build table to build_chroot 29 session = sa.orm.sessionmaker(bind=op.get_bind())() 30 metadata = sa.MetaData() 31 # just what we need of copr table 32 build_table = sa.Table('build', metadata, 33 sa.Column('chroots', sa.Text()), 34 sa.Column('status', sa.Integer()), 35 sa.Column('id', sa.Integer()), 36 ) 37 38 mc_table = sa.Table('mock_chroot', metadata, 39 sa.Column('id', sa.Integer(), nullable=False), 40 sa.Column('os_release', sa.String(length=50), nullable=False), 41 sa.Column('os_version', sa.String(length=50), nullable=False), 42 sa.Column('arch', sa.String(length=50), nullable=False), 43 sa.Column('is_active', sa.Boolean(), nullable=False), 44 ) 45 bc_table = sa.Table('build_chroot', metadata, 46 sa.Column('mock_chroot_id', sa.Integer(), nullable=False), 47 sa.Column('build_id', sa.Integer(), nullable=False), 48 sa.Column('status', sa.Integer(), nullable=True), 49 ) 50 for row in op.get_bind().execute(sa.select([build_table.c.id, build_table.c.chroots, build_table.c.status])): 51 for c in row[1].split(' '): 52 chroot_array = c.split('-') 53 for row2 in (op.get_bind().execute(sa.select([mc_table.c.id], sa.and_( 54 mc_table.c.os_release == op.inline_literal(chroot_array[0]), 55 mc_table.c.os_version == op.inline_literal(chroot_array[1]), 56 mc_table.c.arch == op.inline_literal(chroot_array[2]), 57 )))): # should be just one row 58 op.bulk_insert(bc_table, [{'mock_chroot_id': row2[0], 'build_id': row[0], 'status': row[2]}]) 59 60 # drop old columns 61 op.drop_column(u'build', u'status') 62 op.drop_column(u'build', u'chroots')
63 64
65 -def downgrade():
66 print "Why are you downgrading? You will just lost some data." 67 op.add_column(u'build', sa.Column(u'chroots', sa.TEXT(), nullable=False)) 68 op.add_column(u'build', sa.Column(u'status', sa.INTEGER(), nullable=True)) 69 op.drop_table('build_chroot') 70 print "Data about chroots for builds are gone!"
71