Skip to content

Commit

Permalink
OPSK-1249 - refactoring
Browse files Browse the repository at this point in the history
code to detect db and get bulk insert size is general and not specific to policy authorization, so moved to Seek::Util
  • Loading branch information
stuzart committed May 10, 2017
1 parent 1dbc72f commit 36d6fad
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
28 changes: 1 addition & 27 deletions lib/seek/permissions/policy_based_authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ def update_lookup_table_for_all_users
f = ActiveRecord::Base.connection.quote(false)

# Insert in batches of 10
([0] + User.pluck(:id)).each_slice(bulk_insert_batch_size) do |batch|
([0] + User.pluck(:id)).each_slice(Seek::Util.bulk_insert_batch_size) do |batch|
sql = %(INSERT INTO #{self.class.lookup_table_name}
(user_id, asset_id, can_view ,can_edit, can_download, can_manage, can_delete)
VALUES #{batch.map { |user_id| "(#{user_id}, #{id}, #{f}, #{f}, #{f}, #{f}, #{f})" }.join(', ')};)
Expand Down Expand Up @@ -424,33 +424,7 @@ def lookup_count

private

# determines the batch size for bulk inserts, as sqlite3 below version 3.7.11 doesn't handle it and requires a size
# of 1
def bulk_insert_batch_size
@bulk_insert_batch_size ||= begin
default_size=10
if database_type =='sqlite3' && !sqlite3_supports_bulk_inserts
Rails.logger.info("Sqlite3 version < 3.7.11 detected, so using single rather than bulk inserts")
1
else
default_size
end
end
end

# whether the sqlite3 version can support bulk inserts, needs to be 3.7.11 or greater
def sqlite3_supports_bulk_inserts
Gem::Version.new(sqlite3_version) >= Gem::Version.new('3.7.11')
end

# the version of the current sqlite3 database
def sqlite3_version
@sqlite3_version ||= ActiveRecord::Base.connection.select_one('SELECT SQLITE_VERSION()').values[0]
end

def database_type
ActiveRecord::Base.connection.instance_values['config'][:adapter]
end

# Note, nil user means ALL users, not guest user
def update_lookup(permission, user = nil, overwrite = true)
Expand Down
28 changes: 28 additions & 0 deletions lib/seek/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,33 @@ def self.doiable_asset_types
persistent_classes.select(&:supports_doi?).sort_by(&:name)
end
end

# determines the batch size for bulk inserts, as sqlite3 below version 3.7.11 doesn't handle it and requires a size
# of 1
def self.bulk_insert_batch_size
@@bulk_insert_batch_size ||= begin
default_size=10
if database_type =='sqlite3' && !sqlite3_supports_bulk_inserts
Rails.logger.info("Sqlite3 version < 3.7.11 detected, so using single rather than bulk inserts")
1
else
default_size
end
end
end

# whether the sqlite3 version can support bulk inserts, needs to be 3.7.11 or greater
def self.sqlite3_supports_bulk_inserts
Gem::Version.new(sqlite3_version) >= Gem::Version.new('3.7.11')
end

# the version of the current sqlite3 database
def self.sqlite3_version
@@sqlite3_version ||= ActiveRecord::Base.connection.select_one('SELECT SQLITE_VERSION()').values[0]
end

def self.database_type
ActiveRecord::Base.connection.instance_values['config'][:adapter]
end
end
end

0 comments on commit 36d6fad

Please sign in to comment.