From 16876a1d27839b2b47ffac6f9e8fed1c966f6a3e Mon Sep 17 00:00:00 2001 From: Harsh Singh Date: Tue, 7 Nov 2023 16:17:15 +0530 Subject: [PATCH 1/5] backwards compatibility for package-query --- fastn-core/src/library2022/mod.rs | 5 +- .../src/library2022/processor/sqlite.rs | 49 +++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/fastn-core/src/library2022/mod.rs b/fastn-core/src/library2022/mod.rs index f1647820a2..e5014adc45 100644 --- a/fastn-core/src/library2022/mod.rs +++ b/fastn-core/src/library2022/mod.rs @@ -287,7 +287,10 @@ impl Library2022 { kind, doc, &self.config, - &fastn_core::library2022::processor::sql::get_db_config()?, + &fastn_core::library2022::processor::sql::DatabaseConfig { + db_type: "sqlite".to_string(), + db_url: "".to_string(), + }, ) .await } diff --git a/fastn-core/src/library2022/processor/sqlite.rs b/fastn-core/src/library2022/processor/sqlite.rs index 7cd593d0a3..c8cf3ceeb6 100644 --- a/fastn-core/src/library2022/processor/sqlite.rs +++ b/fastn-core/src/library2022/processor/sqlite.rs @@ -33,6 +33,44 @@ pub async fn process( ) -> ftd::interpreter::Result { let (headers, query) = get_p1_data("package-data", &value, doc.name)?; + let use_db = match headers.get_optional_string_by_key("use", doc.name, value.line_number()) { + Ok(Some(k)) => Some(k), + _ => match headers + .get_optional_string_by_key("db", doc.name, value.line_number()) + .ok() + { + Some(k) => { + println!("Warning: `db` header is deprecated, use `use` instead."); + k + } + None => None, + }, + }; + + let sqlite_database_path = match use_db { + Some(k) => { + let mut db_path = camino::Utf8PathBuf::new().join(k.as_str()); + if !db_path.exists() { + if !config.root.join(db_path.as_path()).exists() { + return ftd::interpreter::utils::e2( + "`use` does not exists for sql processor".to_string(), + doc.name, + value.line_number(), + ); + } + db_path = config.root.join(db_path.as_path()); + } + db_path + } + None => { + assert!( + !db_config.db_url.is_empty(), + "Neither `use` or `db` is present." + ); + config.root.join(&db_config.db_url) + } + }; + // need the query params // question is they can be multiple // so lets say start with passing attributes from ftd file @@ -41,9 +79,14 @@ pub async fn process( // for now they wil be ordered // select * from users where - let db_path = config.root.join(&db_config.db_url); - let query_response = - execute_query(&db_path, query.as_str(), doc, headers, value.line_number()).await; + let query_response = execute_query( + &sqlite_database_path, + query.as_str(), + doc, + headers, + value.line_number(), + ) + .await; match query_response { Ok(result) => result_to_value(Ok(result), kind, doc, &value, super::sql::STATUS_OK), From 5a2cacb6e6c811c1f5417e48a1f21ac116eb2012 Mon Sep 17 00:00:00 2001 From: Harsh Singh Date: Tue, 7 Nov 2023 18:07:58 +0530 Subject: [PATCH 2/5] refactored code and added deprecation message --- fastn-core/src/library2022/mod.rs | 14 +--- fastn-core/src/library2022/processor/mod.rs | 3 +- .../library2022/processor/package_query.rs | 81 +++++++++++++++++++ fastn-core/src/library2022/processor/pg.rs | 7 ++ fastn-core/src/library2022/processor/sql.rs | 4 +- .../src/library2022/processor/sqlite.rs | 41 +--------- fastn-core/src/library2022/utils.rs | 6 ++ 7 files changed, 103 insertions(+), 53 deletions(-) create mode 100644 fastn-core/src/library2022/processor/package_query.rs diff --git a/fastn-core/src/library2022/mod.rs b/fastn-core/src/library2022/mod.rs index e5014adc45..20b161d863 100644 --- a/fastn-core/src/library2022/mod.rs +++ b/fastn-core/src/library2022/mod.rs @@ -282,19 +282,9 @@ impl Library2022 { "is-reader" => processor::user_group::is_reader(value, kind, doc, &self.config).await, "sql" => processor::sql::process(value, kind, doc, &self.config).await, "package-query" => { - processor::sqlite::process( - value, - kind, - doc, - &self.config, - &fastn_core::library2022::processor::sql::DatabaseConfig { - db_type: "sqlite".to_string(), - db_url: "".to_string(), - }, - ) - .await + processor::package_query::process(value, kind, doc, &self.config).await } - "pg" => processor::pg::process(value, kind, doc).await, + "pg" => processor::pg::process(value, kind, doc, true).await, "package-tree" => { processor::package_tree::process(value, kind, doc, &self.config).await } diff --git a/fastn-core/src/library2022/processor/mod.rs b/fastn-core/src/library2022/processor/mod.rs index d9e21caeed..2f4fdd08bc 100644 --- a/fastn-core/src/library2022/processor/mod.rs +++ b/fastn-core/src/library2022/processor/mod.rs @@ -5,12 +5,13 @@ pub(crate) mod figma_tokens; pub(crate) mod figma_typography_tokens; pub(crate) mod get_data; pub(crate) mod http; +pub(crate) mod package_query; pub(crate) mod package_tree; pub(crate) mod pg; pub(crate) mod query; pub(crate) mod request_data; pub(crate) mod sitemap; -pub mod sql; +pub(crate) mod sql; pub(crate) mod sqlite; pub(crate) mod toc; pub(crate) mod user_details; diff --git a/fastn-core/src/library2022/processor/package_query.rs b/fastn-core/src/library2022/processor/package_query.rs new file mode 100644 index 0000000000..b66fe8bf4d --- /dev/null +++ b/fastn-core/src/library2022/processor/package_query.rs @@ -0,0 +1,81 @@ +pub async fn process( + value: ftd::ast::VariableValue, + kind: ftd::interpreter::Kind, + doc: &ftd::interpreter::TDoc<'_>, + config: &fastn_core::Config, +) -> ftd::interpreter::Result { + let (headers, query) = + fastn_core::library2022::processor::sqlite::get_p1_data("package-data", &value, doc.name)?; + + fastn_core::library2022::utils::log_deprecation_warning( + "`package-query` has been deprecated, use `sql` processor instead.", + ); + + let use_db = match headers.get_optional_string_by_key("use", doc.name, value.line_number()) { + Ok(Some(k)) => Some(k), + _ => match headers + .get_optional_string_by_key("db", doc.name, value.line_number()) + .ok() + { + Some(k) => { + fastn_core::library2022::utils::log_deprecation_warning( + "`db` header is deprecated, use `use` instead.", + ); + k + } + None => None, + }, + }; + + let sqlite_database_path = if let Some(db_path) = use_db { + let db_path = config.root.join(db_path.as_str()); + if !db_path.exists() { + return ftd::interpreter::utils::e2( + "`use` does not exist for package-query processor".to_string(), + doc.name, + value.line_number(), + ); + } + db_path + } else { + return ftd::interpreter::utils::e2( + "Neither `use` nor `db` is specified for package-query processor".to_string(), + doc.name, + value.line_number(), + ); + }; + + // need the query params + // question is they can be multiple + // so lets say start with passing attributes from ftd file + // db-: value + // db-: value + // for now they wil be ordered + // select * from users where + + let query_response = fastn_core::library2022::processor::sqlite::execute_query( + &sqlite_database_path, + query.as_str(), + doc, + headers, + value.line_number(), + ) + .await; + + match query_response { + Ok(result) => fastn_core::library2022::processor::sqlite::result_to_value( + Ok(result), + kind, + doc, + &value, + super::sql::STATUS_OK, + ), + Err(e) => fastn_core::library2022::processor::sqlite::result_to_value( + Err(e.to_string()), + kind, + doc, + &value, + super::sql::STATUS_ERROR, + ), + } +} diff --git a/fastn-core/src/library2022/processor/pg.rs b/fastn-core/src/library2022/processor/pg.rs index a3c5162aab..2771ef7f04 100644 --- a/fastn-core/src/library2022/processor/pg.rs +++ b/fastn-core/src/library2022/processor/pg.rs @@ -87,9 +87,16 @@ pub async fn process( value: ftd::ast::VariableValue, kind: ftd::interpreter::Kind, doc: &ftd::interpreter::TDoc<'_>, + deprecated_usage: bool, ) -> ftd::interpreter::Result { let (headers, query) = super::sqlite::get_p1_data("pg", &value, doc.name)?; + if deprecated_usage { + fastn_core::library2022::utils::log_deprecation_warning( + "`pg` has been deprecated, use `sql` processor instead.", + ); + } + let query_response = execute_query(query.as_str(), doc, value.line_number(), headers).await; match query_response { diff --git a/fastn-core/src/library2022/processor/sql.rs b/fastn-core/src/library2022/processor/sql.rs index 3bdcd7fbcb..98a70b56a2 100644 --- a/fastn-core/src/library2022/processor/sql.rs +++ b/fastn-core/src/library2022/processor/sql.rs @@ -29,7 +29,9 @@ pub async fn process( let db_type = db_config.db_type.as_str(); match db_type { - "postgres" => Ok(fastn_core::library2022::processor::pg::process(value, kind, doc).await?), + "postgres" => { + Ok(fastn_core::library2022::processor::pg::process(value, kind, doc, false).await?) + } "sqlite" => Ok(fastn_core::library2022::processor::sqlite::process( value, kind, doc, config, &db_config, ) diff --git a/fastn-core/src/library2022/processor/sqlite.rs b/fastn-core/src/library2022/processor/sqlite.rs index c8cf3ceeb6..e20695649f 100644 --- a/fastn-core/src/library2022/processor/sqlite.rs +++ b/fastn-core/src/library2022/processor/sqlite.rs @@ -32,44 +32,7 @@ pub async fn process( db_config: &fastn_core::library2022::processor::sql::DatabaseConfig, ) -> ftd::interpreter::Result { let (headers, query) = get_p1_data("package-data", &value, doc.name)?; - - let use_db = match headers.get_optional_string_by_key("use", doc.name, value.line_number()) { - Ok(Some(k)) => Some(k), - _ => match headers - .get_optional_string_by_key("db", doc.name, value.line_number()) - .ok() - { - Some(k) => { - println!("Warning: `db` header is deprecated, use `use` instead."); - k - } - None => None, - }, - }; - - let sqlite_database_path = match use_db { - Some(k) => { - let mut db_path = camino::Utf8PathBuf::new().join(k.as_str()); - if !db_path.exists() { - if !config.root.join(db_path.as_path()).exists() { - return ftd::interpreter::utils::e2( - "`use` does not exists for sql processor".to_string(), - doc.name, - value.line_number(), - ); - } - db_path = config.root.join(db_path.as_path()); - } - db_path - } - None => { - assert!( - !db_config.db_url.is_empty(), - "Neither `use` or `db` is present." - ); - config.root.join(&db_config.db_url) - } - }; + let sqlite_database_path = config.root.join(&db_config.db_url); // need the query params // question is they can be multiple @@ -329,7 +292,7 @@ fn extract_named_parameters( Ok(params) } -async fn execute_query( +pub(crate) async fn execute_query( database_path: &camino::Utf8PathBuf, query: &str, doc: &ftd::interpreter::TDoc<'_>, diff --git a/fastn-core/src/library2022/utils.rs b/fastn-core/src/library2022/utils.rs index 2ee2ba5927..5ef54ec8fb 100644 --- a/fastn-core/src/library2022/utils.rs +++ b/fastn-core/src/library2022/utils.rs @@ -14,3 +14,9 @@ pub fn document_full_id( Ok(format!("/{}/", full_document_id.trim_matches('/'))) } + +pub fn log_deprecation_warning(message: &str) { + use colored::Colorize; + + println!("{}", format!("Warning: {}", message).bright_yellow()); +} From 1fb8405d9dc23a261f3ce2837ed9b42b2ffd398c Mon Sep 17 00:00:00 2001 From: Harsh Singh Date: Tue, 7 Nov 2023 18:09:57 +0530 Subject: [PATCH 3/5] removed comments --- fastn-core/src/library2022/processor/package_query.rs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/fastn-core/src/library2022/processor/package_query.rs b/fastn-core/src/library2022/processor/package_query.rs index b66fe8bf4d..621a24d819 100644 --- a/fastn-core/src/library2022/processor/package_query.rs +++ b/fastn-core/src/library2022/processor/package_query.rs @@ -45,14 +45,6 @@ pub async fn process( ); }; - // need the query params - // question is they can be multiple - // so lets say start with passing attributes from ftd file - // db-: value - // db-: value - // for now they wil be ordered - // select * from users where - let query_response = fastn_core::library2022::processor::sqlite::execute_query( &sqlite_database_path, query.as_str(), From e04c182f6cd1e1f7b6d58e31ef44109e3bce33e1 Mon Sep 17 00:00:00 2001 From: Harsh Singh Date: Tue, 7 Nov 2023 18:13:56 +0530 Subject: [PATCH 4/5] minor changes --- fastn-core/src/library2022/processor/package_query.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastn-core/src/library2022/processor/package_query.rs b/fastn-core/src/library2022/processor/package_query.rs index 621a24d819..a157ef434a 100644 --- a/fastn-core/src/library2022/processor/package_query.rs +++ b/fastn-core/src/library2022/processor/package_query.rs @@ -31,7 +31,7 @@ pub async fn process( let db_path = config.root.join(db_path.as_str()); if !db_path.exists() { return ftd::interpreter::utils::e2( - "`use` does not exist for package-query processor".to_string(), + "database file does not exist for package-query processor".to_string(), doc.name, value.line_number(), ); From 9087f0d4f0605eb29155b8726165132f80668cd4 Mon Sep 17 00:00:00 2001 From: Harsh Singh Date: Tue, 7 Nov 2023 19:28:14 +0530 Subject: [PATCH 5/5] applied recommended changes --- fastn-core/src/library2022/mod.rs | 2 +- .../library2022/processor/package_query.rs | 42 +++++++------------ fastn-core/src/library2022/processor/pg.rs | 7 ---- fastn-core/src/library2022/processor/sql.rs | 4 +- 4 files changed, 17 insertions(+), 38 deletions(-) diff --git a/fastn-core/src/library2022/mod.rs b/fastn-core/src/library2022/mod.rs index 20b161d863..8c130d18b4 100644 --- a/fastn-core/src/library2022/mod.rs +++ b/fastn-core/src/library2022/mod.rs @@ -284,7 +284,7 @@ impl Library2022 { "package-query" => { processor::package_query::process(value, kind, doc, &self.config).await } - "pg" => processor::pg::process(value, kind, doc, true).await, + "pg" => processor::pg::process(value, kind, doc).await, "package-tree" => { processor::package_tree::process(value, kind, doc, &self.config).await } diff --git a/fastn-core/src/library2022/processor/package_query.rs b/fastn-core/src/library2022/processor/package_query.rs index a157ef434a..d3a3b4e020 100644 --- a/fastn-core/src/library2022/processor/package_query.rs +++ b/fastn-core/src/library2022/processor/package_query.rs @@ -11,39 +11,27 @@ pub async fn process( "`package-query` has been deprecated, use `sql` processor instead.", ); - let use_db = match headers.get_optional_string_by_key("use", doc.name, value.line_number()) { - Ok(Some(k)) => Some(k), - _ => match headers - .get_optional_string_by_key("db", doc.name, value.line_number()) - .ok() - { - Some(k) => { - fastn_core::library2022::utils::log_deprecation_warning( - "`db` header is deprecated, use `use` instead.", - ); - k + let sqlite_database = + match headers.get_optional_string_by_key("db", doc.name, value.line_number())? { + Some(k) => k, + None => { + return ftd::interpreter::utils::e2( + "`db` is not specified".to_string(), + doc.name, + value.line_number(), + ) } - None => None, - }, - }; + }; - let sqlite_database_path = if let Some(db_path) = use_db { - let db_path = config.root.join(db_path.as_str()); - if !db_path.exists() { - return ftd::interpreter::utils::e2( - "database file does not exist for package-query processor".to_string(), - doc.name, - value.line_number(), - ); - } - db_path - } else { + let sqlite_database_path = config.root.join(sqlite_database.as_str()); + + if !sqlite_database_path.exists() { return ftd::interpreter::utils::e2( - "Neither `use` nor `db` is specified for package-query processor".to_string(), + "`db` does not exists for package-query processor".to_string(), doc.name, value.line_number(), ); - }; + } let query_response = fastn_core::library2022::processor::sqlite::execute_query( &sqlite_database_path, diff --git a/fastn-core/src/library2022/processor/pg.rs b/fastn-core/src/library2022/processor/pg.rs index 2771ef7f04..a3c5162aab 100644 --- a/fastn-core/src/library2022/processor/pg.rs +++ b/fastn-core/src/library2022/processor/pg.rs @@ -87,16 +87,9 @@ pub async fn process( value: ftd::ast::VariableValue, kind: ftd::interpreter::Kind, doc: &ftd::interpreter::TDoc<'_>, - deprecated_usage: bool, ) -> ftd::interpreter::Result { let (headers, query) = super::sqlite::get_p1_data("pg", &value, doc.name)?; - if deprecated_usage { - fastn_core::library2022::utils::log_deprecation_warning( - "`pg` has been deprecated, use `sql` processor instead.", - ); - } - let query_response = execute_query(query.as_str(), doc, value.line_number(), headers).await; match query_response { diff --git a/fastn-core/src/library2022/processor/sql.rs b/fastn-core/src/library2022/processor/sql.rs index 98a70b56a2..3bdcd7fbcb 100644 --- a/fastn-core/src/library2022/processor/sql.rs +++ b/fastn-core/src/library2022/processor/sql.rs @@ -29,9 +29,7 @@ pub async fn process( let db_type = db_config.db_type.as_str(); match db_type { - "postgres" => { - Ok(fastn_core::library2022::processor::pg::process(value, kind, doc, false).await?) - } + "postgres" => Ok(fastn_core::library2022::processor::pg::process(value, kind, doc).await?), "sqlite" => Ok(fastn_core::library2022::processor::sqlite::process( value, kind, doc, config, &db_config, )