Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sea-orm-cli & sea-orm-migration dependencies optional #2367

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions sea-orm-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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"]
141 changes: 81 additions & 60 deletions sea-orm-cli/src/commands/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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::<MySql>(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::<MySql>(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::<Sqlite>(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::<Sqlite>(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::<Postgres>(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::<Postgres>(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()),
};
Expand Down
2 changes: 1 addition & 1 deletion sea-orm-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
26 changes: 13 additions & 13 deletions sea-orm-migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand All @@ -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"]
Expand Down
104 changes: 94 additions & 10 deletions sea-orm-migration/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down Expand Up @@ -109,9 +109,37 @@ impl<'c> SchemaManager<'c> {
C: AsRef<str>,
{
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();
Expand All @@ -130,9 +158,37 @@ impl<'c> SchemaManager<'c> {
I: AsRef<str>,
{
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();
Expand All @@ -152,9 +208,37 @@ where
T: AsRef<str>,
{
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();
Expand Down
Loading
Loading