refactor: 🚧 Use built in migrator (WIP)

This commit is contained in:
ThatOneCalculator 2022-04-04 09:35:50 -07:00
parent be7617476f
commit ead9d9108e
3 changed files with 16 additions and 49 deletions

39
backend/Cargo.lock generated
View File

@ -276,12 +276,10 @@ dependencies = [
"actix-web", "actix-web",
"badge-maker", "badge-maker",
"chrono", "chrono",
"include_dir",
"nanoid", "nanoid",
"serde", "serde",
"serde_json", "serde_json",
"sqlx", "sqlx",
"sqlx-pg-migrate",
] ]
[[package]] [[package]]
@ -751,12 +749,6 @@ dependencies = [
"wasi", "wasi",
] ]
[[package]]
name = "glob"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
[[package]] [[package]]
name = "governor" name = "governor"
version = "0.4.2" version = "0.4.2"
@ -879,26 +871,6 @@ dependencies = [
"unicode-normalization", "unicode-normalization",
] ]
[[package]]
name = "include_dir"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "482a2e29200b7eed25d7fdbd14423326760b7f6658d21a4cf12d55a50713c69f"
dependencies = [
"glob",
"include_dir_macros",
]
[[package]]
name = "include_dir_macros"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e074c19deab2501407c91ba1860fa3d6820bfde307db6d8cb851b55a10be89b"
dependencies = [
"proc-macro2",
"quote",
]
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "1.8.0" version = "1.8.0"
@ -1644,17 +1616,6 @@ dependencies = [
"url", "url",
] ]
[[package]]
name = "sqlx-pg-migrate"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c953f3c114842c806461afc90a68da2fad7072d302cfcf69bdc31c893f6c899b"
dependencies = [
"include_dir",
"sqlx",
"thiserror",
]
[[package]] [[package]]
name = "sqlx-rt" name = "sqlx-rt"
version = "0.5.11" version = "0.5.11"

View File

@ -16,9 +16,7 @@ actix-governor = "0.3.0"
badge-maker = "0.2.1" badge-maker = "0.2.1"
sqlx = { version = "0.5", features = ["runtime-tokio-rustls", "postgres", "chrono"] } sqlx = { version = "0.5.11", features = ["runtime-tokio-rustls", "postgres", "chrono"] }
include_dir = "0.7.2"
sqlx-pg-migrate = "1.0"
chrono = { version = "0.4.19", features = ["serde"] } chrono = { version = "0.4.19", features = ["serde"] }

View File

@ -2,7 +2,7 @@ mod config;
mod models; mod models;
mod routes; mod routes;
use std::{io, path::PathBuf}; use std::{io, path::Path, path::PathBuf};
use actix_cors::Cors; use actix_cors::Cors;
use actix_governor::{Governor, GovernorConfigBuilder}; use actix_governor::{Governor, GovernorConfigBuilder};
@ -12,9 +12,7 @@ use actix_web::{
}; };
use config::Config; use config::Config;
use include_dir::include_dir; use sqlx::{migrate::Migrator, postgres::PgPoolOptions, PgPool};
use sqlx::{postgres::PgPoolOptions, PgPool};
use sqlx_pg_migrate::migrate;
use crate::routes::{ use crate::routes::{
get_paste, get_raw_paste, get_stats, get_total_pastes_badge, get_version_badge, new_paste, get_paste, get_raw_paste, get_stats, get_total_pastes_badge, get_version_badge, new_paste,
@ -26,6 +24,15 @@ pub struct AppState {
pub pool: PgPool, pub pool: PgPool,
} }
pub async fn migrations(pool: sqlx::Pool) -> Result<(), sqlx::Error> {
Migrator::new(Path::new("./migrations"))
.await?
.run(pool)
.await?;
Ok(())
}
#[actix_rt::main] #[actix_rt::main]
async fn main() -> io::Result<()> { async fn main() -> io::Result<()> {
let config = config::load(PathBuf::from("config.json")); let config = config::load(PathBuf::from("config.json"));
@ -35,6 +42,7 @@ async fn main() -> io::Result<()> {
.connect(db_uri) .connect(db_uri)
.await .await
.expect("Failed to connect to database"); .expect("Failed to connect to database");
migrations(pool).await;
let address = format!( let address = format!(
"{}:{}", "{}:{}",
config.server.backend_host, config.server.backend_port config.server.backend_host, config.server.backend_port
@ -45,9 +53,9 @@ async fn main() -> io::Result<()> {
.finish() .finish()
.unwrap(); .unwrap();
let state = AppState { config, pool }; let state = AppState { config, pool };
migrate(db_uri, &include_dir!("migrations")) // migrate(db_uri, &include_dir!("migrations"))
.await // .await
.expect("Error in migrations!"); // .expect("Error in migrations!");
println!("🚀 zer0bin is running on {address}"); println!("🚀 zer0bin is running on {address}");
HttpServer::new(move || { HttpServer::new(move || {