activerecord_reset_pk_sequence 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 208d5c043228431532058e82e89fae1cca7c339b
4
+ data.tar.gz: 35d70b0c7ec5cb37e3f9badddeb3667d0612e31c
5
+ SHA512:
6
+ metadata.gz: 15f69560273380aa55b99fd16a2548d5e623d3173101d224fea4e917504836f4303f334a17244a3c621dd4d7cf28905f1b898eef41f1895943114e3a1adb2fb5
7
+ data.tar.gz: cb3bc78e32f684e2f5771c66c8a072c274043920572a027a9158f9c40d585ce6fc853bf52494f634765e0b9b0c1e015bf0f1c939b3b4d58bf4cbf53060ef5b69
data/.gitignore ADDED
@@ -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,20 @@
1
+ Copyright (c) 2016 Francisco Juan
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.
data/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # Active reset primary key 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
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
@@ -0,0 +1,20 @@
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 = ['Enrique Garcia Cota', 'Thomas Kienlen', 'Francisco Juan']
6
+ gem.email = 'francisco.juan@gmail.com'
7
+ gem.description = 'Id of an AR table cleaner. Works for Postgres, MySQL and Sqlite.'
8
+ gem.summary = '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/fjuan/activerecord_reset_pk_sequence'
10
+ gem.license = 'MIT'
11
+
12
+ gem.files = %x{git ls-files}.split($OUTPUT_RECORD_SEPARATOR)
13
+ gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
14
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
15
+ gem.name = 'activerecord_reset_pk_sequence'
16
+ gem.require_paths = ['lib']
17
+ gem.version = Activerecord::Reset::Pk::Sequence::VERSION
18
+
19
+ gem.add_development_dependency 'rake', '~> 0', '> 0'
20
+ end
@@ -0,0 +1,32 @@
1
+ require 'activerecord_reset_pk_sequence/version'
2
+
3
+ module ActiveRecord
4
+ class Base
5
+ class << self
6
+ def reset_pk_sequence
7
+ case ActiveRecord::Base.connection.adapter_name
8
+ when 'SQLite'
9
+ ActiveRecord::Base.connection.execute(sqlite_update_seq_sql)
10
+ when 'Mysql'
11
+ ActiveRecord::Base.connection.execute(mysql_update_seq_sql)
12
+ when 'PostgreSQL'
13
+ ActiveRecord::Base.connection.reset_pk_sequence!(table_name)
14
+ else
15
+ raise 'Task not implemented for this DB adapter'
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def sqlite_update_seq_sql
22
+ new_max_id = maximum(primary_key) || 0
23
+ "UPDATE sqlite_sequence SET seq = #{new_max_id} WHERE name = '#{table_name}';"
24
+ end
25
+
26
+ def mysql_update_seq_sql
27
+ new_max_id = maximum(primary_key) + 1 || 1
28
+ "ALTER TABLE '#{table_name}' AUTO_INCREMENT = #{new_max_id};"
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,9 @@
1
+ module Activerecord
2
+ module Reset
3
+ module Pk
4
+ module Sequence
5
+ VERSION = '0.3.0'.freeze
6
+ end
7
+ end
8
+ end
9
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_reset_pk_sequence
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ platform: ruby
6
+ authors:
7
+ - Enrique Garcia Cota
8
+ - Thomas Kienlen
9
+ - Francisco Juan
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2016-05-15 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rake
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ - - ">"
23
+ - !ruby/object:Gem::Version
24
+ version: '0'
25
+ type: :development
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ requirements:
29
+ - - "~>"
30
+ - !ruby/object:Gem::Version
31
+ version: '0'
32
+ - - ">"
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ description: Id of an AR table cleaner. Works for Postgres, MySQL and Sqlite.
36
+ email: francisco.juan@gmail.com
37
+ executables: []
38
+ extensions: []
39
+ extra_rdoc_files: []
40
+ files:
41
+ - ".gitignore"
42
+ - Gemfile
43
+ - LICENSE
44
+ - README.md
45
+ - Rakefile
46
+ - activerecord_reset_pk_sequence.gemspec
47
+ - lib/activerecord_reset_pk_sequence.rb
48
+ - lib/activerecord_reset_pk_sequence/version.rb
49
+ homepage: https://github.com/fjuan/activerecord_reset_pk_sequence
50
+ licenses:
51
+ - MIT
52
+ metadata: {}
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ required_rubygems_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ requirements: []
68
+ rubyforge_project:
69
+ rubygems_version: 2.6.3
70
+ signing_key:
71
+ specification_version: 4
72
+ summary: Allows resetting the id of an AR table to 0. Useful after a delete_all. Works
73
+ in Postgres and Sqlite (not MySQL) for now.
74
+ test_files: []