Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync django query and postgres query #653

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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