-
Notifications
You must be signed in to change notification settings - Fork 7
Mysql integration in Canyon #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
8e9a190
mysql-support start with docker configuration to up container and loa…
0nSystem adb63dc
mysql-support - first implementation workspace canyon_connection
0nSystem f99f3b3
mysql - change module bounds pending DateTime<OffSet> look problem an…
0nSystem e067220
mysql-support canyon macros, pending trait implement FromValue in &st…
0nSystem e53d7c6
mysql-support count table implemented
0nSystem bf21ca7
mysql-support - pending implement select test
0nSystem 904e554
mysql-support craete reorder array params to use with mysql params
f3376fc
mysql-support resolve quotes in queries and innecesary loop in reorde…
b1da9bb
mysql-support select, update , and query_builder
b3164c6
mysql-support correction test insert and delete mysql implementation …
0nSystem 39bfa88
develop remove imports not use
0nSystem 9ce698c
mysql-support added integration to returning primary key
0nSystem 9d46976
mysql-support correction cfg features
0nSystem 7245b75
mysql-support remove comment to mysql implementation in canyon_databa…
0nSystem 8b3ecd6
mysql-support canyon_macro.rs added in feature migrations
0nSystem 335fe0d
mysql-support replace error with feature in canyon_macro and separate…
0nSystem 2e9edb2
changes: remove CanyonRowsMysql and implement mysql_async, repair ini…
0nSystem 1715b18
changes: corrections other test
0nSystem 0871b40
changes: insert and multiinsert match_rows
0nSystem 625672a
changes: lib cargo_macros RowMapper
0nSystem 195bd77
changes: select macro remove else if to generate tokenstream with act…
0nSystem 7647373
changes: resolve clippy error format! in expect to create Regex
0nSystem 8631628
changes: resolve clippy error format! in expect to create Regex
0nSystem 144e9df
changes: implement Operator by datasource type
0nSystem 9e4c91d
changes: added default datasources
0nSystem c51a00e
changes: correction error feature specify in datasources.rs enum Auth
0nSystem 5c09a3f
changes: correction cargo fmt
0nSystem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,4 +5,5 @@ Cargo.lock | |
| canyon_tester/ | ||
| macro_utils.rs | ||
| .vscode/ | ||
| postgres-data/ | ||
| postgres-data/ | ||
| mysql-data/ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,12 +2,14 @@ use serde::Deserialize; | |
|
|
||
| #[cfg(feature = "mssql")] | ||
| use async_std::net::TcpStream; | ||
| #[cfg(feature = "mysql")] | ||
| use mysql_async::Pool; | ||
| #[cfg(feature = "mssql")] | ||
| use tiberius::{AuthMethod, Config}; | ||
| #[cfg(feature = "postgres")] | ||
| use tokio_postgres::{Client, NoTls}; | ||
|
|
||
| use crate::datasources::DatasourceConfig; | ||
| use crate::datasources::{Auth, DatasourceConfig}; | ||
|
|
||
| /// Represents the current supported databases by Canyon | ||
| #[derive(Deserialize, Debug, Eq, PartialEq, Clone, Copy)] | ||
|
|
@@ -18,6 +20,19 @@ pub enum DatabaseType { | |
| #[serde(alias = "sqlserver", alias = "mssql")] | ||
| #[cfg(feature = "mssql")] | ||
| SqlServer, | ||
| #[serde(alias = "mysql")] | ||
| #[cfg(feature = "mysql")] | ||
| MySQL, | ||
| } | ||
|
|
||
| impl From<&Auth> for DatabaseType { | ||
| fn from(value: &Auth) -> Self { | ||
| match value { | ||
| crate::datasources::Auth::Postgres(_) => DatabaseType::PostgreSql, | ||
| crate::datasources::Auth::SqlServer(_) => DatabaseType::SqlServer, | ||
| crate::datasources::Auth::MySQL(_) => DatabaseType::MySQL, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// A connection with a `PostgreSQL` database | ||
|
|
@@ -33,6 +48,12 @@ pub struct SqlServerConnection { | |
| pub client: &'static mut tiberius::Client<TcpStream>, | ||
| } | ||
|
|
||
| /// A connection with a `Mysql` database | ||
| #[cfg(feature = "mysql")] | ||
| pub struct MysqlConnection { | ||
| pub client: Pool, | ||
| } | ||
|
|
||
| /// The Canyon database connection handler. When the client's program | ||
| /// starts, Canyon gets the information about the desired datasources, | ||
| /// process them and generates a pool of 1 to 1 database connection for | ||
|
|
@@ -42,6 +63,8 @@ pub enum DatabaseConnection { | |
| Postgres(PostgreSqlConnection), | ||
| #[cfg(feature = "mssql")] | ||
| SqlServer(SqlServerConnection), | ||
| #[cfg(feature = "mysql")] | ||
| MySQL(MysqlConnection), | ||
| } | ||
|
|
||
| unsafe impl Send for DatabaseConnection {} | ||
|
|
@@ -64,6 +87,10 @@ impl DatabaseConnection { | |
| crate::datasources::Auth::SqlServer(_) => { | ||
| panic!("Found SqlServer auth configuration for a PostgreSQL datasource") | ||
| } | ||
| #[cfg(feature = "mysql")] | ||
| crate::datasources::Auth::MySQL(_) => { | ||
| panic!("Found MySql auth configuration for a PostgreSQL datasource") | ||
| } | ||
| }; | ||
| let (new_client, new_connection) = tokio_postgres::connect( | ||
| &format!( | ||
|
|
@@ -109,6 +136,10 @@ impl DatabaseConnection { | |
| } | ||
| crate::datasources::SqlServerAuth::Integrated => AuthMethod::Integrated, | ||
| }, | ||
| #[cfg(feature = "mysql")] | ||
| crate::datasources::Auth::MySQL(_) => { | ||
| panic!("Found PostgreSQL auth configuration for a SqlServer database") | ||
| } | ||
| }); | ||
|
|
||
| // on production, it is not a good idea to do this. We should upgrade | ||
|
|
@@ -136,14 +167,49 @@ impl DatabaseConnection { | |
| )), | ||
| })) | ||
| } | ||
| #[cfg(feature = "mysql")] | ||
| DatabaseType::MySQL => { | ||
| let (user, password) = match &datasource.auth { | ||
| #[cfg(feature = "mssql")] | ||
| crate::datasources::Auth::SqlServer(_) => { | ||
| panic!("Found SqlServer auth configuration for a PostgreSQL datasource") | ||
| } | ||
| #[cfg(feature = "postgres")] | ||
| crate::datasources::Auth::Postgres(_) => { | ||
| panic!("Found MySql auth configuration for a PostgreSQL datasource") | ||
| } | ||
| #[cfg(feature = "mysql")] | ||
| crate::datasources::Auth::MySQL(mysql_auth) => match mysql_auth { | ||
| crate::datasources::MySQLAuth::Basic { username, password } => { | ||
| (username, password) | ||
| } | ||
| }, | ||
| }; | ||
|
|
||
| //TODO add options to optionals params in url | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. options to optionals? |
||
|
|
||
| let url = format!( | ||
| "mysql://{}:{}@{}:{}/{}", | ||
| user, | ||
| password, | ||
| datasource.properties.host, | ||
| datasource.properties.port.unwrap_or_default(), | ||
| datasource.properties.db_name | ||
| ); | ||
| let mysql_connection = Pool::from_url(url)?; | ||
|
|
||
| Ok(DatabaseConnection::MySQL(MysqlConnection { | ||
| client: { mysql_connection }, | ||
| })) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "postgres")] | ||
| pub fn postgres_connection(&self) -> &PostgreSqlConnection { | ||
| match self { | ||
| DatabaseConnection::Postgres(conn) => conn, | ||
| #[cfg(all(feature = "postgres", feature = "mssql"))] | ||
| #[cfg(all(feature = "postgres", feature = "mssql", feature = "mysql"))] | ||
| _ => panic!(), | ||
| } | ||
| } | ||
|
|
@@ -152,7 +218,16 @@ impl DatabaseConnection { | |
| pub fn sqlserver_connection(&mut self) -> &mut SqlServerConnection { | ||
| match self { | ||
| DatabaseConnection::SqlServer(conn) => conn, | ||
| #[cfg(all(feature = "postgres", feature = "mssql"))] | ||
| #[cfg(all(feature = "postgres", feature = "mssql", feature = "mysql"))] | ||
| _ => panic!(), | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "mysql")] | ||
| pub fn mysql_connection(&self) -> &MysqlConnection { | ||
| match self { | ||
| DatabaseConnection::MySQL(conn) => conn, | ||
| #[cfg(all(feature = "postgres", feature = "mssql", feature = "mysql"))] | ||
| _ => panic!(), | ||
| } | ||
| } | ||
|
|
@@ -166,13 +241,14 @@ mod database_connection_handler { | |
| /// Tests the behaviour of the `DatabaseType::from_datasource(...)` | ||
| #[test] | ||
| fn check_from_datasource() { | ||
| #[cfg(all(feature = "postgres", feature = "mssql"))] | ||
| #[cfg(all(feature = "postgres", feature = "mssql", feature = "mysql"))] | ||
| { | ||
| const CONFIG_FILE_MOCK_ALT_ALL: &str = r#" | ||
| [canyon_sql] | ||
| datasources = [ | ||
| {name = 'PostgresDS', auth = { postgresql = { basic = { username = "postgres", password = "postgres" } } }, properties.host = 'localhost', properties.db_name = 'triforce', properties.migrations='enabled' }, | ||
| {name = 'SqlServerDS', auth = { sqlserver = { basic = { username = "sa", password = "SqlServer-10" } } }, properties.host = '192.168.0.250.1', properties.port = 3340, properties.db_name = 'triforce2', properties.migrations='disabled' } | ||
| {name = 'SqlServerDS', auth = { sqlserver = { basic = { username = "sa", password = "SqlServer-10" } } }, properties.host = '192.168.0.250.1', properties.port = 3340, properties.db_name = 'triforce2', properties.migrations='disabled' }, | ||
| {name = 'MysqlDS', auth = { mysql = { basic = { username = "root", password = "root" } } }, properties.host = '192.168.0.250.1', properties.port = 3340, properties.db_name = 'triforce2', properties.migrations='disabled' } | ||
| ] | ||
| "#; | ||
| let config: CanyonSqlConfig = toml::from_str(CONFIG_FILE_MOCK_ALT_ALL) | ||
|
|
@@ -185,6 +261,10 @@ mod database_connection_handler { | |
| config.canyon_sql.datasources[1].get_db_type(), | ||
| DatabaseType::SqlServer | ||
| ); | ||
| assert_eq!( | ||
| config.canyon_sql.datasources[2].get_db_type(), | ||
| DatabaseType::MySQL | ||
| ); | ||
| } | ||
|
|
||
| #[cfg(feature = "postgres")] | ||
|
|
@@ -218,5 +298,22 @@ mod database_connection_handler { | |
| DatabaseType::SqlServer | ||
| ); | ||
| } | ||
|
|
||
| #[cfg(feature = "mysql")] | ||
| { | ||
| const CONFIG_FILE_MOCK_ALT_MYSQL: &str = r#" | ||
| [canyon_sql] | ||
| datasources = [ | ||
| {name = 'MysqlDS', auth = { mysql = { basic = { username = "root", password = "root" } } }, properties.host = '192.168.0.250.1', properties.port = 3340, properties.db_name = 'triforce2', properties.migrations='disabled' } | ||
| ] | ||
| "#; | ||
|
|
||
| let config: CanyonSqlConfig = toml::from_str(CONFIG_FILE_MOCK_ALT_MYSQL) | ||
| .expect("A failure happened retrieving the [canyon_sql] section"); | ||
| assert_eq!( | ||
| config.canyon_sql.datasources[0].get_db_type(), | ||
| DatabaseType::MySQL | ||
| ); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure that we need the two dependencies? Why do we need
mysql_common? I guess that you've said because the format of the rows returned by the library, but we need to review if there's an approach that only need to rely on having the async crate