Skip to content

Commit

Permalink
track trigger runs on update (#3654)
Browse files Browse the repository at this point in the history
* track trigger runs on update

* cleanup track update trigger migration

* trigger track trigger for all unlisted tracks
  • Loading branch information
stereosteve authored Aug 17, 2022
1 parent 5f0803f commit 6401d59
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion discovery-provider/alembic/trigger_sql/handle_track.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ $$ language plpgsql;

do $$ begin
create trigger on_track
after insert on tracks
after insert or update on tracks
for each row execute procedure handle_track();
exception
when others then null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""track trigger update
Revision ID: 5d3f95470222
Revises: f1e86fba0357
Create Date: 2022-08-17 14:06:25.596816
"""
import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = '5d3f95470222'
down_revision = 'f1e86fba0357'
branch_labels = None
depends_on = None

def upgrade():
connection = op.get_bind()
connection.execute(sa.text('''
begin;
drop trigger if exists on_track on tracks;
create trigger on_track
after insert or update on tracks
for each row execute procedure handle_track();
-- trigger trigger for unlisted tracks
update tracks
set is_unlisted=is_unlisted
where is_current=true and is_unlisted=true;
commit;
'''))


def downgrade():
connection = op.get_bind()
connection.execute(sa.text('''
begin;
drop trigger if exists on_track on tracks;
create trigger on_track
after insert or update on tracks
for each row execute procedure handle_track();
commit;
'''))
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from integration_tests.utils import populate_mock_db
from src.models.playlists.aggregate_playlist import AggregatePlaylist
from src.models.tracks.aggregate_track import AggregateTrack
from src.models.tracks.track import Track
from src.models.users.aggregate_user import AggregateUser
from src.utils.db_session import get_db

Expand Down Expand Up @@ -127,6 +128,24 @@ def test_aggregate_counters(app):
agg_tracks[1], AggregateTrack(track_id=2, repost_count=0, save_count=0)
)

# is_unlisted is update in place
track: Track = (
session.query(Track)
.filter(Track.track_id == 2)
.filter(Track.is_current == True)
.first()
)
track.is_unlisted = True
session.add(track)
session.flush()
assert track.is_unlisted == True

# check that agg_user track count is updated
# must call refresh to avoid sqlalchemy object cache
owner_agg: AggregateUser = session.query(AggregateUser).get(track.owner_id)
session.refresh(owner_agg)
assert owner_agg.track_count == 1

# playlists
agg_playlists: List[AggregatePlaylist] = (
session.query(AggregatePlaylist)
Expand Down

0 comments on commit 6401d59

Please sign in to comment.