Run migrations on start

This commit is contained in:
Evie Viau-Chow-Stuart 2023-02-05 03:07:53 -08:00
parent 92f8badb9c
commit af3d8a8e6b
Signed by: evie
GPG key ID: 928652CDFCEC8099
3 changed files with 12 additions and 2 deletions

1
Cargo.lock generated
View file

@ -1175,6 +1175,7 @@ dependencies = [
"log",
"migration",
"sea-orm 0.11.0-rc.1",
"sea-orm-migration",
"serde",
"serde_json",
"tokio",

View file

@ -16,6 +16,7 @@ log = "0.4.17"
# Database
sea-orm = { version = "0.11.0-rc.1", features = ["sqlx-postgres", "runtime-tokio-rustls", "macros"] }
sea-orm-migration = "0.10.7"
migration = { path = "./migration" }
# Web

View file

@ -5,6 +5,8 @@ use axum::{Router, routing::get};
use log::info;
use sea_orm::{Database, DatabaseConnection, ConnectOptions};
use migration::{Migrator, MigratorTrait};
mod routes;
mod entities;
mod block_types;
@ -28,13 +30,19 @@ async fn main() {
info!("Connecting to the database...");
let mut db_opt = ConnectOptions::new(db_url);
let mut migration_db_opt = migration::sea_orm::ConnectOptions::new(db_url.clone());
migration_db_opt.sqlx_logging_level(log::LevelFilter::Debug);
info!("Checking for migrations needed...");
let migrator_conn = migration::sea_orm::Database::connect(migration_db_opt).await.expect("Failed to connect to the database! HALTING");
Migrator::up(&migrator_conn, None).await.expect("Failed to run migrations! HALTING");
let mut db_opt = ConnectOptions::new(db_url.clone());
db_opt.sqlx_logging_level(log::LevelFilter::Debug);
let db_conn = Database::connect(db_opt)
.await
.expect("Failed to connect to the database! HALTING");
//Migrator::up(&db_conn, None).await.expect("Failed to run migrations! HALTING");
info!("Connected to database!");