pg_conn 0.7.3 → 0.9.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: 208fa123e91d3d1020b3c9030ab513bc482dbb5f42803e6c8f5500fa0f020f1f
4
- data.tar.gz: f16371469c57bf3a812643dc537ca8e214f5cef30a40bde1f6517dd99f4edaca
3
+ metadata.gz: 17a782e74c71e670d14ed38e258f7b9c99ee1e832866dd9fa972774b1a21d08c
4
+ data.tar.gz: 226ad145da3b41360080a255bb90b65c4a4bbd2ac1fef8bdc2f2e43bed9feab6
5
5
  SHA512:
6
- metadata.gz: eafa6801dd0b4e6a1c5ea08df77188453c87478086e898a9fa6ddd9cbf15a742dc14ba939c25f63b5742ca98f07c79ffca429fa37ebbaab4448026c7927bb701
7
- data.tar.gz: a433659b74b05ed68fc916dc0bf11c78a53471b865b7f9fab6646241e2c9f525d9c272142cc8121634080a5cc921aacbf22f699c8b451b2c597f22a991a2ce2e
6
+ metadata.gz: c15ad10570d71104ac356f2f0da9a1e034cf400356357621300db3af872a73282a233c0a4258dc2bcc98ac2353143a768c1632f59439812965e4b00b41fc62be
7
+ data.tar.gz: eec1060c95e94fbb8f59fe56fb233d95dd4d111a6980947dbc8e609788f6b3652f01d9befe9d5af053b224fa1ff661b93fc53d77001ee14593fdfdea985104a4
@@ -52,21 +52,31 @@ 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
 
59
59
  # Drop existing users. Return true if any role was dropped. Drop depending
60
- # privileges and objects too if :cascade is true. Note that cascade only
61
- # works if connected to the database where the privileges exist. The
62
- # :silent option is used in tests - fix it somehow!
63
- def drop(*rolenames, cascade: false, silent: false)
60
+ # privileges and objects too if :cascade is true. Returns true if the
61
+ # user(s) was deleted and false if :fail is true and one or more user
62
+ # counldn't be deleted
63
+ #
64
+ # Note that cascade only works if connected to the database where the
65
+ # privileges exist.
66
+ #
67
+ # TODO The :silent option is used in tests - fix it somehow!
68
+ def drop(*rolenames, cascade: false, fail: true, silent: false)
64
69
  rolenames = Array(rolenames).flatten.compact.select { |role| exist?(role) }
65
70
  return false if rolenames.empty?
66
71
  rolenames_sql = PgConn.sql_idents(rolenames)
67
- conn.exec "drop owned by #{rolenames_sql} cascade" if cascade
68
- conn.exec "drop role #{rolenames_sql}"
69
- true
72
+ # begin
73
+ conn.exec("drop owned by #{rolenames_sql} cascade", fail: false, silent: silent) if cascade
74
+ conn.exec("drop role #{rolenames_sql}", fail: fail, silent: silent) && true
75
+ # rescue PG::Error
76
+ # raise if fail
77
+ # conn.cancel_transaction
78
+ # return false
79
+ # end
70
80
  end
71
81
 
72
82
  # List users. TODO Use RE instead of database argument. Also doc this shit
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.7.3"
2
+ VERSION = "0.9.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -509,15 +509,37 @@ module PgConn
509
509
  # that span multiple lines. The empty array is a NOP but the empty string
510
510
  # is not.
511
511
  #
512
- # #exec pass Postgres exceptions to the caller unless :fail is false. If
513
- # fail is false #exec instead return nil but note that postgres doesn't
514
- # ignore it so that if you're inside a transaction, the transaction will be
515
- # in an error state and if you're also using subtransactions the whole
512
+ # #exec pass Postgres exceptions to the caller unless :fail is false in which case
513
+ # it returns nil.
514
+ #
515
+ # Note that postgres crashes the whole transaction stack if any error is
516
+ # met so if you're inside a transaction, the transaction will be in an
517
+ # error state and if you're also using subtransactions the whole
516
518
  # transaction stack has collapsed
517
519
  #
518
520
  # TODO: Make sure the transaction stack is emptied on postgres errors
519
521
  def exec(sql, commit: true, fail: true, silent: false)
520
- transaction(commit: commit) { execute(sql, fail: fail, silent: silent) }
522
+ begin
523
+ transaction(commit: commit) { execute(sql, fail: fail, silent: silent) }
524
+ rescue PG::Error
525
+ raise if fail
526
+ cancel_transaction
527
+ return nil
528
+ end
529
+ end
530
+
531
+ # Like #exec but returns true/false depending on if the command succeeded.
532
+ # There is not corresponding #exeucte? method because any failure rolls
533
+ # back the whole transaction stack. TODO: Check which exceptions that
534
+ # should be captured
535
+ def exec?(sql, commit: true, silent: true)
536
+ begin
537
+ exec(sql, commit: commit, fail: true, silent: silent)
538
+ rescue PG::Error
539
+ cancel_transaction
540
+ return false
541
+ end
542
+ return true
521
543
  end
522
544
 
523
545
  # Execute SQL statement(s) without a transaction block and return the
@@ -530,9 +552,14 @@ module PgConn
530
552
  # TODO: Handle postgres exceptions wrt transaction state and stack
531
553
  def execute(sql, fail: true, silent: false)
532
554
  if @pg_connection
533
- pg_exec(sql, fail: fail, silent: silent)&.cmd_tuples
555
+ begin
556
+ pg_exec(sql, silent: silent)&.cmd_tuples
557
+ rescue PG::Error
558
+ raise if fail
559
+ return nil
560
+ end
534
561
  else
535
- pg_exec(sql, fail: fail, silent: silent)
562
+ pg_exec(sql, silent: silent)
536
563
  end
537
564
  end
538
565
 
@@ -598,10 +625,16 @@ module PgConn
598
625
  end
599
626
 
600
627
  # Does a rollback and empties the stack. This should be called in response
601
- # to PG::Error exceptions because then the whole transaction stack is
602
- # invalid
628
+ # to PG::Error exceptions because the whole transaction stack is
629
+ # invalid and the server is in an invalid state
630
+ #
631
+ # It is not an error to call #cancel_transaction when no transaction is in
632
+ # progress
603
633
  def cancel_transaction
604
- pg_exec("rollback")
634
+ begin
635
+ pg_exec("rollback")
636
+ rescue PG::Error
637
+ end
605
638
  @savepoints = nil
606
639
  end
607
640
 
@@ -618,7 +651,9 @@ module PgConn
618
651
  rescue PgConn::Rollback
619
652
  pop_transaction(commit: false)
620
653
  return nil
621
- # FIXME: Rescue other postgres errors and wipe-out stack
654
+ rescue PG::Error
655
+ @savepoints = nil
656
+ raise
622
657
  end
623
658
  pop_transaction(commit: commit)
624
659
  result
@@ -627,7 +662,6 @@ module PgConn
627
662
  private
628
663
  # Wrapper around PG::Connection.new that switches to the postgres user
629
664
  # before connecting if the current user is the root user
630
- #
631
665
  def make_connection(*args, **opts)
632
666
  if Process.euid == 0
633
667
  begin
@@ -660,8 +694,10 @@ module PgConn
660
694
  #
661
695
  # Execute statement(s) on the server. If the argument is an array of
662
696
  # 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.
664
- # #exec pass Postgres exceptions to the caller unless :fail is false
697
+ # server. #pg_exec returns a PG::Result object or nil if +arg+ was empty
698
+ #
699
+ # Postgres errors are passed through and #error and #err set to the last
700
+ # statement's SQL errors or nil if it succeeded
665
701
  #
666
702
  # FIXME: Error message prints the last statement but what if another
667
703
  # statement failed?
@@ -671,7 +707,7 @@ module PgConn
671
707
  # though
672
708
  #
673
709
  # TODO: Fix silent by not handling exceptions
674
- def pg_exec(arg, fail: true, silent: false)
710
+ def pg_exec(arg, silent: false)
675
711
  if @pg_connection
676
712
  @error = @err = nil
677
713
  begin
@@ -683,25 +719,23 @@ module PgConn
683
719
  else
684
720
  stmts = arg.flatten.compact
685
721
  return nil if stmts.empty?
722
+ # stmts.unshift("set on_error_exit stop")
686
723
  last_stmt = stmts.last
687
724
  @pg_connection.exec(stmts.join(";\n"))
688
725
  end
689
726
 
690
727
  rescue PG::Error => ex
691
728
  @error = ex
692
- if fail
693
- if !silent # FIXME Why do we handle this?
694
- $stderr.puts arg
695
- $stderr.puts
696
- $stderr.puts ex.message
697
- $stderr.flush
698
- end
699
- raise
700
- else
701
- return nil
729
+ if !silent # FIXME Why do we handle this?
730
+ $stderr.puts arg
731
+ $stderr.puts
732
+ $stderr.puts ex.message
733
+ $stderr.flush
702
734
  end
735
+ raise
703
736
  end
704
737
 
738
+ # For dump of SQL statements
705
739
  else # @pg_commands is defined
706
740
  if arg.is_a?(String)
707
741
  @pg_commands << arg if arg != ""
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.3
4
+ version: 0.9.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