aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoe Rafaniello <jrafanie@redhat.com>2013-01-10 19:56:30 +0000
committerAaron Patterson <aaron.patterson@gmail.com>2014-10-31 18:08:34 -0700
commit4f6fb19662331afd90539103cd0d2691e72c54b9 (patch)
tree3a044fa663e65706e720e9d4fd6f869cf99054f2
parent45e8a4b6c9b0294c2fd7af1d6ddbde9c728321a9 (diff)
downloadrails-4f6fb19662331afd90539103cd0d2691e72c54b9.tar.gz
rails-4f6fb19662331afd90539103cd0d2691e72c54b9.tar.bz2
rails-4f6fb19662331afd90539103cd0d2691e72c54b9.zip
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
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
index 799aafbd99..47e030e981 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -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