Skip to content

Commit

Permalink
170 2 adjust event only to fire when due (#664)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrian-codecov authored Sep 3, 2024
1 parent 35c1143 commit 853534a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
11 changes: 8 additions & 3 deletions database/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from google.cloud import pubsub_v1
from shared.config import get_config
from sqlalchemy import event
from sqlalchemy import event, inspect

from database.models.core import Repository

Expand Down Expand Up @@ -51,5 +51,10 @@ def after_insert_repo(mapper, connection, target):

@event.listens_for(Repository, "after_update")
def after_update_repo(mapper, connection, target):
log.info("After update signal")
_sync_repo(target)
state = inspect(target)

for attr in state.attrs:
if attr.key in ["name", "upload_token"] and attr.history.has_changes():
log.info("After update signal")
_sync_repo(target)
break
1 change: 1 addition & 0 deletions database/models/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ class Repository(CodecovBaseModel):
random.choice(string.ascii_uppercase + string.digits) for _ in range(10)
),
)
upload_token = Column(postgresql.UUID)
language = Column(types.Text)
languages = Column(postgresql.ARRAY(types.String), nullable=True, default=[])
languages_last_updated = Column(types.DateTime, server_default=FetchedValue())
Expand Down
5 changes: 5 additions & 0 deletions database/tests/unit/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def test_shelter_repo_sync(dbsession, mock_configuration, mocker):
repo.name = "testing"
dbsession.commit()

# this wouldn't trigger the publish via SQLAlchemy events (after_update) since it's an unimportant attribute
repo.activated = True
dbsession.commit()

# this is from the first trigger
publish.assert_called_once_with(
"projects/test-project-id/topics/test-topic-id",
b'{"type": "repo", "sync": "one", "id": 91728376}',
Expand Down

0 comments on commit 853534a

Please sign in to comment.