activerecord-pg-extensions 0.1.1

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
+ SHA256:
3
+ metadata.gz: 1c73159dc2062391844fc7309dd066720684b43a7d81c4c19c8bc5acaeacb36d
4
+ data.tar.gz: bff7754ec2e0a7817b7806f0e37c80ec91675a2e745841d60421da9192438840
5
+ SHA512:
6
+ metadata.gz: a8ed2044c44c79a5d6f0412ebb95de80e9a43361d22866950605a14c2a59bcf9566a6cd73febe06743d5e4f8c445a76b3c25c9ed7395733ba9159a386e1bbd2b
7
+ data.tar.gz: 3c2b19dccfa6b809f5e9de4a0c94c5bb26000537175957a2782bf8e94daf7f568dd311c45c36261f843128b53f8f9ff038c59a9141a952104316c7cb5b91f4d2
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.1] - 2021-06-07
4
+
5
+ - Add PostgreSQLAdapter#defer_constraints
6
+
7
+ ## [0.1.0] - 2021-06-02
8
+
9
+ - Initial release
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Instructure, Inc.
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.
data/README.md ADDED
@@ -0,0 +1,34 @@
1
+ # ActiveRecord PG Extensions
2
+
3
+ This gem includes a number of extensions to Rails' regular PostgreSQLAdapter to enable access to
4
+ more Postgres specific features.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'activerecord-pg-extensions'
12
+ ```
13
+ ## Usage
14
+
15
+ See individual classes for available methods.
16
+
17
+ ## Development
18
+
19
+ You will need Postgres locally to test. As long as `psql` can connect with no arguments, you're fine. Otherwise use environment variables
20
+ as described at https://www.postgresql.org/docs/current/libpq-envars.html to configure it. The database will be created automatically.
21
+
22
+ `rake` will run both tests and Rubocop.
23
+
24
+ After checking out the repo, run `bundle install` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
25
+
26
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
27
+
28
+ ## Contributing
29
+
30
+ Bug reports and pull requests are welcome on GitHub at https://github.com/instructure/activerecord-pg-extensions.
31
+
32
+ ## License
33
+
34
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: postgresql
3
+ database: arpgextensions
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record/pg_extensions/version"
4
+
5
+ require "active_record/pg_extensions/railtie"
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module PGExtensions
5
+ # Contains general additions to the PostgreSQLAdapter
6
+ module PostgreSQLAdapter
7
+ # set constraint check timing for the current transaction
8
+ # see https://www.postgresql.org/docs/current/sql-set-constraints.html
9
+ def set_constraints(deferred, *constraints)
10
+ raise ArgumentError, "deferred must be :deferred or :immediate" unless %w[deferred
11
+ immediate].include?(deferred.to_s)
12
+
13
+ constraints = constraints.map { |c| quote_table_name(c) }.join(", ")
14
+ constraints = "ALL" if constraints.empty?
15
+ execute("SET CONSTRAINTS #{constraints} #{deferred.to_s.upcase}")
16
+ end
17
+
18
+ # defers constraints, yields to the caller, and then resets back to immediate
19
+ # note that the reset back to immediate is _not_ in an ensure block, since any
20
+ # error thrown would likely mean the transaction is rolled back, and setting
21
+ # constraint checking back to immediate would also fail
22
+ def defer_constraints(*constraints)
23
+ set_constraints(:deferred, *constraints)
24
+ yield
25
+ set_constraints(:immediate, *constraints)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/railtie"
4
+
5
+ module ActiveRecord
6
+ module PGExtensions
7
+ # :nodoc:
8
+ class Railtie < Rails::Railtie
9
+ initializer "pg_extensions.extend_ar", after: "active_record.initialize_database" do
10
+ ActiveSupport.on_load(:active_record) do
11
+ require "active_record/pg_extensions/postgresql_adapter"
12
+
13
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.include(PostgreSQLAdapter)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveRecord
4
+ module PGExtensions
5
+ VERSION = "0.1.1"
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_record/pg_extensions"
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe ActiveRecord::ConnectionAdapters::PostgreSQLAdapter do
4
+ describe "#set_constraints" do
5
+ around do |example|
6
+ connection.dont_execute(&example)
7
+ end
8
+
9
+ it "requires :deferred or :immediate" do
10
+ expect { connection.set_constraints("garbage") }.to raise_error(ArgumentError)
11
+ end
12
+
13
+ it "defaults to all" do
14
+ connection.set_constraints(:deferred)
15
+ expect(connection.executed_statements).to eq ["SET CONSTRAINTS ALL DEFERRED"]
16
+ end
17
+
18
+ it "quotes constraints" do
19
+ connection.set_constraints(:deferred, :my_fk)
20
+ expect(connection.executed_statements).to eq ['SET CONSTRAINTS "my_fk" DEFERRED']
21
+ end
22
+
23
+ it "quotes multiple constraints" do
24
+ connection.set_constraints(:deferred, :my_fk1, :my_fk2)
25
+ expect(connection.executed_statements).to eq ['SET CONSTRAINTS "my_fk1", "my_fk2" DEFERRED']
26
+ end
27
+ end
28
+
29
+ describe "#defer_constraints" do
30
+ around do |example|
31
+ connection.dont_execute(&example)
32
+ end
33
+
34
+ it "defers and resets" do
35
+ block_called = false
36
+ connection.defer_constraints do
37
+ block_called = true
38
+ end
39
+ expect(block_called).to eq true
40
+ expect(connection.executed_statements).to eq ["SET CONSTRAINTS ALL DEFERRED", "SET CONSTRAINTS ALL IMMEDIATE"]
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "activerecord-pg-extensions"
4
+ require "byebug"
5
+ require "active_record/railtie"
6
+
7
+ ActiveRecord::Base # rubocop:disable Lint/Void
8
+ Rails.env = "test"
9
+
10
+ class Application < Rails::Application
11
+ config.eager_load = false
12
+ end
13
+ Application.initialize!
14
+
15
+ ActiveRecord::Tasks::DatabaseTasks.create_all
16
+
17
+ module StatementCaptureConnection
18
+ def dont_execute
19
+ @dont_execute = true
20
+ yield
21
+ ensure
22
+ @dont_execute = false
23
+ end
24
+
25
+ def executed_statements
26
+ @executed_statements ||= []
27
+ end
28
+
29
+ def execute(statement, *)
30
+ executed_statements << statement
31
+ super unless @dont_execute
32
+ end
33
+ end
34
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(StatementCaptureConnection)
35
+
36
+ RSpec.configure do |config|
37
+ config.expect_with :rspec do |c|
38
+ c.syntax = :expect
39
+ end
40
+
41
+ def connection
42
+ ActiveRecord::Base.connection
43
+ end
44
+
45
+ config.before do
46
+ connection.executed_statements.clear
47
+ end
48
+ end
metadata ADDED
@@ -0,0 +1,196 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord-pg-extensions
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Cody Cutrer
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-06-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '6.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '6.2'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ version: '6.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '6.2'
33
+ - !ruby/object:Gem::Dependency
34
+ name: railties
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '6.0'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '6.2'
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: '6.0'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '6.2'
53
+ - !ruby/object:Gem::Dependency
54
+ name: byebug
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: '11.1'
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - "~>"
65
+ - !ruby/object:Gem::Version
66
+ version: '11.1'
67
+ - !ruby/object:Gem::Dependency
68
+ name: pg
69
+ requirement: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - "~>"
72
+ - !ruby/object:Gem::Version
73
+ version: '1.2'
74
+ type: :development
75
+ prerelease: false
76
+ version_requirements: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - "~>"
79
+ - !ruby/object:Gem::Version
80
+ version: '1.2'
81
+ - !ruby/object:Gem::Dependency
82
+ name: rake
83
+ requirement: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - "~>"
86
+ - !ruby/object:Gem::Version
87
+ version: '13.0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - "~>"
93
+ - !ruby/object:Gem::Version
94
+ version: '13.0'
95
+ - !ruby/object:Gem::Dependency
96
+ name: rspec
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '3.0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - "~>"
107
+ - !ruby/object:Gem::Version
108
+ version: '3.0'
109
+ - !ruby/object:Gem::Dependency
110
+ name: rubocop
111
+ requirement: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - "~>"
114
+ - !ruby/object:Gem::Version
115
+ version: '1.7'
116
+ type: :development
117
+ prerelease: false
118
+ version_requirements: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - "~>"
121
+ - !ruby/object:Gem::Version
122
+ version: '1.7'
123
+ - !ruby/object:Gem::Dependency
124
+ name: rubocop-rake
125
+ requirement: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - "~>"
128
+ - !ruby/object:Gem::Version
129
+ version: '0.5'
130
+ type: :development
131
+ prerelease: false
132
+ version_requirements: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - "~>"
135
+ - !ruby/object:Gem::Version
136
+ version: '0.5'
137
+ - !ruby/object:Gem::Dependency
138
+ name: rubocop-rspec
139
+ requirement: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - "~>"
142
+ - !ruby/object:Gem::Version
143
+ version: '2.3'
144
+ type: :development
145
+ prerelease: false
146
+ version_requirements: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - "~>"
149
+ - !ruby/object:Gem::Version
150
+ version: '2.3'
151
+ description: Several extensions to ActiveRecord's PostgreSQLAdapter.
152
+ email:
153
+ - cody@instructure.com
154
+ executables: []
155
+ extensions: []
156
+ extra_rdoc_files: []
157
+ files:
158
+ - CHANGELOG.md
159
+ - LICENSE.txt
160
+ - README.md
161
+ - config/database.yml
162
+ - lib/active_record/pg_extensions.rb
163
+ - lib/active_record/pg_extensions/postgresql_adapter.rb
164
+ - lib/active_record/pg_extensions/railtie.rb
165
+ - lib/active_record/pg_extensions/version.rb
166
+ - lib/activerecord-pg-extensions.rb
167
+ - spec/postgresql_adapter_spec.rb
168
+ - spec/spec_helper.rb
169
+ homepage: https://github.com/instructure/activerecord-pg-extensions
170
+ licenses:
171
+ - MIT
172
+ metadata:
173
+ changelog_uri: https://github.com/instructure/activerecord-pg-extensions/blob/main/CHANGELOG.md
174
+ post_install_message:
175
+ rdoc_options: []
176
+ require_paths:
177
+ - lib
178
+ required_ruby_version: !ruby/object:Gem::Requirement
179
+ requirements:
180
+ - - ">="
181
+ - !ruby/object:Gem::Version
182
+ version: 2.6.0
183
+ required_rubygems_version: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ requirements: []
189
+ rubygems_version: 3.2.15
190
+ signing_key:
191
+ specification_version: 4
192
+ summary: Several extensions to ActiveRecord's PostgreSQLAdapter.
193
+ test_files:
194
+ - spec/spec_helper.rb
195
+ - spec/postgresql_adapter_spec.rb
196
+ - config/database.yml