@@ -7,13 +7,16 @@ use crate::{Schema, SchemaBuilder};
77use std:: future:: Future ;
88use std:: pin:: Pin ;
99
10- /// A wrapper that holds either a reference to a [`DatabaseConnection`] or [`DatabaseTransaction`].
10+ /// A wrapper that holds either a reference to a [`DatabaseConnection`] or [`DatabaseTransaction`],
11+ /// or an owned [`DatabaseTransaction`].
1112#[ derive( Debug ) ]
1213pub enum DatabaseExecutor < ' c > {
1314 /// A reference to a database connection
1415 Connection ( & ' c DatabaseConnection ) ,
1516 /// A reference to a database transaction
1617 Transaction ( & ' c DatabaseTransaction ) ,
18+ /// An owned database transaction (used by migration's `SchemaManager::begin()`)
19+ OwnedTransaction ( DatabaseTransaction ) ,
1720}
1821
1922impl < ' c > From < & ' c DatabaseConnection > for DatabaseExecutor < ' c > {
@@ -33,34 +36,39 @@ impl ConnectionTrait for DatabaseExecutor<'_> {
3336 match self {
3437 DatabaseExecutor :: Connection ( conn) => conn. get_database_backend ( ) ,
3538 DatabaseExecutor :: Transaction ( trans) => trans. get_database_backend ( ) ,
39+ DatabaseExecutor :: OwnedTransaction ( trans) => trans. get_database_backend ( ) ,
3640 }
3741 }
3842
3943 fn execute_raw ( & self , stmt : Statement ) -> Result < ExecResult , DbErr > {
4044 match self {
4145 DatabaseExecutor :: Connection ( conn) => conn. execute_raw ( stmt) ,
4246 DatabaseExecutor :: Transaction ( trans) => trans. execute_raw ( stmt) ,
47+ DatabaseExecutor :: OwnedTransaction ( trans) => trans. execute_raw ( stmt) ,
4348 }
4449 }
4550
4651 fn execute_unprepared ( & self , sql : & str ) -> Result < ExecResult , DbErr > {
4752 match self {
4853 DatabaseExecutor :: Connection ( conn) => conn. execute_unprepared ( sql) ,
4954 DatabaseExecutor :: Transaction ( trans) => trans. execute_unprepared ( sql) ,
55+ DatabaseExecutor :: OwnedTransaction ( trans) => trans. execute_unprepared ( sql) ,
5056 }
5157 }
5258
5359 fn query_one_raw ( & self , stmt : Statement ) -> Result < Option < QueryResult > , DbErr > {
5460 match self {
5561 DatabaseExecutor :: Connection ( conn) => conn. query_one_raw ( stmt) ,
5662 DatabaseExecutor :: Transaction ( trans) => trans. query_one_raw ( stmt) ,
63+ DatabaseExecutor :: OwnedTransaction ( trans) => trans. query_one_raw ( stmt) ,
5764 }
5865 }
5966
6067 fn query_all_raw ( & self , stmt : Statement ) -> Result < Vec < QueryResult > , DbErr > {
6168 match self {
6269 DatabaseExecutor :: Connection ( conn) => conn. query_all_raw ( stmt) ,
6370 DatabaseExecutor :: Transaction ( trans) => trans. query_all_raw ( stmt) ,
71+ DatabaseExecutor :: OwnedTransaction ( trans) => trans. query_all_raw ( stmt) ,
6472 }
6573 }
6674}
@@ -72,6 +80,7 @@ impl TransactionTrait for DatabaseExecutor<'_> {
7280 match self {
7381 DatabaseExecutor :: Connection ( conn) => conn. begin ( ) ,
7482 DatabaseExecutor :: Transaction ( trans) => trans. begin ( ) ,
83+ DatabaseExecutor :: OwnedTransaction ( trans) => trans. begin ( ) ,
7584 }
7685 }
7786
@@ -87,6 +96,9 @@ impl TransactionTrait for DatabaseExecutor<'_> {
8796 DatabaseExecutor :: Transaction ( trans) => {
8897 trans. begin_with_config ( isolation_level, access_mode)
8998 }
99+ DatabaseExecutor :: OwnedTransaction ( trans) => {
100+ trans. begin_with_config ( isolation_level, access_mode)
101+ }
90102 }
91103 }
92104
@@ -97,6 +109,7 @@ impl TransactionTrait for DatabaseExecutor<'_> {
97109 match self {
98110 DatabaseExecutor :: Connection ( conn) => conn. begin_with_options ( options) ,
99111 DatabaseExecutor :: Transaction ( trans) => trans. begin_with_options ( options) ,
112+ DatabaseExecutor :: OwnedTransaction ( trans) => trans. begin_with_options ( options) ,
100113 }
101114 }
102115
@@ -108,6 +121,7 @@ impl TransactionTrait for DatabaseExecutor<'_> {
108121 match self {
109122 DatabaseExecutor :: Connection ( conn) => conn. transaction ( callback) ,
110123 DatabaseExecutor :: Transaction ( trans) => trans. transaction ( callback) ,
124+ DatabaseExecutor :: OwnedTransaction ( trans) => trans. transaction ( callback) ,
111125 }
112126 }
113127
@@ -128,6 +142,9 @@ impl TransactionTrait for DatabaseExecutor<'_> {
128142 DatabaseExecutor :: Transaction ( trans) => {
129143 trans. transaction_with_config ( callback, isolation_level, access_mode)
130144 }
145+ DatabaseExecutor :: OwnedTransaction ( trans) => {
146+ trans. transaction_with_config ( callback, isolation_level, access_mode)
147+ }
131148 }
132149 }
133150}
@@ -159,7 +176,21 @@ impl<'c> IntoDatabaseExecutor<'c> for &'c DatabaseTransaction {
159176 }
160177}
161178
179+ impl IntoDatabaseExecutor < ' static > for DatabaseTransaction {
180+ fn into_database_executor ( self ) -> DatabaseExecutor < ' static > {
181+ DatabaseExecutor :: OwnedTransaction ( self )
182+ }
183+ }
184+
162185impl DatabaseExecutor < ' _ > {
186+ /// Returns `true` if this executor is backed by a transaction (borrowed or owned).
187+ pub fn is_transaction ( & self ) -> bool {
188+ matches ! (
189+ self ,
190+ DatabaseExecutor :: Transaction ( _) | DatabaseExecutor :: OwnedTransaction ( _)
191+ )
192+ }
193+
163194 /// Creates a [`SchemaBuilder`] for this backend
164195 pub fn get_schema_builder ( & self ) -> SchemaBuilder {
165196 Schema :: new ( self . get_database_backend ( ) ) . builder ( )
0 commit comments