mirror of
https://github.com/SECTL/SecScore.git
synced 2026-07-19 21:14:21 +08:00
修复迁移兼容性问题,避免切换数据库失败
This commit is contained in:
@@ -123,24 +123,26 @@ impl Migration {
|
|||||||
sqlite: bool,
|
sqlite: bool,
|
||||||
) -> Result<(), DbErr> {
|
) -> Result<(), DbErr> {
|
||||||
let db_backend = Self::get_db_backend(sqlite);
|
let db_backend = Self::get_db_backend(sqlite);
|
||||||
let exists_sql = if sqlite {
|
let alter_sql = "ALTER TABLE students ADD COLUMN reward_points INTEGER DEFAULT 0";
|
||||||
"SELECT 1 AS exists FROM pragma_table_info('students') WHERE name = 'reward_points' LIMIT 1"
|
let result = conn
|
||||||
.to_string()
|
.execute(Statement::from_string(db_backend.clone(), alter_sql.to_string()))
|
||||||
} else {
|
.await;
|
||||||
"SELECT 1 AS exists FROM information_schema.columns WHERE table_name = 'students' AND column_name = 'reward_points' LIMIT 1"
|
|
||||||
.to_string()
|
|
||||||
};
|
|
||||||
|
|
||||||
let exists = conn
|
match result {
|
||||||
.query_one(Statement::from_string(db_backend.clone(), exists_sql))
|
Ok(_) => {
|
||||||
.await?
|
info!("Added students.reward_points column");
|
||||||
.is_some();
|
}
|
||||||
|
Err(e) => {
|
||||||
if !exists {
|
let msg = e.to_string().to_lowercase();
|
||||||
let alter_sql = "ALTER TABLE students ADD COLUMN reward_points INTEGER DEFAULT 0";
|
let already_exists = msg.contains("duplicate column")
|
||||||
conn.execute(Statement::from_string(db_backend.clone(), alter_sql.to_string()))
|
|| msg.contains("already exists")
|
||||||
.await?;
|
|| msg.contains("duplicate");
|
||||||
info!("Added students.reward_points column");
|
if already_exists {
|
||||||
|
info!("students.reward_points already exists, skip alter");
|
||||||
|
} else {
|
||||||
|
return Err(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user