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
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
Remove tokens on deletion and add migration and foreign key
Oliver Günther4e2b1fc5eace
diff --git a/app/workers/principals/delete_job.rb b/app/workers/principals/delete_job.rbindex 7c4f6d1695d..0a925463033 100644--- a/app/workers/principals/delete_job.rb+++ b/app/workers/principals/delete_job.rb@@ -65,6 +65,7 @@ class Principals::DeleteJob < ApplicationJobdef delete_associated(principal)delete_notifications(principal)delete_private_queries(principal)+ delete_tokens(principal)enddef delete_notifications(principal)@@ -76,6 +77,10 @@ class Principals::DeleteJob < ApplicationJobCostQuery.where(user_id: principal.id, is_public: false).delete_allend+ def delete_tokens(principal)+ ::Token::Base.where(user_id: principal.id).destroy_all+ end+def update_cost_queries(principal)CostQuery.in_batches.each_record do |query|serialized = query.serializeddiff --git a/db/migrate/20221017073431_remove_orphaned_tokens.rb b/db/migrate/20221017073431_remove_orphaned_tokens.rbnew file mode 100644index 00000000000..61c804451ae--- /dev/null+++ b/db/migrate/20221017073431_remove_orphaned_tokens.rb@@ -0,0 +1,10 @@+class RemoveOrphanedTokens < ActiveRecord::Migration[7.0]+ def up+ Token::Base.where.not(user_id: User.select(:id)).delete_all+ add_foreign_key :tokens, :users+ end++ def down+ # Nothing to do+ end+enddiff --git a/spec/workers/principals/delete_job_integration_spec.rb b/spec/workers/principals/delete_job_integration_spec.rbindex 69b1731fbb0..a8abfc0946a 100644--- a/spec/workers/principals/delete_job_integration_spec.rb+++ b/spec/workers/principals/delete_job_integration_spec.rb@@ -220,6 +220,22 @@ describe Principals::DeleteJob, type: :model doit { expect(Query.find_by(id: query.id)).to be_nil }end+ shared_examples_for 'token handling' do+ let!(:backup_token) do+ create(:backup_token, user: principal)+ end++ let!(:invitation_token) do+ create(:invitation_token, user: principal)+ end++ before do+ job+ end++ it { expect(Token::Base.where(user_id: principal.id)).to be_empty }+ end+shared_examples_for 'issue category handling' dolet(:category) docreate(:category,