aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthew Draper <matthew@trebex.net>2017-05-31 03:12:27 +0930
committerGitHub <noreply@github.com>2017-05-31 03:12:27 +0930
commitac3c04513ed3e9c47c14f1d33db02b1e3b4031b8 (patch)
treea919fe80ba9b08a745dc95f5f18eaed627c252ca
parenteb804ad5a783e1e97720ef55a91b6aae7ebd724f (diff)
parent47cb924d9122fc00deeb9aa4db7a92685ae9e7ef (diff)
downloadrails-ac3c04513ed3e9c47c14f1d33db02b1e3b4031b8.tar.gz
rails-ac3c04513ed3e9c47c14f1d33db02b1e3b4031b8.tar.bz2
rails-ac3c04513ed3e9c47c14f1d33db02b1e3b4031b8.zip
Merge pull request #28864 from yahonda/pg10_pg_sequences
Support PostgreSQL 10 `pg_sequence`
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb10
1 files changed, 9 insertions, 1 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 55566f1e34..cb45d7ba6b 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -290,9 +290,17 @@ module ActiveRecord
if pk && sequence
quoted_sequence = quote_table_name(sequence)
+ max_pk = select_value("select MAX(#{quote_column_name pk}) from #{quote_table_name(table)}")
+ if max_pk.nil?
+ if postgresql_version >= 100000
+ minvalue = select_value("SELECT seqmin from pg_sequence where seqrelid = '#{quoted_sequence}'::regclass")
+ else
+ minvalue = select_value("SELECT min_value FROM #{quoted_sequence}")
+ end
+ end
select_value(<<-end_sql, "SCHEMA")
- SELECT setval('#{quoted_sequence}', (SELECT COALESCE(MAX(#{quote_column_name pk})+(SELECT increment_by FROM #{quoted_sequence}), (SELECT min_value FROM #{quoted_sequence})) FROM #{quote_table_name(table)}), false)
+ SELECT setval('#{quoted_sequence}', #{max_pk ? max_pk : minvalue}, #{max_pk ? true : false})
end_sql
end
end