From 5930dd0b3f696ed777df5ee650edad026dbe38cf Mon Sep 17 00:00:00 2001 From: mprahl Date: May 29 2018 14:06:53 +0000 Subject: Set the modulemd's context as the context in the DB to correct the current values --- diff --git a/module_build_service/migrations/versions/708ac8950f55_set_from_mmd_context.py b/module_build_service/migrations/versions/708ac8950f55_set_from_mmd_context.py new file mode 100644 index 0000000..6ba2432 --- /dev/null +++ b/module_build_service/migrations/versions/708ac8950f55_set_from_mmd_context.py @@ -0,0 +1,63 @@ +"""Sets the modulemd's context as the source of truth + +Revision ID: 708ac8950f55 +Revises: c8e2fc555399 +Create Date: 2018-05-29 13:49:43.629831 + +""" + +# revision identifiers, used by Alembic. +revision = '708ac8950f55' +down_revision = 'c8e2fc555399' + +from alembic import op +import sqlalchemy as sa + +# Data migration imports +from module_build_service import Modulemd +import hashlib + + +modulebuild = sa.Table( + 'module_builds', + sa.MetaData(), + sa.Column('id', sa.Integer, primary_key=True), + sa.Column('build_context', sa.String()), + sa.Column('runtime_context', sa.String()), + sa.Column('modulemd', sa.String()), + sa.Column('context', sa.String()), +) + + +def upgrade(): + connection = op.get_bind() + + for build in connection.execute(modulebuild.select()): + if not build.modulemd: + continue + try: + mmd = Modulemd.Module().new_from_string(build.modulemd) + mmd.upgrade() + except Exception: + # If the modulemd isn't parseable then skip this build + continue + + # If the context in the modulemd doesn't match the context in the DB, + # then set the context in the DB to the one in the modulemd + mmd_context = mmd.get_context() + if mmd_context and mmd_context != build.context: + connection.execute( + modulebuild.update().where(modulebuild.c.id == build.id).values( + context=mmd_context)) + + +def downgrade(): + connection = op.get_bind() + + for build in connection.execute(modulebuild.select()): + if build.build_context and build.runtime_context: + combined_hashes = '{0}:{1}'.format( + build.build_context, build.runtime_context).encode('utf-8') + context = hashlib.sha1(combined_hashes).hexdigest()[:8] + connection.execute( + modulebuild.update().where(modulebuild.c.id == build.id).values(context=context))