pg_conn 0.7.2 → 0.8.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: 240a2b6bb03ef6c0ffa37ae12c2610e185c69b873ccebda32a6ef0bfd0626b06
4
- data.tar.gz: 6fcf77b84a14ddf55640a8b359b5843a5fa565b2db849d649df856626220dec6
3
+ metadata.gz: 599ccb65823ae32d0a3baf96ba0d636104c7994f2473d71eafa1927228f47302
4
+ data.tar.gz: b08bb15804c363fb25c03e102f35de6f64694220b9865e5a9d0525516cc6db24
5
5
  SHA512:
6
- metadata.gz: 8d14ccc934203b706bc32b65f2678cd2db9f1ed32b7b0268ad32d22a76d7255d66c0b9d5e3f639fe6553fe174817304519926d9f5d66861f8d9c7999e32d798a
7
- data.tar.gz: 4d21fe7896ec5fa5b9d02e675265c68a4a1460658ddaf0508627c0430032f69dfef3608cf65c90b4cb8d1e13951f4ce871b74370fa5fc93bf9971ac737dcceba
6
+ metadata.gz: 1dba080845fa70f52f4b0614392be42169822809a6a436da49f3c8a2453614c5b9586927ee5abfa5546933d43370d691458fb0677b350a0c40d1257acd42b79b
7
+ data.tar.gz: 8cd9e0b2481ba760dd250a7e68bda460ddf38564843929143ca3622904484866818b75bad0bc9c8a9fbbacd8bd9980eaa53b8670f4e68d9f0f04886fe24f573c
@@ -52,7 +52,7 @@ module PgConn
52
52
  conn.exec stmt
53
53
  end
54
54
 
55
- # Remove all privileges from the given role
55
+ # Remove all privileges from the given role. TODO #demote!, strip! ?
56
56
  def clean(rolename)
57
57
  end
58
58
 
@@ -60,12 +60,18 @@ module PgConn
60
60
  # privileges and objects too if :cascade is true. Note that cascade only
61
61
  # works if connected to the database where the privileges exist. The
62
62
  # :silent option is used in tests - fix it somehow!
63
- def drop(*rolenames, cascade: false, silent: false)
63
+ def drop(*rolenames, cascade: false, fail: true, silent: false)
64
64
  rolenames = Array(rolenames).flatten.compact.select { |role| exist?(role) }
65
65
  return false if rolenames.empty?
66
66
  rolenames_sql = PgConn.sql_idents(rolenames)
67
- conn.exec "drop owned by #{rolenames_sql} cascade" if cascade
68
- conn.exec "drop role #{rolenames_sql}"
67
+ begin
68
+ conn.exec("drop owned by #{rolenames_sql} cascade", fail: false, silent: silent) if cascade
69
+ conn.exec("drop role #{rolenames_sql}", fail: fail, silent: silent)
70
+ rescue PG::Error
71
+ raise if fail
72
+ conn.cancel_transaction
73
+ return false
74
+ end
69
75
  true
70
76
  end
71
77
 
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.7.2"
2
+ VERSION = "0.8.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -70,7 +70,7 @@ module PgConn
70
70
  # element defaults to nil if not found
71
71
  def err
72
72
  @err ||=
73
- if error&.message =~ /.*?ERROR:\s*(.*?)\n(?:.*?(\s*LINE\s+(\d+):\s*).*?\n(?:(\s+)\^\n)?)?/
73
+ if error&.message =~ /.*?ERROR:\s*(.*?)\n(?:.*?(\s*LINE\s+(\d+): ).*?\n(?:(\s+)\^\n)?)?/
74
74
  [$1.capitalize, $3&.to_i, $4 && ($4.size - $2.size + 1)]
75
75
  else
76
76
  [nil, nil, nil]
@@ -520,6 +520,19 @@ module PgConn
520
520
  transaction(commit: commit) { execute(sql, fail: fail, silent: silent) }
521
521
  end
522
522
 
523
+ # Like #exec but returns true/false depending on if the command succeeded.
524
+ # There is not corresponding #exeucte? method because any failure rolls
525
+ # back the whole transaction stack. TODO: Check which exceptions that
526
+ # should be captured
527
+ def exec?(sql, commit: true, silent: true)
528
+ begin
529
+ exec(sql, commit: commit, fail: true, silent: silent)
530
+ rescue PG::Error
531
+ return false
532
+ end
533
+ return true
534
+ end
535
+
523
536
  # Execute SQL statement(s) without a transaction block and return the
524
537
  # number of affected records (if any). This used to call procedures that
525
538
  # may manipulate transactions. The +sql+ argument can be a SQL command or
@@ -660,7 +673,7 @@ module PgConn
660
673
  #
661
674
  # Execute statement(s) on the server. If the argument is an array of
662
675
  # commands, the commands are concatenated with ';' before being sent to the
663
- # server. #pg_exec returns a PG::Result object or nil if +arg+ was empty.
676
+ # server. #pg_exec returns a PG::Result object or nil if +arg+ was empty.
664
677
  # #exec pass Postgres exceptions to the caller unless :fail is false
665
678
  #
666
679
  # FIXME: Error message prints the last statement but what if another
@@ -683,6 +696,7 @@ module PgConn
683
696
  else
684
697
  stmts = arg.flatten.compact
685
698
  return nil if stmts.empty?
699
+ # stmts.unshift("set on_error_exit stop")
686
700
  last_stmt = stmts.last
687
701
  @pg_connection.exec(stmts.join(";\n"))
688
702
  end
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.7.2
4
+ version: 0.8.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-01-21 00:00:00.000000000 Z
11
+ date: 2024-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg