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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 204745e57b411c35071db75b18bb3acfa186fb04cb86579e9618a816ef0f0133
4
- data.tar.gz: 5a51db5597afebe8e3ba89be2448d78f1e114ae9e587b206ab0600a7c6e1fdf2
3
+ metadata.gz: 8dd70cb7a067886566b8ae751bb319115450ed3ebc82d3420c74c7f77e5f72ce
4
+ data.tar.gz: 53393a8ef217ed7502b78d5d7f2f1cb092cc5adbe8e7da30f1cac901a262d31c
5
5
  SHA512:
6
- metadata.gz: 24b6af4c0443e9aa198f48f409ceeef23412bf348853f96f1b58eb940534446b3b9a22d0e72c500b08b6da0d0a72d6627d4a268075c764ef8fcdd4c02eb7ff64
7
- data.tar.gz: 147825fd829abaea034fa436c2c8fa47551204d05d6062bc8ec9f6e5b5f3195de4e8f51cec328915a421667e1c82a0b38011a7433bd77e6d758eeffa90aa1b2d
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.58.1"
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
@@ -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
- # Normalize arguments
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
- # On-conflict clause
937
- upsert_sql =
938
- case upsert
939
- when true; "on conflict do nothing"
940
- when String; "on conlict #{upsert}"
941
- when false, nil; ""
942
- else
943
- raise ArgumentError, "Illegal value for :upsert option: #{upsert.inspect}"
944
- 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
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} (#{quote_identifiers(fields)})
949
- values #{quote_tuples(tuples)}
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. 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
+ #
956
968
  def upsert(*args)
957
- 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)
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 (if any). Also sets #timestamp unless a transaction is
1008
- # already in progress. The +sql+ argument can be a command (String) or an
1009
- # arbitrarily nested array of commands. Note that you can't have commands
1010
- # that span multiple lines. The empty array is a NOP but the empty string
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
- # Note that postgres crashes the whole transaction stack if any error is
1017
- # met so if you're inside a transaction, the transaction will be in an
1018
- # error state and if you're also using subtransactions the whole
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)
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.58.1
4
+ version: 0.59.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen