├── .gitignore ├── Gemfile ├── LICENSE ├── README.md ├── Rakefile ├── activerecord-reset-pk-sequence.gemspec └── lib ├── activerecord-reset-pk-sequence.rb └── activerecord-reset-pk-sequence └── version.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in activerecord-reset-pk-sequence.gemspec 4 | gemspec 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012 Splendeo Innovación 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # active-reset-pk-sequence 2 | 3 | This gem allows resetting the id of an AR table to 0. It is useful after a delete_all command. It works in Postgres, Sqlite and MySQL up to now. 4 | 5 | ## Installation 6 | 7 | Add this line to your application's Gemfile: 8 | 9 | gem 'activerecord-reset-pk-sequence' 10 | 11 | And then execute: 12 | 13 | $ bundle 14 | 15 | Or install it yourself as: 16 | 17 | $ gem install activerecord-reset-pk-sequence 18 | 19 | ## Usage 20 | 21 | This gem adds a method to the class ActiveRecord::Base. Now it is shown an example of how it works. 22 | 23 | The first thing is to install the gem as it is explained before. 24 | 25 | After that you need a model. If you don't have one you can create it directly with rails as shown. 26 | 27 | rails g model Person name:string 28 | 29 | Then you do a database migration with: 30 | 31 | rake db:migrate 32 | 33 | To test the gem you have to start a rails console: 34 | 35 | rails c 36 | 37 | And then write the following commands: 38 | 39 | p = Person.create(:name => 'David') 40 | p = Person.create(:name => 'James') 41 | 42 | You will see that the have the id's 1 and 2, then delete all elements in the table 43 | 44 | Person.delete_all 45 | 46 | If you create a new person now you will notice that the id is going to be 3 47 | 48 | p = Person.create(:name => 'Peter') 49 | id = p.id 50 | => 3 51 | 52 | 53 | And this is what this gem is for, to reset the id's after deleting the elements in a table, to check if it works delete all elements and then run reset_pk_sequence method, after that create a new "Person" and check if its id is 1. 54 | 55 | Person.delete_all 56 | Person.reset_pk_sequence 57 | p = Person.create(:name => 'Jhon') 58 | 59 | So to sum up to use this gem you only have to add the call to the new method after deleting all the elements in a table. 60 | 61 | ## Contributing 62 | 63 | 1. Fork it 64 | 2. Create your feature branch (`git checkout -b my-new-feature`) 65 | 3. Commit your changes (`git commit -am 'Added some feature'`) 66 | 4. Push to the branch (`git push origin my-new-feature`) 67 | 5. Create new Pull Request 68 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env rake 2 | require "bundler/gem_tasks" 3 | -------------------------------------------------------------------------------- /activerecord-reset-pk-sequence.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | require File.expand_path('../lib/activerecord-reset-pk-sequence/version', __FILE__) 3 | 4 | Gem::Specification.new do |gem| 5 | gem.authors = ["Spendeo Innovación"] 6 | gem.email = ["support@splendeo.es"] 7 | gem.description = %q{Id of an AR table cleaner. Works for Postgres and Sqlite.} 8 | gem.summary = %q{Allows resetting the id of an AR table to 0. Useful after a delete_all. Works in Postgres and Sqlite (not MySQL) for now.} 9 | gem.homepage = "https://github.com/splendeo/activerecord-reset-pk-sequence" 10 | 11 | gem.files = `git ls-files`.split($\) 12 | gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } 13 | gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) 14 | gem.name = "activerecord-reset-pk-sequence" 15 | gem.require_paths = ["lib"] 16 | gem.version = Activerecord::Reset::Pk::Sequence::VERSION 17 | 18 | gem.add_development_dependency "rake" 19 | end 20 | -------------------------------------------------------------------------------- /lib/activerecord-reset-pk-sequence.rb: -------------------------------------------------------------------------------- 1 | require "activerecord-reset-pk-sequence/version" 2 | 3 | module ActiveRecord 4 | class Base 5 | def self.reset_pk_sequence 6 | case ActiveRecord::Base.connection.adapter_name 7 | when 'SQLite' 8 | new_max = maximum(primary_key) || 0 9 | update_seq_sql = "UPDATE sqlite_sequence SET seq = #{new_max} WHERE name = '#{table_name}';" 10 | ActiveRecord::Base.connection.execute(update_seq_sql) 11 | when 'Mysql' 12 | new_max = maximum(primary_key) + 1 || 1 13 | update_seq_sql = "ALTER TABLE '#{table_name}' AUTO_INCREMENT = #{new_max};" 14 | ActiveRecord::Base.connection.execute(update_seq_sql) 15 | when 'PostgreSQL' 16 | ActiveRecord::Base.connection.reset_pk_sequence!(table_name) 17 | else 18 | raise "Task not implemented for this DB adapter" 19 | end 20 | end 21 | end 22 | end 23 | 24 | -------------------------------------------------------------------------------- /lib/activerecord-reset-pk-sequence/version.rb: -------------------------------------------------------------------------------- 1 | module Activerecord 2 | module Reset 3 | module Pk 4 | module Sequence 5 | VERSION = "0.2.0" 6 | end 7 | end 8 | end 9 | end 10 | --------------------------------------------------------------------------------