Skip to content

Commit 596d3ad

Browse files
authored
Merge pull request diesel-rs#1027 from diesel-rs/sg-set-timezone-and-encoding
Explicitly set the time zone and client encoding at connection startup
2 parents ffe3221 + 7da4328 commit 596d3ad

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
2222
remove the `Nullable` portion (Unless you are using it with fields that are
2323
actually nullable)
2424

25+
* Connections will now explicitly set the session time zone to UTC when the
26+
connection is established
27+
2528
## [0.14.1] - 2017-07-10
2629

2730
### Changed

diesel/src/mysql/connection/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ impl MysqlConnection {
115115
}
116116

117117
fn set_config_options(&self) -> QueryResult<()> {
118-
try!(self.execute("SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT'))"));
118+
self.execute("SET sql_mode=(SELECT CONCAT(@@sql_mode, ',PIPES_AS_CONCAT'))")?;
119+
self.execute("SET time_zone = '+00:00';")?;
120+
self.execute("SET character_set_client = 'utf8mb4'")?;
121+
self.execute("SET character_set_connection = 'utf8mb4'")?;
122+
self.execute("SET character_set_results = 'utf8mb4'")?;
119123
Ok(())
120124
}
121125
}

diesel/src/pg/connection/mod.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use query_builder::*;
1414
use query_builder::bind_collector::RawBytesBindCollector;
1515
use query_source::Queryable;
1616
use result::*;
17+
use result::ConnectionError::CouldntSetupConfiguration;
1718
use self::cursor::Cursor;
1819
use self::raw::RawConnection;
1920
use self::result::PgResult;
@@ -48,12 +49,14 @@ impl Connection for PgConnection {
4849
type TransactionManager = AnsiTransactionManager;
4950

5051
fn establish(database_url: &str) -> ConnectionResult<PgConnection> {
51-
RawConnection::establish(database_url).map(|raw_conn| {
52-
PgConnection {
52+
RawConnection::establish(database_url).and_then(|raw_conn| {
53+
let conn = PgConnection {
5354
raw_connection: raw_conn,
5455
transaction_manager: AnsiTransactionManager::new(),
5556
statement_cache: StatementCache::new(),
56-
}
57+
};
58+
conn.set_config_options().map_err(CouldntSetupConfiguration)?;
59+
Ok(conn)
5760
})
5861
}
5962

@@ -129,6 +132,12 @@ impl PgConnection {
129132
let query = try!(Statement::prepare(&self.raw_connection, query, None, &[]));
130133
query.execute(&self.raw_connection, &Vec::new())
131134
}
135+
136+
fn set_config_options(&self) -> QueryResult<()> {
137+
self.execute("SET TIME ZONE 'UTC'")?;
138+
self.execute("SET CLIENT_ENCODING TO 'UTF8'")?;
139+
Ok(())
140+
}
132141
}
133142

134143
extern "C" fn noop_notice_processor(_: *mut libc::c_void, _message: *const libc::c_char) {

0 commit comments

Comments
 (0)