mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Added region sequencing of primary keys for Postgres.
Skip setting sequence on a table create if the value is 0 since it will start the first value at 1 anyway. This fixes the PG error 'setval: value 0 is out of bounds for sequence vms_id_seq...' encountered when migrating a new DB. BugzID: 15452,9772,13475,16850
This commit is contained in:
parent
45e8a4b6c9
commit
4f6fb19662
1 changed files with 21 additions and 0 deletions
|
@ -293,6 +293,27 @@ module ActiveRecord
|
|||
result.rows.first.first
|
||||
end
|
||||
|
||||
# Sets the sequence of a table's primary key to the specified value.
|
||||
def set_pk_sequence!(table, value, pk = nil, sequence = nil) #:nodoc:
|
||||
unless pk and sequence
|
||||
default_pk, default_sequence = pk_and_sequence_for(table)
|
||||
pk ||= default_pk
|
||||
sequence ||= default_sequence
|
||||
end
|
||||
|
||||
if pk
|
||||
if sequence
|
||||
quoted_sequence = quote_column_name(sequence)
|
||||
|
||||
select_value <<-end_sql, 'SCHEMA'
|
||||
SELECT setval('#{quoted_sequence}', #{value})
|
||||
end_sql
|
||||
else
|
||||
@logger.warn "#{table} has primary key #{pk} with no default sequence" if @logger
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# Resets the sequence of a table's primary key to the maximum value.
|
||||
def reset_pk_sequence!(table, pk = nil, sequence = nil) #:nodoc:
|
||||
unless pk and sequence
|
||||
|
|
Loading…
Reference in a new issue