Compare commits

...

2 Commits

Author SHA1 Message Date
Frédéric Péters 35c4a04504 general: switch to django postgresql json field (#65265)
gitea/fargo/pipeline/head Build started... Details
2022-05-15 11:35:46 +02:00
Frédéric Péters e9577b661f general: ensure json columns are of jsonb type (#65265) 2022-05-15 11:32:27 +02:00
7 changed files with 71 additions and 8 deletions

49
fargo/db_utils.py Normal file
View File

@ -0,0 +1,49 @@
# fargo - document box
# Copyright (C) 2016-2022 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.db import connection
from django.db.migrations.operations.base import Operation
class EnsureJsonbType(Operation):
reversible = True
def __init__(self, model_name, field_name):
self.model_name = model_name
self.field_name = field_name
def state_forwards(self, app_label, state):
pass
def database_forwards(self, app_label, schema_editor, from_state, to_state):
if connection.vendor == 'postgresql':
model = from_state.apps.get_model(app_label, self.model_name)
table_name = model._meta.db_table
field = model._meta.get_field(self.field_name)
_, column_name = field.get_attname_column()
with schema_editor.connection.cursor() as cursor:
cursor.execute(
'ALTER TABLE {table} ALTER COLUMN {col} TYPE jsonb USING {col}::jsonb;'.format(
table=table_name, col=column_name
)
)
def database_backwards(self, app_label, schema_editor, from_state, to_state):
pass
def describe(self):
return "Migrate to postgres jsonb type"

View File

@ -3,8 +3,8 @@
import datetime
import django.db.models.deletion
import jsonfield.fields
from django.conf import settings
from django.contrib.postgres.fields import JSONField
from django.db import migrations, models
from django.utils.timezone import utc
@ -124,7 +124,7 @@ class Migration(migrations.Migration):
models.CharField(max_length=128, null=True, verbose_name='content hash', blank=True),
),
('document_type', models.CharField(max_length=256, verbose_name='document type')),
('data', jsonfield.fields.JSONField(null=True, verbose_name='data')),
('data', JSONField(null=True, verbose_name='data', default=dict)),
('start', models.DateField(verbose_name='start date')),
('end', models.DateField(verbose_name='end date')),
('creator', models.CharField(max_length=256, verbose_name='creator')),

View File

@ -1,7 +1,7 @@
import datetime
import jsonfield.fields
from django.conf import settings
from django.contrib.postgres.fields import JSONField
from django.db import migrations, models
from django.utils.timezone import utc
@ -48,7 +48,7 @@ class Migration(migrations.Migration):
models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True),
),
('document_type', models.CharField(max_length=256, verbose_name='document type')),
('data', jsonfield.fields.JSONField(null=True, verbose_name='data')),
('data', JSONField(null=True, verbose_name='data', default=dict)),
('start', models.DateField(verbose_name='start date')),
('end', models.DateField(verbose_name='end date')),
('creator', models.CharField(max_length=256, verbose_name='creator')),

View File

@ -0,0 +1,16 @@
# Generated by Django 2.2.28 on 2022-05-15 09:29
from django.db import migrations
from fargo.db_utils import EnsureJsonbType
class Migration(migrations.Migration):
dependencies = [
('fargo', '0002_auto_20210824_0957'),
]
operations = [
EnsureJsonbType(model_name='Validation', field_name='data'),
]

View File

@ -22,6 +22,7 @@ import subprocess
import threading
from django.conf import settings
from django.contrib.postgres.fields import JSONField
from django.core.files.storage import default_storage
from django.db import models
from django.db.models.signals import post_delete, post_save
@ -32,7 +33,6 @@ from django.utils.html import format_html
from django.utils.http import urlquote
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _
from jsonfield import JSONField
from sorl.thumbnail import delete, get_thumbnail
from sorl.thumbnail.conf import settings as thumbnail_settings
@ -125,7 +125,7 @@ class Validation(models.Model):
content_hash = models.CharField(max_length=128, verbose_name=_('content hash'), blank=True, null=True)
origin = models.ForeignKey(Origin, verbose_name=_('origin'), null=True, on_delete=models.CASCADE)
document_type = models.CharField(max_length=256, verbose_name=_('document type'))
data = JSONField(null=True, verbose_name=_('data'))
data = JSONField(null=True, verbose_name=_('data'), default=dict)
start = models.DateField(verbose_name=_('start date'))
end = models.DateField(verbose_name=_('end date'))
creator = models.CharField(max_length=256, verbose_name=_('creator'))

View File

@ -1,5 +1,4 @@
django>=1.7,<1.9
django-tables2<1.1
django-jsonfield >= 0.9.3
djangorestframework>=3.3,<3.10
file-magic

View File

@ -110,7 +110,6 @@ setup(
'django>=2.2,<2.3',
'gadjo',
'django-tables2>=1.5,<2.2',
'django-jsonfield >= 0.9.3',
'django-filter>1,<2.5',
'djangorestframework>=3.4',
'file-magic',