Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres 10 and Rails 3.2 - patch to fix bug in seed-fu 2.3.5 #126

Open
stephendrobinson opened this issue Mar 8, 2018 · 0 comments
Open

Comments

@stephendrobinson
Copy link

stephendrobinson commented Mar 8, 2018

Patch to get seed-fu version 2.3.5 working with postgres10

For those with a legacy Rails 3.2 app needing to upgrade to Postgres 10 (e.g. running on heroku). This patch will enable you to use the 2.3.5 version of seed-fu (that works with Rails 3.2) against your postgres 10 database.

Add the patch file here ..

Add the following file at config/initializers/seed_fu_patch_for_v2_3_5_to_work_with_postgres_10.rb

SeedFu::Seeder.class_eval do

      def update_id_sequence
        if @model_class.connection.adapter_name == "PostgreSQL" or @model_class.connection.adapter_name == "PostGIS"
          return if @model_class.primary_key.nil? || @model_class.sequence_name.nil?

          quoted_id       = @model_class.connection.quote_column_name(@model_class.primary_key)
          sequence = @model_class.sequence_name

          if ActiveRecord::Base.connection.select_value('SELECT version()').include?('PostgreSQL 10')
            sql =<<-EOS
              SELECT setval('#{sequence}', (SELECT GREATEST(MAX(#{quoted_id})+(SELECT seqincrement FROM pg_sequence WHERE seqrelid = '#{sequence}'::regclass), (SELECT seqmin FROM pg_sequence WHERE seqrelid = '#{sequence}'::regclass)) FROM #{@model_class.quoted_table_name}), false)
            EOS
          else
            sql =<<-EOS
              SELECT setval('#{sequence}', (SELECT GREATEST(MAX(#{quoted_id})+(SELECT increment_by FROM #{sequence}), (SELECT min_value FROM #{sequence})) FROM #{@model_class.quoted_table_name}), false)
            EOS
          end

          @model_class.connection.execute sql
        end
      end

end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant