diff --git a/sea-orm-cli/Cargo.toml b/sea-orm-cli/Cargo.toml index f69e687eb..ea0282eea 100644 --- a/sea-orm-cli/Cargo.toml +++ b/sea-orm-cli/Cargo.toml @@ -38,8 +38,8 @@ clap = { version = "4.3", features = ["env", "derive"], optional = true } dotenvy = { version = "0.15", default-features = false, optional = true } async-std = { version = "1.9", default-features = false, features = ["attributes", "tokio1"], optional = true } sea-orm-codegen = { version = "=1.1.0-rc.1", path = "../sea-orm-codegen", default-features = false, optional = true } -sea-schema = { version = "0.16.0-rc.1" } -sqlx = { version = "0.8", default-features = false, features = ["mysql", "postgres"], optional = true } +sea-schema = { version = "0.16.0-rc.1", default-features = false, features = ["discovery", "writer", "probe"], optional = true } +sqlx = { version = "0.8", default-features = false, optional = true } tracing-subscriber = { version = "0.3.17", default-features = false, features = ["env-filter", "fmt"] } tracing = { version = "0.1", default-features = false } url = { version = "2.2", default-features = false } @@ -51,15 +51,18 @@ glob = { version = "0.3", default-features = false } smol = "1.2.5" [features] -default = ["codegen", "cli", "runtime-async-std-native-tls", "async-std"] -codegen = ["sea-schema/sqlx-all", "sea-orm-codegen"] +default = ["codegen", "sqlx-mysql", "sqlx-postgres", "sqlx-sqlite", "runtime-async-std-native-tls", "async-std"] +codegen = ["cli", "sqlx", "sea-schema", "sea-orm-codegen"] cli = ["clap", "dotenvy"] -runtime-actix = ["sqlx/runtime-tokio", "sea-schema/runtime-actix"] -runtime-async-std = ["sqlx/runtime-async-std", "sea-schema/runtime-async-std"] -runtime-tokio = ["sqlx/runtime-tokio", "sea-schema/runtime-tokio"] -runtime-actix-native-tls = ["sqlx/runtime-tokio-native-tls", "sea-schema/runtime-actix-native-tls"] -runtime-async-std-native-tls = ["sqlx/runtime-async-std-native-tls", "sea-schema/runtime-async-std-native-tls"] -runtime-tokio-native-tls = ["sqlx/runtime-tokio-native-tls", "sea-schema/runtime-tokio-native-tls"] -runtime-actix-rustls = ["sqlx/runtime-tokio-rustls", "sea-schema/runtime-actix-rustls"] -runtime-async-std-rustls = ["sqlx/runtime-async-std-rustls", "sea-schema/runtime-async-std-rustls"] -runtime-tokio-rustls = ["sqlx/runtime-tokio-rustls", "sea-schema/runtime-tokio-rustls"] +sqlx-mysql = ["sqlx?/sqlx-mysql", "sea-schema?/sqlx-mysql", "sea-schema?/mysql"] +sqlx-postgres = ["sqlx?/sqlx-postgres", "sea-schema?/sqlx-postgres", "sea-schema?/postgres"] +sqlx-sqlite = ["sqlx?/sqlx-sqlite", "sea-schema?/sqlx-sqlite", "sea-schema?/sqlite"] +runtime-actix = ["sqlx?/runtime-tokio", "sea-schema?/runtime-actix"] +runtime-async-std = ["sqlx?/runtime-async-std", "sea-schema?/runtime-async-std"] +runtime-tokio = ["sqlx?/runtime-tokio", "sea-schema?/runtime-tokio"] +runtime-actix-native-tls = ["sqlx?/runtime-tokio-native-tls", "sea-schema?/runtime-actix-native-tls"] +runtime-async-std-native-tls = ["sqlx?/runtime-async-std-native-tls", "sea-schema?/runtime-async-std-native-tls"] +runtime-tokio-native-tls = ["sqlx?/runtime-tokio-native-tls", "sea-schema?/runtime-tokio-native-tls"] +runtime-actix-rustls = ["sqlx?/runtime-tokio-rustls", "sea-schema?/runtime-actix-rustls"] +runtime-async-std-rustls = ["sqlx?/runtime-async-std-rustls", "sea-schema?/runtime-async-std-rustls"] +runtime-tokio-rustls = ["sqlx?/runtime-tokio-rustls", "sea-schema?/runtime-tokio-rustls"] diff --git a/sea-orm-cli/src/commands/generate.rs b/sea-orm-cli/src/commands/generate.rs index 31e32af1a..a3138367f 100644 --- a/sea-orm-cli/src/commands/generate.rs +++ b/sea-orm-cli/src/commands/generate.rs @@ -77,7 +77,7 @@ pub async fn run_generate_command( let filter_skip_tables = |table: &String| -> bool { !ignore_tables.contains(table) }; - let database_name = if !is_sqlite { + let _database_name = if !is_sqlite { // The database name should be the first element of the path string // // Throwing an error if there is no database name since it might be @@ -109,69 +109,90 @@ pub async fn run_generate_command( let (schema_name, table_stmts) = match url.scheme() { "mysql" => { - use sea_schema::mysql::discovery::SchemaDiscovery; - use sqlx::MySql; - - println!("Connecting to MySQL ..."); - let connection = - sqlx_connect::(max_connections, url.as_str(), None).await?; - println!("Discovering schema ..."); - let schema_discovery = SchemaDiscovery::new(connection, database_name); - let schema = schema_discovery.discover().await?; - let table_stmts = schema - .tables - .into_iter() - .filter(|schema| filter_tables(&schema.info.name)) - .filter(|schema| filter_hidden_tables(&schema.info.name)) - .filter(|schema| filter_skip_tables(&schema.info.name)) - .map(|schema| schema.write()) - .collect(); - (None, table_stmts) + #[cfg(not(feature = "sqlx-mysql"))] + { + panic!("mysql feature is off") + } + #[cfg(feature = "sqlx-mysql")] + { + use sea_schema::mysql::discovery::SchemaDiscovery; + use sqlx::MySql; + + println!("Connecting to MySQL ..."); + let connection = + sqlx_connect::(max_connections, url.as_str(), None).await?; + println!("Discovering schema ..."); + let schema_discovery = SchemaDiscovery::new(connection, _database_name); + let schema = schema_discovery.discover().await?; + let table_stmts = schema + .tables + .into_iter() + .filter(|schema| filter_tables(&schema.info.name)) + .filter(|schema| filter_hidden_tables(&schema.info.name)) + .filter(|schema| filter_skip_tables(&schema.info.name)) + .map(|schema| schema.write()) + .collect(); + (None, table_stmts) + } } "sqlite" => { - use sea_schema::sqlite::discovery::SchemaDiscovery; - use sqlx::Sqlite; - - println!("Connecting to SQLite ..."); - let connection = - sqlx_connect::(max_connections, url.as_str(), None).await?; - println!("Discovering schema ..."); - let schema_discovery = SchemaDiscovery::new(connection); - let schema = schema_discovery - .discover() - .await? - .merge_indexes_into_table(); - let table_stmts = schema - .tables - .into_iter() - .filter(|schema| filter_tables(&schema.name)) - .filter(|schema| filter_hidden_tables(&schema.name)) - .filter(|schema| filter_skip_tables(&schema.name)) - .map(|schema| schema.write()) - .collect(); - (None, table_stmts) + #[cfg(not(feature = "sqlx-sqlite"))] + { + panic!("sqlite feature is off") + } + #[cfg(feature = "sqlx-sqlite")] + { + use sea_schema::sqlite::discovery::SchemaDiscovery; + use sqlx::Sqlite; + + println!("Connecting to SQLite ..."); + let connection = + sqlx_connect::(max_connections, url.as_str(), None).await?; + println!("Discovering schema ..."); + let schema_discovery = SchemaDiscovery::new(connection); + let schema = schema_discovery + .discover() + .await? + .merge_indexes_into_table(); + let table_stmts = schema + .tables + .into_iter() + .filter(|schema| filter_tables(&schema.name)) + .filter(|schema| filter_hidden_tables(&schema.name)) + .filter(|schema| filter_skip_tables(&schema.name)) + .map(|schema| schema.write()) + .collect(); + (None, table_stmts) + } } "postgres" | "postgresql" => { - use sea_schema::postgres::discovery::SchemaDiscovery; - use sqlx::Postgres; - - println!("Connecting to Postgres ..."); - let schema = database_schema.as_deref().unwrap_or("public"); - let connection = - sqlx_connect::(max_connections, url.as_str(), Some(schema)) - .await?; - println!("Discovering schema ..."); - let schema_discovery = SchemaDiscovery::new(connection, schema); - let schema = schema_discovery.discover().await?; - let table_stmts = schema - .tables - .into_iter() - .filter(|schema| filter_tables(&schema.info.name)) - .filter(|schema| filter_hidden_tables(&schema.info.name)) - .filter(|schema| filter_skip_tables(&schema.info.name)) - .map(|schema| schema.write()) - .collect(); - (database_schema, table_stmts) + #[cfg(not(feature = "sqlx-postgres"))] + { + panic!("postgres feature is off") + } + #[cfg(feature = "sqlx-postgres")] + { + use sea_schema::postgres::discovery::SchemaDiscovery; + use sqlx::Postgres; + + println!("Connecting to Postgres ..."); + let schema = database_schema.as_deref().unwrap_or("public"); + let connection = + sqlx_connect::(max_connections, url.as_str(), Some(schema)) + .await?; + println!("Discovering schema ..."); + let schema_discovery = SchemaDiscovery::new(connection, schema); + let schema = schema_discovery.discover().await?; + let table_stmts = schema + .tables + .into_iter() + .filter(|schema| filter_tables(&schema.info.name)) + .filter(|schema| filter_hidden_tables(&schema.info.name)) + .filter(|schema| filter_skip_tables(&schema.info.name)) + .map(|schema| schema.write()) + .collect(); + (database_schema, table_stmts) + } } _ => unimplemented!("{} is not supported", url.scheme()), }; diff --git a/sea-orm-macros/Cargo.toml b/sea-orm-macros/Cargo.toml index b596a0bbf..49c691263 100644 --- a/sea-orm-macros/Cargo.toml +++ b/sea-orm-macros/Cargo.toml @@ -26,7 +26,7 @@ proc-macro2 = { version = "1", default-features = false } unicode-ident = { version = "1" } [dev-dependencies] -sea-orm = { path = "../", features = ["macros", "tests-cfg"] } +sea-orm = { path = "../", default-features = false, features = ["macros", "tests-cfg"] } serde = { version = "1.0", features = ["derive"] } [features] diff --git a/sea-orm-migration/Cargo.toml b/sea-orm-migration/Cargo.toml index f8813c88b..ab7055a3e 100644 --- a/sea-orm-migration/Cargo.toml +++ b/sea-orm-migration/Cargo.toml @@ -25,7 +25,7 @@ clap = { version = "4.3", features = ["env", "derive"], optional = true } dotenvy = { version = "0.15", default-features = false, optional = true } sea-orm = { version = "~1.1.0-rc.1", path = "../", default-features = false, features = ["macros"] } sea-orm-cli = { version = "~1.1.0-rc.1", path = "../sea-orm-cli", default-features = false, optional = true } -sea-schema = { version = "0.16.0-rc.1" } +sea-schema = { version = "0.16.0-rc.1", default-features = false, features = ["discovery", "writer", "probe"] } tracing = { version = "0.1", default-features = false, features = ["log"] } tracing-subscriber = { version = "0.3.17", default-features = false, features = ["env-filter", "fmt"] } futures = { version = "0.3", default-features = false, features = ["std"] } @@ -36,19 +36,19 @@ async-std = { version = "1", features = ["attributes", "tokio1"] } [features] default = ["cli"] cli = ["clap", "dotenvy", "sea-orm-cli/cli"] -sqlx-mysql = ["sea-orm/sqlx-mysql"] -sqlx-postgres = ["sea-orm/sqlx-postgres"] -sqlx-sqlite = ["sea-orm/sqlx-sqlite"] +sqlx-mysql = ["sea-orm/sqlx-mysql", "sea-schema/sqlx-mysql", "sea-schema/mysql", "sea-orm-cli?/sqlx-mysql"] +sqlx-postgres = ["sea-orm/sqlx-postgres", "sea-schema/sqlx-postgres", "sea-schema/postgres", "sea-orm-cli?/sqlx-postgres"] +sqlx-sqlite = ["sea-orm/sqlx-sqlite", "sea-schema/sqlx-sqlite", "sea-schema/sqlite", "sea-orm-cli?/sqlx-sqlite"] sqlite-use-returning-for-3_35 = ["sea-orm/sqlite-use-returning-for-3_35"] -runtime-actix = ["sea-orm/runtime-actix"] -runtime-async-std = ["sea-orm/runtime-async-std"] -runtime-tokio = ["sea-orm/runtime-tokio"] -runtime-actix-native-tls = ["sea-orm/runtime-actix-native-tls"] -runtime-async-std-native-tls = ["sea-orm/runtime-async-std-native-tls"] -runtime-tokio-native-tls = ["sea-orm/runtime-tokio-native-tls"] -runtime-actix-rustls = ["sea-orm/runtime-actix-rustls"] -runtime-async-std-rustls = ["sea-orm/runtime-async-std-rustls"] -runtime-tokio-rustls = ["sea-orm/runtime-tokio-rustls"] +runtime-actix = ["sea-orm/runtime-actix", "sea-schema/runtime-actix", "sea-orm-cli?/runtime-actix"] +runtime-async-std = ["sea-orm/runtime-async-std", "sea-schema/runtime-async-std", "sea-orm-cli?/runtime-async-std"] +runtime-tokio = ["sea-orm/runtime-tokio", "sea-schema/runtime-tokio", "sea-orm-cli?/runtime-tokio"] +runtime-actix-native-tls = ["sea-orm/runtime-actix-native-tls", "sea-schema/runtime-actix-native-tls", "sea-orm-cli?/runtime-actix-native-tls"] +runtime-async-std-native-tls = ["sea-orm/runtime-async-std-native-tls", "sea-schema/runtime-async-std-native-tls", "sea-orm-cli?/runtime-async-std-native-tls"] +runtime-tokio-native-tls = ["sea-orm/runtime-tokio-native-tls", "sea-schema/runtime-tokio-native-tls", "sea-orm-cli?/runtime-tokio-native-tls"] +runtime-actix-rustls = ["sea-orm/runtime-actix-rustls", "sea-schema/runtime-actix-rustls", "sea-orm-cli?/runtime-actix-rustls"] +runtime-async-std-rustls = ["sea-orm/runtime-async-std-rustls", "sea-schema/runtime-async-std-rustls", "sea-orm-cli?/runtime-async-std-rustls"] +runtime-tokio-rustls = ["sea-orm/runtime-tokio-rustls", "sea-schema/runtime-tokio-rustls", "sea-orm-cli?/runtime-tokio-rustls"] with-json = ["sea-orm/with-json"] with-chrono = ["sea-orm/with-chrono"] with-rust_decimal = ["sea-orm/with-rust_decimal"] diff --git a/sea-orm-migration/src/manager.rs b/sea-orm-migration/src/manager.rs index d1cc3b6a3..5b0011799 100644 --- a/sea-orm-migration/src/manager.rs +++ b/sea-orm-migration/src/manager.rs @@ -6,7 +6,7 @@ use sea_orm::sea_query::{ TableTruncateStatement, }; use sea_orm::{ConnectionTrait, DbBackend, DbErr, StatementBuilder}; -use sea_schema::{mysql::MySql, postgres::Postgres, probe::SchemaProbe, sqlite::Sqlite}; +use sea_schema::probe::SchemaProbe; /// Helper struct for writing migration scripts in migration file pub struct SchemaManager<'c> { @@ -109,9 +109,37 @@ impl<'c> SchemaManager<'c> { C: AsRef, { let stmt = match self.conn.get_database_backend() { - DbBackend::MySql => MySql.has_column(table, column), - DbBackend::Postgres => Postgres.has_column(table, column), - DbBackend::Sqlite => Sqlite.has_column(table, column), + DbBackend::MySql => { + #[cfg(feature = "sqlx-mysql")] + { + sea_schema::mysql::MySql.has_column(table, column) + } + #[cfg(not(feature = "sqlx-mysql"))] + { + panic!("mysql feature is off") + } + } + #[cfg(feature = "sqlx-postgres")] + DbBackend::Postgres => { + #[cfg(feature = "sqlx-postgres")] + { + sea_schema::postgres::Postgres.has_column(table, column) + } + #[cfg(not(feature = "sqlx-postgres"))] + { + panic!("postgres feature is off") + } + } + DbBackend::Sqlite => { + #[cfg(feature = "sqlx-sqlite")] + { + sea_schema::sqlite::Sqlite.has_column(table, column) + } + #[cfg(not(feature = "sqlx-sqlite"))] + { + panic!("sqlite feature is off") + } + } }; let builder = self.conn.get_database_backend(); @@ -130,9 +158,37 @@ impl<'c> SchemaManager<'c> { I: AsRef, { let stmt = match self.conn.get_database_backend() { - DbBackend::MySql => MySql.has_index(table, index), - DbBackend::Postgres => Postgres.has_index(table, index), - DbBackend::Sqlite => Sqlite.has_index(table, index), + DbBackend::MySql => { + #[cfg(feature = "sqlx-mysql")] + { + sea_schema::mysql::MySql.has_index(table, index) + } + #[cfg(not(feature = "sqlx-mysql"))] + { + panic!("mysql feature is off") + } + } + #[cfg(feature = "sqlx-postgres")] + DbBackend::Postgres => { + #[cfg(feature = "sqlx-postgres")] + { + sea_schema::postgres::Postgres.has_index(table, index) + } + #[cfg(not(feature = "sqlx-postgres"))] + { + panic!("postgres feature is off") + } + } + DbBackend::Sqlite => { + #[cfg(feature = "sqlx-sqlite")] + { + sea_schema::sqlite::Sqlite.has_index(table, index) + } + #[cfg(not(feature = "sqlx-sqlite"))] + { + panic!("sqlite feature is off") + } + } }; let builder = self.conn.get_database_backend(); @@ -152,9 +208,37 @@ where T: AsRef, { let stmt = match conn.get_database_backend() { - DbBackend::MySql => MySql.has_table(table), - DbBackend::Postgres => Postgres.has_table(table), - DbBackend::Sqlite => Sqlite.has_table(table), + DbBackend::MySql => { + #[cfg(feature = "sqlx-mysql")] + { + sea_schema::mysql::MySql.has_table(table) + } + #[cfg(not(feature = "sqlx-mysql"))] + { + panic!("mysql feature is off") + } + } + #[cfg(feature = "sqlx-postgres")] + DbBackend::Postgres => { + #[cfg(feature = "sqlx-postgres")] + { + sea_schema::postgres::Postgres.has_table(table) + } + #[cfg(not(feature = "sqlx-postgres"))] + { + panic!("postgres feature is off") + } + } + DbBackend::Sqlite => { + #[cfg(feature = "sqlx-sqlite")] + { + sea_schema::sqlite::Sqlite.has_table(table) + } + #[cfg(not(feature = "sqlx-sqlite"))] + { + panic!("sqlite feature is off") + } + } }; let builder = conn.get_database_backend(); diff --git a/sea-orm-migration/src/migrator.rs b/sea-orm-migration/src/migrator.rs index 05329e5fe..9117f89ed 100644 --- a/sea-orm-migration/src/migrator.rs +++ b/sea-orm-migration/src/migrator.rs @@ -14,7 +14,7 @@ use sea_orm::{ DynIden, EntityTrait, FromQueryResult, Iterable, QueryFilter, Schema, Statement, TransactionTrait, }; -use sea_schema::{mysql::MySql, postgres::Postgres, probe::SchemaProbe, sqlite::Sqlite}; +use sea_schema::probe::SchemaProbe; use super::{seaql_migrations, IntoSchemaManagerConnection, MigrationTrait, SchemaManager}; @@ -442,9 +442,37 @@ where C: ConnectionTrait, { match db.get_database_backend() { - DbBackend::MySql => MySql.query_tables(), - DbBackend::Postgres => Postgres.query_tables(), - DbBackend::Sqlite => Sqlite.query_tables(), + DbBackend::MySql => { + #[cfg(feature = "sqlx-mysql")] + { + sea_schema::mysql::MySql.query_tables() + } + #[cfg(not(feature = "sqlx-mysql"))] + { + panic!("mysql feature is off") + } + } + #[cfg(feature = "sqlx-postgres")] + DbBackend::Postgres => { + #[cfg(feature = "sqlx-postgres")] + { + sea_schema::postgres::Postgres.query_tables() + } + #[cfg(not(feature = "sqlx-postgres"))] + { + panic!("postgres feature is off") + } + } + DbBackend::Sqlite => { + #[cfg(feature = "sqlx-sqlite")] + { + sea_schema::sqlite::Sqlite.query_tables() + } + #[cfg(not(feature = "sqlx-sqlite"))] + { + panic!("sqlite feature is off") + } + } } } @@ -453,9 +481,37 @@ where C: ConnectionTrait, { match db.get_database_backend() { - DbBackend::MySql => MySql::get_current_schema(), - DbBackend::Postgres => Postgres::get_current_schema(), - DbBackend::Sqlite => unimplemented!(), + DbBackend::MySql => { + #[cfg(feature = "sqlx-mysql")] + { + sea_schema::mysql::MySql::get_current_schema() + } + #[cfg(not(feature = "sqlx-mysql"))] + { + panic!("mysql feature is off") + } + } + #[cfg(feature = "sqlx-postgres")] + DbBackend::Postgres => { + #[cfg(feature = "sqlx-postgres")] + { + sea_schema::postgres::Postgres::get_current_schema() + } + #[cfg(not(feature = "sqlx-postgres"))] + { + panic!("postgres feature is off") + } + } + DbBackend::Sqlite => { + #[cfg(feature = "sqlx-sqlite")] + { + unimplemented!() + } + #[cfg(not(feature = "sqlx-sqlite"))] + { + panic!("sqlite feature is off") + } + } } }