Class: ActiveRecord::ConnectionAdapters::PostgreSQLAdapter

Inherits:
AbstractAdapter
  • Object
show all
Defined in:
lib/bulk_data_methods/monkey_patch_postgres.rb

Instance Method Summary collapse

Instance Method Details

#next_sequence_value(sequence_name) ⇒ Integer

Get the next value in a sequence. Used on INSERT operation for partitioning like by_id because the ID is required before the insert so that the specific child table is known ahead of time.

Parameters:

  • sequence_name (String)

    the name of the sequence to fetch the next value from

Returns:

  • (Integer)

    the value from the sequence



15
16
17
# File 'lib/bulk_data_methods/monkey_patch_postgres.rb', line 15

def next_sequence_value(sequence_name)
  return execute("select nextval('#{sequence_name}')").field_values("nextval").first.to_i
end

#next_sequence_values(sequence_name, batch_size) ⇒ Array<Integer>

Get the some next values in a sequence.

Parameters:

  • sequence_name (String)

    the name of the sequence to fetch the next values from

  • batch_size (Integer)

    count of values.

Returns:

  • (Array<Integer>)

    an array of values from the sequence



25
26
27
28
# File 'lib/bulk_data_methods/monkey_patch_postgres.rb', line 25

def next_sequence_values(sequence_name, batch_size)
  result = execute("select nextval('#{sequence_name}') from generate_series(1, #{batch_size})")
  return result.field_values("nextval").map(&:to_i)
end