ar-sequence 0.1.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/ar-sequence.gemspec CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "./lib/ar/sequence/version"
2
4
 
3
5
  Gem::Specification.new do |spec|
@@ -5,8 +7,10 @@ Gem::Specification.new do |spec|
5
7
  spec.version = AR::Sequence::VERSION
6
8
  spec.authors = ["Nando Vieira"]
7
9
  spec.email = ["fnando.vieira@gmail.com"]
8
-
9
- spec.summary = "Add support for PostgreSQL's SEQUENCE on ActiveRecord migrations"
10
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
11
+ spec.metadata = {"rubygems_mfa_required" => "true"}
12
+ spec.summary = "Add support for PostgreSQL's SEQUENCE on " \
13
+ "ActiveRecord migrations"
10
14
  spec.description = spec.summary
11
15
  spec.homepage = "https://rubygems.org/gems/ar-sequence"
12
16
  spec.license = "MIT"
@@ -21,10 +25,11 @@ Gem::Specification.new do |spec|
21
25
  spec.add_dependency "activerecord"
22
26
 
23
27
  spec.add_development_dependency "bundler"
24
- spec.add_development_dependency "rake"
25
28
  spec.add_development_dependency "minitest-utils"
26
29
  spec.add_development_dependency "pg"
27
30
  spec.add_development_dependency "pry-meta"
28
- spec.add_development_dependency "codeclimate-test-reporter"
31
+ spec.add_development_dependency "rake"
32
+ spec.add_development_dependency "rubocop"
33
+ spec.add_development_dependency "rubocop-fnando"
29
34
  spec.add_development_dependency "simplecov"
30
35
  end
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ar-sequence"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ begin
11
+ require "pry"
12
+ Pry.start
13
+ rescue LoadError
14
+ require "irb"
15
+ IRB.start(__FILE__)
16
+ end
data/bin/setup ADDED
@@ -0,0 +1,10 @@
1
+ #!/usr/bin/env bash
2
+
3
+ set -euo pipefail
4
+
5
+ IFS=$'\n\t'
6
+ set -vx
7
+
8
+ bundle install
9
+
10
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec path: ".."
5
+
6
+ gem "activerecord", "~> 6.0.0"
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec path: ".."
5
+
6
+ gem "activerecord", "~> 6.1.0"
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+ gemspec path: ".."
5
+
6
+ gem "activerecord", "~> 7.0.0"
@@ -1,17 +1,31 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AR
2
4
  module Sequence
3
5
  module Adapter
6
+ SEQUENCE_COMMENT = "created by ar-sequence"
7
+
8
+ def custom_sequence?(sequence_name)
9
+ execute(
10
+ "SELECT obj_description('#{sequence_name}'::regclass, 'pg_class');"
11
+ ).first["obj_description"] == SEQUENCE_COMMENT
12
+ end
13
+
4
14
  def check_sequences
5
- select_all("SELECT * FROM information_schema.sequences").to_a
15
+ select_all(
16
+ "SELECT * FROM information_schema.sequences ORDER BY sequence_name"
17
+ ).to_a
6
18
  end
7
19
 
8
20
  def create_sequence(name, options = {})
9
21
  increment = options[:increment] || options[:step]
10
- name = quote_column_name(name)
22
+ name = quote_name(name)
11
23
 
12
24
  sql = ["CREATE SEQUENCE IF NOT EXISTS #{name}"]
13
25
  sql << "INCREMENT BY #{increment}" if increment
14
26
  sql << "START WITH #{options[:start]}" if options[:start]
27
+ sql << ";"
28
+ sql << "COMMENT ON SEQUENCE #{name} IS '#{SEQUENCE_COMMENT}';"
15
29
 
16
30
  execute(sql.join("\n"))
17
31
  end
@@ -21,10 +35,14 @@ module AR
21
35
  # drop_sequence :user_position
22
36
  #
23
37
  def drop_sequence(name)
24
- name = quote_column_name(name)
38
+ name = quote_name(name)
25
39
  sql = "DROP SEQUENCE #{name}"
26
40
  execute(sql)
27
41
  end
42
+
43
+ def quote_name(name)
44
+ name.split(".", 2).map {|part| quote_column_name(part) }.join(".")
45
+ end
28
46
  end
29
47
  end
30
48
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AR
2
4
  module Sequence
3
5
  module CommandRecorder
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AR
2
4
  module Sequence
3
5
  module ModelMethods
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AR
2
4
  module Sequence
3
5
  module SchemaDumper
@@ -6,20 +8,54 @@ module AR
6
8
  sequences(stream)
7
9
  end
8
10
 
11
+ def retrieve_search_path
12
+ user = @connection.select_one("select user").values.first
13
+
14
+ @connection
15
+ .select_one("show search_path")
16
+ .values
17
+ .first
18
+ .split(", ")
19
+ .map {|path| path == '"$user"' ? user : path }
20
+ end
21
+
9
22
  def sequences(stream)
10
23
  sequences = @connection.check_sequences
24
+ search_path = retrieve_search_path
25
+
11
26
  return if sequences.empty?
12
27
 
13
28
  sequences.each do |seq|
29
+ schema = seq["sequence_schema"]
30
+
31
+ sequence_full_name = [
32
+ search_path.include?(schema) ? nil : schema,
33
+ seq["sequence_name"]
34
+ ].compact.join(".")
35
+
36
+ next unless @connection.custom_sequence?(sequence_full_name)
37
+
14
38
  start_value = seq["start_value"]
15
39
  increment = seq["increment"]
16
40
 
17
41
  options = []
18
- options << "start: #{start_value}" if start_value && Integer(start_value) != 1
19
- options << "increment: #{increment}" if increment && Integer(increment) != 1
20
42
 
21
- statement = ["create_sequence", seq["sequence_name"].inspect].join(" ")
22
- statement << (options.any? ? ", " << options.join(", ") : "") if options.any?
43
+ if start_value && Integer(start_value) != 1
44
+ options << "start: #{start_value}"
45
+ end
46
+
47
+ if increment && Integer(increment) != 1
48
+ options << "increment: #{increment}"
49
+ end
50
+
51
+ statement = [
52
+ "create_sequence",
53
+ sequence_full_name.inspect
54
+ ].join(" ")
55
+
56
+ if options.any?
57
+ statement << (options.any? ? ", #{options.join(', ')}" : "")
58
+ end
23
59
 
24
60
  stream.puts " #{statement}"
25
61
  end
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module AR
2
4
  module Sequence
3
- VERSION = "0.1.0"
5
+ VERSION = "0.2.1"
4
6
  end
5
7
  end
data/lib/ar/sequence.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "active_support/all"
2
4
  require "active_record"
3
5
  require "active_record/connection_adapters/postgresql_adapter"
@@ -15,7 +17,9 @@ module AR
15
17
  end
16
18
  end
17
19
 
18
- ActiveRecord::Migration::CommandRecorder.include AR::Sequence::CommandRecorder
19
- ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include AR::Sequence::Adapter
20
- ActiveRecord::SchemaDumper.prepend AR::Sequence::SchemaDumper
21
- ActiveRecord::Base.extend AR::Sequence::ModelMethods
20
+ ActiveRecord::Migration::CommandRecorder.include(AR::Sequence::CommandRecorder)
21
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(
22
+ AR::Sequence::Adapter
23
+ )
24
+ ActiveRecord::SchemaDumper.prepend(AR::Sequence::SchemaDumper)
25
+ ActiveRecord::Base.extend(AR::Sequence::ModelMethods)
data/lib/ar-sequence.rb CHANGED
@@ -1 +1,3 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require "ar/sequence"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar-sequence
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nando Vieira
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-02 00:00:00.000000000 Z
11
+ date: 2021-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: rake
42
+ name: minitest-utils
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: minitest-utils
56
+ name: pg
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: pg
70
+ name: pry-meta
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -81,7 +81,21 @@ dependencies:
81
81
  - !ruby/object:Gem::Version
82
82
  version: '0'
83
83
  - !ruby/object:Gem::Dependency
84
- name: pry-meta
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop
85
99
  requirement: !ruby/object:Gem::Requirement
86
100
  requirements:
87
101
  - - ">="
@@ -95,7 +109,7 @@ dependencies:
95
109
  - !ruby/object:Gem::Version
96
110
  version: '0'
97
111
  - !ruby/object:Gem::Dependency
98
- name: codeclimate-test-reporter
112
+ name: rubocop-fnando
99
113
  requirement: !ruby/object:Gem::Requirement
100
114
  requirements:
101
115
  - - ">="
@@ -129,21 +143,28 @@ executables: []
129
143
  extensions: []
130
144
  extra_rdoc_files: []
131
145
  files:
146
+ - ".github/FUNDING.yml"
147
+ - ".github/ISSUE_TEMPLATE/bug_report.md"
148
+ - ".github/ISSUE_TEMPLATE/feature_request.md"
149
+ - ".github/PULL_REQUEST_TEMPLATE.md"
150
+ - ".github/dependabot.yml"
151
+ - ".github/workflows/tests.yml"
132
152
  - ".gitignore"
153
+ - ".rubocop.yml"
133
154
  - ".travis.yml"
134
155
  - CHANGELOG.md
135
156
  - CODE_OF_CONDUCT.md
157
+ - CONTRIBUTING.md
136
158
  - Gemfile
137
- - LICENSE.txt
159
+ - LICENSE.md
138
160
  - README.md
139
161
  - Rakefile
140
162
  - ar-sequence.gemspec
141
- - gemfiles/activerecord_5_0.Gemfile.lock
142
- - gemfiles/activerecord_5_0.gemfile
143
- - gemfiles/activerecord_5_1.Gemfile.lock
144
- - gemfiles/activerecord_5_1.gemfile
145
- - gemfiles/activerecord_5_2.gemfile
146
- - gemfiles/activerecord_5_2.gemfile.lock
163
+ - bin/console
164
+ - bin/setup
165
+ - gemfiles/6_0.gemfile
166
+ - gemfiles/6_1.gemfile
167
+ - gemfiles/7_0.gemfile
147
168
  - lib/ar-sequence.rb
148
169
  - lib/ar/sequence.rb
149
170
  - lib/ar/sequence/adapter.rb
@@ -154,8 +175,9 @@ files:
154
175
  homepage: https://rubygems.org/gems/ar-sequence
155
176
  licenses:
156
177
  - MIT
157
- metadata: {}
158
- post_install_message:
178
+ metadata:
179
+ rubygems_mfa_required: 'true'
180
+ post_install_message:
159
181
  rdoc_options: []
160
182
  require_paths:
161
183
  - lib
@@ -163,16 +185,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
163
185
  requirements:
164
186
  - - ">="
165
187
  - !ruby/object:Gem::Version
166
- version: '0'
188
+ version: 2.7.0
167
189
  required_rubygems_version: !ruby/object:Gem::Requirement
168
190
  requirements:
169
191
  - - ">="
170
192
  - !ruby/object:Gem::Version
171
193
  version: '0'
172
194
  requirements: []
173
- rubyforge_project:
174
- rubygems_version: 2.7.6
175
- signing_key:
195
+ rubygems_version: 3.2.32
196
+ signing_key:
176
197
  specification_version: 4
177
198
  summary: Add support for PostgreSQL's SEQUENCE on ActiveRecord migrations
178
199
  test_files: []
data/LICENSE.txt DELETED
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2015 Nando Vieira
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in
13
- all copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
- THE SOFTWARE.
@@ -1,77 +0,0 @@
1
- PATH
2
- remote: ..
3
- specs:
4
- ar-sequence (0.0.0)
5
- activerecord
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activemodel (5.0.7)
11
- activesupport (= 5.0.7)
12
- activerecord (5.0.7)
13
- activemodel (= 5.0.7)
14
- activesupport (= 5.0.7)
15
- arel (~> 7.0)
16
- activesupport (5.0.7)
17
- concurrent-ruby (~> 1.0, >= 1.0.2)
18
- i18n (>= 0.7, < 2)
19
- minitest (~> 5.1)
20
- tzinfo (~> 1.1)
21
- arel (7.1.4)
22
- awesome_print (1.8.0)
23
- byebug (10.0.2)
24
- codeclimate-test-reporter (1.0.8)
25
- simplecov (<= 0.13)
26
- coderay (1.1.2)
27
- concurrent-ruby (1.0.5)
28
- docile (1.1.5)
29
- i18n (1.1.0)
30
- concurrent-ruby (~> 1.0)
31
- json (2.1.0)
32
- method_source (0.9.0)
33
- minitest (5.11.3)
34
- minitest-utils (0.4.1)
35
- minitest
36
- pg (1.1.3)
37
- pry (0.11.3)
38
- coderay (~> 1.1.0)
39
- method_source (~> 0.9.0)
40
- pry-byebug (3.6.0)
41
- byebug (~> 10.0)
42
- pry (~> 0.10)
43
- pry-meta (0.0.10)
44
- awesome_print
45
- pry
46
- pry-byebug
47
- pry-remote
48
- pry-remote (0.1.8)
49
- pry (~> 0.9)
50
- slop (~> 3.0)
51
- rake (12.3.1)
52
- simplecov (0.13.0)
53
- docile (~> 1.1.0)
54
- json (>= 1.8, < 3)
55
- simplecov-html (~> 0.10.0)
56
- simplecov-html (0.10.2)
57
- slop (3.6.0)
58
- thread_safe (0.3.6)
59
- tzinfo (1.2.5)
60
- thread_safe (~> 0.1)
61
-
62
- PLATFORMS
63
- ruby
64
-
65
- DEPENDENCIES
66
- activerecord (~> 5.0.0)
67
- ar-sequence!
68
- bundler
69
- codeclimate-test-reporter
70
- minitest-utils
71
- pg
72
- pry-meta
73
- rake
74
- simplecov
75
-
76
- BUNDLED WITH
77
- 1.16.4
@@ -1,4 +0,0 @@
1
- source "https://rubygems.org"
2
- gemspec path: ".."
3
-
4
- gem "activerecord", "~> 5.0.0"
@@ -1,77 +0,0 @@
1
- PATH
2
- remote: ..
3
- specs:
4
- ar-sequence (0.0.0)
5
- activerecord
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activemodel (5.1.6)
11
- activesupport (= 5.1.6)
12
- activerecord (5.1.6)
13
- activemodel (= 5.1.6)
14
- activesupport (= 5.1.6)
15
- arel (~> 8.0)
16
- activesupport (5.1.6)
17
- concurrent-ruby (~> 1.0, >= 1.0.2)
18
- i18n (>= 0.7, < 2)
19
- minitest (~> 5.1)
20
- tzinfo (~> 1.1)
21
- arel (8.0.0)
22
- awesome_print (1.8.0)
23
- byebug (10.0.2)
24
- codeclimate-test-reporter (1.0.8)
25
- simplecov (<= 0.13)
26
- coderay (1.1.2)
27
- concurrent-ruby (1.0.5)
28
- docile (1.1.5)
29
- i18n (1.1.0)
30
- concurrent-ruby (~> 1.0)
31
- json (2.1.0)
32
- method_source (0.9.0)
33
- minitest (5.11.3)
34
- minitest-utils (0.4.1)
35
- minitest
36
- pg (1.1.3)
37
- pry (0.11.3)
38
- coderay (~> 1.1.0)
39
- method_source (~> 0.9.0)
40
- pry-byebug (3.6.0)
41
- byebug (~> 10.0)
42
- pry (~> 0.10)
43
- pry-meta (0.0.10)
44
- awesome_print
45
- pry
46
- pry-byebug
47
- pry-remote
48
- pry-remote (0.1.8)
49
- pry (~> 0.9)
50
- slop (~> 3.0)
51
- rake (12.3.1)
52
- simplecov (0.13.0)
53
- docile (~> 1.1.0)
54
- json (>= 1.8, < 3)
55
- simplecov-html (~> 0.10.0)
56
- simplecov-html (0.10.2)
57
- slop (3.6.0)
58
- thread_safe (0.3.6)
59
- tzinfo (1.2.5)
60
- thread_safe (~> 0.1)
61
-
62
- PLATFORMS
63
- ruby
64
-
65
- DEPENDENCIES
66
- activerecord (~> 5.1.0)
67
- ar-sequence!
68
- bundler
69
- codeclimate-test-reporter
70
- minitest-utils
71
- pg
72
- pry-meta
73
- rake
74
- simplecov
75
-
76
- BUNDLED WITH
77
- 1.16.4
@@ -1,4 +0,0 @@
1
- source "https://rubygems.org"
2
- gemspec path: ".."
3
-
4
- gem "activerecord", "~> 5.1.0"
@@ -1,4 +0,0 @@
1
- source "https://rubygems.org"
2
- gemspec path: ".."
3
-
4
- gem "activerecord", "~> 5.2.0"
@@ -1,77 +0,0 @@
1
- PATH
2
- remote: ..
3
- specs:
4
- ar-sequence (0.0.0)
5
- activerecord
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activemodel (5.2.1)
11
- activesupport (= 5.2.1)
12
- activerecord (5.2.1)
13
- activemodel (= 5.2.1)
14
- activesupport (= 5.2.1)
15
- arel (>= 9.0)
16
- activesupport (5.2.1)
17
- concurrent-ruby (~> 1.0, >= 1.0.2)
18
- i18n (>= 0.7, < 2)
19
- minitest (~> 5.1)
20
- tzinfo (~> 1.1)
21
- arel (9.0.0)
22
- awesome_print (1.8.0)
23
- byebug (10.0.2)
24
- codeclimate-test-reporter (1.0.8)
25
- simplecov (<= 0.13)
26
- coderay (1.1.2)
27
- concurrent-ruby (1.0.5)
28
- docile (1.1.5)
29
- i18n (1.1.0)
30
- concurrent-ruby (~> 1.0)
31
- json (2.1.0)
32
- method_source (0.9.0)
33
- minitest (5.11.3)
34
- minitest-utils (0.4.1)
35
- minitest
36
- pg (1.1.3)
37
- pry (0.11.3)
38
- coderay (~> 1.1.0)
39
- method_source (~> 0.9.0)
40
- pry-byebug (3.6.0)
41
- byebug (~> 10.0)
42
- pry (~> 0.10)
43
- pry-meta (0.0.10)
44
- awesome_print
45
- pry
46
- pry-byebug
47
- pry-remote
48
- pry-remote (0.1.8)
49
- pry (~> 0.9)
50
- slop (~> 3.0)
51
- rake (12.3.1)
52
- simplecov (0.13.0)
53
- docile (~> 1.1.0)
54
- json (>= 1.8, < 3)
55
- simplecov-html (~> 0.10.0)
56
- simplecov-html (0.10.2)
57
- slop (3.6.0)
58
- thread_safe (0.3.6)
59
- tzinfo (1.2.5)
60
- thread_safe (~> 0.1)
61
-
62
- PLATFORMS
63
- ruby
64
-
65
- DEPENDENCIES
66
- activerecord (~> 5.2.0)
67
- ar-sequence!
68
- bundler
69
- codeclimate-test-reporter
70
- minitest-utils
71
- pg
72
- pry-meta
73
- rake
74
- simplecov
75
-
76
- BUNDLED WITH
77
- 1.16.4