@@ -163,25 +163,31 @@ async fn main() {
163
163
# Running Migrations
164
164
165
165
You can run migrations directly or integrate them into a CLI:
166
- ## Direct Execution
166
+ ## Programmatic Execution
167
167
``` rust
168
168
use sqlx_migrator :: migrator :: Plan ;
169
169
let mut conn = pool . acquire (). await ? ;
170
170
// use apply all to apply all pending migration
171
171
migrator . run (& mut * conn , Plan :: apply_all ()). await . unwrap ();
172
172
// or use revert all to revert all applied migrations
173
173
migrator . run (& mut * conn , Plan :: revert_all ()). await . unwrap ();
174
+ // If you need to apply or revert to certain stage than see `Plan` docs
174
175
```
175
176
176
177
## CLI Integration
178
+ To integrate sqlx_migrator into your CLI, you can either use the built-in
179
+ ` MigrationCommand ` or extend your own CLI with migrator support. Below are
180
+ examples of both approaches:
181
+
182
+ #### Built-in Migration Command
177
183
178
184
``` rust
179
185
use sqlx_migrator :: cli :: MigrationCommand ;
180
186
181
187
MigrationCommand :: parse_and_run (& mut * conn , Box :: new (migrator )). await . unwrap ();
182
188
```
183
189
184
- Or extend your own CLI with migrator support:
190
+ #### Extending Your Own CLI with Migrator Support
185
191
186
192
``` rust
187
193
#[derive(clap:: Parser )]
@@ -211,31 +217,29 @@ impl Cli {
211
217
212
218
# Migrate from sqlx default sql based migration
213
219
214
- To migrate from sqlx sql based migration you have two alternative:
220
+ To migrate from sqlx sql based migration to rust migration the recommended approach
221
+ is to rewrite your SQL migrations as Rust operations and migrations as explained above.
222
+ After rewriting your SQL migrations, you need to mark them as applied without re-executing them.
223
+ This step ensures that the migration state aligns with the existing database.
224
+ There are two ways to perform a fake apply:
215
225
216
- 1 . Rewrite SQL migrations as Rust operations and run fake apply cli command
217
- ` <COMMAND_NAME> apply --fake ` : Follow the usage example above.
218
- 2 . Create a single Rust operation to apply/revert SQL migrations:
226
+ #### Programmatic Fake Apply
219
227
228
+ Use the fake option with the ` Plan::apply_all() ` function:
220
229
``` rust
221
- use sqlx_migrator :: error :: Error ;
222
- use sqlx_migrator :: operation :: Operation ;
230
+ use sqlx_migrator :: migrator :: {Plan , Migrator };
223
231
224
- pub (crate ) struct SqlxOperation ;
232
+ migrator . run (& mut * conn , Plan :: apply_all (). fake (true )). await . unwrap ();
233
+ ```
225
234
226
- #[async_trait:: async_trait]
227
- impl Operation <sqlx :: Postgres > for SqlxOperation {
228
- async fn up (& self , connection : & mut sqlx :: PgConnection ) -> Result <(), Error > {
229
- sqlx :: migrate! (" migrations" ). run (connection ). await ? ;
230
- Ok (())
231
- }
235
+ #### CLI-Based Fake Apply
236
+ If you're using a CLI, use the --fake flag with the apply command: ` <migrator_cli_command> apply --fake `
232
237
233
- async fn down (& self , connection : & mut sqlx :: PgConnection ) -> Result <(), Error > {
234
- sqlx :: migrate! (" migrations" ). undo (connection , 0 ). await ? ;
235
- Ok (())
236
- }
237
- }
238
- ```
238
+ ### Note: Before writing any other migrations
239
+
240
+ Before adding new migrations for future updates, ensure you complete the above steps to mark existing migrations as applied. Run the fake apply only once to align the migration state. After this, remove the ` fake(true) ` option or the ` --fake ` flag to allow new migrations to execute normally.
241
+
242
+ By following these steps, you can seamlessly transition from SQLX SQL-based migrations to Rust migrations while maintaining an accurate migration state and ensuring compatibility for future updates.
239
243
240
244
[ license_badge ] : https://img.shields.io/github/license/iamsauravsharma/sqlx_migrator.svg?style=for-the-badge
241
245
[ license_link ] : LICENSE
0 commit comments