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

MBS-12685: Avoild ISE because of concurrent rating transactions #2711

Merged
merged 1 commit into from
Dec 1, 2022
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
36 changes: 15 additions & 21 deletions lib/MusicBrainz/Server/Data/Rating.pm
Original file line number Diff line number Diff line change
Expand Up @@ -148,27 +148,21 @@ sub update
my $type = $self->type;
my $table_raw = $type . '_rating_raw';

# Check if user has already rated this entity
my $whetherrated = $sql->select_single_value("
SELECT rating FROM $table_raw
WHERE $type = ? AND editor = ?", $entity_id, $user_id);
if (defined $whetherrated) {
# Already rated - so update
if ($rating) {
$sql->do("UPDATE $table_raw SET rating = ?
WHERE $type = ? AND editor = ?",
$rating, $entity_id, $user_id);
}
else {
$sql->do("DELETE FROM $table_raw
WHERE $type = ? AND editor = ?",
$entity_id, $user_id);
}
}
elsif ($rating) {
# Not rated - so insert raw rating value, unless rating = 0
$sql->do("INSERT INTO $table_raw (rating, $type, editor)
VALUES (?, ?, ?)", $rating, $entity_id, $user_id);
if ($rating) {
# Rating is not 0, so insert or update as needed
$sql->do(<<~"SQL", $rating, $entity_id, $user_id, $rating);
INSERT INTO $table_raw (rating, $type, editor)
VALUES (?, ?, ?)
ON CONFLICT ($type, editor) DO UPDATE
SET rating = ?
SQL
} else {
# Rating is 0, so delete the rating, if there is one
$sql->do(<<~"SQL", $entity_id, $user_id);
DELETE FROM $table_raw
WHERE $type = ?
AND editor = ?
SQL
}

# Get the new aggregate rating.
Expand Down
26 changes: 23 additions & 3 deletions t/lib/t/MusicBrainz/Server/Controller/Rating.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,38 @@ use Test::More;

with 't::Context', 't::Mechanize';

test 'Can rate' => sub {
test 'Ratings are inserted / updated as expected' => sub {
my $test = shift;
my $mech = $test->mech;
my $c = $test->c;

MusicBrainz::Server::Test->prepare_test_database($c);

$mech->get_ok('/login');
$mech->submit_form( with_fields => { username => 'new_editor', password => 'password' } );
$mech->submit_form(
with_fields => {username => 'new_editor', password => 'password' }
);

$mech->get('/rating/rate/?entity_type=label&entity_id=2&rating=100');
is ($mech->status, 200);
is($mech->status, 200, 'First time rating submission went through');

my $label = $c->model('Label')->get_by_id(2);
$c->model('Label')->load_meta($label);
is($label->rating, 100, 'The rating was successfully added');

$mech->get('/rating/rate/?entity_type=label&entity_id=2&rating=20');
is($mech->status, 200, 'Re-rating submission went through');

$label = $c->model('Label')->get_by_id(2);
$c->model('Label')->load_meta($label);
is($label->rating, 20, 'The rating was successfully updated');

$mech->get('/rating/rate/?entity_type=label&entity_id=2&rating=0');
is($mech->status, 200, 'Delete rating submission went through');

$label = $c->model('Label')->get_by_id(2);
$c->model('Label')->load_meta($label);
is($label->rating, undef, 'The rating was successfully deleted');
};

test 'Cannot rate without a confirmed email address' => sub {
Expand Down