Debian Codegraph @

tests/mysql/migrate.rs

raw symbols SCIP
1 use sqlx::migrate::Migrator;
2 use sqlx::mysql::{MySql, MySqlConnection};
3 use sqlx::pool::PoolConnection;
4 use sqlx::Executor;
5 use sqlx::Row;
6 use std::path::Path;
7
8 #[sqlx::test(migrations = false)]
9 async fn simple(mut conn: PoolConnection<MySql>) -> anyhow::Result<()> {
10 clean_up(&mut conn).await?;
11
12 let migrator = Migrator::new(Path::new("tests/mysql/migrations_simple")).await?;
13
14 // run migration
15 migrator.run(&mut conn).await?;
16
17 // check outcome
18 let res: String = conn
19 .fetch_one("SELECT some_payload FROM migrations_simple_test")
20 .await?
21 .get(0);
22 assert_eq!(res, "110_suffix");
23
24 // running it a 2nd time should still work
25 migrator.run(&mut conn).await?;
26
27 Ok(())
28 }
29
30 #[sqlx::test(migrations = false)]
31 async fn reversible(mut conn: PoolConnection<MySql>) -> anyhow::Result<()> {
32 clean_up(&mut conn).await?;
33
34 let migrator = Migrator::new(Path::new("tests/mysql/migrations_reversible")).await?;
35
36 // run migration
37 migrator.run(&mut conn).await?;
38
39 // check outcome
40 let res: i64 = conn
41 .fetch_one("SELECT some_payload FROM migrations_reversible_test")
42 .await?
43 .get(0);
44 assert_eq!(res, 101);
45
46 // roll back nothing (last version)
47 migrator.undo(&mut conn, 20220721125033).await?;
48
49 // check outcome
50 let res: i64 = conn
51 .fetch_one("SELECT some_payload FROM migrations_reversible_test")
52 .await?
53 .get(0);
54 assert_eq!(res, 101);
55
56 // roll back one version
57 migrator.undo(&mut conn, 20220721124650).await?;
58
59 // check outcome
60 let res: i64 = conn
61 .fetch_one("SELECT some_payload FROM migrations_reversible_test")
62 .await?
63 .get(0);
64 assert_eq!(res, 100);
65
66 Ok(())
67 }
68
69 /// Ensure that we have a clean initial state.
70 async fn clean_up(conn: &mut MySqlConnection) -> anyhow::Result<()> {
71 conn.execute("DROP TABLE migrations_simple_test").await.ok();
72 conn.execute("DROP TABLE migrations_reversible_test")
73 .await
74 .ok();
75 conn.execute("DROP TABLE _sqlx_migrations").await.ok();
76
77 Ok(())
78 }