Skip to content

Commit

Permalink
Switch from JSONField when PG is too old (<9.4).
Browse files Browse the repository at this point in the history
  • Loading branch information
audaciouscode committed Mar 25, 2017
1 parent 50f59ab commit e2313d6
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 54 deletions.
47 changes: 32 additions & 15 deletions migrations/0001_initial.py
Expand Up @@ -6,25 +6,42 @@
import django.contrib.postgres.fields.jsonb
from django.db import migrations, models

from ..models import install_supports_jsonfield

class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='DataPoint',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('source', models.CharField(max_length=1024)),
('generator', models.CharField(max_length=1024)),
('created', models.DateTimeField()),
('generated_at', django.contrib.gis.db.models.fields.PointField(null=True, srid=4326)),
('recorded', models.DateTimeField()),
('properties', django.contrib.postgres.fields.jsonb.JSONField()),
],
),
]

if install_supports_jsonfield():
operations = [
migrations.CreateModel(
name='DataPoint',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('source', models.CharField(max_length=1024)),
('generator', models.CharField(max_length=1024)),
('created', models.DateTimeField()),
('generated_at', django.contrib.gis.db.models.fields.PointField(null=True, srid=4326)),
('recorded', models.DateTimeField()),
('properties', django.contrib.postgres.fields.jsonb.JSONField()),
],
),
]
else:
operations = [
migrations.CreateModel(
name='DataPoint',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('source', models.CharField(max_length=1024)),
('generator', models.CharField(max_length=1024)),
('created', models.DateTimeField()),
('generated_at', django.contrib.gis.db.models.fields.PointField(null=True, srid=4326)),
('recorded', models.DateTimeField()),
('properties', models.TextField(max_length=(32 * 1024 * 1024 * 1024))),
],
),
]
36 changes: 25 additions & 11 deletions migrations/0002_databundle.py
Expand Up @@ -5,21 +5,35 @@
import django.contrib.postgres.fields.jsonb
from django.db import migrations, models

from ..models import install_supports_jsonfield

class Migration(migrations.Migration):

dependencies = [
('passive_data_kit', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='DataBundle',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('recorded', models.DateTimeField()),
('properties', django.contrib.postgres.fields.jsonb.JSONField()),
('processed', models.BooleanField(default=False)),
],
),
]
if install_supports_jsonfield():
operations = [
migrations.CreateModel(
name='DataBundle',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('recorded', models.DateTimeField()),
('properties', django.contrib.postgres.fields.jsonb.JSONField()),
('processed', models.BooleanField(default=False)),
],
),
]
else:
operations = [
migrations.CreateModel(
name='DataBundle',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('recorded', models.DateTimeField()),
('properties', models.TextField(max_length=(32 * 1024 * 1024 * 1024))),
('processed', models.BooleanField(default=False)),
],
),
]
56 changes: 38 additions & 18 deletions migrations/0009_reportjob.py
Expand Up @@ -7,28 +7,48 @@
from django.db import migrations, models
import django.db.models.deletion

from ..models import install_supports_jsonfield

class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('passive_data_kit', '0008_datapointvisualizations'),
]

operations = [
migrations.CreateModel(
name='ReportJob',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('requested', models.DateTimeField(db_index=True)),
('started', models.DateTimeField(db_index=True)),
('completed', models.DateTimeField(db_index=True)),
('parameters', django.contrib.postgres.fields.jsonb.JSONField()),
('destination', models.EmailField(max_length=1024)),
('report', models.FileField(upload_to='pdk_reports')),
('generator_identifier', models.CharField(db_index=True, max_length=1024)),
('last_updated', models.DateTimeField(db_index=True)),
('requester', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]

if install_supports_jsonfield():
operations = [
migrations.CreateModel(
name='ReportJob',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('requested', models.DateTimeField(db_index=True)),
('started', models.DateTimeField(db_index=True)),
('completed', models.DateTimeField(db_index=True)),
('parameters', django.contrib.postgres.fields.jsonb.JSONField()),
('destination', models.EmailField(max_length=1024)),
('report', models.FileField(upload_to='pdk_reports')),
('generator_identifier', models.CharField(db_index=True, max_length=1024)),
('last_updated', models.DateTimeField(db_index=True)),
('requester', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
else:
operations = [
migrations.CreateModel(
name='ReportJob',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('requested', models.DateTimeField(db_index=True)),
('started', models.DateTimeField(db_index=True)),
('completed', models.DateTimeField(db_index=True)),
('parameters', models.TextField(max_length=(32 * 1024 * 1024 * 1024))),
('destination', models.EmailField(max_length=1024)),
('report', models.FileField(upload_to='pdk_reports')),
('generator_identifier', models.CharField(db_index=True, max_length=1024)),
('last_updated', models.DateTimeField(db_index=True)),
('requester', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
],
),
]
25 changes: 15 additions & 10 deletions models.py
Expand Up @@ -26,15 +26,9 @@ def generator_label(identifier):

return identifier

_CACHED_SUPPORTS_JSON_FIELD = None

def install_supports_jsonfield():
if _CACHED_SUPPORTS_JSON_FIELD is not None:
return _CACHED_SUPPORTS_JSON_FIELD

print(connection.backend_info)

return False
return (connection.pg_version >= 90400)


class DataPoint(models.Model):
Expand All @@ -47,11 +41,19 @@ class DataPoint(models.Model):

recorded = models.DateTimeField(db_index=True)

properties = JSONField()
if install_supports_jsonfield():
properties = JSONField()
else:
properties = models.TextField(max_length=(32 * 1024 * 1024 * 1024))


class DataBundle(models.Model):
recorded = models.DateTimeField(db_index=True)
properties = JSONField()

if install_supports_jsonfield():
properties = JSONField()
else:
properties = models.TextField(max_length=(32 * 1024 * 1024 * 1024))

processed = models.BooleanField(default=False, db_index=True)

Expand Down Expand Up @@ -139,7 +141,10 @@ class ReportJob(models.Model):
started = models.DateTimeField(db_index=True, null=True, blank=True)
completed = models.DateTimeField(db_index=True, null=True, blank=True)

parameters = JSONField()
if install_supports_jsonfield():
parameters = JSONField()
else:
parameters = models.TextField(max_length=(32 * 1024 * 1024 * 1024))

report = models.FileField(upload_to='pdk_reports', null=True, blank=True)

Expand Down

0 comments on commit e2313d6

Please sign in to comment.