activerecord-reset-pk-sequence 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
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
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord-reset-pk-sequence.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Splendeo Innovación
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,67 @@
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 and Sqlite but not in 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
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,17 @@
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
+ end
@@ -0,0 +1,18 @@
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 'PostgreSQL'
12
+ ActiveRecord::Base.connection.reset_pk_sequence!(table_name)
13
+ else
14
+ raise "Task not implemented for this DB adapter"
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module Activerecord
2
+ module Reset
3
+ module Pk
4
+ module Sequence
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,54 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-reset-pk-sequence
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Spendeo Innovación
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-03-27 00:00:00.000000000Z
13
+ dependencies: []
14
+ description: Id of an AR table cleaner. Works for Postgres and Sqlite.
15
+ email:
16
+ - support@splendeo.es
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE
24
+ - README.md
25
+ - Rakefile
26
+ - activerecord-reset-pk-sequence.gemspec
27
+ - lib/activerecord-reset-pk-sequence.rb
28
+ - lib/activerecord-reset-pk-sequence/version.rb
29
+ homepage: https://github.com/splendeo/activerecord-reset-pk-sequence
30
+ licenses: []
31
+ post_install_message:
32
+ rdoc_options: []
33
+ require_paths:
34
+ - lib
35
+ required_ruby_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubyforge_project:
49
+ rubygems_version: 1.8.6
50
+ signing_key:
51
+ specification_version: 3
52
+ summary: Allows resetting the id of an AR table to 0. Useful after a delete_all. Works
53
+ in Postgres and Sqlite (not MySQL) for now.
54
+ test_files: []