pg_conn 0.9.0 → 0.10.0

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: 17a782e74c71e670d14ed38e258f7b9c99ee1e832866dd9fa972774b1a21d08c
4
- data.tar.gz: 226ad145da3b41360080a255bb90b65c4a4bbd2ac1fef8bdc2f2e43bed9feab6
3
+ metadata.gz: 7708b08a6ef0fcc12d0241ef9a084fe8adcfee342e03933f0f48ec313b9c48b5
4
+ data.tar.gz: 716d8e6de64dacab36db01303a47480e0d603a31a8fd24025441ca3a5c8adc2e
5
5
  SHA512:
6
- metadata.gz: c15ad10570d71104ac356f2f0da9a1e034cf400356357621300db3af872a73282a233c0a4258dc2bcc98ac2353143a768c1632f59439812965e4b00b41fc62be
7
- data.tar.gz: eec1060c95e94fbb8f59fe56fb233d95dd4d111a6980947dbc8e609788f6b3652f01d9befe9d5af053b224fa1ff661b93fc53d77001ee14593fdfdea985104a4
6
+ metadata.gz: 12f8f90e3853a0882f93bccde0ea1ce564068612d5d876d26c159625eea4b541bd391b81fe10cbda24b96458129e7caf3c52fe0471d81efc210c151708224887
7
+ data.tar.gz: 5fd5a73696fbb9daa9dbc873daee9cf580974ab17bca8fc62743967160f2757e5aaafbb8ece7f8d8e32d122e5d0b3e7b6848a834b5bc16ee65099975ff7337dc
@@ -0,0 +1,82 @@
1
+ module PgConn
2
+ # Schema methods
3
+ class SessionMethods
4
+ attr_reader :conn
5
+
6
+ def initialize(conn)
7
+ @conn = conn
8
+ end
9
+
10
+ # Returns a list of users connected to the given database. If database is
11
+ # nil, it returns a list of database/username tuples for all connected users
12
+ def list(database)
13
+ if database
14
+ conn.values "select usename from pg_stat_activity where datname = '#{database}'"
15
+ else
16
+ conn.tuples %(
17
+ select datname, usename
18
+ from pg_stat_activity
19
+ where datname is not null and usename is not null
20
+ )
21
+ end
22
+ end
23
+
24
+ # Terminate sessions in the database of the given users or of all users if
25
+ # the users is nil. Note that 'terminate(database)' is a nop because the
26
+ # absent users argument defaults to an empty list
27
+ #
28
+ # TODO: Make is possible to terminate a single session of a user with
29
+ # multiple sessions (is this ever relevant?)
30
+ def terminate(database, *users)
31
+ !database.nil? or raise ArgumentError
32
+ users = Array(users).flatten
33
+ case users
34
+ when []; return
35
+ when [nil]; users = list(database)
36
+ else users = Array(users).flatten
37
+ end
38
+ pids = self.pids(database, users)
39
+ return if pids.empty?
40
+ pids_sql = pids.map { |pid| "(#{pid})" }.join(", ")
41
+ conn.execute "select pg_terminate_backend(pid) from ( values #{pids_sql} ) as x(pid)"
42
+ end
43
+
44
+ def disable(database)
45
+ !database.nil? or raise ArgumentError
46
+ conn.execute "alter database #{database} allow_connections = false"
47
+ end
48
+
49
+ def enable(database)
50
+ !database.nil? or raise ArgumentError
51
+ conn.execute "alter database #{database} allow_connections = true"
52
+ end
53
+
54
+ # Run block without any connected users. Existing sessions are terminated
55
+ def exclusive(database, &block)
56
+ !database.nil? or raise ArgumentError
57
+ begin
58
+ disable(database)
59
+ users = list(database)
60
+ terminate(database, users)
61
+ yield
62
+ ensure
63
+ enable(database)
64
+ end
65
+ end
66
+
67
+ private
68
+ # Like #list but returns the PIDs of the users
69
+ def pids(database, users)
70
+ users ||= list(database)
71
+ if !users.empty?
72
+ users_sql = "(" + users.map { |user| "'#{user}'" }.join(", ") + ")"
73
+ conn.values "select pid from pg_stat_activity where datname = '#{database}' and usename in #{users_sql}"
74
+ else
75
+ conn.values "select pid from pg_stat_activity where datname = '#{database}'"
76
+ end
77
+ end
78
+ end
79
+ end
80
+
81
+
82
+
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.9.0"
2
+ VERSION = "0.10.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -5,6 +5,7 @@ require "pg_conn/version"
5
5
  require "pg_conn/role_methods"
6
6
  require "pg_conn/schema_methods"
7
7
  require "pg_conn/rdbms_methods"
8
+ require "pg_conn/session_methods"
8
9
 
9
10
  module PgConn
10
11
  class Error < StandardError; end
@@ -59,6 +60,9 @@ module PgConn
59
60
  # #exist?/#list for relations/tables/views/columns
60
61
  attr_reader :schema
61
62
 
63
+ # Session manipulation methods: #list, #terminate, #disable, #enable
64
+ attr_reader :session
65
+
62
66
  # The transaction timestamp of the most recent SQL statement executed by
63
67
  # #exec or #transaction block
64
68
  attr_reader :timestamp
@@ -198,11 +202,12 @@ module PgConn
198
202
  @schema = SchemaMethods.new(self)
199
203
  @role = RoleMethods.new(self)
200
204
  @rdbms = RdbmsMethods.new(self)
205
+ @session = SessionMethods.new(self)
201
206
  @timestamp = nil
202
207
  @savepoints = nil # Stack of savepoint names. Nil if no transaction in progress
203
208
  end
204
209
 
205
- # Close the database connection
210
+ # Close the database connection. TODO: Rename 'close'
206
211
  def terminate()
207
212
  @pg_connection.close if @pg_connection && !@pg_connection.finished?
208
213
  end
@@ -416,6 +421,9 @@ module PgConn
416
421
  h
417
422
  end
418
423
 
424
+ # TODO: An #array method that returns a map from id to tuple. Hmm... almost
425
+ # the same as #map
426
+
419
427
  # Return a hash from the record id column to an OpenStruct representation
420
428
  # of the record. If the :key_column option is defined it will be used
421
429
  # instead of id as the key. It is an error if the id field value is not
@@ -567,6 +575,7 @@ module PgConn
567
575
  # back to the original user
568
576
  #
569
577
  # FIXME: The out-commented transaction block makes postspec fail for some reason
578
+ # TODO: Rename 'sudo' because it acts just like it.
570
579
  def su(username, &block)
571
580
  raise Error, "Missing block in call to PgConn::Connection#su" if !block_given?
572
581
  realuser = self.value "select current_user"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_conn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-06 00:00:00.000000000 Z
11
+ date: 2024-02-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -85,6 +85,7 @@ files:
85
85
  - lib/pg_conn/rdbms_methods.rb
86
86
  - lib/pg_conn/role_methods.rb
87
87
  - lib/pg_conn/schema_methods.rb
88
+ - lib/pg_conn/session_methods.rb
88
89
  - lib/pg_conn/version.rb
89
90
  - pg_conn.gemspec
90
91
  homepage: http://www.nowhere.com/