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
Use bulk insert to improve ldap group perf
Oliver Günther3527923b7deb
diff --git a/modules/ldap_groups/app/models/ldap_groups/synchronized_group.rb b/modules/ldap_groups/app/models/ldap_groups/synchronized_group.rbindex 4b066614c4e..3616274fae0 100644--- a/modules/ldap_groups/app/models/ldap_groups/synchronized_group.rb+++ b/modules/ldap_groups/app/models/ldap_groups/synchronized_group.rb@@ -4,7 +4,9 @@ require 'net/ldap/dn'module LdapGroupsclass SynchronizedGroup < ApplicationRecordbelongs_to :group+belongs_to :auth_source+belongs_to :filter,class_name: '::LdapGroups::SynchronizedFilter',foreign_key: :filter_id@@ -38,18 +40,12 @@ module LdapGroups# We don't have access to the join table# so we need to ensure we delete the users that are still present in the group# since users MAY want to remove users manually- user_ids.each { |id| remove_from_actual_group(id) }+ group.users.where(id: user_ids).destroy_allendendprivate- def remove_from_actual_group(id)- group.users.delete(id)- rescue ActiveRecord::RecordNotFound- Rails.logger.warn "Tried to remove user##{id} from #{group.name}, but it was already removed."- end-def remove_all_membersremove_members!(users)enddiff --git a/modules/ldap_groups/db/migrate/20200427082928_ldap_groups_timestamps.rb b/modules/ldap_groups/db/migrate/20200427082928_ldap_groups_timestamps.rbnew file mode 100644index 00000000000..acf31e85c4d--- /dev/null+++ b/modules/ldap_groups/db/migrate/20200427082928_ldap_groups_timestamps.rb@@ -0,0 +1,16 @@+class LdapGroupsTimestamps < ActiveRecord::Migration[6.0]+ def up+ change_column_default :ldap_groups_synchronized_groups, :created_at, -> { 'CURRENT_TIMESTAMP' }+ change_column_default :ldap_groups_synchronized_groups, :updated_at, -> { 'CURRENT_TIMESTAMP' }++ change_column_default :ldap_groups_synchronized_filters, :created_at, -> { 'CURRENT_TIMESTAMP' }+ change_column_default :ldap_groups_synchronized_filters, :updated_at, -> { 'CURRENT_TIMESTAMP' }++ change_column_default :ldap_groups_memberships, :created_at, -> { 'CURRENT_TIMESTAMP' }+ change_column_default :ldap_groups_memberships, :updated_at, -> { 'CURRENT_TIMESTAMP' }+ end++ def down+ # Nothing to do+ end+enddiff --git a/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb b/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rbindex 47eef61cdc8..1b96e087cca 100644--- a/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb+++ b/modules/ldap_groups/lib/open_project/ldap_groups/synchronization.rb@@ -62,9 +62,11 @@ module OpenProject::LdapGroups# Add new membershipsgroup_members = sync.group.users.pluck(:id)- new_members = ldap_member_ids - set_by_us - group_members- new_users = User.where(id: new_members)- add_memberships!(new_users, sync)+ new_member_ids = ldap_member_ids - set_by_us - group_members+ add_memberships!(new_member_ids, sync)++ # Reset the counters after manually inserting items+ LdapGroups::SynchronizedGroup.reset_counters(sync.id, :users, touch: true)end##@@ -92,15 +94,25 @@ module OpenProject::LdapGroups### Add new users to the synced group- def add_memberships!(new_users, sync)- if new_users.empty?+ def add_memberships!(new_member_ids, sync)+ if new_member_ids.empty?Rails.logger.info "[LDAP groups] No new users to add for #{sync.dn}"returnend- Rails.logger.info { "[LDAP groups] Adding users #{new_users.pluck(:login)} to #{sync.dn}" }- sync.users << new_users.map { |user| ::LdapGroups::Membership.new(group: sync, user: user) }- sync.group.users << new_users+ Rails.logger.info { "[LDAP groups] Adding #{new_member_ids.length} users to #{sync.dn}" }++ # Bulk insert the memberships+ memberships = new_member_ids.map do |user_id|+ {+ group_id: sync.id,+ user_id: user_id+ }+ end+ ::LdapGroups::Membership.insert_all memberships++ # Have to manually add the users to the group :-/+ sync.group.users << User.where(id: new_member_ids).select(:id)end##diff --git a/modules/ldap_groups/lib/open_project/ldap_groups/synchronize_filter.rb b/modules/ldap_groups/lib/open_project/ldap_groups/synchronize_filter.rbindex cb3bd2348ae..d54d9c43d96 100644--- a/modules/ldap_groups/lib/open_project/ldap_groups/synchronize_filter.rb+++ b/modules/ldap_groups/lib/open_project/ldap_groups/synchronize_filter.rb@@ -28,18 +28,10 @@ module OpenProject::LdapGroupsgroups = []each_group do |dn, name|- entry = LdapGroups::SynchronizedGroup.find_or_initialize_by(dn: dn)- # Always set the filter and auth source, in case multiple filters match the same group- # they are simply being re-assigned to the latest one- entry.filter_id = filter.id- entry.auth_source_id = filter.auth_source_id-- # Create an OpenProject group- unless entry.group_id- entry.group = Group.find_or_initialize_by(groupname: name)- end+ sync = create_or_update_sync_group(dn)+ create_or_update_group(sync, name)- groups << entry+ groups << syncendgroups@@ -65,5 +57,29 @@ module OpenProject::LdapGroupsendendend++ ##+ # Create or update the synchronized group item+ def create_or_update_sync_group(dn)+ LdapGroups::SynchronizedGroup.find_or_initialize_by(dn: dn).tap do |sync|+ # Always set the filter and auth source, in case multiple filters match the same group+ # they are simply being re-assigned to the latest one+ sync.filter_id = filter.id+ sync.auth_source_id = filter.auth_source_id+ end+ end++ ##+ # Create or update the group+ def create_or_update_group(sync, name)+ if sync.group_id+ # Update the group name+ Group.where(id: sync.group_id).update_all(lastname: name)+ else+ # Create an OpenProject group+ entry.group = Group.find_or_initialize_by(groupname: name)+ end+ end+endenddiff --git a/modules/ldap_groups/spec/lib/synchronization_spec.rb b/modules/ldap_groups/spec/lib/synchronization_spec.rbindex 540267bbf32..c40d6f53b7b 100644--- a/modules/ldap_groups/spec/lib/synchronization_spec.rb+++ b/modules/ldap_groups/spec/lib/synchronization_spec.rb@@ -129,11 +129,9 @@ describe OpenProject::LdapGroups::Synchronization, with_ee: %i[ldap_groups] doendit 'removes all memberships and groups after removing auth source' do- expect { auth_source.destroy }+ expect { auth_source.destroy! }.to change { ::LdapGroups::Membership.count }.from(4).to(0)- auth_source.destroy-expect { synced_foo.reload }.to raise_error ActiveRecord::RecordNotFoundexpect { synced_bar.reload }.to raise_error ActiveRecord::RecordNotFoundend