Skip to content

Commit

Permalink
fix: use right database resource in back and front
Browse files Browse the repository at this point in the history
  • Loading branch information
dieriba committed Jan 8, 2025
1 parent 681f193 commit 4c05214
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 44 deletions.
70 changes: 34 additions & 36 deletions backend/windmill-api/src/database_triggers/handler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::collections::{
hash_map::Entry::{Occupied, Vacant},
HashMap,
use std::{
collections::{
hash_map::Entry::{Occupied, Vacant},
HashMap,
},
str::FromStr,
};

use axum::{
Expand All @@ -14,7 +17,7 @@ use rust_postgres::types::Type;
use serde::{Deserialize, Deserializer, Serialize};
use sql_builder::{bind::Bind, SqlBuilder};
use sqlx::{
postgres::{types::Oid, PgConnectOptions},
postgres::{types::Oid, PgConnectOptions, PgSslMode},
Connection, FromRow, PgConnection, QueryBuilder,
};
use windmill_audit::{audit_ee::audit_log, ActionKind};
Expand All @@ -35,11 +38,13 @@ use crate::{

#[derive(FromRow, Serialize, Deserialize, Debug)]
pub struct Database {
pub username: String,
pub password: Option<String>,
pub user: String,
pub password: String,
pub host: String,
pub port: u16,
pub db_name: String,
pub dbname: String,
pub sslmode: String,
pub root_certificate_pem: String,
}

#[derive(Debug, Clone, FromRow, Serialize, Deserialize)]
Expand Down Expand Up @@ -99,14 +104,26 @@ pub struct NewDatabaseTrigger {

async fn get_raw_postgres_connection(db: &Database) -> Result<PgConnection, Error> {
let options = {
let sslmode = if !db.sslmode.is_empty() {
PgSslMode::from_str(&db.sslmode)?
} else {
PgSslMode::Prefer
};
let options = PgConnectOptions::new()
.host(&db.host)
.database(&db.db_name)
.database(&db.dbname)
.port(db.port)
.username(&db.username);
.ssl_mode(sslmode)
.username(&db.user);

let options = if !db.root_certificate_pem.is_empty() {
options.ssl_root_cert_from_pem(db.root_certificate_pem.as_bytes().to_vec())
} else {
options
};

if let Some(password) = &db.password {
options.password(password)
if !db.password.is_empty() {
options.password(&db.password)
} else {
options
}
Expand Down Expand Up @@ -219,9 +236,8 @@ pub async fn get_database_resource(
.await
.map_err(Error::SqlErr)?;

if resource.value.password.is_some() {
let password = get_variable_or_self(resource.value.password.unwrap(), db, w_id).await?;
resource.value.password = Some(password)
if !resource.value.password.is_empty() {
resource.value.password = get_variable_or_self(resource.value.password, db, w_id).await?;
}

Ok(resource.value)
Expand Down Expand Up @@ -1112,6 +1128,8 @@ pub async fn get_template_script(
Path(w_id): Path<String>,
Json(template_script): Json<TemplateScript>,
) -> error::Result<String> {
use windmill_common::error::Error;

let TemplateScript { database_resource_path, relations, language } = template_script;
if relations.is_none() {
return Err(error::Error::BadRequest(
Expand All @@ -1121,29 +1139,9 @@ pub async fn get_template_script(

let resource = get_resource::<Database>(&db, &database_resource_path, &w_id)
.await
.map_err(|_| {
windmill_common::error::Error::NotFound("Database resource do not exist".to_string())
})?;
.map_err(|_| Error::NotFound("Database resource do not exist".to_string()))?;

let Database { username, password, host, port, db_name } = resource.value;

let options = {
let options = PgConnectOptions::new()
.port(port)
.database(&db_name)
.username(&username)
.host(&host);
if let Some(password_path) = password {
let password = get_variable_or_self(password_path, &db, &w_id).await?;
options.password(&password)
} else {
options
}
};

let mut pg_connection = PgConnection::connect_with(&options)
.await
.map_err(|e| error::Error::ConnectingToDatabase(e.to_string()))?;
let mut pg_connection = get_raw_postgres_connection(&resource.value).await?;

#[derive(Debug, FromRow, Deserialize)]
struct ColumnInfo {
Expand Down
11 changes: 5 additions & 6 deletions backend/windmill-api/src/database_triggers/trigger.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, pin::Pin};
use std::{collections::HashMap, pin::Pin, str::FromStr};

use crate::{
database_triggers::{
Expand Down Expand Up @@ -70,16 +70,15 @@ pub struct PostgresSimpleClient(Client);
impl PostgresSimpleClient {
async fn new(database: &Database) -> Result<Self, Error> {
let mut config = Config::new();

config
.dbname(&database.db_name)
.dbname(&database.dbname)
.host(&database.host)
.port(database.port)
.user(&database.username)
.user(&database.user)
.replication_mode(rust_postgres::config::ReplicationMode::Logical);

if let Some(password) = &database.password {
config.password(password);
if !database.password.is_empty() {
config.password(&database.password);
}

let (client, connection) = config.connect(NoTls).await.map_err(Error::Postgres)?;
Expand Down
2 changes: 1 addition & 1 deletion backend/windmill-common/src/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub async fn get_resource<'de, T>(
) -> Result<Resource<T>, sqlx::Error>
where
T: Send + Sync + std::marker::Unpin + DeserializeOwned,
{ println!("{} {}", path, w_id);
{
let resource = sqlx::query_as!(Resource, "SELECT resource_type, value, extra_perms FROM resource WHERE path = $1 AND workspace_id = $2", path, w_id).fetch_one(db).await?;

let val: T = serde_json::from_value(resource.value).unwrap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
Pick a database to connect to <Required required={true} />
</p>
<div class="flex flex-row mb-2">
<ResourcePicker bind:value={database_resource_path} resourceType={'database'} />
<ResourcePicker bind:value={database_resource_path} resourceType={'postgresql'} />
</div>
</Section>

Expand Down

0 comments on commit 4c05214

Please sign in to comment.