Skip to content

Commit

Permalink
Merge pull request #4441 from freelawproject/4431-upgrade-to-pghistor…
Browse files Browse the repository at this point in the history
…y-30

Upgrade to django-pghistory 3.4.0
  • Loading branch information
mlissner authored Sep 30, 2024
2 parents a341c3a + 823f0e0 commit 8d1e916
Show file tree
Hide file tree
Showing 24 changed files with 2,987 additions and 125 deletions.
109 changes: 109 additions & 0 deletions cl/alerts/migrations/0010_pghistory_v3_4_0_trigger_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Generated by Django 5.0.8 on 2024-09-10 16:53

import django.db.models.deletion
import pgtrigger.compiler
import pgtrigger.migrations
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("alerts", "0009_alter_alert_search_types_noop"),
]

operations = [
pgtrigger.migrations.RemoveTrigger(
model_name="alert",
name="update_or_delete_snapshot_update",
),
pgtrigger.migrations.RemoveTrigger(
model_name="alert",
name="update_or_delete_snapshot_delete",
),
pgtrigger.migrations.RemoveTrigger(
model_name="docketalert",
name="update_or_delete_snapshot_delete",
),
pgtrigger.migrations.RemoveTrigger(
model_name="docketalert",
name="update_or_delete_snapshot_update",
),
migrations.AlterField(
model_name="alertevent",
name="pgh_obj",
field=models.ForeignKey(
db_constraint=False,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="events",
to="alerts.alert",
),
),
migrations.AlterField(
model_name="docketalertevent",
name="pgh_obj",
field=models.ForeignKey(
db_constraint=False,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="events",
to="alerts.docketalert",
),
),
pgtrigger.migrations.AddTrigger(
model_name="alert",
trigger=pgtrigger.compiler.Trigger(
name="update_update",
sql=pgtrigger.compiler.UpsertTriggerSql(
condition='WHEN (OLD."alert_type" IS DISTINCT FROM (NEW."alert_type") OR OLD."date_last_hit" IS DISTINCT FROM (NEW."date_last_hit") OR OLD."id" IS DISTINCT FROM (NEW."id") OR OLD."name" IS DISTINCT FROM (NEW."name") OR OLD."query" IS DISTINCT FROM (NEW."query") OR OLD."rate" IS DISTINCT FROM (NEW."rate") OR OLD."secret_key" IS DISTINCT FROM (NEW."secret_key") OR OLD."user_id" IS DISTINCT FROM (NEW."user_id"))',
func='INSERT INTO "alerts_alertevent" ("alert_type", "date_created", "date_last_hit", "date_modified", "id", "name", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "query", "rate", "secret_key", "user_id") VALUES (OLD."alert_type", OLD."date_created", OLD."date_last_hit", OLD."date_modified", OLD."id", OLD."name", _pgh_attach_context(), NOW(), \'update\', OLD."id", OLD."query", OLD."rate", OLD."secret_key", OLD."user_id"); RETURN NULL;',
hash="d10c466ad18caf5d1aeea5985af5c6e6e14e17d3",
operation="UPDATE",
pgid="pgtrigger_update_update_953db",
table="alerts_alert",
when="AFTER",
),
),
),
pgtrigger.migrations.AddTrigger(
model_name="alert",
trigger=pgtrigger.compiler.Trigger(
name="delete_delete",
sql=pgtrigger.compiler.UpsertTriggerSql(
func='INSERT INTO "alerts_alertevent" ("alert_type", "date_created", "date_last_hit", "date_modified", "id", "name", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "query", "rate", "secret_key", "user_id") VALUES (OLD."alert_type", OLD."date_created", OLD."date_last_hit", OLD."date_modified", OLD."id", OLD."name", _pgh_attach_context(), NOW(), \'delete\', OLD."id", OLD."query", OLD."rate", OLD."secret_key", OLD."user_id"); RETURN NULL;',
hash="7c45dc1ce256430854bb607919cb01032db5ff5a",
operation="DELETE",
pgid="pgtrigger_delete_delete_2fa4e",
table="alerts_alert",
when="AFTER",
),
),
),
pgtrigger.migrations.AddTrigger(
model_name="docketalert",
trigger=pgtrigger.compiler.Trigger(
name="update_update",
sql=pgtrigger.compiler.UpsertTriggerSql(
condition='WHEN (OLD."alert_type" IS DISTINCT FROM (NEW."alert_type") OR OLD."date_last_hit" IS DISTINCT FROM (NEW."date_last_hit") OR OLD."docket_id" IS DISTINCT FROM (NEW."docket_id") OR OLD."id" IS DISTINCT FROM (NEW."id") OR OLD."secret_key" IS DISTINCT FROM (NEW."secret_key") OR OLD."user_id" IS DISTINCT FROM (NEW."user_id"))',
func='INSERT INTO "alerts_docketalertevent" ("alert_type", "date_created", "date_last_hit", "date_modified", "docket_id", "id", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "secret_key", "user_id") VALUES (OLD."alert_type", OLD."date_created", OLD."date_last_hit", OLD."date_modified", OLD."docket_id", OLD."id", _pgh_attach_context(), NOW(), \'update\', OLD."id", OLD."secret_key", OLD."user_id"); RETURN NULL;',
hash="cf12ed58779f12bde3fd0bdaec1ac03cbc487dd3",
operation="UPDATE",
pgid="pgtrigger_update_update_5b7b3",
table="alerts_docketalert",
when="AFTER",
),
),
),
pgtrigger.migrations.AddTrigger(
model_name="docketalert",
trigger=pgtrigger.compiler.Trigger(
name="delete_delete",
sql=pgtrigger.compiler.UpsertTriggerSql(
func='INSERT INTO "alerts_docketalertevent" ("alert_type", "date_created", "date_last_hit", "date_modified", "docket_id", "id", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "secret_key", "user_id") VALUES (OLD."alert_type", OLD."date_created", OLD."date_last_hit", OLD."date_modified", OLD."docket_id", OLD."id", _pgh_attach_context(), NOW(), \'delete\', OLD."id", OLD."secret_key", OLD."user_id"); RETURN NULL;',
hash="1ba35d31c8ea4a81ea2c41a1dcb4f57ad89b329e",
operation="DELETE",
pgid="pgtrigger_delete_delete_5ad6b",
table="alerts_docketalert",
when="AFTER",
),
),
),
]
5 changes: 2 additions & 3 deletions cl/alerts/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
from django.utils.crypto import get_random_string

from cl.lib.models import AbstractDateTimeModel
from cl.lib.pghistory import AfterUpdateOrDeleteSnapshot
from cl.search.models import SEARCH_TYPES, Docket


@pghistory.track(AfterUpdateOrDeleteSnapshot())
@pghistory.track()
class Alert(AbstractDateTimeModel):
REAL_TIME = "rt"
DAILY = "dly"
Expand Down Expand Up @@ -81,7 +80,7 @@ def subscriptions(self):
return self.filter(alert_type=DocketAlert.SUBSCRIPTION)


@pghistory.track(AfterUpdateOrDeleteSnapshot())
@pghistory.track()
class DocketAlert(AbstractDateTimeModel):
UNSUBSCRIPTION = 0
SUBSCRIPTION = 1
Expand Down
65 changes: 65 additions & 0 deletions cl/api/migrations/0011_pghistory_v3_4_0_trigger_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Generated by Django 5.0.8 on 2024-09-10 16:53

import django.db.models.deletion
import pgtrigger.compiler
import pgtrigger.migrations
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
(
"api",
"0010_remove_webhook_update_or_delete_snapshot_update_and_more",
),
]

operations = [
pgtrigger.migrations.RemoveTrigger(
model_name="webhook",
name="update_or_delete_snapshot_delete",
),
pgtrigger.migrations.RemoveTrigger(
model_name="webhook",
name="update_or_delete_snapshot_update",
),
migrations.AlterField(
model_name="webhookhistoryevent",
name="pgh_obj",
field=models.ForeignKey(
db_constraint=False,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="events",
to="api.webhook",
),
),
pgtrigger.migrations.AddTrigger(
model_name="webhook",
trigger=pgtrigger.compiler.Trigger(
name="update_update",
sql=pgtrigger.compiler.UpsertTriggerSql(
condition='WHEN (OLD."enabled" IS DISTINCT FROM (NEW."enabled") OR OLD."event_type" IS DISTINCT FROM (NEW."event_type") OR OLD."failure_count" IS DISTINCT FROM (NEW."failure_count") OR OLD."id" IS DISTINCT FROM (NEW."id") OR OLD."url" IS DISTINCT FROM (NEW."url") OR OLD."user_id" IS DISTINCT FROM (NEW."user_id") OR OLD."version" IS DISTINCT FROM (NEW."version"))',
func='INSERT INTO "api_webhookhistoryevent" ("date_created", "date_modified", "enabled", "event_type", "failure_count", "id", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "url", "user_id", "version") VALUES (OLD."date_created", OLD."date_modified", OLD."enabled", OLD."event_type", OLD."failure_count", OLD."id", _pgh_attach_context(), NOW(), \'update\', OLD."id", OLD."url", OLD."user_id", OLD."version"); RETURN NULL;',
hash="7f59f4f4c58dbace852130b00bbc731277c71574",
operation="UPDATE",
pgid="pgtrigger_update_update_a1c57",
table="api_webhook",
when="AFTER",
),
),
),
pgtrigger.migrations.AddTrigger(
model_name="webhook",
trigger=pgtrigger.compiler.Trigger(
name="delete_delete",
sql=pgtrigger.compiler.UpsertTriggerSql(
func='INSERT INTO "api_webhookhistoryevent" ("date_created", "date_modified", "enabled", "event_type", "failure_count", "id", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "url", "user_id", "version") VALUES (OLD."date_created", OLD."date_modified", OLD."enabled", OLD."event_type", OLD."failure_count", OLD."id", _pgh_attach_context(), NOW(), \'delete\', OLD."id", OLD."url", OLD."user_id", OLD."version"); RETURN NULL;',
hash="2a7c2d234520eeeef098f08c9f09b6f825f13826",
operation="DELETE",
pgid="pgtrigger_delete_delete_30383",
table="api_webhook",
when="AFTER",
),
),
),
]
7 changes: 5 additions & 2 deletions cl/api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from django.db import models

from cl.lib.models import AbstractDateTimeModel
from cl.lib.pghistory import AfterUpdateOrDeleteSnapshot


class WebhookEventType(models.IntegerChoices):
Expand All @@ -23,7 +22,11 @@ class WebhookEventType(models.IntegerChoices):


@pghistory.track(
AfterUpdateOrDeleteSnapshot(), model_name="WebhookHistoryEvent"
pghistory.UpdateEvent(
condition=pghistory.AnyChange(exclude_auto=True), row=pghistory.Old
),
pghistory.DeleteEvent(),
model_name="WebhookHistoryEvent",
)
class Webhook(AbstractDateTimeModel):
user: models.ForeignKey = models.ForeignKey(
Expand Down
101 changes: 101 additions & 0 deletions cl/audio/migrations/0010_pghistory_v3_4_0_trigger_update.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# Generated by Django 5.0.8 on 2024-09-10 16:53

import django.db.models.deletion
import pgtrigger.compiler
import pgtrigger.migrations
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
(
"audio",
"0009_alter_audio_stt_status_alter_audioevent_stt_status_noop",
),
]

operations = [
pgtrigger.migrations.RemoveTrigger(
model_name="audio",
name="update_or_delete_snapshot_update",
),
pgtrigger.migrations.RemoveTrigger(
model_name="audio",
name="update_or_delete_snapshot_delete",
),
pgtrigger.migrations.RemoveTrigger(
model_name="audiopanel",
name="update_or_delete_snapshot_update",
),
pgtrigger.migrations.RemoveTrigger(
model_name="audiopanel",
name="update_or_delete_snapshot_delete",
),
migrations.AlterField(
model_name="audioevent",
name="pgh_obj",
field=models.ForeignKey(
db_constraint=False,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="events",
to="audio.audio",
),
),
pgtrigger.migrations.AddTrigger(
model_name="audio",
trigger=pgtrigger.compiler.Trigger(
name="update_update",
sql=pgtrigger.compiler.UpsertTriggerSql(
condition='WHEN (OLD."blocked" IS DISTINCT FROM (NEW."blocked") OR OLD."case_name" IS DISTINCT FROM (NEW."case_name") OR OLD."case_name_full" IS DISTINCT FROM (NEW."case_name_full") OR OLD."case_name_short" IS DISTINCT FROM (NEW."case_name_short") OR OLD."date_blocked" IS DISTINCT FROM (NEW."date_blocked") OR OLD."docket_id" IS DISTINCT FROM (NEW."docket_id") OR OLD."download_url" IS DISTINCT FROM (NEW."download_url") OR OLD."duration" IS DISTINCT FROM (NEW."duration") OR OLD."filepath_ia" IS DISTINCT FROM (NEW."filepath_ia") OR OLD."ia_upload_failure_count" IS DISTINCT FROM (NEW."ia_upload_failure_count") OR OLD."id" IS DISTINCT FROM (NEW."id") OR OLD."judges" IS DISTINCT FROM (NEW."judges") OR OLD."local_path_mp3" IS DISTINCT FROM (NEW."local_path_mp3") OR OLD."local_path_original_file" IS DISTINCT FROM (NEW."local_path_original_file") OR OLD."processing_complete" IS DISTINCT FROM (NEW."processing_complete") OR OLD."sha1" IS DISTINCT FROM (NEW."sha1") OR OLD."source" IS DISTINCT FROM (NEW."source") OR OLD."stt_source" IS DISTINCT FROM (NEW."stt_source") OR OLD."stt_status" IS DISTINCT FROM (NEW."stt_status") OR OLD."stt_transcript" IS DISTINCT FROM (NEW."stt_transcript"))',
func='INSERT INTO "audio_audioevent" ("blocked", "case_name", "case_name_full", "case_name_short", "date_blocked", "date_created", "date_modified", "docket_id", "download_url", "duration", "filepath_ia", "ia_upload_failure_count", "id", "judges", "local_path_mp3", "local_path_original_file", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "processing_complete", "sha1", "source", "stt_source", "stt_status", "stt_transcript") VALUES (OLD."blocked", OLD."case_name", OLD."case_name_full", OLD."case_name_short", OLD."date_blocked", OLD."date_created", OLD."date_modified", OLD."docket_id", OLD."download_url", OLD."duration", OLD."filepath_ia", OLD."ia_upload_failure_count", OLD."id", OLD."judges", OLD."local_path_mp3", OLD."local_path_original_file", _pgh_attach_context(), NOW(), \'update\', OLD."id", OLD."processing_complete", OLD."sha1", OLD."source", OLD."stt_source", OLD."stt_status", OLD."stt_transcript"); RETURN NULL;',
hash="666831a578b79c48bc3a26d307bad268ce4e4e07",
operation="UPDATE",
pgid="pgtrigger_update_update_581d8",
table="audio_audio",
when="AFTER",
),
),
),
pgtrigger.migrations.AddTrigger(
model_name="audio",
trigger=pgtrigger.compiler.Trigger(
name="delete_delete",
sql=pgtrigger.compiler.UpsertTriggerSql(
func='INSERT INTO "audio_audioevent" ("blocked", "case_name", "case_name_full", "case_name_short", "date_blocked", "date_created", "date_modified", "docket_id", "download_url", "duration", "filepath_ia", "ia_upload_failure_count", "id", "judges", "local_path_mp3", "local_path_original_file", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "processing_complete", "sha1", "source", "stt_source", "stt_status", "stt_transcript") VALUES (OLD."blocked", OLD."case_name", OLD."case_name_full", OLD."case_name_short", OLD."date_blocked", OLD."date_created", OLD."date_modified", OLD."docket_id", OLD."download_url", OLD."duration", OLD."filepath_ia", OLD."ia_upload_failure_count", OLD."id", OLD."judges", OLD."local_path_mp3", OLD."local_path_original_file", _pgh_attach_context(), NOW(), \'delete\', OLD."id", OLD."processing_complete", OLD."sha1", OLD."source", OLD."stt_source", OLD."stt_status", OLD."stt_transcript"); RETURN NULL;',
hash="3cd590791fb720db3c7ec3d0a8ecd7de9f7d9e84",
operation="DELETE",
pgid="pgtrigger_delete_delete_15ad9",
table="audio_audio",
when="AFTER",
),
),
),
pgtrigger.migrations.AddTrigger(
model_name="audiopanel",
trigger=pgtrigger.compiler.Trigger(
name="insert_insert",
sql=pgtrigger.compiler.UpsertTriggerSql(
func='INSERT INTO "audio_audiopanelevent" ("audio_id", "id", "person_id", "pgh_context_id", "pgh_created_at", "pgh_label") VALUES (NEW."audio_id", NEW."id", NEW."person_id", _pgh_attach_context(), NOW(), \'insert\'); RETURN NULL;',
hash="2f84722ed145863e5187a0488f6aad725927f1ac",
operation="INSERT",
pgid="pgtrigger_insert_insert_b8778",
table="audio_audio_panel",
when="AFTER",
),
),
),
pgtrigger.migrations.AddTrigger(
model_name="audiopanel",
trigger=pgtrigger.compiler.Trigger(
name="delete_delete",
sql=pgtrigger.compiler.UpsertTriggerSql(
func='INSERT INTO "audio_audiopanelevent" ("audio_id", "id", "person_id", "pgh_context_id", "pgh_created_at", "pgh_label") VALUES (OLD."audio_id", OLD."id", OLD."person_id", _pgh_attach_context(), NOW(), \'delete\'); RETURN NULL;',
hash="61df118b866f8dbcf1c9b4f7d90281c98aeaec4f",
operation="DELETE",
pgid="pgtrigger_delete_delete_cae12",
table="audio_audio_panel",
when="AFTER",
),
),
),
]
7 changes: 4 additions & 3 deletions cl/audio/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from cl.lib.date_time import midnight_pt
from cl.lib.model_helpers import make_upload_path
from cl.lib.models import AbstractDateTimeModel, s3_warning_note
from cl.lib.pghistory import AfterUpdateOrDeleteSnapshot
from cl.lib.search_index_utils import (
InvalidDocumentError,
normalize_search_dicts,
Expand All @@ -22,7 +21,7 @@
from cl.search.models import SOURCES, Docket


@pghistory.track(AfterUpdateOrDeleteSnapshot())
@pghistory.track()
class Audio(AbstractDateTimeModel):
"""A class representing oral arguments and their associated metadata"""

Expand Down Expand Up @@ -290,7 +289,9 @@ def as_search_dict(self) -> Dict[str, Union[int, List[int], str]]:
return normalize_search_dicts(out)


@pghistory.track(AfterUpdateOrDeleteSnapshot(), obj_field=None)
@pghistory.track(
pghistory.InsertEvent(), pghistory.DeleteEvent(), obj_field=None
)
class AudioPanel(Audio.panel.through): # type: ignore
"""A model class to track audio panel m2m relation"""

Expand Down
Loading

0 comments on commit 8d1e916

Please sign in to comment.