1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use ;
use crateAsyncConnectionWrapper;
use crateAsyncConnection;
/// A diesel-migration [`MigrationHarness`](diesel_migrations::MigrationHarness) to run migrations
/// via an [`AsyncConnection`](crate::AsyncConnection)
///
/// Internally this harness is using [`tokio::task::block_in_place`] and [`AsyncConnectionWrapper`]
/// to utilize sync Diesel's migration infrastructure. For most applications this shouldn't
/// be problematic as migrations are usually run at application startup and most applications
/// default to use the multithreaded tokio runtime. In turn this also means that you cannot use
/// this migration harness if you use the current thread variant of the tokio runtime or if
/// you run migrations in a very special setup (e.g by using [`tokio::select!`] or [`tokio::join!`]
/// on a future produced by running the migrations). Consider manually construct a blocking task via
/// [`tokio::task::spawn_blocking`] instead.
///
/// ## Example
///
/// ```no_run
/// # include!("doctest_setup.rs");
/// # async fn run_test() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>{
/// use diesel_async::AsyncMigrationHarness;
/// use diesel_migrations::{FileBasedMigrations, MigrationHarness};
///
/// let mut connection = connection_no_data().await;
///
/// // Alternativly use `diesel_migrations::embed_migrations!()`
/// // to get a list of migrations
/// let migrations = FileBasedMigrations::find_migrations_directory()?;
///
/// let mut harness = AsyncMigrationHarness::new(connection);
/// harness.run_pending_migrations(migrations)?;
/// // get back the connection from the harness
/// let connection = harness.into_inner();
/// # Ok(())
/// # }
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # run_test().await?;
/// # Ok(())
/// # }
/// ```
///
/// ## Example with pool
///
/// ```no_run
/// # include!("doctest_setup.rs");
/// # #[cfg(feature = "deadpool")]
/// # use diesel_async::pooled_connection::AsyncDieselConnectionManager;
/// #
/// # #[cfg(all(feature = "postgres", feature = "deadpool"))]
/// # fn get_config() -> AsyncDieselConnectionManager<diesel_async::AsyncPgConnection> {
/// # let db_url = database_url_from_env("PG_DATABASE_URL");
/// let config = AsyncDieselConnectionManager::<diesel_async::AsyncPgConnection>::new(db_url);
/// # config
/// # }
/// #
/// # #[cfg(all(feature = "mysql", feature = "deadpool"))]
/// # fn get_config() -> AsyncDieselConnectionManager<diesel_async::AsyncMysqlConnection> {
/// # let db_url = database_url_from_env("MYSQL_DATABASE_URL");
/// # let config = AsyncDieselConnectionManager::<diesel_async::AsyncMysqlConnection>::new(db_url);
/// # config
/// # }
/// #
/// # #[cfg(all(feature = "sqlite", feature = "deadpool"))]
/// # fn get_config() -> AsyncDieselConnectionManager<diesel_async::sync_connection_wrapper::SyncConnectionWrapper<diesel::SqliteConnection>> {
/// # let db_url = database_url_from_env("SQLITE_DATABASE_URL");
/// # let config = AsyncDieselConnectionManager::<diesel_async::sync_connection_wrapper::SyncConnectionWrapper<diesel::SqliteConnection>>::new(db_url);
/// # config
/// # }
/// # #[cfg(feature = "deadpool")]
/// # async fn run_test() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>{
/// use diesel_async::pooled_connection::deadpool::Pool;
/// use diesel_async::AsyncMigrationHarness;
/// use diesel_migrations::{FileBasedMigrations, MigrationHarness};
///
/// // Alternativly use `diesel_migrations::embed_migrations!()`
/// // to get a list of migrations
/// let migrations = FileBasedMigrations::find_migrations_directory()?;
///
/// let pool = Pool::builder(get_config()).build()?;
/// let mut harness = AsyncMigrationHarness::new(pool.get().await?);
/// harness.run_pending_migrations(migrations)?;
/// # Ok(())
/// # }
///
/// # #[cfg(not(feature = "deadpool"))]
/// # async fn run_test() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>>
/// # {
/// # Ok(())
/// # }
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
/// # run_test().await?;
/// # Ok(())
/// # }
/// ```