Skip to content

Commit

Permalink
#feature - Removed the db_type field from the options of the configur…
Browse files Browse the repository at this point in the history
…ation file. The in-use per datasource database type will be inferred from the auth key
  • Loading branch information
TheRustifyer committed Apr 10, 2023
1 parent 8835935 commit f9bcf68
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 18 deletions.
10 changes: 5 additions & 5 deletions canyon_connection/src/canyon_database_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl DatabaseConnection {
pub async fn new(
datasource: &DatasourceConfig,
) -> Result<DatabaseConnection, Box<(dyn std::error::Error + Send + Sync + 'static)>> {
match datasource.db_type {
match datasource.get_db_type() {
DatabaseType::PostgreSql => {
let (username, password) = match &datasource.auth {
crate::datasources::Auth::Postgres(postgres_auth) => match postgres_auth {
Expand Down Expand Up @@ -152,8 +152,8 @@ mod database_connection_handler {
const CONFIG_FILE_MOCK_ALT: &str = r#"
[canyon_sql]
datasources = [
{name = 'PostgresDS', db_type = 'postgresql', auth = { postgresql = { basic = { username = "postgres", password = "postgres" } } }, properties.host = 'localhost', properties.db_name = 'triforce', properties.migrations='enabled' },
{name = 'SqlServerDS', db_type = 'sqlserver', 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 = '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' }
]
"#;

Expand All @@ -164,11 +164,11 @@ mod database_connection_handler {
.expect("A failure happened retrieving the [canyon_sql] section");

assert_eq!(
config.canyon_sql.datasources[0].db_type,
config.canyon_sql.datasources[0].get_db_type(),
DatabaseType::PostgreSql
);
assert_eq!(
config.canyon_sql.datasources[1].db_type,
config.canyon_sql.datasources[1].get_db_type(),
DatabaseType::SqlServer
);
}
Expand Down
18 changes: 13 additions & 5 deletions canyon_connection/src/datasources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ fn load_ds_config_from_array() {
const CONFIG_FILE_MOCK_ALT: &str = r#"
[canyon_sql]
datasources = [
{name = 'PostgresDS', db_type = 'postgresql', auth = { postgresql = { basic = { username = "postgres", password = "postgres" } } }, properties.host = 'localhost', properties.db_name = 'triforce', properties.migrations='enabled' },
{name = 'SqlServerDS', db_type = 'sqlserver', 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 = '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' }
]
"#;

Expand All @@ -20,7 +20,7 @@ fn load_ds_config_from_array() {
let ds_1 = &config.canyon_sql.datasources[1];

assert_eq!(ds_0.name, "PostgresDS");
assert_eq!(ds_0.db_type, DatabaseType::PostgreSql);
assert_eq!(ds_0.get_db_type(), DatabaseType::PostgreSql);
assert_eq!(
ds_0.auth,
Auth::Postgres(PostgresAuth::Basic {
Expand All @@ -34,7 +34,7 @@ fn load_ds_config_from_array() {
assert_eq!(ds_0.properties.migrations, Some(Migrations::Enabled));

assert_eq!(ds_1.name, "SqlServerDS");
assert_eq!(ds_1.db_type, DatabaseType::SqlServer);
assert_eq!(ds_1.get_db_type(), DatabaseType::SqlServer);
assert_eq!(
ds_1.auth,
Auth::SqlServer(SqlServerAuth::Basic {
Expand All @@ -60,11 +60,19 @@ pub struct Datasources {
#[derive(Deserialize, Debug, Clone)]
pub struct DatasourceConfig {
pub name: String,
pub db_type: DatabaseType,
pub auth: Auth,
pub properties: DatasourceProperties,
}

impl DatasourceConfig {
pub fn get_db_type(&self) -> DatabaseType {
match self.auth {
Auth::Postgres(_) => DatabaseType::PostgreSql,
Auth::SqlServer(_) => DatabaseType::SqlServer,
}
}
}

#[derive(Deserialize, Debug, Clone, PartialEq)]
pub enum Auth {
#[serde(alias = "PostgreSQL", alias = "postgresql")]
Expand Down
2 changes: 1 addition & 1 deletion canyon_observer/src/migrations/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl Migrations {
let canyon_memory = CanyonMemory::remember(datasource, &canyon_entities).await;

// Tracked entities that must be migrated whenever Canyon starts
let schema_status = Self::fetch_database(&datasource.name, datasource.db_type).await;
let schema_status = Self::fetch_database(&datasource.name, datasource.get_db_type()).await;
let database_tables_schema_info = Self::map_rows(schema_status);

// We filter the tables from the schema that aren't Canyon entities
Expand Down
2 changes: 1 addition & 1 deletion canyon_observer/src/migrations/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl CanyonMemory {
canyon_entities: &[CanyonRegisterEntity<'_>],
) -> Self {
// Creates the memory table if not exists
Self::create_memory(&datasource.name, &datasource.db_type).await;
Self::create_memory(&datasource.name, &datasource.get_db_type()).await;

// Retrieve the last status data from the `canyon_memory` table
let res = Self::query("SELECT * FROM canyon_memory", [], &datasource.name)
Expand Down
8 changes: 4 additions & 4 deletions canyon_observer/src/migrations/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl MigrationsProcessor {
datasource: &'_ DatasourceConfig,
) {
// The database type formally represented in Canyon
let db_type = datasource.db_type;
let db_type = datasource.get_db_type();
// For each entity (table) on the register (Rust structs)
for canyon_register_entity in canyon_entities {
let entity_name = canyon_register_entity.entity_db_table_name;
Expand Down Expand Up @@ -748,7 +748,7 @@ impl<T: Debug> Transaction<T> for TableOperation {}
#[async_trait]
impl DatabaseOperation for TableOperation {
async fn generate_sql(&self, datasource: &DatasourceConfig) {
let db_type = datasource.db_type;
let db_type = datasource.get_db_type();

let stmt = match self {
TableOperation::CreateTable(table_name, table_fields) => {
Expand Down Expand Up @@ -888,7 +888,7 @@ impl Transaction<Self> for ColumnOperation {}
#[async_trait]
impl DatabaseOperation for ColumnOperation {
async fn generate_sql(&self, datasource: &DatasourceConfig) {
let db_type = datasource.db_type;
let db_type = datasource.get_db_type();

let stmt = match self {
ColumnOperation::CreateColumn(table_name, entity_field) =>
Expand Down Expand Up @@ -980,7 +980,7 @@ impl Transaction<Self> for SequenceOperation {}
#[async_trait]
impl DatabaseOperation for SequenceOperation {
async fn generate_sql(&self, datasource: &DatasourceConfig) {
let db_type = datasource.db_type;
let db_type = datasource.get_db_type();

let stmt = match self {
SequenceOperation::ModifySequence(table_name, entity_field) => {
Expand Down
2 changes: 0 additions & 2 deletions tests/canyon.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

[[canyon_sql.datasources]]
name = 'postgres_docker'
db_type = 'postgresql'

[canyon_sql.datasources.auth]
postgresql = { basic = { username = 'postgres', password = 'postgres'}}
Expand All @@ -15,7 +14,6 @@ db_name = 'postgres'

[[canyon_sql.datasources]]
name = 'sqlserver_docker'
db_type = 'sqlserver'

[canyon_sql.datasources.auth]
sqlserver = { basic = { username = 'sa', password = 'SqlServer-10' } }
Expand Down

0 comments on commit f9bcf68

Please sign in to comment.