@@ -11,6 +11,7 @@ use sea_schema::{mysql::MySql, postgres::Postgres, probe::SchemaProbe, sqlite::S
11
11
/// Helper struct for writing migration scripts in migration file
12
12
pub struct SchemaManager < ' c > {
13
13
conn : SchemaManagerConnection < ' c > ,
14
+ schema : Option < String > ,
14
15
}
15
16
16
17
impl < ' c > SchemaManager < ' c > {
@@ -20,6 +21,7 @@ impl<'c> SchemaManager<'c> {
20
21
{
21
22
Self {
22
23
conn : conn. into_schema_manager_connection ( ) ,
24
+ schema : None ,
23
25
}
24
26
}
25
27
@@ -38,6 +40,18 @@ impl<'c> SchemaManager<'c> {
38
40
pub fn get_connection ( & self ) -> & SchemaManagerConnection < ' c > {
39
41
& self . conn
40
42
}
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
+ }
41
55
}
42
56
43
57
/// Schema Creation
@@ -100,20 +114,7 @@ impl<'c> SchemaManager<'c> {
100
114
where
101
115
T : AsRef < str > ,
102
116
{
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
117
118
}
118
119
119
120
pub async fn has_column < T , C > ( & self , table : T , column : C ) -> Result < bool , DbErr >
@@ -158,3 +159,27 @@ impl<'c> SchemaManager<'c> {
158
159
res. try_get ( "" , "has_index" )
159
160
}
160
161
}
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
+ }
0 commit comments