CSCfi/pebbles

View on GitHub
migrations/versions/f8484696a67f_make_applications_more_independent.py

Summary

Maintainability
A
0 mins
Test Coverage
"""make applications more independent of templates

Revision ID: f8484696a67f
Revises: 2db948e18094
Create Date: 2022-03-24 15:23:27.013866

"""

# revision identifiers, used by Alembic.
revision = 'f8484696a67f'
down_revision = '2db948e18094'

from alembic import op
import sqlalchemy as sa


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column('applications', sa.Column('allowed_attrs', sa.Text(), nullable=True))
    op.add_column('applications', sa.Column('application_type', sa.String(length=128), nullable=True))
    op.add_column('applications', sa.Column('base_config', sa.Text(), nullable=True))

    # populate new columns from template data
    op.execute('''
      UPDATE applications a
         SET base_config=(SELECT base_config FROM application_templates at WHERE a.template_id=at.id)
       WHERE a.base_config is NULL 
    ''')
    op.execute('''
      UPDATE applications a
         SET application_type=(SELECT application_type FROM application_templates at WHERE a.template_id=at.id)
       WHERE a.application_type is NULL 
    ''')
    op.execute('''
      UPDATE applications a
         SET allowed_attrs=(SELECT allowed_attrs FROM application_templates at WHERE a.template_id=at.id)
       WHERE a.allowed_attrs is NULL 
    ''')
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_column('applications', 'base_config')
    op.drop_column('applications', 'application_type')
    op.drop_column('applications', 'allowed_attrs')
    # ### end Alembic commands ###