pg_conn 0.58.1 → 0.59.2
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 +4 -4
- data/lib/pg_conn/schema_methods.rb +7 -5
- data/lib/pg_conn/version.rb +1 -1
- data/lib/pg_conn.rb +41 -29
- 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: 8dd70cb7a067886566b8ae751bb319115450ed3ebc82d3420c74c7f77e5f72ce
|
|
4
|
+
data.tar.gz: 53393a8ef217ed7502b78d5d7f2f1cb092cc5adbe8e7da30f1cac901a262d31c
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: e09cbbffebf131810a76a0470e85ad130148f556756ca3a6885ebd87cd181b7f354a5a8601f4428d77f1a1ba2fca47c58f7ba5fb9c5d3ac53ea159c5f4207ea2
|
|
7
|
+
data.tar.gz: 95a2986bcc458774f532adf6c2a28dff576d0ea7596c8c58fd3bf03aa86f218d2911536848ac3c0e5bac6e528ebd8ec6b47a9c3a17f96173a91ba0140fc1c9fc
|
|
@@ -36,7 +36,7 @@ module PgConn
|
|
|
36
36
|
end
|
|
37
37
|
|
|
38
38
|
# Hollow out a schema by dropping all tables and views (but still not
|
|
39
|
-
# functions and procedures TODO)
|
|
39
|
+
# functions and procedures TODO). TODO: Swap names with #clean!
|
|
40
40
|
def empty!(schema, exclude: [])
|
|
41
41
|
self.list_tables(schema, exclude: exclude).each { |table|
|
|
42
42
|
conn.execute "drop table if exists #{schema}.#{table} cascade"
|
|
@@ -46,11 +46,13 @@ module PgConn
|
|
|
46
46
|
}
|
|
47
47
|
end
|
|
48
48
|
|
|
49
|
-
# Empty all tables in the given schema
|
|
50
|
-
def clean!(
|
|
49
|
+
# Empty all tables in the given schema(s)
|
|
50
|
+
def clean!(*schemas, exclude: [])
|
|
51
51
|
conn.session.triggers(false) {
|
|
52
|
-
|
|
53
|
-
|
|
52
|
+
schemas.flatten.each { |schema|
|
|
53
|
+
self.list_tables(schema, exclude: exclude).each { |table|
|
|
54
|
+
conn.execute "delete from #{schema}.#{table}"
|
|
55
|
+
}
|
|
54
56
|
}
|
|
55
57
|
}
|
|
56
58
|
end
|
data/lib/pg_conn/version.rb
CHANGED
data/lib/pg_conn.rb
CHANGED
|
@@ -96,7 +96,7 @@ module PgConn
|
|
|
96
96
|
|
|
97
97
|
# Simple shorthand for ::quote_value
|
|
98
98
|
def self.quote(value, elem_type: nil, json_type: nil)
|
|
99
|
-
quote_value(value, elem_type:
|
|
99
|
+
quote_value(value, elem_type: elem_type, json_type: json_type)
|
|
100
100
|
end
|
|
101
101
|
|
|
102
102
|
# Quote values and concatenate them using ',' as separator
|
|
@@ -463,6 +463,9 @@ module PgConn
|
|
|
463
463
|
@pg_connection.close if @pg_connection && !@pg_connection.finished?
|
|
464
464
|
end
|
|
465
465
|
|
|
466
|
+
# TODO: Rename #closed?
|
|
467
|
+
def terminated?() = @pg_connection&.finished?
|
|
468
|
+
|
|
466
469
|
def self.new(*args, **opts, &block)
|
|
467
470
|
if block_given?
|
|
468
471
|
begin
|
|
@@ -882,9 +885,7 @@ module PgConn
|
|
|
882
885
|
# { database-column-name: object-method-name } # calls method on object
|
|
883
886
|
#
|
|
884
887
|
def insert(*args, upsert: nil, **opts)
|
|
885
|
-
#
|
|
886
|
-
|
|
887
|
-
# Add options to args except the special :upsert option
|
|
888
|
+
# Add options to args except the special :conflict option
|
|
888
889
|
args << opts if !opts.empty?
|
|
889
890
|
|
|
890
891
|
# Add schema (=nil) if absent
|
|
@@ -932,29 +933,42 @@ module PgConn
|
|
|
932
933
|
else
|
|
933
934
|
raise ArgumentError, "Illegal argument '#{data.inspect}'"
|
|
934
935
|
end
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
upsert_sql =
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
936
|
+
identifiers_sql = quote_identifiers(fields)
|
|
937
|
+
values_sql = quote_tuples(tuples)
|
|
938
|
+
upsert_sql = ""
|
|
939
|
+
|
|
940
|
+
# On upsert
|
|
941
|
+
if upsert
|
|
942
|
+
other_identifiers_sql = quote_identifiers(fields.map { "excluded.#{_1}" })
|
|
943
|
+
upsert_sql = %(
|
|
944
|
+
on conflict (#{quote_identifiers(upsert)}) do update
|
|
945
|
+
set (#{identifiers_sql}) = (#{other_identifiers_sql}))
|
|
946
|
+
end
|
|
945
947
|
|
|
946
948
|
# Execute SQL statement using either :value or :values depending on data arity
|
|
947
949
|
self.send method, <<~SQL
|
|
948
|
-
insert into #{table} (#{
|
|
949
|
-
values #{
|
|
950
|
+
insert into #{table} (#{identifiers_sql})
|
|
951
|
+
values #{values_sql}
|
|
950
952
|
#{upsert_sql}
|
|
951
953
|
returning id
|
|
952
954
|
SQL
|
|
953
955
|
end
|
|
954
956
|
|
|
955
|
-
# Use upsert
|
|
957
|
+
# Use upsert instead of insert and resolve conflicts by updating the target
|
|
958
|
+
# record. Called like #insert but with an additional key argument that is
|
|
959
|
+
# an array key to check for conflicts, default [:id]
|
|
960
|
+
#
|
|
961
|
+
# :call-seq:
|
|
962
|
+
# upsert(table, struct|structs|record|records, key)
|
|
963
|
+
# upsert(table, fields, struct|structs|record|records|tuples|values, key)
|
|
964
|
+
#
|
|
965
|
+
# upsert(schema, table, struct|structs|record|records, key)
|
|
966
|
+
# upsert(schema, table, fields, struct|structs|record|records|tuples|values, key)
|
|
967
|
+
#
|
|
956
968
|
def upsert(*args)
|
|
957
|
-
|
|
969
|
+
key = args.pop
|
|
970
|
+
key.is_a?(Array) && !key.empty? or raise ArgumentError, "Illegal value for key: #{key.inspect}"
|
|
971
|
+
insert(*args, upsert: key)
|
|
958
972
|
end
|
|
959
973
|
|
|
960
974
|
# Update record(s)
|
|
@@ -1004,19 +1018,17 @@ module PgConn
|
|
|
1004
1018
|
end
|
|
1005
1019
|
|
|
1006
1020
|
# Execute SQL statement(s) in a transaction and return the number of
|
|
1007
|
-
# affected records
|
|
1008
|
-
# already in progress. The +sql+ argument can be a
|
|
1009
|
-
# arbitrarily nested array of commands.
|
|
1010
|
-
#
|
|
1011
|
-
# is not.
|
|
1021
|
+
# selected or affected records in the last statement. Also sets #timestamp
|
|
1022
|
+
# unless a transaction is already in progress. The +sql+ argument can be a
|
|
1023
|
+
# command (String) or an arbitrarily nested array of commands. The empty
|
|
1024
|
+
# array is a NOP but the empty string is not.
|
|
1012
1025
|
#
|
|
1013
1026
|
# #exec pass Postgres exceptions to the caller unless :fail is false in
|
|
1014
|
-
# which case it returns nil if an error occurred
|
|
1015
|
-
#
|
|
1016
|
-
#
|
|
1017
|
-
#
|
|
1018
|
-
#
|
|
1019
|
-
# transaction stack has collapsed
|
|
1027
|
+
# which case it returns nil if an error occurred. Note that postgres
|
|
1028
|
+
# crashes the whole transaction stack if any error is met so if you're
|
|
1029
|
+
# inside a transaction, the transaction will be in an error state and if
|
|
1030
|
+
# you're also using subtransactions the whole transaction stack has
|
|
1031
|
+
# collapsed
|
|
1020
1032
|
#
|
|
1021
1033
|
# TODO: Make sure the transaction stack is emptied on postgres errors
|
|
1022
1034
|
def exec(sql, commit: true, fail: true, silent: self.silent)
|