Skip to content

Commit 65f544c

Browse files
committed
Change discover API
we can't take an owned transaction
1 parent 2fd945b commit 65f544c

6 files changed

Lines changed: 129 additions & 86 deletions

File tree

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub mod sqlite;
1515
pub use sea_query;
1616

1717
pub(crate) mod parser;
18-
pub(crate) mod sqlx_types;
18+
pub mod sqlx_types;
1919
pub(crate) mod util;
2020

2121
pub mod name;

src/mysql/discovery/mod.rs

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,46 @@ use sea_query::{Alias, DynIden, IntoIden, SeaRc};
1616
mod executor;
1717
pub use executor::*;
1818

19-
pub struct SchemaDiscovery<C: Connection> {
19+
pub struct SchemaDiscovery {
2020
pub query: SchemaQueryBuilder,
2121
pub schema: DynIden,
22-
conn: C,
22+
exec: Option<Executor>,
2323
}
2424

25-
impl SchemaDiscovery<Executor> {
25+
impl SchemaDiscovery {
2626
/// Discover schema from a SQLx pool
2727
pub fn new(pool: MySqlPool, schema: &str) -> Self {
28-
Self::conn(pool.into_executor(), schema)
28+
SchemaDiscovery {
29+
query: SchemaQueryBuilder::default(),
30+
schema: Alias::new(schema).into_iden(),
31+
exec: Some(pool.into_executor()),
32+
}
2933
}
30-
}
3134

32-
impl<C: Connection> SchemaDiscovery<C> {
33-
/// Discover schema from a generic SQLx connection
34-
pub fn conn(conn: C, schema: &str) -> Self {
35-
SchemaDiscovery {
35+
#[doc(hidden)]
36+
pub fn new_no_exec(schema: &str) -> Self {
37+
Self {
3638
query: SchemaQueryBuilder::default(),
3739
schema: Alias::new(schema).into_iden(),
38-
conn,
40+
exec: None,
3941
}
4042
}
4143

4244
pub async fn discover(mut self) -> Result<Schema, SqlxError> {
43-
self.query = SchemaQueryBuilder::new(self.discover_system().await?);
45+
let conn = match self.exec.take() {
46+
Some(exec) => exec,
47+
None => return Err(SqlxError::PoolClosed),
48+
};
49+
self.discover_with(&conn).await
50+
}
51+
52+
#[doc(hidden)]
53+
pub async fn discover_with<C: Connection>(mut self, conn: &C) -> Result<Schema, SqlxError> {
54+
self.query = SchemaQueryBuilder::new(self.discover_system_with(conn).await?);
4455
let mut tables = Vec::new();
4556

46-
for table in self.discover_tables().await? {
47-
tables.push(self.discover_table(table).await?);
57+
for table in self.discover_tables_with(conn).await? {
58+
tables.push(self.discover_table_with(conn, table).await?);
4859
}
4960

5061
Ok(Schema {
@@ -54,8 +65,9 @@ impl<C: Connection> SchemaDiscovery<C> {
5465
})
5566
}
5667

57-
pub async fn discover_system(&mut self) -> Result<SystemInfo, SqlxError> {
58-
let rows = self.conn.query_all(self.query.query_version()).await?;
68+
#[doc(hidden)]
69+
async fn discover_system_with<C: Connection>(&self, conn: &C) -> Result<SystemInfo, SqlxError> {
70+
let rows = conn.query_all(self.query.query_version()).await?;
5971

6072
#[allow(clippy::never_loop)]
6173
for row in rows {
@@ -68,9 +80,11 @@ impl<C: Connection> SchemaDiscovery<C> {
6880
Err(SqlxError::RowNotFound)
6981
}
7082

71-
pub async fn discover_tables(&mut self) -> Result<Vec<TableInfo>, SqlxError> {
72-
let rows = self
73-
.conn
83+
async fn discover_tables_with<C: Connection>(
84+
&self,
85+
conn: &C,
86+
) -> Result<Vec<TableInfo>, SqlxError> {
87+
let rows = conn
7488
.query_all(self.query.query_tables(self.schema.clone()))
7589
.await?;
7690

@@ -88,16 +102,20 @@ impl<C: Connection> SchemaDiscovery<C> {
88102
Ok(tables)
89103
}
90104

91-
pub async fn discover_table(&self, info: TableInfo) -> Result<TableDef, SqlxError> {
105+
async fn discover_table_with<C: Connection>(
106+
&self,
107+
conn: &C,
108+
info: TableInfo,
109+
) -> Result<TableDef, SqlxError> {
92110
let table = SeaRc::new(Alias::new(info.name.as_str()));
93111
let columns = self
94-
.discover_columns(self.schema.clone(), table.clone(), &self.query.system)
112+
.discover_columns_with(conn, self.schema.clone(), table.clone(), &self.query.system)
95113
.await?;
96114
let indexes = self
97-
.discover_indexes(self.schema.clone(), table.clone())
115+
.discover_indexes_with(conn, self.schema.clone(), table.clone())
98116
.await?;
99117
let foreign_keys = self
100-
.discover_foreign_keys(self.schema.clone(), table.clone())
118+
.discover_foreign_keys_with(conn, self.schema.clone(), table.clone())
101119
.await?;
102120

103121
Ok(TableDef {
@@ -108,14 +126,14 @@ impl<C: Connection> SchemaDiscovery<C> {
108126
})
109127
}
110128

111-
pub async fn discover_columns(
129+
async fn discover_columns_with<C: Connection>(
112130
&self,
131+
conn: &C,
113132
schema: DynIden,
114133
table: DynIden,
115134
system: &SystemInfo,
116135
) -> Result<Vec<ColumnInfo>, SqlxError> {
117-
let rows = self
118-
.conn
136+
let rows = conn
119137
.query_all(self.query.query_columns(schema.clone(), table.clone()))
120138
.await?;
121139

@@ -133,13 +151,13 @@ impl<C: Connection> SchemaDiscovery<C> {
133151
Ok(columns)
134152
}
135153

136-
pub async fn discover_indexes(
154+
async fn discover_indexes_with<C: Connection>(
137155
&self,
156+
conn: &C,
138157
schema: DynIden,
139158
table: DynIden,
140159
) -> Result<Vec<IndexInfo>, SqlxError> {
141-
let rows = self
142-
.conn
160+
let rows = conn
143161
.query_all(self.query.query_indexes(schema.clone(), table.clone()))
144162
.await?;
145163

@@ -156,13 +174,13 @@ impl<C: Connection> SchemaDiscovery<C> {
156174
.collect())
157175
}
158176

159-
pub async fn discover_foreign_keys(
177+
async fn discover_foreign_keys_with<C: Connection>(
160178
&self,
179+
conn: &C,
161180
schema: DynIden,
162181
table: DynIden,
163182
) -> Result<Vec<ForeignKeyInfo>, SqlxError> {
164-
let rows = self
165-
.conn
183+
let rows = conn
166184
.query_all(self.query.query_foreign_key(schema.clone(), table.clone()))
167185
.await?;
168186

src/postgres/discovery/mod.rs

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,40 +21,54 @@ pub use executor::*;
2121

2222
pub(crate) type EnumVariantMap = HashMap<String, Vec<String>>;
2323

24-
pub struct SchemaDiscovery<C: Connection> {
24+
pub struct SchemaDiscovery {
2525
pub query: SchemaQueryBuilder,
2626
pub schema: DynIden,
27-
conn: C,
27+
exec: Option<Executor>,
2828
}
2929

30-
impl SchemaDiscovery<Executor> {
30+
impl SchemaDiscovery {
3131
/// Discover schema from a SQLx pool
3232
pub fn new(pool: PgPool, schema: &str) -> Self {
33-
Self::conn(pool.into_executor(), schema)
33+
SchemaDiscovery {
34+
query: SchemaQueryBuilder::default(),
35+
schema: Alias::new(schema).into_iden(),
36+
exec: Some(pool.into_executor()),
37+
}
3438
}
35-
}
3639

37-
impl<C: Connection> SchemaDiscovery<C> {
38-
/// Discover schema from a generic SQLx connection
39-
pub fn conn(conn: C, schema: &str) -> Self {
40-
SchemaDiscovery {
40+
#[doc(hidden)]
41+
pub fn new_no_exec(schema: &str) -> Self {
42+
Self {
4143
query: SchemaQueryBuilder::default(),
4244
schema: Alias::new(schema).into_iden(),
43-
conn,
45+
exec: None,
46+
}
47+
}
48+
49+
fn conn(&self) -> Result<&Executor, SqlxError> {
50+
match &self.exec {
51+
Some(exec) => Ok(exec),
52+
None => Err(SqlxError::PoolClosed),
4453
}
4554
}
4655

4756
pub async fn discover(&self) -> Result<Schema, SqlxError> {
57+
self.discover_with(self.conn()?).await
58+
}
59+
60+
#[doc(hidden)]
61+
pub async fn discover_with<C: Connection>(&self, conn: &C) -> Result<Schema, SqlxError> {
4862
let enums: EnumVariantMap = self
49-
.discover_enums()
63+
.discover_enums_with(conn)
5064
.await?
5165
.into_iter()
5266
.map(|enum_def| (enum_def.typename, enum_def.values))
5367
.collect();
5468

5569
let mut tables = Vec::new();
56-
for table in self.discover_tables().await? {
57-
tables.push(self.discover_table(table, &enums).await?);
70+
for table in self.discover_tables_with(conn).await? {
71+
tables.push(self.discover_table_with(conn, table, &enums).await?);
5872
}
5973

6074
Ok(Schema {
@@ -63,9 +77,11 @@ impl<C: Connection> SchemaDiscovery<C> {
6377
})
6478
}
6579

66-
pub async fn discover_tables(&self) -> Result<Vec<TableInfo>, SqlxError> {
67-
let rows = self
68-
.conn
80+
async fn discover_tables_with<C: Connection>(
81+
&self,
82+
conn: &C,
83+
) -> Result<Vec<TableInfo>, SqlxError> {
84+
let rows = conn
6985
.query_all(self.query.query_tables(self.schema.clone()))
7086
.await?;
7187

@@ -83,17 +99,18 @@ impl<C: Connection> SchemaDiscovery<C> {
8399
Ok(tables)
84100
}
85101

86-
pub async fn discover_table(
102+
async fn discover_table_with<C: Connection>(
87103
&self,
104+
conn: &C,
88105
info: TableInfo,
89106
enums: &EnumVariantMap,
90107
) -> Result<TableDef, SqlxError> {
91108
let table = SeaRc::new(Alias::new(info.name.as_str()));
92109
let columns = self
93-
.discover_columns(self.schema.clone(), table.clone(), enums)
110+
.discover_columns_with(conn, self.schema.clone(), table.clone(), enums)
94111
.await?;
95112
let constraints = self
96-
.discover_constraints(self.schema.clone(), table.clone())
113+
.discover_constraints_with(conn, self.schema.clone(), table.clone())
97114
.await?;
98115
let (
99116
check_constraints,
@@ -117,7 +134,7 @@ impl<C: Connection> SchemaDiscovery<C> {
117134
);
118135

119136
let unique_constraints = self
120-
.discover_unique_indexes(self.schema.clone(), table.clone())
137+
.discover_unique_indexes_with(conn, self.schema.clone(), table.clone())
121138
.await?;
122139

123140
Ok(TableDef {
@@ -132,14 +149,14 @@ impl<C: Connection> SchemaDiscovery<C> {
132149
})
133150
}
134151

135-
pub async fn discover_columns(
152+
async fn discover_columns_with<C: Connection>(
136153
&self,
154+
conn: &C,
137155
schema: DynIden,
138156
table: DynIden,
139157
enums: &EnumVariantMap,
140158
) -> Result<Vec<ColumnInfo>, SqlxError> {
141-
let rows = self
142-
.conn
159+
let rows = conn
143160
.query_all(self.query.query_columns(schema.clone(), table.clone()))
144161
.await?;
145162

@@ -155,13 +172,13 @@ impl<C: Connection> SchemaDiscovery<C> {
155172
.collect())
156173
}
157174

158-
pub async fn discover_constraints(
175+
async fn discover_constraints_with<C: Connection>(
159176
&self,
177+
conn: &C,
160178
schema: DynIden,
161179
table: DynIden,
162180
) -> Result<Vec<Constraint>, SqlxError> {
163-
let rows = self
164-
.conn
181+
let rows = conn
165182
.query_all(
166183
self.query
167184
.query_table_constraints(schema.clone(), table.clone()),
@@ -181,13 +198,13 @@ impl<C: Connection> SchemaDiscovery<C> {
181198
.collect())
182199
}
183200

184-
pub async fn discover_unique_indexes(
201+
async fn discover_unique_indexes_with<C: Connection>(
185202
&self,
203+
conn: &C,
186204
schema: DynIden,
187205
table: DynIden,
188206
) -> Result<Vec<Unique>, SqlxError> {
189-
let rows = self
190-
.conn
207+
let rows = conn
191208
.query_all(
192209
self.query
193210
.query_table_unique_indexes(schema.clone(), table.clone()),
@@ -208,7 +225,15 @@ impl<C: Connection> SchemaDiscovery<C> {
208225
}
209226

210227
pub async fn discover_enums(&self) -> Result<Vec<EnumDef>, SqlxError> {
211-
let rows = self.conn.query_all(self.query.query_enums()).await?;
228+
self.discover_enums_with(self.conn()?).await
229+
}
230+
231+
#[doc(hidden)]
232+
pub async fn discover_enums_with<C: Connection>(
233+
&self,
234+
conn: &C,
235+
) -> Result<Vec<EnumDef>, SqlxError> {
236+
let rows = conn.query_all(self.query.query_enums()).await?;
212237

213238
let enum_rows = rows.into_iter().map(|row| {
214239
let result: EnumQueryResult = row.into();

0 commit comments

Comments
 (0)