pg_conn 0.8.0 → 0.9.0

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: 599ccb65823ae32d0a3baf96ba0d636104c7994f2473d71eafa1927228f47302
4
- data.tar.gz: b08bb15804c363fb25c03e102f35de6f64694220b9865e5a9d0525516cc6db24
3
+ metadata.gz: 17a782e74c71e670d14ed38e258f7b9c99ee1e832866dd9fa972774b1a21d08c
4
+ data.tar.gz: 226ad145da3b41360080a255bb90b65c4a4bbd2ac1fef8bdc2f2e43bed9feab6
5
5
  SHA512:
6
- metadata.gz: 1dba080845fa70f52f4b0614392be42169822809a6a436da49f3c8a2453614c5b9586927ee5abfa5546933d43370d691458fb0677b350a0c40d1257acd42b79b
7
- data.tar.gz: 8cd9e0b2481ba760dd250a7e68bda460ddf38564843929143ca3622904484866818b75bad0bc9c8a9fbbacd8bd9980eaa53b8670f4e68d9f0f04886fe24f573c
6
+ metadata.gz: c15ad10570d71104ac356f2f0da9a1e034cf400356357621300db3af872a73282a233c0a4258dc2bcc98ac2353143a768c1632f59439812965e4b00b41fc62be
7
+ data.tar.gz: eec1060c95e94fbb8f59fe56fb233d95dd4d111a6980947dbc8e609788f6b3652f01d9befe9d5af053b224fa1ff661b93fc53d77001ee14593fdfdea985104a4
@@ -57,22 +57,26 @@ module PgConn
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!
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!
63
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
- 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
75
- 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
76
80
  end
77
81
 
78
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.8.0"
2
+ VERSION = "0.9.0"
3
3
  end
data/lib/pg_conn.rb CHANGED
@@ -509,15 +509,23 @@ 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
521
529
  end
522
530
 
523
531
  # Like #exec but returns true/false depending on if the command succeeded.
@@ -528,6 +536,7 @@ module PgConn
528
536
  begin
529
537
  exec(sql, commit: commit, fail: true, silent: silent)
530
538
  rescue PG::Error
539
+ cancel_transaction
531
540
  return false
532
541
  end
533
542
  return true
@@ -543,9 +552,14 @@ module PgConn
543
552
  # TODO: Handle postgres exceptions wrt transaction state and stack
544
553
  def execute(sql, fail: true, silent: false)
545
554
  if @pg_connection
546
- 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
547
561
  else
548
- pg_exec(sql, fail: fail, silent: silent)
562
+ pg_exec(sql, silent: silent)
549
563
  end
550
564
  end
551
565
 
@@ -611,10 +625,16 @@ module PgConn
611
625
  end
612
626
 
613
627
  # Does a rollback and empties the stack. This should be called in response
614
- # to PG::Error exceptions because then the whole transaction stack is
615
- # 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
616
633
  def cancel_transaction
617
- pg_exec("rollback")
634
+ begin
635
+ pg_exec("rollback")
636
+ rescue PG::Error
637
+ end
618
638
  @savepoints = nil
619
639
  end
620
640
 
@@ -631,7 +651,9 @@ module PgConn
631
651
  rescue PgConn::Rollback
632
652
  pop_transaction(commit: false)
633
653
  return nil
634
- # FIXME: Rescue other postgres errors and wipe-out stack
654
+ rescue PG::Error
655
+ @savepoints = nil
656
+ raise
635
657
  end
636
658
  pop_transaction(commit: commit)
637
659
  result
@@ -640,7 +662,6 @@ module PgConn
640
662
  private
641
663
  # Wrapper around PG::Connection.new that switches to the postgres user
642
664
  # before connecting if the current user is the root user
643
- #
644
665
  def make_connection(*args, **opts)
645
666
  if Process.euid == 0
646
667
  begin
@@ -673,8 +694,10 @@ module PgConn
673
694
  #
674
695
  # Execute statement(s) on the server. If the argument is an array of
675
696
  # commands, the commands are concatenated with ';' before being sent to the
676
- # server. #pg_exec returns a PG::Result object or nil if +arg+ was empty.
677
- # #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
678
701
  #
679
702
  # FIXME: Error message prints the last statement but what if another
680
703
  # statement failed?
@@ -684,7 +707,7 @@ module PgConn
684
707
  # though
685
708
  #
686
709
  # TODO: Fix silent by not handling exceptions
687
- def pg_exec(arg, fail: true, silent: false)
710
+ def pg_exec(arg, silent: false)
688
711
  if @pg_connection
689
712
  @error = @err = nil
690
713
  begin
@@ -703,19 +726,16 @@ module PgConn
703
726
 
704
727
  rescue PG::Error => ex
705
728
  @error = ex
706
- if fail
707
- if !silent # FIXME Why do we handle this?
708
- $stderr.puts arg
709
- $stderr.puts
710
- $stderr.puts ex.message
711
- $stderr.flush
712
- end
713
- raise
714
- else
715
- 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
716
734
  end
735
+ raise
717
736
  end
718
737
 
738
+ # For dump of SQL statements
719
739
  else # @pg_commands is defined
720
740
  if arg.is_a?(String)
721
741
  @pg_commands << arg if arg != ""
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg_conn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen