Class: ActiveRecord::Sequence
- Inherits:
-
Object
- Object
- ActiveRecord::Sequence
- 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
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.create(name, options = {}) ⇒ Sequence
Create sequence.
- .drop(name) ⇒ void
- .handle_postgres_errors(mappings) ⇒ Object private
- .with_connection ⇒ Object private
Instance Method Summary collapse
-
#initialize(name) ⇒ Sequence
constructor
A new instance of Sequence.
- #next ⇒ Integer
- #peek ⇒ Integer
Constructor Details
#initialize(name) ⇒ Sequence
Returns a new instance of Sequence.
81 82 83 |
# File 'lib/active_record/sequence.rb', line 81 def initialize(name) @name = name end |
Instance Attribute Details
#name ⇒ Object (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
36 37 38 39 40 41 42 43 44 |
# File 'lib/active_record/sequence.rb', line 36 def create(name, = {}) create_sql = SequenceSQLBuilder.new(name, ).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.
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.
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_connection ⇒ 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.
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
#next ⇒ 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 |
#peek ⇒ 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 |