general: ensure jsonfields are using jsonb columns (fixup, #52915)

This commit is contained in:
Frédéric Péters 2021-04-11 18:03:34 +02:00
parent 21976d8f20
commit b9ff8540d7
3 changed files with 12 additions and 14 deletions

View File

@ -2,7 +2,7 @@
from django.db import migrations
from combo.db.utils import EnsureJsonbType
from combo.utils.db import EnsureJsonbType
class Migration(migrations.Migration):

View File

@ -14,6 +14,7 @@
# 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
@ -29,16 +30,17 @@ class EnsureJsonbType(Operation):
pass
def database_forwards(self, app_label, schema_editor, from_state, to_state):
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
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

View File

@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
import pytest
from django.core.management import call_command
from django.db import connection
@ -14,9 +13,6 @@ def test_ensure_jsonb_fields():
'bank_data',
'bank_result',
'cached_json',
'cached_json',
'categories',
'categories',
'categories',
'filter_params',
'filters',