From 359d66035edabec005351c4976cefefa9557f3c8 Mon Sep 17 00:00:00 2001 From: jkomyno Date: Wed, 18 Dec 2024 21:35:38 +0100 Subject: [PATCH] feat(schema-engine): let "sql-schema-describer" be wasm-compatible; partial revert of prisma/prisma-engines/pull/3093; close ORM-456 --- schema-engine/sql-schema-describer/Cargo.toml | 37 ++++++++++--- schema-engine/sql-schema-describer/src/lib.rs | 8 +++ .../sql-schema-describer/src/sqlite.rs | 54 ++++--------------- 3 files changed, 48 insertions(+), 51 deletions(-) diff --git a/schema-engine/sql-schema-describer/Cargo.toml b/schema-engine/sql-schema-describer/Cargo.toml index 17b8eae63684..31e74cf5f835 100644 --- a/schema-engine/sql-schema-describer/Cargo.toml +++ b/schema-engine/sql-schema-describer/Cargo.toml @@ -3,9 +3,38 @@ edition = "2021" name = "sql-schema-describer" version = "0.1.0" +[features] +postgresql = ["relation_joins", "quaint/postgresql", "psl/postgresql"] +postgresql-native = ["postgresql", "quaint/postgresql-native", "quaint/pooled"] +mysql = ["relation_joins", "quaint/mysql", "psl/mysql"] +mysql-native = ["mysql", "quaint/mysql-native", "quaint/pooled"] +sqlite = ["quaint/sqlite", "psl/sqlite"] +sqlite-native = ["sqlite", "quaint/sqlite-native", "quaint/pooled"] +mssql = ["quaint/mssql"] +mssql-native = ["mssql", "quaint/mssql-native", "quaint/pooled"] +cockroachdb = ["relation_joins", "quaint/postgresql", "psl/cockroachdb"] +cockroachdb-native = [ + "cockroachdb", + "quaint/postgresql-native", + "quaint/pooled", +] +vendored-openssl = ["quaint/vendored-openssl"] +all-native = [ + "sqlite-native", + "mysql-native", + "postgresql-native", + "mssql-native", + "cockroachdb-native", +] +# TODO: At the moment of writing (rustc 1.77.0), can_have_capability from psl does not eliminate joins +# code from bundle for some reason, so we are doing it explicitly. Check with a newer version of compiler - if elimination +# happens successfully, we don't need this feature anymore +relation_joins = [] + [dependencies] prisma-value = { path = "../../libs/prisma-value" } -psl = { workspace = true, features = ["all"] } +psl.workspace = true +quaint.workspace = true either = "1.8.0" async-trait.workspace = true @@ -19,12 +48,6 @@ serde.workspace = true tracing.workspace = true tracing-error = "0.2" tracing-futures.workspace = true -quaint = { workspace = true, features = [ - "all-native", - "pooled", - "expose-drivers", - "fmt-sql", -] } [dev-dependencies] expect-test = "1.2.2" diff --git a/schema-engine/sql-schema-describer/src/lib.rs b/schema-engine/sql-schema-describer/src/lib.rs index 8f65c175b2a3..b7c86bf72e6c 100644 --- a/schema-engine/sql-schema-describer/src/lib.rs +++ b/schema-engine/sql-schema-describer/src/lib.rs @@ -3,10 +3,18 @@ #![deny(rust_2018_idioms, unsafe_code)] #![allow(clippy::derive_partial_eq_without_eq)] +#[cfg(feature = "mssql")] pub mod mssql; + +#[cfg(feature = "mysql")] pub mod mysql; + +#[cfg(any(feature = "postgresql", feature = "cockroachdb"))] pub mod postgres; + +#[cfg(feature = "sqlite")] pub mod sqlite; + pub mod walkers; mod connector_data; diff --git a/schema-engine/sql-schema-describer/src/sqlite.rs b/schema-engine/sql-schema-describer/src/sqlite.rs index bd82c52fce0e..9130d1622c67 100644 --- a/schema-engine/sql-schema-describer/src/sqlite.rs +++ b/schema-engine/sql-schema-describer/src/sqlite.rs @@ -8,9 +8,9 @@ use crate::{ use either::Either; use indexmap::IndexMap; use quaint::{ - ast::{Value, ValueType}, - connector::{ColumnType as QuaintColumnType, GetRow, ToColumnNames}, - prelude::ResultRow, + ast::Value, + prelude::{Queryable, ResultRow}, + ValueType, }; use std::{any::type_name, borrow::Cow, collections::BTreeMap, convert::TryInto, fmt::Debug, path::Path}; use tracing::trace; @@ -24,44 +24,8 @@ pub trait Connection { ) -> quaint::Result; } -#[async_trait::async_trait] -impl Connection for std::sync::Mutex { - async fn query_raw<'a>( - &'a self, - sql: &'a str, - params: &'a [quaint::prelude::Value<'a>], - ) -> quaint::Result { - let conn = self.lock().unwrap(); - let mut stmt = conn.prepare_cached(sql)?; - let column_types = stmt.columns().iter().map(QuaintColumnType::from).collect::>(); - let mut rows = stmt.query(quaint::connector::rusqlite::params_from_iter(params.iter()))?; - let column_names = rows.to_column_names(); - let mut converted_rows = Vec::new(); - while let Some(row) = rows.next()? { - converted_rows.push(row.get_result_row().unwrap()); - } - - Ok(quaint::prelude::ResultSet::new( - column_names, - column_types, - converted_rows, - )) - } -} - -#[async_trait::async_trait] -impl Connection for quaint::single::Quaint { - async fn query_raw<'a>( - &'a self, - sql: &'a str, - params: &'a [quaint::prelude::Value<'a>], - ) -> quaint::Result { - quaint::prelude::Queryable::query_raw(self, sql, params).await - } -} - pub struct SqlSchemaDescriber<'a> { - conn: &'a (dyn Connection + Send + Sync), + conn: &'a dyn Queryable, } impl Debug for SqlSchemaDescriber<'_> { @@ -92,7 +56,9 @@ impl SqlSchemaDescriberBackend for SqlSchemaDescriber<'_> { } async fn version(&self) -> DescriberResult> { - Ok(Some(quaint::connector::sqlite_version().to_owned())) + // TODO: implement `SELECT version` via Driver Adapters + // Ok(Some(quaint::connector::sqlite_version().to_owned())) + Ok(None) } } @@ -100,7 +66,7 @@ impl Parser for SqlSchemaDescriber<'_> {} impl<'a> SqlSchemaDescriber<'a> { /// Constructor. - pub fn new(conn: &'a (dyn Connection + Send + Sync)) -> SqlSchemaDescriber<'a> { + pub fn new(conn: &'a dyn Queryable) -> SqlSchemaDescriber<'a> { SqlSchemaDescriber { conn } } @@ -330,7 +296,7 @@ async fn push_columns( table_name: &str, container_id: Either, schema: &mut SqlSchema, - conn: &(dyn Connection + Send + Sync), + conn: &dyn Queryable, ) -> DescriberResult<()> { let sql = format!(r#"PRAGMA table_info ("{table_name}")"#); let result_set = conn.query_raw(&sql, &[]).await?; @@ -469,7 +435,7 @@ async fn push_indexes( table: &str, table_id: TableId, schema: &mut SqlSchema, - conn: &(dyn Connection + Send + Sync), + conn: &dyn Queryable, ) -> DescriberResult<()> { let sql = format!(r#"PRAGMA index_list("{table}");"#); let result_set = conn.query_raw(&sql, &[]).await?;