Skip to content

Commit

Permalink
Merge pull request #550 from arachnys/fix-jenkins-migrations
Browse files Browse the repository at this point in the history
Fix jenkins check migrations
  • Loading branch information
frankh authored Sep 6, 2017
2 parents 89e9290 + 4659e60 commit 39cd729
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
19 changes: 16 additions & 3 deletions cabot/cabotapp/migrations/0005_auto_20170818_1202.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import os

import django.db.models.deletion
from django.contrib.contenttypes.models import ContentType
from django.db import migrations, models


Expand All @@ -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,
Expand Down Expand Up @@ -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)


Expand Down

0 comments on commit 39cd729

Please sign in to comment.