diff --git a/CHANGES b/CHANGES index 0b9db9ca2..565bae3dd 100644 --- a/CHANGES +++ b/CHANGES @@ -1,6 +1,7 @@ master ------ +* Fix migration requiring jenkins environment variables are set * Reduce time to store old check results to 7 days - Currently stores for 2 months, but there's no actual way to view the old data. diff --git a/cabot/cabotapp/migrations/0005_auto_20170818_1202.py b/cabot/cabotapp/migrations/0005_auto_20170818_1202.py index 7b1995d80..1eca49be0 100644 --- a/cabot/cabotapp/migrations/0005_auto_20170818_1202.py +++ b/cabot/cabotapp/migrations/0005_auto_20170818_1202.py @@ -5,6 +5,7 @@ import os import django.db.models.deletion +from django.contrib.contenttypes.models import ContentType from django.db import migrations, models @@ -15,17 +16,26 @@ def move_old_jenkins_checks(apps, schema_editor): JenkinsCheck = apps.get_model("cabotapp", "JenkinsCheck") JenkinsConfig = apps.get_model("cabotapp", "JenkinsConfig") + # Due to a polymorphic bug, JenkinsStatusCheck actually returns all status checks + # Use this to filter out the other checks. + jenkins_content_type = ContentType.objects.filter(model="jenkinsstatuscheck").first() + + if jenkins_content_type and not JenkinsStatusCheck.objects.filter(polymorphic_ctype_id=jenkins_content_type.id).exists(): + return + if not JenkinsConfig.objects.exists(): JenkinsConfig.objects.create( name="Default Jenkins", - jenkins_api=os.environ.get("JENKINS_API"), - jenkins_user=os.environ.get("JENKINS_USER"), - jenkins_pass=os.environ.get("JENKINS_PASS"), + jenkins_api=os.environ.get("JENKINS_API", "http://jenkins.example.com"), + jenkins_user=os.environ.get("JENKINS_USER", ""), + jenkins_pass=os.environ.get("JENKINS_PASS", ""), ) default_config = JenkinsConfig.objects.first() for old_check in JenkinsStatusCheck.objects.all(): + if old_check.polymorphic_ctype_id != jenkins_content_type.id: + continue new_check = JenkinsCheck( active=old_check.active, allowed_num_failures=old_check.allowed_num_failures, @@ -55,6 +65,9 @@ def move_old_jenkins_checks(apps, schema_editor): polymorphic_ctype_id=old_check.polymorphic_ctype_id ) new_check.save(using=db_alias) + new_check.service_set.add(*old_check.service_set.all()) + new_check.instance_set.add(*old_check.instance_set.all()) + new_check.save(using=db_alias) old_check.delete(using=db_alias)