1
0
mirror of https://github.com/daniviga/bite.git synced 2025-04-20 14:31:20 +02:00

Fix a regression in the hyperscale creation

This commit is contained in:
Daniele Viganò 2021-03-25 16:58:05 +01:00
parent 5cff8b9c2c
commit f4d1387f32
Signed by: dani
GPG Key ID: DB49AFC03C40EE02
3 changed files with 28 additions and 3 deletions

View File

@ -1,4 +1,4 @@
# Generated by Django 3.1.3 on 2021-03-19 08:08 # Generated by Django 3.1.7 on 2021-03-25 10:55
import django.core.validators import django.core.validators
from django.db import migrations, models from django.db import migrations, models
@ -18,7 +18,7 @@ class Migration(migrations.Migration):
migrations.CreateModel( migrations.CreateModel(
name='Telemetry', name='Telemetry',
fields=[ fields=[
('id', models.AutoField(primary_key=True, serialize=False)), ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('time', models.DateTimeField(auto_now_add=True, db_index=True)), ('time', models.DateTimeField(auto_now_add=True, db_index=True)),
('transport', models.CharField(choices=[('http', 'http'), ('mqtt', 'mqtt')], default='http', max_length=4)), ('transport', models.CharField(choices=[('http', 'http'), ('mqtt', 'mqtt')], default='http', max_length=4)),
('clock', models.IntegerField(null=True, validators=[django.core.validators.MinValueValidator(0)])), ('clock', models.IntegerField(null=True, validators=[django.core.validators.MinValueValidator(0)])),
@ -28,6 +28,7 @@ class Migration(migrations.Migration):
options={ options={
'verbose_name_plural': 'Telemetry', 'verbose_name_plural': 'Telemetry',
'ordering': ['-time', 'device'], 'ordering': ['-time', 'device'],
'unique_together': {('time', 'device')},
}, },
), ),
] ]

View File

@ -0,0 +1,24 @@
# Generated by Django 3.1.7 on 2021-03-25 10:55
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('telemetry', '0001_initial'),
]
# Timescale requires an hyperscale table to have the field used for the
# partitioning ('time') to be in the any UNIQUE constraint.
# Because of that we have a unique_together on 'time' and 'device_id',
# however Django always adds an 'id' as PRIMARY_KEY.
# Django's 'id' isn't used as a foreign key, so we are dropping
# the contraint and simply adding an index to the 'id' columns.
# We can now create the hypertable.
operations = [
migrations.RunSQL(
"ALTER TABLE telemetry_telemetry DROP CONSTRAINT telemetry_telemetry_pkey ;" # noqa: E501
"CREATE INDEX telemetry_telemetry_id_idx ON telemetry_telemetry(id);" # noqa: E501
"SELECT create_hypertable('telemetry_telemetry', 'time');"),
]

View File

@ -30,7 +30,6 @@ def telemetry_validation(value):
class Telemetry(models.Model): class Telemetry(models.Model):
id = models.AutoField(primary_key=True)
device = models.ForeignKey(Device, on_delete=models.CASCADE) device = models.ForeignKey(Device, on_delete=models.CASCADE)
time = models.DateTimeField(db_index=True, auto_now_add=True) time = models.DateTimeField(db_index=True, auto_now_add=True)
transport = models.CharField(max_length=4, transport = models.CharField(max_length=4,
@ -43,6 +42,7 @@ class Telemetry(models.Model):
class Meta: class Meta:
ordering = ['-time', 'device'] ordering = ['-time', 'device']
unique_together = ['time', 'device']
verbose_name_plural = "Telemetry" verbose_name_plural = "Telemetry"
def __str__(self): def __str__(self):