forked from GithubBackups/healthchecks
set Check.user to not null, add uniqueness constraint to Check.code
This commit is contained in:
parent
179b085df4
commit
be4c4f7a26
@ -1,6 +1,13 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
All notable changes to this project will be documented in this file.
|
All notable changes to this project will be documented in this file.
|
||||||
|
|
||||||
|
## Unreleased
|
||||||
|
|
||||||
|
### Improvements
|
||||||
|
- Database schema: set Check.user to not null
|
||||||
|
- Database schema: add uniqueness constraint to Check.code
|
||||||
|
|
||||||
|
|
||||||
## 1.4.0 - 2018-12-25
|
## 1.4.0 - 2018-12-25
|
||||||
|
|
||||||
### Improvements
|
### Improvements
|
||||||
|
@ -20,7 +20,7 @@ class Command(BaseCommand):
|
|||||||
for user in User.objects.filter(profile=None):
|
for user in User.objects.filter(profile=None):
|
||||||
Profile.objects.get_or_create(user_id=user.id)
|
Profile.objects.get_or_create(user_id=user.id)
|
||||||
|
|
||||||
checks = Check.objects.filter(user__isnull=False)
|
checks = Check.objects
|
||||||
checks = checks.annotate(limit=F("user__profile__ping_log_limit"))
|
checks = checks.annotate(limit=F("user__profile__ping_log_limit"))
|
||||||
|
|
||||||
for check in checks:
|
for check in checks:
|
||||||
|
19
hc/api/migrations/0048_auto_20190102_0737.py
Normal file
19
hc/api/migrations/0048_auto_20190102_0737.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# Generated by Django 2.1.4 on 2019-01-02 07:37
|
||||||
|
|
||||||
|
from django.db import migrations
|
||||||
|
|
||||||
|
|
||||||
|
def remove_anon_checks(apps, schema_editor):
|
||||||
|
Check = apps.get_model("api", "Check")
|
||||||
|
Check.objects.filter(user=None).delete()
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('api', '0047_auto_20181225_2315'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.RunPython(remove_anon_checks, migrations.RunPython.noop),
|
||||||
|
]
|
26
hc/api/migrations/0049_auto_20190102_0743.py
Normal file
26
hc/api/migrations/0049_auto_20190102_0743.py
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
# Generated by Django 2.1.4 on 2019-01-02 07:43
|
||||||
|
|
||||||
|
from django.conf import settings
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('api', '0048_auto_20190102_0737'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='check',
|
||||||
|
name='code',
|
||||||
|
field=models.UUIDField(default=uuid.uuid4, editable=False, unique=True),
|
||||||
|
),
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='check',
|
||||||
|
name='user',
|
||||||
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
|
||||||
|
),
|
||||||
|
]
|
@ -64,9 +64,9 @@ def isostring(dt):
|
|||||||
class Check(models.Model):
|
class Check(models.Model):
|
||||||
name = models.CharField(max_length=100, blank=True)
|
name = models.CharField(max_length=100, blank=True)
|
||||||
tags = models.CharField(max_length=500, blank=True)
|
tags = models.CharField(max_length=500, blank=True)
|
||||||
code = models.UUIDField(default=uuid.uuid4, editable=False, db_index=True)
|
code = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
|
||||||
desc = models.TextField(blank=True)
|
desc = models.TextField(blank=True)
|
||||||
user = models.ForeignKey(User, models.CASCADE, blank=True, null=True)
|
user = models.ForeignKey(User, models.CASCADE)
|
||||||
created = models.DateTimeField(auto_now_add=True)
|
created = models.DateTimeField(auto_now_add=True)
|
||||||
kind = models.CharField(max_length=10, default="simple",
|
kind = models.CharField(max_length=10, default="simple",
|
||||||
choices=CHECK_KINDS)
|
choices=CHECK_KINDS)
|
||||||
|
@ -1,11 +1,11 @@
|
|||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from django.test import TestCase
|
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from hc.api.models import Check
|
from hc.api.models import Check
|
||||||
|
from hc.test import BaseTestCase
|
||||||
|
|
||||||
|
|
||||||
class CheckModelTestCase(TestCase):
|
class CheckModelTestCase(BaseTestCase):
|
||||||
|
|
||||||
def test_it_strips_tags(self):
|
def test_it_strips_tags(self):
|
||||||
check = Check()
|
check = Check()
|
||||||
@ -155,11 +155,12 @@ class CheckModelTestCase(TestCase):
|
|||||||
dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)
|
dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)
|
||||||
|
|
||||||
# Expect ping every round hour
|
# Expect ping every round hour
|
||||||
check = Check()
|
check = Check(user=self.alice)
|
||||||
check.kind = "cron"
|
check.kind = "cron"
|
||||||
check.schedule = "0 * * * *"
|
check.schedule = "0 * * * *"
|
||||||
check.status = "up"
|
check.status = "up"
|
||||||
check.last_ping = dt
|
check.last_ping = dt
|
||||||
|
# Need to save it for M2M relations to work:
|
||||||
check.save()
|
check.save()
|
||||||
|
|
||||||
d = check.to_dict()
|
d = check.to_dict()
|
||||||
|
@ -1,15 +1,16 @@
|
|||||||
from datetime import timedelta as td
|
from datetime import timedelta as td
|
||||||
|
|
||||||
from django.test import Client, TestCase
|
from django.test import Client
|
||||||
from django.utils.timezone import now
|
from django.utils.timezone import now
|
||||||
from hc.api.models import Check, Flip, Ping
|
from hc.api.models import Check, Flip, Ping
|
||||||
|
from hc.test import BaseTestCase
|
||||||
|
|
||||||
|
|
||||||
class PingTestCase(TestCase):
|
class PingTestCase(BaseTestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
super(PingTestCase, self).setUp()
|
super().setUp()
|
||||||
self.check = Check.objects.create()
|
self.check = Check.objects.create(user=self.alice)
|
||||||
|
|
||||||
def test_it_works(self):
|
def test_it_works(self):
|
||||||
r = self.client.get("/ping/%s/" % self.check.code)
|
r = self.client.get("/ping/%s/" % self.check.code)
|
||||||
|
@ -34,11 +34,6 @@ class LastPingTestCase(BaseTestCase):
|
|||||||
r = self.client.get("/checks/%s/last_ping/" % check.code)
|
r = self.client.get("/checks/%s/last_ping/" % check.code)
|
||||||
self.assertContains(r, "/start", status_code=200)
|
self.assertContains(r, "/start", status_code=200)
|
||||||
|
|
||||||
def test_it_requires_user(self):
|
|
||||||
check = Check.objects.create()
|
|
||||||
r = self.client.get("/checks/%s/last_ping/" % check.code)
|
|
||||||
self.assertEqual(r.status_code, 404)
|
|
||||||
|
|
||||||
def test_it_accepts_n(self):
|
def test_it_accepts_n(self):
|
||||||
check = Check(user=self.alice)
|
check = Check(user=self.alice)
|
||||||
check.save()
|
check.save()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user