zulip/zerver
Zixuan Li a0cf624eaa
migrations: Backfill extra_data_json for audit log entries.
This migration is reasonably complex because of various anomalies in existing
data.

Note that there are cases when extra_data does not contain data that is
proper json with possibly single quotes. Thus we need to use
"ast.literal_eval" to cover that.

There is also a special case for "event_type == USER_FULL_NAME_CHANGED",
where extra_data is a plain str. This event_type is only used for
RealmAuditLog, so the zilencer migration script does not need to handle
it.

The migration does not handle "event_type == REALM_DISCOUNT_CHANGED"
because ast.literal_eval only allow Python literals. We expect the admin
to populate the jsonified extra_data for extra_data_json manually
beforehand.

This chunks the backfilling migration to reduce potential block time.

The migration for zilencer is mostly similar to the one for zerver; except that
the backfill helper is added in a wrapper and unrelated events are
removed.

**Logging and error recovery**

We print out a warning when the extra_data_json field of an entry
would have been overwritten by a value inconsistent with what we derived
from extra_data. Usually this only happens when the extra_data was
corrupted before this migration. This prevents data loss by backing up
possibly corrupted data in extra_data_json with the keys
"inconsistent_old_extra_data" and "inconsistent_old_extra_data_json".
More roundtrips to the database are needed for inconsistent data, which are
expected to be infrequent.

This also outputs messages when there are audit log entries with decimals,
indicating that such entries are not backfilled. Do note that audit log
entries with decimals are not populated with "inconsistent_old_extra_data_*"
in the JSONField, because they are not overwritten.

For such audit log entries with "extra_data_json" marked as inconsistent,
we skip them in the migration.  Because when we have discovered anomalies in a
previous run, there is no need to overwrite them again nesting the extra keys
we added to it.

**Testing**

We create a migration test case utilizing the property of bulk_create
that it doesn't call our modified save method.

We extend ZulipTestCase to support verifying console output at the test
case level. The implementation is crude but the use case should be rare
enough that we don't need it to be too elaborate.

Signed-off-by: Zixuan James Li <p359101898@gmail.com>
2023-07-15 09:43:23 -07:00
..
actions streams: Send stream creation events when subscribing guests. 2023-07-13 14:04:51 -07:00
data_import slack: Handle the special case of permissions denied on team.info call. 2023-06-27 11:04:41 -07:00
integration_fixtures/nagios
lib migrations: Backfill extra_data_json for audit log entries. 2023-07-15 09:43:23 -07:00
management soft activation: Avoid QuerySet and use List instead. 2023-07-13 08:09:14 -07:00
migrations migrations: Backfill extra_data_json for audit log entries. 2023-07-15 09:43:23 -07:00
openapi streams: Send stream creation events when subscribing guests. 2023-07-13 14:04:51 -07:00
tests migrations: Backfill extra_data_json for audit log entries. 2023-07-15 09:43:23 -07:00
tornado mention: Replace 'wildcards' with 'stream_wildcards'. 2023-07-03 22:03:17 -07:00
transaction_tests test_classes: Do not necessary wrap test cases in a transaction. 2023-07-06 11:44:50 -07:00
views welcome-emails: Separate followup_day1 email from other welcome emails. 2023-07-11 14:15:52 -07:00
webhooks integrations: Update documentation for Grafana Integration. 2023-07-13 16:56:01 -07:00
worker missed-message: Lock ScheduledMessageNotificationEmail rows. 2023-07-13 11:50:42 -07:00
__init__.py
apps.py requirements: Upgrade Python requirements. 2023-04-03 22:39:21 -07:00
context_processors.py emails: Add corporate_enabled to context for emails. 2023-05-01 10:32:43 -07:00
decorator.py logging_util: Remove dependence on get_current_request. 2023-07-11 22:23:47 -07:00
filters.py
forms.py accounts: Allow user to change email visibility during first login. 2023-05-16 13:52:56 -07:00
logging_handlers.py python: Use Python 3.8 typing.{Protocol,TypedDict}. 2022-04-27 12:57:49 -07:00
middleware.py logging_util: Remove dependence on get_current_request. 2023-07-11 22:23:47 -07:00
models.py models: Convert deprecated index_together option to indexes. 2023-07-12 07:12:43 -07:00
signals.py requirements: Upgrade to Django 4.0. 2022-07-13 16:07:17 -07:00