Skip to content

Commit

Permalink
Sync django query and postgres query (#653)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
GreatBahram authored Jun 12, 2024
1 parent 2c0bd0f commit 5bb701d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion auditlog/management/commands/auditlogmigratejson.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ def migrate_using_sql(self, database):
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

Expand Down
15 changes: 15 additions & 0 deletions auditlog_tests/test_two_step_json_migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,21 @@ def test_native_postgres(self):
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()
Expand Down

0 comments on commit 5bb701d

Please sign in to comment.