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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 27b96a8da5a5f1910e4ab2f84a6d78d81de21771dff4a425ea7e9ac239409ea0
4
- data.tar.gz: 24a733484ea400d8f91543538a1792d2e133cdfb5af0d46e2ffb232cea3cb6ce
3
+ metadata.gz: 8dd70cb7a067886566b8ae751bb319115450ed3ebc82d3420c74c7f77e5f72ce
4
+ data.tar.gz: 53393a8ef217ed7502b78d5d7f2f1cb092cc5adbe8e7da30f1cac901a262d31c
5
5
  SHA512:
6
- metadata.gz: e03a1bd04679bfef0249fa79b4d040c0125604222bc0e598f3c1dbd80ba46e652d63284ca8663a7d324ecded894b7b5f7d0f913ff52e5e3f9631f4ac3a21997d
7
- data.tar.gz: 4c56396c12d531ee5a59e8f275fc3b8abc32cf53e761c148012ba380081b124fab9b33e6dceeb10f48c922f52454eb6206f4f61ab799e27342f8bb42a1e43f26
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!(schema, exclude: [])
49
+ # Empty all tables in the given schema(s)
50
+ def clean!(*schemas, exclude: [])
51
51
  conn.session.triggers(false) {
52
- self.list_tables(schema, exclude: exclude).each { |table|
53
- conn.execute "delete from #{schema}.#{table}"
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
@@ -1,3 +1,3 @@
1
1
  module PgConn
2
- VERSION = "0.59.0"
2
+ VERSION = "0.59.2"
3
3
  end
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: nil, json_type: nil)
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
- # Normalize arguments
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
- # On-conflict clause
940
- upsert_sql =
941
- case upsert
942
- when true; "on conflict do nothing"
943
- when String; "on conlict #{upsert}"
944
- when false, nil; ""
945
- else
946
- raise ArgumentError, "Illegal value for :upsert option: #{upsert.inspect}"
947
- end
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} (#{quote_identifiers(fields)})
952
- values #{quote_tuples(tuples)}
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. Currently only 'on conflict do nothing' is supported
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
- insert(*args, upsert: true)
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 (if any). Also sets #timestamp unless a transaction is
1011
- # already in progress. The +sql+ argument can be a command (String) or an
1012
- # arbitrarily nested array of commands. Note that you can't have commands
1013
- # that span multiple lines. The empty array is a NOP but the empty string
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
- # Note that postgres crashes the whole transaction stack if any error is
1020
- # met so if you're inside a transaction, the transaction will be in an
1021
- # error state and if you're also using subtransactions the whole
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)
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.59.0
4
+ version: 0.59.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen