@@ 13,14 13,21 @@ use diesel::{
r2d2::{self, ConnectionManager},
PgConnection,
};
+use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
type PgPool = r2d2::Pool<ConnectionManager<PgConnection>>;
+const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
+
#[get("/ping")]
async fn ping() -> impl Responder {
HttpResponse::Ok().body("pong")
}
+fn run_migrations(conn: &mut PgConnection) {
+ conn.run_pending_migrations(MIGRATIONS).unwrap();
+}
+
#[actix_web::main]
async fn main() -> std::io::Result<()> {
pretty_env_logger::init();
@@ 35,6 42,10 @@ async fn main() -> std::io::Result<()> {
.build(manager)
.expect("failed to create pool");
+ // run migrations
+ log::info!("running migrations");
+ run_migrations(&mut pg_pool.get().unwrap());
+
HttpServer::new(move || {
App::new()
.app_data(web::Data::new(pg_pool.clone()))