Skip to content

Commit 26f9666

Browse files
committed
stop migration of migration state when we find an unexpected version
1 parent ce16710 commit 26f9666

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/db/mod.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,27 +38,29 @@ pub async fn migrate(conn: &mut sqlx::PgConnection, target: Option<i64>) -> Resu
3838
.await?
3939
.is_some()
4040
{
41-
let count: i64 = sqlx::query_scalar("SELECT count(*) FROM database_versions")
41+
let max_version: i64 = sqlx::query_scalar("SELECT max(version) FROM database_versions")
4242
.fetch_one(&mut *conn)
4343
.await?;
4444

45-
if count > 0 {
46-
sqlx::query(
47-
"INSERT INTO _sqlx_migrations ( version, description, success, checksum, execution_time )
48-
VALUES ( $1, $2, TRUE, $3, -1 )",
49-
)
50-
// the next two parameters relate to the filename of the initial migration file
51-
.bind(20231021111635i64)
52-
.bind("initial")
53-
// this is the hash of the initial migration file, as sqlx requires it.
54-
// if the initial migration file changes, this has to be updated with the new value,
55-
// easiest to get from the `_sqlx_migrations` table when the migration was normally
56-
// executed.
57-
.bind(hex::decode("df802e0ec416063caadd1c06b13348cd885583c44962998886b929d5fe6ef3b70575d5101c5eb31daa989721df08d806").unwrap())
58-
.execute(&mut *conn)
59-
.await?;
45+
if max_version != 39 {
46+
anyhow::bail!("database_versions table has unexpected version");
6047
}
6148

49+
sqlx::query(
50+
"INSERT INTO _sqlx_migrations ( version, description, success, checksum, execution_time )
51+
VALUES ( $1, $2, TRUE, $3, -1 )",
52+
)
53+
// the next two parameters relate to the filename of the initial migration file
54+
.bind(20231021111635i64)
55+
.bind("initial")
56+
// this is the hash of the initial migration file, as sqlx requires it.
57+
// if the initial migration file changes, this has to be updated with the new value,
58+
// easiest to get from the `_sqlx_migrations` table when the migration was normally
59+
// executed.
60+
.bind(hex::decode("df802e0ec416063caadd1c06b13348cd885583c44962998886b929d5fe6ef3b70575d5101c5eb31daa989721df08d806").unwrap())
61+
.execute(&mut *conn)
62+
.await?;
63+
6264
sqlx::query("DROP TABLE database_versions")
6365
.execute(&mut *conn)
6466
.await?;

0 commit comments

Comments
 (0)