Skip to content

Commit 81d61b9

Browse files
GreatBahramMercymeIlya
authored andcommitted
Sync django query and postgres query (jazzband#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
1 parent f83e2c3 commit 81d61b9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

auditlog/management/commands/auditlogmigratejson.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,13 @@ def migrate_using_sql(self, database):
125125
def postgres():
126126
with connection.cursor() as cursor:
127127
cursor.execute(
128-
'UPDATE auditlog_logentry SET changes="changes_text"::jsonb'
128+
"""
129+
UPDATE auditlog_logentry
130+
SET changes="changes_text"::jsonb
131+
WHERE changes_text IS NOT NULL
132+
AND changes_text <> ''
133+
AND changes IS NULL
134+
"""
129135
)
130136
return cursor.cursor.rowcount
131137

auditlog_tests/test_two_step_json_migration.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,21 @@ def test_native_postgres(self):
136136
self.assertEqual(errbuf, "")
137137
self.assertIsNotNone(log_entry.changes)
138138

139+
def test_native_postgres_changes_not_overwritten(self):
140+
# Arrange
141+
log_entry = self.make_logentry()
142+
log_entry.changes = original_changes = {"key": "value"}
143+
log_entry.changes_text = '{"key": "new value"}'
144+
log_entry.save()
145+
146+
# Act
147+
outbuf, errbuf = self.call_command("-d=postgres")
148+
log_entry.refresh_from_db()
149+
150+
# Assert
151+
self.assertEqual(errbuf, "")
152+
self.assertEqual(log_entry.changes, original_changes)
153+
139154
def test_native_unsupported(self):
140155
# Arrange
141156
log_entry = self.make_logentry()

0 commit comments

Comments
 (0)