@uniflowed/sql 0.20.0 → 0.21.0

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.
Files changed (2) hide show
  1. package/index.js +33 -2
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -735,8 +735,25 @@ async function scoped<T>(
735
735
  }
736
736
  throw error;
737
737
  }
738
+ // A failed COMMIT leaves the transaction open. Roll it back before the
739
+ // error leaves, or the next statement runs inside a transaction this
740
+ // handle has already forgotten. A nested RELEASE fails the same way;
741
+ // `ROLLBACK TO` then `RELEASE` is the body-error path.
738
742
  open = false;
739
- await connection.run(commit, [], "exec");
743
+ try {
744
+ await connection.run(commit, [], "exec");
745
+ } catch (error) {
746
+ try {
747
+ await connection.run(rollback, [], "exec");
748
+ if (depth > 0) {
749
+ await connection.run(commit, [], "exec");
750
+ }
751
+ } catch {
752
+ // The rollback failed too — usually because the connection is gone.
753
+ // The error worth reporting is the commit's.
754
+ }
755
+ throw error;
756
+ }
740
757
  return value;
741
758
  }
742
759
 
@@ -805,8 +822,22 @@ function scopedSync<T>(
805
822
  }
806
823
  throw error;
807
824
  }
825
+ // Same as `scoped`: a failed COMMIT or RELEASE must not clear the
826
+ // transaction while the database still holds it.
808
827
  open = false;
809
- runSync(commit, [], "exec");
828
+ try {
829
+ runSync(commit, [], "exec");
830
+ } catch (error) {
831
+ try {
832
+ runSync(rollback, [], "exec");
833
+ if (depth > 0) {
834
+ runSync(commit, [], "exec");
835
+ }
836
+ } catch {
837
+ // The rollback failed too. The error worth reporting is the commit's.
838
+ }
839
+ throw error;
840
+ }
810
841
  return value;
811
842
  }
812
843
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uniflowed/sql",
3
- "version": "0.20.0",
3
+ "version": "0.21.0",
4
4
  "description": "The runtime for sqlc's Flow target: exact codecs and driver adapters, part of the Unified Toolchain for Flow.",
5
5
  "type": "module",
6
6
  "license": "MIT",