pg_conn 0.59.0 → 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 +38 -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
|
|
@@ -885,9 +885,7 @@ module PgConn
|
|
|
885
885
|
# { database-column-name: object-method-name } # calls method on object
|
|
886
886
|
#
|
|
887
887
|
def insert(*args, upsert: nil, **opts)
|
|
888
|
-
#
|
|
889
|
-
|
|
890
|
-
# Add options to args except the special :upsert option
|
|
888
|
+
# Add options to args except the special :conflict option
|
|
891
889
|
args << opts if !opts.empty?
|
|
892
890
|
|
|
893
891
|
# Add schema (=nil) if absent
|
|
@@ -935,29 +933,42 @@ module PgConn
|
|
|
935
933
|
else
|
|
936
934
|
raise ArgumentError, "Illegal argument '#{data.inspect}'"
|
|
937
935
|
end
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
upsert_sql =
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
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
|
|
948
947
|
|
|
949
948
|
# Execute SQL statement using either :value or :values depending on data arity
|
|
950
949
|
self.send method, <<~SQL
|
|
951
|
-
insert into #{table} (#{
|
|
952
|
-
values #{
|
|
950
|
+
insert into #{table} (#{identifiers_sql})
|
|
951
|
+
values #{values_sql}
|
|
953
952
|
#{upsert_sql}
|
|
954
953
|
returning id
|
|
955
954
|
SQL
|
|
956
955
|
end
|
|
957
956
|
|
|
958
|
-
# 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
|
+
#
|
|
959
968
|
def upsert(*args)
|
|
960
|
-
|
|
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)
|
|
961
972
|
end
|
|
962
973
|
|
|
963
974
|
# Update record(s)
|
|
@@ -1007,19 +1018,17 @@ module PgConn
|
|
|
1007
1018
|
end
|
|
1008
1019
|
|
|
1009
1020
|
# Execute SQL statement(s) in a transaction and return the number of
|
|
1010
|
-
# affected records
|
|
1011
|
-
# already in progress. The +sql+ argument can be a
|
|
1012
|
-
# arbitrarily nested array of commands.
|
|
1013
|
-
#
|
|
1014
|
-
# 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.
|
|
1015
1025
|
#
|
|
1016
1026
|
# #exec pass Postgres exceptions to the caller unless :fail is false in
|
|
1017
|
-
# which case it returns nil if an error occurred
|
|
1018
|
-
#
|
|
1019
|
-
#
|
|
1020
|
-
#
|
|
1021
|
-
#
|
|
1022
|
-
# 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
|
|
1023
1032
|
#
|
|
1024
1033
|
# TODO: Make sure the transaction stack is emptied on postgres errors
|
|
1025
1034
|
def exec(sql, commit: true, fail: true, silent: self.silent)
|