This repository has been archived on 2020-03-24. You can view files and clone it, but cannot push or open issues or pull requests.
quay/data/migrations/versions/a6c463dfb9fe_back_fill_build_expand_config.py
Joseph Schorr f6ff0d6ca0 Add support for populating test data during migration testing
This change ensures that the tables in the database during migration have at least one row of "real" data, which should help catch issues in the future where we forget to set column defaults and other such schema oversights that can only be truly tested with non-empty tables

Fixes https://jira.coreos.com/browse/QUAY-913
2018-06-18 14:14:32 -04:00

91 lines
2.3 KiB
Python

"""back fill build expand_config
Revision ID: a6c463dfb9fe
Revises: b4df55dea4b3
Create Date: 2017-03-17 10:00:19.739858
"""
# revision identifiers, used by Alembic.
import json
import os
from peewee import *
from data.database import BaseModel
revision = 'a6c463dfb9fe'
down_revision = 'b4df55dea4b3'
from alembic import op
class RepositoryBuildTrigger(BaseModel):
config = TextField(default='{}')
def upgrade(tables, tester):
repostioryBuildTriggers = RepositoryBuildTrigger.select()
for repositoryBuildTrigger in repostioryBuildTriggers:
config = json.loads(repositoryBuildTrigger.config)
repositoryBuildTrigger.config = json.dumps(get_config_expand(config))
repositoryBuildTrigger.save()
def downgrade(tables, tester):
repostioryBuildTriggers = RepositoryBuildTrigger.select()
for repositoryBuildTrigger in repostioryBuildTriggers:
config = json.loads(repositoryBuildTrigger.config)
repositoryBuildTrigger.config = json.dumps(get_config_expand(config))
repositoryBuildTrigger.save()
def create_context(current_subdir):
if current_subdir == "":
current_subdir = os.path.sep + current_subdir
if current_subdir[len(current_subdir) - 1] != os.path.sep:
current_subdir += os.path.sep
context, _ = os.path.split(current_subdir)
return context
def create_dockerfile_path(current_subdir):
if current_subdir == "":
current_subdir = os.path.sep + current_subdir
if current_subdir[len(current_subdir) - 1] != os.path.sep:
current_subdir += os.path.sep
return current_subdir + "Dockerfile"
def get_config_expand(config):
""" A function to transform old records into new records """
if not config:
return config
# skip records that have been updated
if "context" in config or "dockerfile_path" in config:
return config
config_expand = {}
if "subdir" in config:
config_expand = dict(config)
config_expand["context"] = create_context(config["subdir"])
config_expand["dockerfile_path"] = create_dockerfile_path(config["subdir"])
return config_expand
def get_config_contract(config):
""" A function to delete context and dockerfile_path from config """
if not config:
return config
if "context" in config:
del config["context"]
if "dockerfile_path" in config:
del config["dockerfile_path"]
return config