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

170 2 adjust event only to fire when due #664

Merged
merged 4 commits into from
Sep 3, 2024
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
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing crazy here, we've had this field in postgres for a while, just never tracked it in worker

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
Loading