Module: PostgreSQLAdapterExtensions::SequenceMethods
- Defined in:
- lib/postgresql_adapter_extensions/sequence_methods.rb
Overview
This module is designed for PostgreSQL databases and may not be compatible with other database systems.
This module provides methods for managing PostgreSQL sequences, including creating, altering, and dropping sequences with various customization options.
Instance Method Summary collapse
-
#alter_sequence(name, options = {}) ⇒ void
Alters an existing PostgreSQL sequence with the given options.
-
#create_sequence(name, options = {}) ⇒ void
Creates a new sequence in the PostgreSQL database with customizable options.
-
#drop_sequence(name, options = {}) ⇒ void
Drops an existing sequence from the PostgreSQL database with optional conditions.
-
#rename_sequence(name, options = {}) ⇒ void
Renames an existing PostgreSQL sequence.
Instance Method Details
#alter_sequence(name, options = {}) ⇒ void
Uses ‘ALTER SEQUENCE` SQL statement with PostgreSQL-specific options.
This method returns an undefined value.
Alters an existing PostgreSQL sequence with the given options.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/postgresql_adapter_extensions/sequence_methods.rb', line 117 def alter_sequence(name, = {}) sql = +"ALTER SEQUENCE" sql << " IF EXISTS" if [:if_exists] sql << " #{quote_table_name(name)}" sql << " AS #{[:data_type]}" if [:data_type] sql << " INCREMENT BY #{[:increment_by]}" if [:increment_by] sql << ([:minvalue] ? " MINVALUE #{[:minvalue]}" : " NO MINVALUE") sql << ([:maxvalue] ? " MAXVALUE #{[:maxvalue]}" : " NO MAXVALUE") sql << " START WITH #{[:start]}" if [:start] sql << " RESTART" if [:restart].nil? && .key?(:restart) sql << " RESTART WITH #{[:restart]}" if [:restart] sql << " CACHE #{[:cache]}" if [:cache] sql << " #{[:cycle] ? 'CYCLE' : 'NO CYCLE'}" sql << ([:owned_by] ? " OWNED BY #{[:owned_by]}" : " OWNED BY NONE") execute(sql).tap { reload_type_map } end |
#create_sequence(name, options = {}) ⇒ void
Uses ‘CREATE SEQUENCE` SQL statement with PostgreSQL-specific options.
This method returns an undefined value.
Creates a new sequence in the PostgreSQL database with customizable options.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/postgresql_adapter_extensions/sequence_methods.rb', line 50 def create_sequence(name, = {}) = .reverse_merge( start: 1, increment_by: 1, minvalue: 1, cache: 1, cycle: false, if_not_exists: false ) sql = +"CREATE SEQUENCE" sql << " IF NOT EXISTS" if [:if_not_exists] sql << " #{quote_table_name(name)}" sql << " AS #{[:data_type]}" if [:data_type] sql << " INCREMENT BY #{[:increment_by]}" if [:increment_by] sql << ([:minvalue] ? " MINVALUE #{[:minvalue]}" : " NO MINVALUE") sql << ([:maxvalue] ? " MAXVALUE #{[:maxvalue]}" : " NO MAXVALUE") sql << " START WITH #{[:start]}" if [:start] sql << " CACHE #{[:cache]}" if [:cache] sql << " #{[:cycle] ? 'CYCLE' : 'NO CYCLE'}" sql << " OWNED BY #{[:owned_by]}" if [:owned_by] execute(sql).tap { reload_type_map } end |
#drop_sequence(name, options = {}) ⇒ void
Uses ‘DROP SEQUENCE` SQL statement with PostgreSQL-specific options.
This method returns an undefined value.
Drops an existing sequence from the PostgreSQL database with optional conditions.
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/postgresql_adapter_extensions/sequence_methods.rb', line 165 def drop_sequence(name, = {}) = .reverse_merge( if_exists: false ) sql = +"DROP SEQUENCE" sql << " IF EXISTS" if [:if_exists] sql << " #{quote_table_name(name)}" sql << " #{[:drop_behavior].to_s.upcase}" if [:drop_behavior].in?([:cascade, :restrict]) execute(sql).tap { reload_type_map } end |
#rename_sequence(name, options = {}) ⇒ void
Uses ‘ALTER SEQUENCE … RENAME TO` SQL statement in PostgreSQL.
This method returns an undefined value.
Renames an existing PostgreSQL sequence.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/postgresql_adapter_extensions/sequence_methods.rb', line 202 def rename_sequence(name, = {}) to = .fetch(:to) { raise ArgumentError, ":to is required" } = .reverse_merge( if_exists: false ) sql = +"ALTER SEQUENCE" sql << " IF EXISTS" if [:if_exists] sql << " #{quote_table_name(name)}" sql << " RENAME TO #{quote_table_name(to)}" execute(sql).tap { reload_type_map } end |