use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
/// The width an argon2id hash needs. The encoded form is about 98 characters
/// for the default parameters, so this leaves room to tune them upward.
const HASH_LEN: u32 = 128;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// The column holds an argon2id hash now, not a password, and a hash is
// far longer than the 32 characters `passwd` declared. SQLite cannot
// change a column's type in place, so the replacement is added, filled
// from the old column, and the old one dropped.
manager
.alter_table(
Table::alter()
.table(Users::Table)
.add_column(
ColumnDef::new(Users::PasswordHash)
.string_len(HASH_LEN)
.not_null()
.default("")
.to_owned(),
)
.to_owned(),
)
.await?;
manager
.get_connection()
.execute_unprepared("UPDATE users SET password_hash = passwd")
.await?;
manager
.alter_table(
Table::alter()
.table(Users::Table)
.drop_column(Users::Passwd)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(Users::Table)
.add_column(
ColumnDef::new(Users::Passwd)
.string_len(32)
.not_null()
.default("")
.to_owned(),
)
.to_owned(),
)
.await?;
manager
.get_connection()
.execute_unprepared("UPDATE users SET passwd = password_hash")
.await?;
manager
.alter_table(
Table::alter()
.table(Users::Table)
.drop_column(Users::PasswordHash)
.to_owned(),
)
.await
}
}
#[derive(Iden)]
pub enum Users {
Table,
Passwd,
PasswordHash,
}