Sync django query and postgres query (#653)

* run postgres query for rows that changes is null for them and there is value for changes_text

* add a test case to make when changes has value it wont be overwritten by changes_text
This commit is contained in:
Bahram Aghaei 2024-06-12 14:30:29 +02:00 committed by GitHub
parent 2c0bd0fac6
commit 5bb701d821
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 1 deletions

View file

@ -125,7 +125,13 @@ class Command(BaseCommand):
def postgres():
with connection.cursor() as cursor:
cursor.execute(
'UPDATE auditlog_logentry SET changes="changes_text"::jsonb'
"""
UPDATE auditlog_logentry
SET changes="changes_text"::jsonb
WHERE changes_text IS NOT NULL
AND changes_text <> ''
AND changes IS NULL
"""
)
return cursor.cursor.rowcount

View file

@ -135,6 +135,21 @@ class AuditlogMigrateJsonTest(TestCase):
self.assertEqual(errbuf, "")
self.assertIsNotNone(log_entry.changes)
def test_native_postgres_changes_not_overwritten(self):
# Arrange
log_entry = self.make_logentry()
log_entry.changes = original_changes = {"key": "value"}
log_entry.changes_text = '{"key": "new value"}'
log_entry.save()
# Act
outbuf, errbuf = self.call_command("-d=postgres")
log_entry.refresh_from_db()
# Assert
self.assertEqual(errbuf, "")
self.assertEqual(log_entry.changes, original_changes)
def test_native_unsupported(self):
# Arrange
log_entry = self.make_logentry()