@@ -11,8 +11,8 @@ use tokio_postgres::{types::ToSql, Client, NoTls, Row, SimpleQueryMessage};
1111use tracing_subscriber:: { filter:: Directive , EnvFilter , FmtSubscriber } ;
1212
1313pub const PROXY : u16 = 6432 ;
14- pub const PG_LATEST : u16 = 5532 ;
15- pub const PG_V17_TLS : u16 = 5617 ;
14+ pub const PG_PORT : u16 = 5532 ;
15+ pub const PG_TLS_PORT : u16 = 5617 ;
1616
1717pub const TEST_SCHEMA_SQL : & str = include_str ! ( concat!( "../../../tests/sql/schema.sql" ) ) ;
1818
@@ -52,7 +52,7 @@ pub async fn clear() {
5252pub async fn reset_schema ( ) {
5353 let port = std:: env:: var ( "CS_DATABASE__PORT" )
5454 . map ( |s| s. parse ( ) . unwrap ( ) )
55- . unwrap_or ( PG_LATEST ) ;
55+ . unwrap_or ( PG_PORT ) ;
5656
5757 let client = connect_with_tls ( port) . await ;
5858 client. simple_query ( TEST_SCHEMA_SQL ) . await . unwrap ( ) ;
@@ -61,7 +61,7 @@ pub async fn reset_schema() {
6161pub async fn reset_schema_to ( schema : & ' static str ) {
6262 let port = std:: env:: var ( "CS_DATABASE__PORT" )
6363 . map ( |s| s. parse ( ) . unwrap ( ) )
64- . unwrap_or ( PG_LATEST ) ;
64+ . unwrap_or ( PG_PORT ) ;
6565
6666 let client = connect_with_tls ( port) . await ;
6767 client. simple_query ( schema) . await . unwrap ( ) ;
@@ -81,7 +81,7 @@ pub async fn table_exists(table: &str) -> bool {
8181
8282 let port = std:: env:: var ( "CS_DATABASE__PORT" )
8383 . map ( |s| s. parse ( ) . unwrap ( ) )
84- . unwrap_or ( PG_LATEST ) ;
84+ . unwrap_or ( PG_PORT ) ;
8585
8686 let client = connect_with_tls ( port) . await ;
8787 let messages = client. simple_query ( & query) . await . unwrap ( ) ;
@@ -116,7 +116,7 @@ pub fn trace() {
116116pub fn connection_config ( port : u16 ) -> tokio_postgres:: Config {
117117 let mut db_config = tokio_postgres:: Config :: new ( ) ;
118118
119- let host = " localhost". to_string ( ) ;
119+ let host = std :: env :: var ( "CS_DATABASE__HOST" ) . unwrap_or_else ( |_| " localhost". to_string ( ) ) ;
120120 let name = "cipherstash" . to_string ( ) ;
121121 let username = "cipherstash" . to_string ( ) ;
122122 let password = "p@ssword" . to_string ( ) ;
@@ -209,22 +209,34 @@ where
209209 rows. iter ( ) . map ( |row| row. get ( 0 ) ) . collect :: < Vec < T > > ( )
210210}
211211
212+ /// Get database port from environment or use default.
213+ fn get_database_port ( ) -> u16 {
214+ std:: env:: var ( "CS_DATABASE__PORT" )
215+ . ok ( )
216+ . and_then ( |s| s. parse ( ) . ok ( ) )
217+ . unwrap_or ( PG_PORT )
218+ }
219+
212220/// Query directly from PostgreSQL, bypassing the proxy.
213- /// Used for sanity check tests to verify data is actually encrypted .
221+ /// Uses CS_DATABASE__HOST and CS_DATABASE__PORT to ensure same DB as proxy .
214222pub async fn query_direct < T > ( sql : & str ) -> Vec < T >
215223where
216224 T : for < ' a > tokio_postgres:: types:: FromSql < ' a > ,
217225{
218- let client = connect_with_tls ( PG_LATEST ) . await ;
226+ let port = get_database_port ( ) ;
227+ let client = connect_with_tls ( port) . await ;
219228 let rows = client. query ( sql, & [ ] ) . await . unwrap ( ) ;
220229 rows. iter ( ) . map ( |row| row. get ( 0 ) ) . collect ( )
221230}
222231
232+ /// Query directly from PostgreSQL with a parameter, bypassing the proxy.
233+ /// Uses CS_DATABASE__HOST and CS_DATABASE__PORT to ensure same DB as proxy.
223234pub async fn query_direct_by < T > ( sql : & str , param : & ( dyn ToSql + Sync ) ) -> Vec < T >
224235where
225236 T : for < ' a > tokio_postgres:: types:: FromSql < ' a > ,
226237{
227- let client = connect_with_tls ( PG_LATEST ) . await ;
238+ let port = get_database_port ( ) ;
239+ let client = connect_with_tls ( port) . await ;
228240 let rows = client. query ( sql, & [ param] ) . await . unwrap ( ) ;
229241 rows. iter ( ) . map ( |row| row. get ( 0 ) ) . collect ( )
230242}
0 commit comments