Skip to content

Commit 6a01730

Browse files
committed
Added ReadOnly session attr
1 parent a9ca481 commit 6a01730

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

tokio-postgres/src/connect.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ where
160160
let has_hostname = hostname.is_some();
161161
let (mut client, mut connection) = connect_raw(socket, tls, has_hostname, config).await?;
162162

163-
if let TargetSessionAttrs::ReadWrite = config.target_session_attrs {
163+
if config.target_session_attrs != TargetSessionAttrs::Any {
164164
let rows = client.simple_query_raw("SHOW transaction_read_only");
165165
pin_mut!(rows);
166166

@@ -185,11 +185,21 @@ where
185185

186186
match next.await.transpose()? {
187187
Some(SimpleQueryMessage::Row(row)) => {
188-
if row.try_get(0)? == Some("on") {
188+
let read_only_result = row.try_get(0)?;
189+
if read_only_result == Some("on")
190+
&& config.target_session_attrs == TargetSessionAttrs::ReadWrite
191+
{
189192
return Err(Error::connect(io::Error::new(
190193
io::ErrorKind::PermissionDenied,
191194
"database does not allow writes",
192195
)));
196+
} else if read_only_result == Some("off")
197+
&& config.target_session_attrs == TargetSessionAttrs::ReadOnly
198+
{
199+
return Err(Error::connect(io::Error::new(
200+
io::ErrorKind::PermissionDenied,
201+
"database is not read only",
202+
)));
193203
} else {
194204
break;
195205
}

0 commit comments

Comments
 (0)