pg_conn 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/pg_conn/role_methods.rb +16 -12
- data/lib/pg_conn/version.rb +1 -1
- data/lib/pg_conn.rb +45 -25
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 17a782e74c71e670d14ed38e258f7b9c99ee1e832866dd9fa972774b1a21d08c
|
4
|
+
data.tar.gz: 226ad145da3b41360080a255bb90b65c4a4bbd2ac1fef8bdc2f2e43bed9feab6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c15ad10570d71104ac356f2f0da9a1e034cf400356357621300db3af872a73282a233c0a4258dc2bcc98ac2353143a768c1632f59439812965e4b00b41fc62be
|
7
|
+
data.tar.gz: eec1060c95e94fbb8f59fe56fb233d95dd4d111a6980947dbc8e609788f6b3652f01d9befe9d5af053b224fa1ff661b93fc53d77001ee14593fdfdea985104a4
|
data/lib/pg_conn/role_methods.rb
CHANGED
@@ -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.
|
61
|
-
#
|
62
|
-
#
|
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
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
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
|
data/lib/pg_conn/version.rb
CHANGED
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
|
513
|
-
#
|
514
|
-
#
|
515
|
-
#
|
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
|
-
|
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
|
-
|
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,
|
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
|
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
|
-
|
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
|
-
|
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
|
-
#
|
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,
|
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
|
707
|
-
|
708
|
-
|
709
|
-
|
710
|
-
|
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 != ""
|