-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathmigrate.rs
118 lines (90 loc) · 3.05 KB
/
migrate.rs
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
use sqlx::migrate::Migrator;
use sqlx::pool::PoolConnection;
use sqlx::postgres::{PgConnection, Postgres};
use sqlx::Executor;
use sqlx::Row;
use std::path::Path;
#[sqlx::test(migrations = false)]
async fn simple(mut conn: PoolConnection<Postgres>) -> anyhow::Result<()> {
clean_up(&mut conn).await?;
let migrator = Migrator::new(Path::new("tests/postgres/migrations_simple")).await?;
// run migration
migrator.run(&mut conn).await?;
// check outcome
let res: String = conn
.fetch_one("SELECT some_payload FROM migrations_simple_test")
.await?
.get(0);
assert_eq!(res, "110_suffix");
// running it a 2nd time should still work
migrator.run(&mut conn).await?;
Ok(())
}
#[sqlx::test(migrations = false)]
async fn reversible(mut conn: PoolConnection<Postgres>) -> anyhow::Result<()> {
clean_up(&mut conn).await?;
let migrator = Migrator::new(Path::new("tests/postgres/migrations_reversible")).await?;
// run migration
migrator.run(&mut conn).await?;
// check outcome
let res: i64 = conn
.fetch_one("SELECT some_payload FROM migrations_reversible_test")
.await?
.get(0);
assert_eq!(res, 101);
// roll back nothing (last version)
migrator.undo(&mut conn, 20220721125033).await?;
// check outcome
let res: i64 = conn
.fetch_one("SELECT some_payload FROM migrations_reversible_test")
.await?
.get(0);
assert_eq!(res, 101);
// roll back one version
migrator.undo(&mut conn, 20220721124650).await?;
// check outcome
let res: i64 = conn
.fetch_one("SELECT some_payload FROM migrations_reversible_test")
.await?
.get(0);
assert_eq!(res, 100);
Ok(())
}
#[sqlx::test(migrations = false)]
async fn no_tx(mut conn: PoolConnection<Postgres>) -> anyhow::Result<()> {
clean_up(&mut conn).await?;
let migrator = Migrator::new(Path::new("tests/postgres/migrations_no_tx")).await?;
// run migration
migrator.run(&mut conn).await?;
// check outcome
let res: String = conn
.fetch_one("SELECT datname FROM pg_database WHERE datname = 'test_db'")
.await?
.get(0);
assert_eq!(res, "test_db");
Ok(())
}
#[sqlx::test(migrations = false)]
async fn split(mut conn: PoolConnection<Postgres>) -> anyhow::Result<()> {
clean_up(&mut conn).await?;
let migrator = Migrator::new(Path::new("tests/postgres/migrations_split")).await?;
// run migration
migrator.run(&mut conn).await?;
// check outcome
let res: i32 = conn
.fetch_one("SELECT * FROM test_table")
.await?
.get(0);
assert_eq!(res, 1);
Ok(())
}
/// Ensure that we have a clean initial state.
async fn clean_up(conn: &mut PgConnection) -> anyhow::Result<()> {
conn.execute("DROP DATABASE IF EXISTS test_db").await.ok();
conn.execute("DROP TABLE migrations_simple_test").await.ok();
conn.execute("DROP TABLE migrations_reversible_test")
.await
.ok();
conn.execute("DROP TABLE _sqlx_migrations").await.ok();
Ok(())
}