question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

database: Migrate JSON fields to ArrayField or other native types.

See original GitHub issue

This issue requires expertise with Django and postgres and the ability to make some executive decisions without too much guidance. It’s not a great issue for beginners, although first-time contributors with strong Django backgrounds may do well here.

We have some things in our database that are serialized as JSON to a database field, rather than introducing a whole new database table. We are generally fine with this approach, but we can use more modern Django features like ArrayField to support certain types of data. Here is one example of JSON in our code:

1117     # Contains serialized JSON of the form:
1118     #    [("step 1", true), ("step 2", false)]
1119     # where the second element of each tuple is if the step has been
1120     # completed.
1121     onboarding_steps: str = models.TextField(default='[]')

If you search for JSON in zerver/models.py you will find candidates for fields that we possibly want to migrate to ArrayField. Obviously, part of the work here will be to create backfill migrations. And then, of course, we want to make sure that we take advantage of the Django features and ensure that tests still pass.

See https://docs.djangoproject.com/en/3.1/ref/contrib/postgres/fields/ for more info.

It’s possible that certain fields won’t map nicely to ArrayField (including possibly the example above), and we’ll want to keep them in JSON. As part of your audit, you can update this issue with your findings. In some cases you can also do small commits in a PR to clarify comments.

Also, see https://chat.zulip.org/#narrow/stream/2-general/topic/Mute.20user.20.2316915/near/1113151

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:10 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
ligmitzcommented, Mar 4, 2021

@edith007 Not now. You can go ahead and work on this if you want.

0reactions
asahcommented, Dec 25, 2021

PostgreSQL guy here (but zulip newb): is this for performance? or some other reason? I’m asking because the world is migrating to more use of JSON fields and therefore better support for them…

(y, I can maybe help…)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Migrating to the Django 3.1 JSON Field - Oak-Tree Technologies
JSONField allows for you to store semi-structured data alongside other data fields in PostgreSQL, MySQL, and SQLite databases and supports ...
Read more >
PostgreSQL specific model fields - Django documentation
ArrayField ¶ ... A field for storing lists of data. Most field types can be used, and you pass another field instance as...
Read more >
How to write a migration to convert JSON field to Postgres ...
One example assuming a json field: \d json_test Table "public.json_test" Column | Type | Collation | Nullable | Default ...
Read more >
How to turn JSON array into Postgres array?
Replace 'jsonb' with 'json' for type json in all following SQL code. TLDR: Use a custom function. Encapsulate the logic in a function...
Read more >
Semi-structured Data Types - Snowflake Documentation
To convert a value to or from the VARIANT data type, you can explicitly ... For data that is mostly regular and uses...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found