|
| 1 | +Upgrading |
| 2 | +========= |
| 3 | + |
| 4 | +This page contains specific information on upgrading between certain versions of ``invenio-oauthclient`` where special steps are involved. |
| 5 | + |
| 6 | +====== |
| 7 | +v5.4.0 |
| 8 | +====== |
| 9 | + |
| 10 | +This version involves an Alembic migration (``1758275763``) moving the ``extra_data`` column from the ``JSON`` type to the ``JSONB`` type (**only for PostgreSQL**). |
| 11 | +By doing this, we can improve performance and use more advanced JSON queries. |
| 12 | + |
| 13 | +For the majority of users, this shouldn't be an issue as the migration will be handled automatically when upgrading. |
| 14 | +However, users with an ``oauthclient_remoteaccount`` table with ~50k+ rows should use an alternative approach, as this operation could overfill the WAL and affect the stability of the database, as well as creating a full lock for several minutes. |
| 15 | + |
| 16 | +Instead, use these steps: |
| 17 | + |
| 18 | +1. Install the new version of `invenio-oauthclient`, but do not run the Alembic migrations. |
| 19 | + |
| 20 | +2. In an SQL console, run: |
| 21 | + |
| 22 | + .. code-block:: sql |
| 23 | +
|
| 24 | + ALTER TABLE oauthclient_remoteaccount ADD COLUMN extra_data_b jsonb; |
| 25 | +
|
| 26 | +3. Next, run this query repeatedly until the response indicates that no new rows are being updated. |
| 27 | + You can control the batch size depending on your requirements using the ``LIMIT`` value. |
| 28 | + |
| 29 | + .. code-block:: sql |
| 30 | +
|
| 31 | + WITH cte AS ( |
| 32 | + SELECT id |
| 33 | + FROM oauthclient_remoteaccount |
| 34 | + WHERE extra_data_b IS NULL |
| 35 | + ORDER BY id |
| 36 | + LIMIT 1000 |
| 37 | + FOR UPDATE SKIP LOCKED |
| 38 | + ) |
| 39 | + UPDATE oauthclient_remoteaccount r |
| 40 | + SET extra_data_b = r.extra_data::jsonb |
| 41 | + FROM cte |
| 42 | + WHERE r.id = cte.id |
| 43 | + RETURNING r.id; |
| 44 | +
|
| 45 | +4. Double check there are no rows left to migrate. |
| 46 | + |
| 47 | + .. code-block:: sql |
| 48 | +
|
| 49 | + SELECT COUNT(*) FROM oauthclient_remoteaccount WHERE extra_data_b IS NULL; |
| 50 | +
|
| 51 | + This should return ``0``. If it does not, continue repeating step 3. |
| 52 | + |
| 53 | +5. Drop and rename the columns in a simultaneous operation (this requires a brief lock but is much faster than the normal migration). |
| 54 | + |
| 55 | + .. code-block:: sql |
| 56 | +
|
| 57 | + ALTER TABLE oauthclient_remoteaccount DROP COLUMN extra_data; |
| 58 | + ALTER TABLE oauthclient_remoteaccount RENAME COLUMN extra_data_b TO extra_data; |
| 59 | +
|
| 60 | +6. Finally, mark the relevant migration as having been manually performed. |
| 61 | + |
| 62 | + .. code-block:: bash |
| 63 | +
|
| 64 | + invenio alembic stamp 1758275763 |
0 commit comments