Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
KirIgor committed May 17, 2024
1 parent 060429b commit 9b600aa
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions lib/umbrellio_utils/database.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

# rubocop:disable Metrics/ModuleLength
module UmbrellioUtils
module Database
extend self
Expand Down Expand Up @@ -44,14 +45,7 @@ def with_temp_table(dataset, page_size: 1_000, sleep: nil, **options)

loop do
DB.transaction do
pk_column = primary_key.is_a?(Array) ? :temp_table_id : primary_key
pk_expr = DB[temp_table_name].select(pk_column).reverse(pk_column).limit(page_size)
deleted_items = DB[temp_table_name].where(pk_column => pk_expr).returning.delete
pk_set = deleted_items.map do |item|
next complex_key_expr(primary_key, item) if primary_key.is_a?(Array)
item[primary_key]
end

pk_set = pop_pk_batch(primary_key, temp_table_name, page_size)
yield(pk_set) if pk_set.any?
end

Expand All @@ -75,17 +69,17 @@ def create_temp_table(dataset, primary_key:)

DB.drop_table?(temp_table_name)
if primary_key.is_a?(Array)
create_complex_key_temp_table(temp_table_name, dataset, primary_key:)
create_complex_key_temp_table(temp_table_name, dataset, primary_key)
else
create_simple_key_temp_table(temp_table_name, dataset, primary_key:)
create_simple_key_temp_table(temp_table_name, dataset, primary_key)
end

temp_table_name
end

private

def create_simple_key_temp_table(temp_table_name, dataset, primary_key:)
def create_simple_key_temp_table(temp_table_name, dataset, primary_key)
model = dataset.model
type = model.db_schema[primary_key][:db_type]

Expand All @@ -97,7 +91,7 @@ def create_simple_key_temp_table(temp_table_name, dataset, primary_key:)
DB[temp_table_name].disable_insert_returning.insert(insert_ds)
end

def create_complex_key_temp_table(temp_table_name, dataset, primary_key:)
def create_complex_key_temp_table(temp_table_name, dataset, primary_key)
model = dataset.model

DB.create_table(temp_table_name, unlogged: true) do
Expand All @@ -115,6 +109,16 @@ def create_complex_key_temp_table(temp_table_name, dataset, primary_key:)
DB[temp_table_name].disable_insert_returning.insert(insert_ds)
end

def pop_pk_batch(primary_key, temp_table_name, batch_size)
pk_column = primary_key.is_a?(Array) ? :temp_table_id : primary_key
pk_expr = DB[temp_table_name].select(pk_column).reverse(pk_column).limit(batch_size)
deleted_items = DB[temp_table_name].where(pk_column => pk_expr).returning.delete
deleted_items.map do |item|
next complex_key_expr(primary_key, item) if primary_key.is_a?(Array)
item[primary_key]
end
end

def primary_key_from(**options)
options.fetch(:primary_key, :id)
end
Expand All @@ -135,3 +139,4 @@ def complex_key_expr(primary_key, record)
end
end
end
# rubocop:enable Metrics/ModuleLength

0 comments on commit 9b600aa

Please sign in to comment.