Skip to content

Commit a54ee29

Browse files
committed
Allow opt-out of transaction settings
If you changed the default transaction settings of the session, you may need an explicit opt-out.
1 parent f1ac3bd commit a54ee29

File tree

3 files changed

+32
-22
lines changed

3 files changed

+32
-22
lines changed

postgres/src/transaction_builder.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,19 @@ impl<'a> TransactionBuilder<'a> {
2121
self
2222
}
2323

24-
/// Sets the transaction to read-only.
25-
pub fn read_only(mut self) -> Self {
26-
self.builder = self.builder.read_only();
24+
/// Sets the access mode of the transaction.
25+
pub fn read_only(mut self, read_only: bool) -> Self {
26+
self.builder = self.builder.read_only(read_only);
2727
self
2828
}
2929

30-
/// Sets the transaction to be deferrable.
30+
/// Sets the deferrability of the transaction.
3131
///
3232
/// If the transaction is also serializable and read only, creation of the transaction may block, but when it
3333
/// completes the transaction is able to run with less overhead and a guarantee that it will not be aborted due to
3434
/// serialization failure.
35-
pub fn deferrable(mut self) -> Self {
36-
self.builder = self.builder.deferrable();
35+
pub fn deferrable(mut self, deferrable: bool) -> Self {
36+
self.builder = self.builder.deferrable(deferrable);
3737
self
3838
}
3939

tokio-postgres/src/transaction_builder.rs

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ pub enum IsolationLevel {
2323
pub struct TransactionBuilder<'a> {
2424
client: &'a mut Client,
2525
isolation_level: Option<IsolationLevel>,
26-
read_only: bool,
27-
deferrable: bool,
26+
read_only: Option<bool>,
27+
deferrable: Option<bool>,
2828
}
2929

3030
impl<'a> TransactionBuilder<'a> {
3131
pub(crate) fn new(client: &'a mut Client) -> TransactionBuilder<'a> {
3232
TransactionBuilder {
3333
client,
3434
isolation_level: None,
35-
read_only: false,
36-
deferrable: false,
35+
read_only: None,
36+
deferrable: None,
3737
}
3838
}
3939

@@ -43,19 +43,19 @@ impl<'a> TransactionBuilder<'a> {
4343
self
4444
}
4545

46-
/// Sets the transaction to read-only.
47-
pub fn read_only(mut self) -> Self {
48-
self.read_only = true;
46+
/// Sets the access mode of the transaction.
47+
pub fn read_only(mut self, read_only: bool) -> Self {
48+
self.read_only = Some(read_only);
4949
self
5050
}
5151

52-
/// Sets the transaction to be deferrable.
52+
/// Sets the deferrability of the transaction.
5353
///
5454
/// If the transaction is also serializable and read only, creation of the transaction may block, but when it
5555
/// completes the transaction is able to run with less overhead and a guarantee that it will not be aborted due to
5656
/// serialization failure.
57-
pub fn deferrable(mut self) -> Self {
58-
self.deferrable = true;
57+
pub fn deferrable(mut self, deferrable: bool) -> Self {
58+
self.deferrable = Some(deferrable);
5959
self
6060
}
6161

@@ -79,21 +79,31 @@ impl<'a> TransactionBuilder<'a> {
7979
query.push_str(level);
8080
}
8181

82-
if self.read_only {
82+
if let Some(read_only) = self.read_only {
8383
if !first {
8484
query.push(',');
8585
}
8686
first = false;
8787

88-
query.push_str(" READ ONLY");
88+
let s = if read_only {
89+
" READ ONLY"
90+
} else {
91+
" READ WRITE"
92+
};
93+
query.push_str(s);
8994
}
9095

91-
if self.deferrable {
96+
if let Some(deferrable) = self.deferrable {
9297
if !first {
9398
query.push(',');
9499
}
95100

96-
query.push_str(" DEFERRABLE");
101+
let s = if deferrable {
102+
" DEFERRABLE"
103+
} else {
104+
" NOT DEFERRABLE"
105+
};
106+
query.push_str(s);
97107
}
98108

99109
self.client.batch_execute(&query).await?;

tokio-postgres/tests/test/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,8 @@ async fn transaction_builder() {
417417
let transaction = client
418418
.build_transaction()
419419
.isolation_level(IsolationLevel::Serializable)
420-
.read_only()
421-
.deferrable()
420+
.read_only(true)
421+
.deferrable(true)
422422
.start()
423423
.await
424424
.unwrap();

0 commit comments

Comments
 (0)