Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

Drop AR in migrations for continuity

Kabiru Mwenja53c090747094
diff --git a/db/migrate/20250108100511_remove_incorrect_manage_own_reminders_permission.rb b/db/migrate/20250108100511_remove_incorrect_manage_own_reminders_permission.rb
index 02ac3e39c7c..8602188f556 100644
--- a/db/migrate/20250108100511_remove_incorrect_manage_own_reminders_permission.rb
+++ b/db/migrate/20250108100511_remove_incorrect_manage_own_reminders_permission.rb
@@ -1,14 +1,31 @@
class RemoveIncorrectManageOwnRemindersPermission < ActiveRecord::Migration[7.1]
def up
# Remove manage_own_reminders permission from non member and anonymous roles
- RolePermission
- .where(
- role: Role.where(builtin: [Role::BUILTIN_NON_MEMBER, Role::BUILTIN_ANONYMOUS]),
- permission: :manage_own_reminders
- ).destroy_all
+ execute <<-SQL.squish
+ DELETE FROM role_permissions
+ WHERE role_id IN (
+ SELECT id FROM roles WHERE builtin IN (#{Role::BUILTIN_NON_MEMBER}, #{Role::BUILTIN_ANONYMOUS})
+ )
+ AND permission = 'manage_own_reminders'
+ SQL
- # Remove all reminders created by anonymous user
- Reminder.where(creator_id: AnonymousUser.first.id).destroy_all
+ # Remove all reminders created by anonymous user and cascade delete related records
+ execute <<-SQL.squish
+ WITH deleted_reminders AS (
+ DELETE FROM reminders
+ WHERE creator_id IN (
+ SELECT id FROM users WHERE type = 'AnonymousUser'
+ )
+ RETURNING id
+ ),
+ deleted_reminder_notifications AS (
+ DELETE FROM reminder_notifications
+ WHERE reminder_id IN (SELECT id FROM deleted_reminders)
+ RETURNING notification_id
+ )
+ DELETE FROM notifications
+ WHERE id IN (SELECT notification_id FROM deleted_reminder_notifications)
+ SQL
end
# No-op