@@ -21,40 +21,54 @@ pub use executor::*;
2121
2222pub ( 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