build.rs 987 B

123456789101112131415161718192021222324252627282930313233343536
  1. use std::env;
  2. use std::error::Error;
  3. use std::fs;
  4. use std::str::FromStr;
  5. #[tokio::main]
  6. async fn main() -> Result<(), Box<dyn Error>> {
  7. let out_dir = env::var("CARGO_TARGET_DIR").unwrap();
  8. let db_file = format!("{out_dir}/dim_dev.db");
  9. println!("cargo:rustc-env=DATABASE_URL=sqlite://{db_file}");
  10. println!(
  11. "cargo:warning=Generating {:?} from latest migrations.",
  12. db_file
  13. );
  14. let _ = fs::remove_file(&db_file);
  15. let pool = sqlx::sqlite::SqlitePoolOptions::new()
  16. .connect_with(
  17. sqlx::sqlite::SqliteConnectOptions::from_str(db_file.as_ref())?.create_if_missing(true),
  18. )
  19. .await?;
  20. sqlx::migrate!().run(&pool).await.map_err(|e| {
  21. println!("cargo:error=Migration failed: {:?}", e);
  22. e
  23. })?;
  24. println!("cargo:warning=Built database {}.", db_file);
  25. println!("cargo:rerun-if-changed=database/src/build.rs");
  26. println!("cargo:rerun-if-changed=database/migrations");
  27. Ok(())
  28. }