pgreset 0.3 → 0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97f6d30c6fcbbba220f065297f7eb6441101609a99d0b39e176e7f9a61be7637
4
- data.tar.gz: 6d886b10d4e0da04137c0ec8086aea356228fcbe86d8702218921325e6f6b445
3
+ metadata.gz: de67aa02241019195553648e33408586563205a64baf3fb219b26de23fb9e180
4
+ data.tar.gz: db83454a328df654b9000b5b08bb8c378ed36ddb020e8dd786329dacfdff920e
5
5
  SHA512:
6
- metadata.gz: f3c218c0d55321c25a5b1679902c06cfa878ce5bc51ff09fbd60325554afbecf797c258920b7cf335f8edb9524f73368de8b6e78348d7c7144d9408f6e3acb0b
7
- data.tar.gz: d22de6723053d9306598d6689834b6855b3b43247292405a40622d085447bb511f75b3c6a2f716f3cc00fa37e9c1c3cf54cf54a9bddaa0295cc8a9aa26c9097a
6
+ metadata.gz: ceb11b130b41efa4eebb8af64e7a6787b694b795a10d206e5157de20caedb0a9b1db102e6b8d9dcaf8573767c5af8949e6fca03069130f73378e3310209fcef5
7
+ data.tar.gz: bdc44ff256602541c9434be34f0860881c6baf1211163656167f58cace2ff41531f9e43f88cf6ff512b4031bd7528e4c7bb9b15b2adb42e35a01b5f965fd3480
data/README.md CHANGED
@@ -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.3"
2
+ VERSION = "0.4"
3
3
  end
data/lib/pgreset.rb CHANGED
@@ -4,15 +4,16 @@ module ActiveRecord
4
4
  module Tasks
5
5
  class PostgreSQLDatabaseTasks
6
6
  def drop
7
- establish_master_connection
8
-
9
- # `configuration_hash` was introduced in 6.1.0, prior versions have the database name in `configuration`.
10
- database_name =
11
- if respond_to?(:configuration_hash, true)
12
- configuration_hash.with_indifferent_access.fetch(:database)
13
- else
14
- configuration['database']
15
- 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
16
17
 
17
18
  pid_column = 'pid' # Postgresql 9.2 and newer
18
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.3'
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-24 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