[deploy] Cleanup orphaned Ahoy rows in the DB (#9710)

* Cleanup orphaned Ahoy rows in the DB

* Trigger Travis correctly
This commit is contained in:
rhymes 2020-08-11 16:20:54 +02:00 committed by GitHub
parent d5e13f5ba7
commit 3a56e4b442
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,32 @@
module DataUpdateScripts
class RemoveOrphanedAhoyRows
def run
# Delete all Ahoy::Events belonging to users that don't exist anymore
ActiveRecord::Base.connection.execute(
<<~SQL,
DELETE FROM ahoy_events
WHERE user_id IS NOT NULL
AND user_id NOT IN (SELECT id FROM users);
SQL
)
# Delete all Ahoy::Messages belonging to users that don't exist anymore
ActiveRecord::Base.connection.execute(
<<~SQL,
DELETE FROM ahoy_messages
WHERE user_id IS NOT NULL
AND user_id NOT IN (SELECT id FROM users);
SQL
)
# Delete all Ahoy::Visits belonging to users that don't exist anymore
ActiveRecord::Base.connection.execute(
<<~SQL,
DELETE FROM ahoy_visits
WHERE user_id IS NOT NULL
AND user_id NOT IN (SELECT id FROM users);
SQL
)
end
end
end