Sha256: 6198e126177e9da9a0b8e1b1a2ee110f3a9d9579ec062d6a2c36967b32245e9d

Contents?: true

Size: 800 Bytes

Versions: 2

Compression:

Stored size: 800 Bytes

Contents

require "activerecord-reset-pk-sequence/version"

module ActiveRecord
  class Base
    def self.reset_pk_sequence
      case ActiveRecord::Base.connection.adapter_name
      when 'SQLite'
        new_max = maximum(primary_key) || 0
        update_seq_sql = "UPDATE sqlite_sequence SET seq = #{new_max} WHERE name = '#{table_name}';"
        ActiveRecord::Base.connection.execute(update_seq_sql)
      when 'Mysql'
        new_max = maximum(primary_key) + 1 || 1
        update_seq_sql = "ALTER TABLE '#{table_name}' AUTO_INCREMENT = #{new_max};"
        ActiveRecord::Base.connection.execute(update_seq_sql)
      when 'PostgreSQL'
        ActiveRecord::Base.connection.reset_pk_sequence!(table_name)
      else 
        raise "Task not implemented for this DB adapter"
      end 
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord-reset-pk-sequence-0.2.1 lib/activerecord-reset-pk-sequence.rb
activerecord-reset-pk-sequence-0.2.0 lib/activerecord-reset-pk-sequence.rb