The code I used to connect Rails 3 to PostgreSQL 10

I had to connect to PostgreSQL 10 and test it with Rails 3.0.1, so I decided to add an initializer.

config/initializers/backport_pg_10_support_to_rails_3.rb

if Rails.env.test?
  module ActiveRecord
    class Base
      class << self
        def establish_connection(*arg)
        end
      end
    end
  end
  require 'active_record/connection_adapters/postgresql_adapter'
  module ActiveRecord
    module ConnectionAdapters
      class PostgreSQLAdapter < AbstractAdapter
        # Resets the sequence of a table's primary key to the maximum value.
        def reset_pk_sequence!(table, pk = nil, sequence = nil)
          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)
              if ActiveRecord::Base.connection.select_value('SELECT version()').include?('PostgreSQL 10')
                # language=sql
                sql =<<-EOS
                  SELECT setval('#{quoted_sequence}', (SELECT GREATEST(MAX(#{quote_column_name pk})+(SELECT seqincrement FROM pg_sequence WHERE seqrelid = '#{quoted_sequence}'::regclass), (SELECT seqmin FROM pg_sequence WHERE seqrelid = '#{quoted_sequence}'::regclass)) FROM #{quote_table_name(table)}), false)
                EOS
              else
                # language=sql
                sql =<<-EOS
                  SELECT setval('#{quoted_sequence}', (SELECT GREATEST(MAX(#{quote_column_name pk})+(SELECT increment_by FROM #{quoted_sequence}), (SELECT min_value FROM #{quoted_sequence})) FROM #{quote_table_name(table)}), false)
                EOS
              end
            else
              @logger.warn "#{table} has primary key #{pk} with no default sequence" if @logger
            end
          end
        end
      end
    end
  end
end

I put require'active_record / connection_adapters / postgresql_adapter' and overwrotedef reset_pk_sequence! (Table, pk = nil, sequence = nil), but that alone gives an error that ʻestablish_connection does not exist. , I wrote ʻestablish_connection before require.

reset_pk_sequence! is basically a copy from https://github.com/rails/rails/blob/3-0-stable/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb, and the change is the SQL creation part is.

Recommended Posts

The code I used to connect Rails 3 to PostgreSQL 10
I tried to summarize the methods used
[Rails] I tried to raise the Rails version from 5.0 to 5.2
I tried to organize the session in Rails
[Java / PostgreSQL] Connect the WEB application to the database
A review of the code used by rails beginners
I tried to organize the cases used in programming
[Rails] I don't know how to use the model ...
Connect from Java to PostgreSQL
I tried to introduce Bootstrap 4 to the Rails 6 app [for beginners]
[Rails] [bootstrap] I want to change the font size responsively
I want to create a form to select the [Rails] category
Post back the coupon code you used to the specified endpoint
Dynamically switch the database to connect to
I tried to explain the method
[Rails] I tried deleting the application
The story I wanted to unzip
Connect to Rails server with iPhone
Rails DB PostgreSQL changed to MySQL
[PostgreSQL] If you want to delete the Rails app, delete the database first!
I tried to sort the data in descending order, ascending order / Rails
I tried to implement the image preview function with Rails / jQuery
I tried to understand how the rails method "redirect_to" is defined
I tried to understand how the rails method "link_to" is defined
I used the Mediator pattern to express a river crossing puzzle.
[Rails] I learned about migration files! (Adding a column to the table)
I tried to introduce CircleCI 2.0 to Rails app
I tried migrating Processing to VS Code
[Rails] I was addicted to the nginx settings when using Action Cable.
Try using the Rails API (zip code)
[Rails] How to use the map method
[Ruby] I tried to diet the if statement code with the ternary operator
I wanted to add @VisibleForTesting to the method
I used Docker to solidify the template to be developed with spring boot.
I was addicted to the roll method
I tried to implement the Iterator pattern
[CircleCI] I was addicted to the automatic test of CircleCI (rails + mysql) [Memo]
I was addicted to the Spring-Batch test
I tried to summarize the Stream API
I went to the Java Women's Club # 1
The process of introducing Vuetify to Rails
I want to introduce the committee with Rails without getting too dirty
[Rails] How to apply the CSS used in the main app with Administrate
Why was the code painful to read?
[Rails6] How to connect the posting function generated by Scaffold with the user function generated by devise
03. I sent a request from Spring Boot to the zip code search API
[Rails / ActiveRecord] I want to validate the value before the type is converted (_before_type_cast)
I used it without knowing the O / R mapping of rails, so I checked it.
I was addicted to not being able to connect to AWS-S3 from the Docker container
[Rails] How to operate the helper method used in the main application with Administrate
[Rails] How to decide the destination by "rails routes"
I rewrote the Rails tutorial test with RSpec
I want to output the day of the week
[Rails] Button to return to the top of the page
[Rails] When I use form_with, the screen freezes! ??
A memorandum to clean up the code Ruby
The road to Japaneseizing Rails devise error messages
[Rails] rails new to create a database with PostgreSQL
I want to var_dump the contents of the intent
[Ruby] Code to display the day of the week
[API] I tried using the zip code search API