Skip to content

Commit fa7f2af

Browse files
committed
add db type
this will be used to execute the correct sql for migrations.
1 parent ae3b024 commit fa7f2af

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

sqlx-core/src/aurora/connection/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::aurora::options::AuroraConnectOptions;
1+
use crate::aurora::options::{AuroraConnectOptions, AuroraDbType};
22
use crate::aurora::statement::AuroraStatementMetadata;
33
use crate::aurora::Aurora;
44
use crate::common::StatementCache;
@@ -17,6 +17,7 @@ mod executor;
1717

1818
/// A connection to an Aurora database.
1919
pub struct AuroraConnection {
20+
pub(crate) db_type: AuroraDbType,
2021
pub(crate) resource_arn: String,
2122
pub(crate) secret_arn: String,
2223
pub(crate) database: Option<String>,
@@ -35,6 +36,10 @@ pub struct AuroraConnection {
3536

3637
impl AuroraConnection {
3738
pub(crate) fn new(options: &AuroraConnectOptions) -> Result<Self, Error> {
39+
let db_type = options
40+
.db_type
41+
.ok_or_else(|| Error::Configuration("db type not specified".into()))?;
42+
3843
let region = options.region.parse().map_err(Error::config)?;
3944

4045
let resource_arn = options
@@ -51,6 +56,7 @@ impl AuroraConnection {
5156
let client = RdsDataClient::new(region);
5257

5358
Ok(Self {
59+
db_type,
5460
resource_arn,
5561
secret_arn,
5662
database: options.database.clone(),

sqlx-core/src/aurora/options/mod.rs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,39 @@
1+
use crate::connection::LogSettings;
2+
use crate::error::Error;
3+
14
use std::env::var;
5+
use std::str::FromStr;
26

37
mod connect;
48
mod parse;
5-
use crate::connection::LogSettings;
9+
10+
#[derive(Debug, Clone, Copy)]
11+
pub enum AuroraDbType {
12+
MySQL,
13+
Postgres,
14+
}
15+
16+
impl FromStr for AuroraDbType {
17+
type Err = Error;
18+
19+
fn from_str(s: &str) -> Result<Self, Self::Err> {
20+
let t = match s {
21+
"mysql" => AuroraDbType::MySQL,
22+
"postgres" => AuroraDbType::Postgres,
23+
_ => {
24+
return Err(Error::Configuration(
25+
"db type must be `postgres` or `mysql`".into(),
26+
))
27+
}
28+
};
29+
30+
Ok(t)
31+
}
32+
}
633

734
#[derive(Debug, Clone)]
835
pub struct AuroraConnectOptions {
36+
pub(crate) db_type: Option<AuroraDbType>,
937
pub(crate) region: String,
1038
pub(crate) resource_arn: Option<String>,
1139
pub(crate) secret_arn: Option<String>,
@@ -23,6 +51,15 @@ impl Default for AuroraConnectOptions {
2351

2452
impl AuroraConnectOptions {
2553
pub fn new() -> Self {
54+
let db_type = match var("AURORA_DB_TYPE").map(|s| s.parse()) {
55+
Ok(Ok(t)) => Some(t),
56+
Ok(Err(e)) => {
57+
log::error!("{}", e);
58+
None
59+
}
60+
Err(_) => None,
61+
};
62+
2663
let region = var("AURORA_REGION")
2764
.ok()
2865
.unwrap_or_else(|| "us-east-1".to_owned());
@@ -31,6 +68,7 @@ impl AuroraConnectOptions {
3168
let secret_arn = var("AURORA_SECRET_ARN").ok();
3269

3370
AuroraConnectOptions {
71+
db_type,
3472
region,
3573
resource_arn,
3674
secret_arn,
@@ -41,6 +79,11 @@ impl AuroraConnectOptions {
4179
}
4280
}
4381

82+
pub fn db_type(mut self, db_type: AuroraDbType) -> Self {
83+
self.db_type = Some(db_type);
84+
self
85+
}
86+
4487
pub fn region(mut self, region: &str) -> Self {
4588
self.region = region.to_owned();
4689
self

sqlx-core/src/aurora/options/parse.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ impl FromStr for AuroraConnectOptions {
1717
options =
1818
options.statement_cache_capacity(value.parse().map_err(Error::config)?);
1919
}
20+
"db-type" => {
21+
options = options.db_type(value.parse()?);
22+
}
2023
"region" => {
2124
options = options.region(&*value);
2225
}

0 commit comments

Comments
 (0)