feat(server): initialize database migrations

repo-db
Jef Roosens 2023-07-30 15:52:11 +02:00
parent af27b06df1
commit e08048d0f0
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
7 changed files with 1732 additions and 8 deletions

1660
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -11,6 +11,8 @@ axum = { version = "0.6.18", features = ["http2"] }
clap = { version = "4.3.12", features = ["env", "derive"] }
futures = "0.3.28"
libarchive = { path = "../libarchive" }
sea-orm = { version = "0.12.1", features = ["sqlx-sqlite", "runtime-tokio-rustls", "macros"] }
sea-orm-migration = "0.12.1"
sha256 = "1.1.4"
tokio = { version = "1.29.1", features = ["full"] }
tokio-util = { version = "0.7.8", features = ["io"] }

View File

@ -45,6 +45,8 @@ impl Cli {
pub async fn run(&self) {
self.init_tracing();
let db = crate::db::init("sqlite://test.db?mode=rwc").await.unwrap();
let config = Config {
repo_dir: self.repo_dir.clone(),
pkg_dir: self.pkg_dir.clone(),

View File

@ -0,0 +1,47 @@
use sea_orm_migration::prelude::*;
pub struct Migration;
impl MigrationName for Migration {
fn name(&self) -> &str {
"m_20230730_000001_create_repo_tables"
}
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
// Define how to apply this migration: Create the Bakery table.
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Repo::Table)
.col(
ColumnDef::new(Repo::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Repo::Name).string().not_null())
.col(ColumnDef::new(Repo::Description).string())
.to_owned(),
)
.await
}
// Define how to rollback this migration: Drop the Bakery table.
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Repo::Table).to_owned())
.await
}
}
#[derive(Iden)]
pub enum Repo {
Table,
Id,
Name,
Description,
}

View File

@ -0,0 +1,12 @@
use sea_orm_migration::prelude::*;
pub struct Migrator;
mod m20230730_000001_create_repo_tables;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20230730_000001_create_repo_tables::Migration)]
}
}

View File

@ -0,0 +1,16 @@
mod migrator;
use migrator::Migrator;
use sea_orm::ConnectOptions;
use sea_orm::Database;
use sea_orm_migration::MigratorTrait;
pub async fn init<C: Into<ConnectOptions>>(
opt: C,
) -> Result<sea_orm::DatabaseConnection, sea_orm::DbErr> {
let mut db = Database::connect(opt).await?;
Migrator::refresh(&db).await?;
Ok(db)
}

View File

@ -1,4 +1,5 @@
mod cli;
mod db;
mod error;
mod repo;