Created
February 21, 2019 04:06
-
-
Save halostatue/e0709eae5bf902de464cfe9e9e0cdd5a to your computer and use it in GitHub Desktop.
Backport PostgreSQL 10 support to Rails 4.2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_record/connection_adapters/postgresql/schema_statements' | |
# | |
# Monkey-patch the refused Rails 4.2 patch at https://github.com/rails/rails/pull/31330 | |
# | |
# Updates sequence logic to support PostgreSQL 10. | |
# | |
# Monkey patch developed by @replaid: https://github.com/rails/rails/issues/28780#issuecomment-354868174 | |
# put into config/initializers or some other early-loading code. | |
# config/initializers/backport_pg_10_support_to_rails_4.rb | |
module ActiveRecord | |
module ConnectionAdapters | |
module PostgreSQL | |
module SchemaStatements | |
# 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 | |
default_pk, default_sequence = pk_and_sequence_for(table) | |
pk ||= default_pk | |
sequence ||= default_sequence | |
end | |
if @logger && pk && !sequence | |
@logger.warn "#{table} has primary key #{pk} with no default sequence" | |
end | |
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 = #{quote(quoted_sequence)}::regclass") | |
else | |
minvalue = select_value("SELECT min_value FROM #{quoted_sequence}") | |
end | |
end | |
select_value <<-end_sql, 'SCHEMA' | |
SELECT setval(#{quote(quoted_sequence)}, #{max_pk ? max_pk : minvalue}, #{max_pk ? true : false}) | |
end_sql | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment