Use env variable DATABASE_URL for sqlite

This commit is contained in:
Tony Klink 2024-01-12 14:14:53 -06:00
parent 4b364b0419
commit 488fa685d3
Signed by: klink
GPG key ID: 85175567C4D19231
2 changed files with 9 additions and 8 deletions

View file

@ -44,7 +44,7 @@
sqlite sqlite
]; ];
env = { env = {
DATABASE_URL = "sqlite://sqlite.db"; DATABASE_URL = "/tmp/sqlite.db";
RUST_BACKTRACE = 1; RUST_BACKTRACE = 1;
}; };
}; };

View file

@ -153,15 +153,16 @@ impl SqliteDb {
.idle_timeout(None) .idle_timeout(None)
.max_connections(max_size); .max_connections(max_size);
let db_url = "sqlite://sqlite.db"; let env_db_path = std::env::var("DATABASE_URL").unwrap_or("/tmp/sqlite.db".to_string());
if !Sqlite::database_exists(db_url).await.unwrap_or(false) {
log::info!("Creating database {}", db_url); if !Sqlite::database_exists(&env_db_path).await.unwrap_or(false) {
match Sqlite::create_database(db_url).await { log::info!("Creating database {}", &env_db_path);
Ok(_) => log::info!("Db {} created", db_url), match Sqlite::create_database(&env_db_path).await {
Err(_) => panic!("Failed to create database {}", db_url), Ok(_) => log::info!("Db {} created", &env_db_path),
Err(_) => panic!("Failed to create database {}", &env_db_path),
} }
} }
if let Ok(pool) = pool_options.connect(db_url).await { if let Ok(pool) = pool_options.connect(&env_db_path).await {
log::info!("Connected to sqlite pool {}", name); log::info!("Connected to sqlite pool {}", name);
pool pool