@@ -38,27 +38,29 @@ pub async fn migrate(conn: &mut sqlx::PgConnection, target: Option<i64>) -> Resu
38
38
. await ?
39
39
. is_some ( )
40
40
{
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" )
42
42
. fetch_one ( & mut * conn)
43
43
. await ?;
44
44
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" ) ;
60
47
}
61
48
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
+
62
64
sqlx:: query ( "DROP TABLE database_versions" )
63
65
. execute ( & mut * conn)
64
66
. await ?;
0 commit comments