Class: ActiveRecord::Sequence

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/sequence.rb,
lib/active_record/sequence/error.rb,
lib/active_record/sequence/version.rb,
lib/active_record/sequence/sequence_sql_builder.rb

Overview

rubocop:disable Style/Documentation

Constant Summary collapse

CREATE_ERRORS =
{
  'PG::DuplicateTable' => ActiveRecord::Sequence::AlreadyExist,
}.freeze
DROP_ERRORS =
{
  'PG::UndefinedTable' => NotExist,
}.freeze
NEXT_ERRORS =
{
  'PG::ObjectNotInPrerequisiteState' => StopIteration,
  'PG::SequenceGeneratorLimitExceeded' => StopIteration,
  'PG::UndefinedTable' => ActiveRecord::Sequence::NotExist,
}.freeze
PEEK_ERRORS =
{
  'PG::ObjectNotInPrerequisiteState' => ActiveRecord::Sequence::CurrentValueUndefined,
  'PG::UndefinedTable' => ActiveRecord::Sequence::NotExist,
}.freeze
Error =
Class.new(StandardError)
AlreadyExist =

Sequence is already exists and thus could not be created.

Class.new(Error)
CurrentValueUndefined =

To obtain current value, you have to call ‘#next` first.

Class.new(Error)
NotExist =

Sequence is not exists and thus could not be deleted or accessed.

Class.new(Error)
VERSION =
'0.3.0'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Sequence

Returns a new instance of Sequence.

Parameters:

  • name (String)


81
82
83
# File 'lib/active_record/sequence.rb', line 81

def initialize(name)
  @name = name
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



78
79
80
# File 'lib/active_record/sequence.rb', line 78

def name
  @name
end

Class Method Details

.create(name, options = {}) ⇒ Sequence

Create sequence

Parameters:

  • name (String)
  • options ({}) (defaults to: {})

Options Hash (options):

  • :start (Integer) — default: 1
  • :increment (Integer) — default: 1

    specifies which value is added to the current sequence value to create a new value. A positive value will make an ascending sequence, a negative one a descending sequence.

  • :min (Integer)

    determines the minimum value a sequence can generate. The defaults are 1 and -2^63-1 for ascending and descending sequences, respectively.

  • :max (Integer)

    determines the maximum value for the sequence. The defaults are 2^63-1 and -1 for ascending and descending sequences, respectively.

  • :cycle (Boolean) — default: false

    allows the sequence to wrap around when the max value or min value has been reached by an ascending or descending sequence respectively. If the limit is reached, the next number generated will be the min value or max value, respectively.

Returns:

See Also:



36
37
38
39
40
41
42
43
44
# File 'lib/active_record/sequence.rb', line 36

def create(name, options = {})
  create_sql = SequenceSQLBuilder.new(name, options).to_sql
  handle_postgres_errors(CREATE_ERRORS) do
    with_connection do |connection|
      connection.execute(create_sql)
    end
  end
  new(name)
end

.drop(name) ⇒ void

This method returns an undefined value.

Parameters:

  • name (String)


52
53
54
55
56
57
58
59
# File 'lib/active_record/sequence.rb', line 52

def drop(name)
  drop_sql = format('DROP SEQUENCE %s', name)
  handle_postgres_errors(DROP_ERRORS) do
    with_connection do |connection|
      connection.execute(drop_sql)
    end
  end
end

.handle_postgres_errors(mappings) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • mappings ({})

    from PG errors to library errors



70
71
72
73
74
75
# File 'lib/active_record/sequence.rb', line 70

def handle_postgres_errors(mappings)
  yield
rescue ActiveRecord::StatementInvalid => error
  library_error = mappings.fetch(error.cause.class.name) { raise }
  raise library_error
end

.with_connectionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



62
63
64
65
66
# File 'lib/active_record/sequence.rb', line 62

def with_connection
  ActiveRecord::Base.connection_pool.with_connection do |connection|
    yield(connection)
  end
end

Instance Method Details

#nextInteger

Returns:

  • (Integer)


92
93
94
95
96
97
# File 'lib/active_record/sequence.rb', line 92

def next
  next_sql = 'SELECT nextval(%s)'.freeze
  handle_postgres_errors(NEXT_ERRORS) do
    execute(next_sql, name)
  end
end

#peekInteger

Returns:

  • (Integer)


105
106
107
108
109
110
# File 'lib/active_record/sequence.rb', line 105

def peek
  current_sql = 'SELECT currval(%s)'.freeze
  handle_postgres_errors(PEEK_ERRORS) do
    execute(current_sql, name)
  end
end