DEV: Avoid subquery in the ensure-consistency onceoff

This commit is contained in:
Nat 2024-08-02 18:27:18 +08:00
parent 4ebcd1187b
commit 2202986a2f
No known key found for this signature in database
GPG Key ID: 4938B35D927EC773
1 changed files with 3 additions and 1 deletions

View File

@ -27,7 +27,9 @@ class EnsureConsistency < ActiveRecord::Migration[7.0]
# delete votes associated with no user
DB.exec(<<~SQL)
DELETE FROM topic_voting_votes
WHERE user_id NOT IN (SELECT id FROM users)
USING topic_voting_votes tvv
LEFT JOIN users u ON tvv.user_id = u.id
WHERE u.id IS NULL;
SQL
# delete votes associated with no topics