Create user and keypair migrations

This commit is contained in:
Evie Viau-Chow-Stuart 2023-06-20 20:12:45 -04:00
parent 3d18134278
commit 8e4adedf98
Signed by: evie
GPG key ID: 928652CDFCEC8099
4 changed files with 95 additions and 22 deletions

View file

@ -14,9 +14,6 @@ async-std = { version = "1", features = ["attributes", "tokio1"] }
[dependencies.sea-orm-migration] [dependencies.sea-orm-migration]
version = "0.11.0" version = "0.11.0"
features = [ features = [
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI. "runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
# View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime. "sqlx-postgres", # `DATABASE_DRIVER` feature
# e.g.
# "runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
# "sqlx-postgres", # `DATABASE_DRIVER` feature
] ]

View file

@ -1,6 +1,7 @@
pub use sea_orm_migration::prelude::*; pub use sea_orm_migration::prelude::*;
mod m20230619_221344_create_user; mod m20230619_221344_create_user;
mod m20230620_232650_create_keypair;
pub struct Migrator; pub struct Migrator;
@ -9,6 +10,7 @@ impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> { fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![ vec![
Box::new(m20230619_221344_create_user::Migration), Box::new(m20230619_221344_create_user::Migration),
Box::new(m20230620_232650_create_keypair::Migration),
] ]
} }
} }

View file

@ -6,43 +6,69 @@ pub struct Migration;
#[async_trait::async_trait] #[async_trait::async_trait]
impl MigrationTrait for Migration { impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Replace the sample below with your own migration scripts
todo!();
manager manager
.create_table( .create_table(
Table::create() Table::create()
.table(Post::Table) .table(User::Table)
.if_not_exists() .if_not_exists()
.col(ColumnDef::new(User::Id).text().not_null().primary_key())
.col( .col(
ColumnDef::new(Post::Id) ColumnDef::new(User::Activated)
.integer() .boolean()
.not_null() .not_null()
.auto_increment() .default(false),
.primary_key(), )
.col(ColumnDef::new(User::Username).text().not_null())
.col(ColumnDef::new(User::Email).text().not_null())
.col(ColumnDef::new(User::PasswordHash).text().not_null())
.col(ColumnDef::new(User::MfaKey).text())
.col(ColumnDef::new(User::DisplayName).text().not_null())
.col(ColumnDef::new(User::Summary).text().not_null().default(""))
.col(ColumnDef::new(User::Created).date_time().not_null())
.col(
ColumnDef::new(User::ManuallyApprovesFollowers)
.boolean()
.not_null()
.default(false),
)
.col(
ColumnDef::new(User::Discoverable)
.boolean()
.not_null()
.default(false),
)
.col(
ColumnDef::new(User::AlwaysMarkAsNsfw)
.boolean()
.not_null()
.default(false),
) )
.col(ColumnDef::new(Post::Title).string().not_null())
.col(ColumnDef::new(Post::Text).string().not_null())
.to_owned(), .to_owned(),
) )
.await .await
} }
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Replace the sample below with your own migration scripts
todo!();
manager manager
.drop_table(Table::drop().table(Post::Table).to_owned()) .drop_table(Table::drop().table(User::Table).to_owned())
.await .await
} }
} }
/// Learn more at https://docs.rs/sea-query#iden /// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)] #[derive(Iden)]
enum User { pub enum User {
Table, Table,
Id, Id,
Title, Activated,
Text, Username,
Email,
PasswordHash,
MfaKey,
DisplayName,
Summary,
Created,
ManuallyApprovesFollowers,
Discoverable,
AlwaysMarkAsNsfw,
} }

View file

@ -0,0 +1,48 @@
use crate::m20230619_221344_create_user::User;
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(KeyPair::Table)
.if_not_exists()
.col(
ColumnDef::new(KeyPair::User)
.text()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(KeyPair::Public).string().not_null())
.col(ColumnDef::new(KeyPair::Private).string().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-user_id-keypair_user")
.from(KeyPair::Table, KeyPair::User)
.to(User::Table, User::Id),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(KeyPair::Table).to_owned())
.await
}
}
/// Learn more at https://docs.rs/sea-query#iden
#[derive(Iden)]
enum KeyPair {
Table,
User,
Public,
Private,
}