Skip to content

Commit a321f0a

Browse files
authored
Upstream Changes - 3 (#2155)
* Upstream Changes - 3 * refactor * revert
1 parent 5c8bca0 commit a321f0a

File tree

6 files changed

+58
-27
lines changed

6 files changed

+58
-27
lines changed

sea-orm-cli/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ clap = { version = "4.3", features = ["env", "derive"], optional = true }
3838
dotenvy = { version = "0.15", default-features = false, optional = true }
3939
async-std = { version = "1.9", default-features = false, features = ["attributes", "tokio1"], optional = true }
4040
sea-orm-codegen = { version = "=1.0.0-rc.2", path = "../sea-orm-codegen", default-features = false, optional = true }
41-
sea-schema = { version = "0.15.0-rc.3" }
41+
sea-schema = { version = "0.15.0-rc.3", git = "https://github.com/SeaQL/sea-schema", branch = "upstream-changes-2" }
4242
sqlx = { version = "0.7", default-features = false, features = ["mysql", "postgres"], optional = true }
4343
tracing-subscriber = { version = "0.3.17", default-features = false, features = ["env-filter", "fmt"] }
4444
tracing = { version = "0.1", default-features = false }

sea-orm-cli/src/cli.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,11 @@ pub enum GenerateSubcommands {
214214
short = 's',
215215
long,
216216
env = "DATABASE_SCHEMA",
217-
default_value = "public",
218217
long_help = "Database schema\n \
219218
- For MySQL, this argument is ignored.\n \
220219
- For PostgreSQL, this argument is optional with default value 'public'."
221220
)]
222-
database_schema: String,
221+
database_schema: Option<String>,
223222

224223
#[arg(short = 'u', long, env = "DATABASE_URL", help = "Database URL")]
225224
database_url: String,

sea-orm-cli/src/commands/generate.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,8 @@ pub async fn run_generate_command(
113113
use sqlx::MySql;
114114

115115
println!("Connecting to MySQL ...");
116-
let connection = connect::<MySql>(max_connections, url.as_str(), None).await?;
116+
let connection =
117+
sqlx_connect::<MySql>(max_connections, url.as_str(), None).await?;
117118
println!("Discovering schema ...");
118119
let schema_discovery = SchemaDiscovery::new(connection, database_name);
119120
let schema = schema_discovery.discover().await?;
@@ -132,7 +133,8 @@ pub async fn run_generate_command(
132133
use sqlx::Sqlite;
133134

134135
println!("Connecting to SQLite ...");
135-
let connection = connect::<Sqlite>(max_connections, url.as_str(), None).await?;
136+
let connection =
137+
sqlx_connect::<Sqlite>(max_connections, url.as_str(), None).await?;
136138
println!("Discovering schema ...");
137139
let schema_discovery = SchemaDiscovery::new(connection);
138140
let schema = schema_discovery.discover().await?;
@@ -151,9 +153,13 @@ pub async fn run_generate_command(
151153
use sqlx::Postgres;
152154

153155
println!("Connecting to Postgres ...");
154-
let schema = &database_schema;
156+
let schema = database_schema
157+
.as_ref()
158+
.map(|s| s.as_str())
159+
.unwrap_or("public");
155160
let connection =
156-
connect::<Postgres>(max_connections, url.as_str(), Some(schema)).await?;
161+
sqlx_connect::<Postgres>(max_connections, url.as_str(), Some(schema))
162+
.await?;
157163
println!("Discovering schema ...");
158164
let schema_discovery = SchemaDiscovery::new(connection, schema);
159165
let schema = schema_discovery.discover().await?;
@@ -214,7 +220,7 @@ pub async fn run_generate_command(
214220
Ok(())
215221
}
216222

217-
async fn connect<DB>(
223+
async fn sqlx_connect<DB>(
218224
max_connections: u32,
219225
url: &str,
220226
schema: Option<&str>,

sea-orm-migration/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ clap = { version = "4.3", features = ["env", "derive"], optional = true }
2525
dotenvy = { version = "0.15", default-features = false, optional = true }
2626
sea-orm = { version = "1.0.0-rc.2", path = "../", default-features = false, features = ["macros"] }
2727
sea-orm-cli = { version = "1.0.0-rc.2", path = "../sea-orm-cli", default-features = false, optional = true }
28-
sea-schema = { version = "0.15.0-rc.3" }
28+
sea-schema = { version = "0.15.0-rc.3", git = "https://github.com/SeaQL/sea-schema", branch = "upstream-changes-2" }
2929
tracing = { version = "0.1", default-features = false, features = ["log"] }
3030
tracing-subscriber = { version = "0.3.17", default-features = false, features = ["env-filter", "fmt"] }
3131
futures = { version = "0.3", default-features = false, features = ["std"] }

sea-orm-migration/src/manager.rs

+39-14
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use sea_schema::{mysql::MySql, postgres::Postgres, probe::SchemaProbe, sqlite::S
1111
/// Helper struct for writing migration scripts in migration file
1212
pub struct SchemaManager<'c> {
1313
conn: SchemaManagerConnection<'c>,
14+
schema: Option<String>,
1415
}
1516

1617
impl<'c> SchemaManager<'c> {
@@ -20,6 +21,7 @@ impl<'c> SchemaManager<'c> {
2021
{
2122
Self {
2223
conn: conn.into_schema_manager_connection(),
24+
schema: None,
2325
}
2426
}
2527

@@ -38,6 +40,18 @@ impl<'c> SchemaManager<'c> {
3840
pub fn get_connection(&self) -> &SchemaManagerConnection<'c> {
3941
&self.conn
4042
}
43+
44+
pub fn set_schema<T>(&mut self, schema: T) -> &mut Self
45+
where
46+
T: Into<String>,
47+
{
48+
self.schema = Some(schema.into());
49+
self
50+
}
51+
52+
pub fn get_schema(&self) -> &Option<String> {
53+
&self.schema
54+
}
4155
}
4256

4357
/// Schema Creation
@@ -100,20 +114,7 @@ impl<'c> SchemaManager<'c> {
100114
where
101115
T: AsRef<str>,
102116
{
103-
let stmt = match self.conn.get_database_backend() {
104-
DbBackend::MySql => MySql.has_table(table),
105-
DbBackend::Postgres => Postgres.has_table(table),
106-
DbBackend::Sqlite => Sqlite.has_table(table),
107-
};
108-
109-
let builder = self.conn.get_database_backend();
110-
let res = self
111-
.conn
112-
.query_one(builder.build(&stmt))
113-
.await?
114-
.ok_or_else(|| DbErr::Custom("Failed to check table exists".to_owned()))?;
115-
116-
res.try_get("", "has_table")
117+
has_table(&self.conn, self.schema.as_deref(), table).await
117118
}
118119

119120
pub async fn has_column<T, C>(&self, table: T, column: C) -> Result<bool, DbErr>
@@ -158,3 +159,27 @@ impl<'c> SchemaManager<'c> {
158159
res.try_get("", "has_index")
159160
}
160161
}
162+
163+
pub(crate) async fn has_table<C, T>(
164+
conn: &C,
165+
_schema: Option<&str>,
166+
table: T,
167+
) -> Result<bool, DbErr>
168+
where
169+
C: ConnectionTrait,
170+
T: AsRef<str>,
171+
{
172+
let stmt = match conn.get_database_backend() {
173+
DbBackend::MySql => MySql.has_table(table),
174+
DbBackend::Postgres => Postgres.has_table(table),
175+
DbBackend::Sqlite => Sqlite.has_table(table),
176+
};
177+
178+
let builder = conn.get_database_backend();
179+
let res = conn
180+
.query_one(builder.build(&stmt))
181+
.await?
182+
.ok_or_else(|| DbErr::Custom("Failed to check table exists".to_owned()))?;
183+
184+
res.try_get("", "has_table")
185+
}

sea-orm-migration/src/migrator.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,11 @@ pub trait MigratorTrait: Send {
164164
C: ConnectionTrait,
165165
{
166166
let builder = db.get_database_backend();
167+
let table_name = Self::migration_table_name();
167168
let schema = Schema::new(builder);
168169
let mut stmt = schema
169170
.create_table_from_entity(seaql_migrations::Entity)
170-
.table_name(Self::migration_table_name());
171+
.table_name(table_name);
171172
stmt.if_not_exists();
172173
db.execute(builder.build(&stmt)).await.map(|_| ())
173174
}
@@ -441,9 +442,9 @@ where
441442
C: ConnectionTrait,
442443
{
443444
match db.get_database_backend() {
444-
DbBackend::MySql => MySql::query_tables(),
445-
DbBackend::Postgres => Postgres::query_tables(),
446-
DbBackend::Sqlite => Sqlite::query_tables(),
445+
DbBackend::MySql => MySql.query_tables(),
446+
DbBackend::Postgres => Postgres.query_tables(),
447+
DbBackend::Sqlite => Sqlite.query_tables(),
447448
}
448449
}
449450

0 commit comments

Comments
 (0)