pgreset 0.2 → 0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 34faaca27defbd9899bcc44b48b501a1c10e2da614bac7a44549fcebd1434aea
4
- data.tar.gz: 71f1207de1fd4e907dfab41811f3eb1cb82ffdec573ade06f1877f7450d3c3d0
3
+ metadata.gz: de67aa02241019195553648e33408586563205a64baf3fb219b26de23fb9e180
4
+ data.tar.gz: db83454a328df654b9000b5b08bb8c378ed36ddb020e8dd786329dacfdff920e
5
5
  SHA512:
6
- metadata.gz: 9dedf9dbffd685fd05073f9ad7a2321b98b7b40b2a043301e1160a3656ad71a0ea72edf0f842edf40f8e9a2fdfa86d9392b669a8b4c576a51681cb60f026c048
7
- data.tar.gz: 8148337cad52a15fe7a70a3cfb2a21e1003a59727db228220ef65940e6828516016bf3d137517a44917ddc1fcceb64289068df6ebd47dfddc60fb84c12e877bc
6
+ metadata.gz: ceb11b130b41efa4eebb8af64e7a6787b694b795a10d206e5157de20caedb0a9b1db102e6b8d9dcaf8573767c5af8949e6fca03069130f73378e3310209fcef5
7
+ data.tar.gz: bdc44ff256602541c9434be34f0860881c6baf1211163656167f58cace2ff41531f9e43f88cf6ff512b4031bd7528e4c7bb9b15b2adb42e35a01b5f965fd3480
data/README.md CHANGED
@@ -4,7 +4,7 @@ The pgreset gem makes it possible to run rails db:reset against a postgres datab
4
4
 
5
5
  Credit for the [original solution](https://github.com/basecamp/pow/issues/212) goes to [Manuel Meurer](https://github.com/manuelmeurer).
6
6
 
7
- Special thanks to [Emil Kampp](https://github.com/ekampp) and [Michael Yin](https://github.com/layerssss) for adding Rails 6.1 support.
7
+ Special thanks to [Emil Kampp](https://github.com/ekampp), [Michael Yin](https://github.com/layerssss), and [Kate Donaldson](https://github.com/katelovescode) for adding Rails 6.1 support.
8
8
 
9
9
  ## Installation and Usage
10
10
 
@@ -31,7 +31,20 @@ Now you can run rails db:reset as normal, and rails will kill active connections
31
31
 
32
32
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
33
 
34
- 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
34
+ To install this gem onto your local machine, run `bundle exec rake install`.
35
+
36
+ ## Testing
37
+
38
+ There's a script called pgreset-test.sh that can test a local copy of pgreset against various versions of ruby and rails. The script assumes postgres is listening on a socket at /tmp/.s.PGSQL.5432 and accepts connections for a user with no password, and that rvm is installed. You should at least test against these versions, since there were breaking changes to pgreset in them (replace PG_USER with your postgres username):
39
+
40
+ cd /path/to/your/pgreset/clone
41
+ ./pgreset-test.sh 3.0.4 6.0.4 PG_USER
42
+ ./pgreset-test.sh 3.1.2 7.0.8 PG_USER
43
+ ./pgreset-test.sh 3.1.2 7.1.1 PG_USER
44
+
45
+ ## Releasing
46
+
47
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
48
 
36
49
  ## Contributing
37
50
 
@@ -1,3 +1,3 @@
1
1
  module Pgreset
2
- VERSION = "0.2"
2
+ VERSION = "0.4"
3
3
  end
data/lib/pgreset.rb CHANGED
@@ -4,14 +4,16 @@ module ActiveRecord
4
4
  module Tasks
5
5
  class PostgreSQLDatabaseTasks
6
6
  def drop
7
- establish_master_connection
8
-
9
- database_name =
10
- begin
11
- self.configuration_hash[:database] || self.configuration_hash.fetch('database')
12
- rescue NoMethodError
13
- configuration['database']
14
- end
7
+ # Extract the name of the application database from the configuration and
8
+ # establish a connection to the "postgres" database in the public schema.
9
+ # `configuration_hash` was introduced in rails 6.1.0, prior versions use `configuration`.
10
+ if respond_to?(:configuration_hash, true)
11
+ database_name = configuration_hash.with_indifferent_access.fetch(:database)
12
+ establish_connection(configuration_hash.merge(database: "postgres", schema_search_path: "public"))
13
+ else
14
+ database_name = configuration['database']
15
+ establish_connection(configuration.merge(database: "postgres", schema_search_path: "public"))
16
+ end
15
17
 
16
18
  pid_column = 'pid' # Postgresql 9.2 and newer
17
19
  if 0 == connection.select_all("SELECT column_name FROM information_schema.columns WHERE table_name = 'pg_stat_activity' AND column_name = 'pid';").count
data/pgreset-test.sh ADDED
@@ -0,0 +1,76 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is a simple script to test pgreset against different versions of ruby and rails.
4
+ # Right now it expects to connect to postgres through a socket in /tmp as a given user
5
+ # with no password.
6
+
7
+ function die
8
+ {
9
+ echo "$*" >&2
10
+ exit 1
11
+ }
12
+
13
+ if [[ ! -f "pgreset.gemspec" ]]; then
14
+ die "This script must be run from the pgreset source directory"
15
+ fi
16
+
17
+ if [[ -z "$3" ]]; then
18
+ die "Usage: $0 RUBY_VERSION RAILS_VERSION POSTGRES_USERNAME"
19
+ fi
20
+
21
+
22
+ source "$HOME/.rvm/scripts/rvm"
23
+
24
+ set -e
25
+
26
+ PGRESET_DIR="$(pwd)"
27
+ RUBY_VERSION="ruby-$1"
28
+ RAILS_VERSION="$2"
29
+ PG_USER="$3"
30
+
31
+ # can't have periods in the database name
32
+ TEST_NAME="$(echo pgreset-test-rails-$RAILS_VERSION | sed 's/\./-/g')"
33
+
34
+ rvm install "$RUBY_VERSION"
35
+ rvm use "$RUBY_VERSION"
36
+ rvm --force gemset delete "$TEST_NAME"
37
+ rvm gemset create "$TEST_NAME"
38
+ rvm gemset use "$TEST_NAME"
39
+ cd /tmp
40
+ rm -rf "$TEST_NAME"
41
+ gem install rails -N -v "$RAILS_VERSION"
42
+ rails new "$TEST_NAME"
43
+ cd "$TEST_NAME"
44
+ echo "gem 'pg'" >> Gemfile
45
+ echo "gem 'pgreset', path: \"${PGRESET_DIR}\"" >> Gemfile
46
+ bundle install
47
+ rails g model user name:string
48
+
49
+ echo "development:
50
+ adapter: postgresql
51
+ encoding: UTF8
52
+ username: $PG_USER
53
+ host: /tmp
54
+ database: $TEST_NAME
55
+ " > config/database.yml
56
+
57
+ dropdb --if-exists -f "$TEST_NAME"
58
+ rails db:create
59
+ rails db:migrate
60
+
61
+
62
+ # Start a connection to the database.
63
+ # If pgreset works properly, this will automatically be disconnected
64
+ # with an error saying the server closed the connection unexpectedly.
65
+ psql -U "$PG_USER" "$TEST_NAME" -c 'select pg_sleep(120);' >log/psql.log 2>&1 &
66
+ sleep 1 # make sure psql is fully connected
67
+
68
+ rails db:reset
69
+
70
+ if grep 'terminating connection due to administrator command' log/psql.log >/dev/null ; then
71
+ echo "pgreset successfully terminated the postgres connection!"
72
+ else
73
+ echo "Hmm something doesn't look right. Here's the psql output:"
74
+ echo
75
+ cat log/psql.log
76
+ fi
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pgreset
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.2'
4
+ version: '0.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dan Falcone
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-01-07 00:00:00.000000000 Z
11
+ date: 2023-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -55,6 +55,7 @@ files:
55
55
  - bin/setup
56
56
  - lib/pgreset.rb
57
57
  - lib/pgreset/version.rb
58
+ - pgreset-test.sh
58
59
  - pgreset.gemspec
59
60
  homepage: https://github.com/dafalcon/pgreset
60
61
  licenses:
@@ -75,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
76
  - !ruby/object:Gem::Version
76
77
  version: '0'
77
78
  requirements: []
78
- rubygems_version: 3.0.6
79
+ rubygems_version: 3.2.33
79
80
  signing_key:
80
81
  specification_version: 4
81
82
  summary: Automatically kill postgres connections during db:reset