-
I use SQLite as database and connect with diesel in my project Lettura. I set the DATABASE_URL environment variable in the .env file
and connect database with diesel pub fn establish_connection() -> SqliteConnection {
dotenv().ok();
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
SqliteConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))
} Everything is fine when I run
I'm stuck here with this problem.Is that mean the app can't find Can someone kindly help me with this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
hey guy, I have solved this problem. here is my solution, hope it can help someone in need 😄 The dotenv crate reads the file dynamically at runtime. So it's reasonable that this does not work in production, as there is not dotenv file. So I declared variable 'DATABASE_URL' in pub fn establish_connection() -> SqliteConnection {
let database_url = "./lettura.db";
SqliteConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))
} Then do migrations at compile time, here is the detail #[macro_use]
extern crate diesel_migrations;
use diesel_migrations::{embed_migrations};
embed_migrations!("./migrations");
fn main () {
...
let connection = db::establish_connection();
embedded_migrations::run(&connection).expect("Error migrating");
...
} |
Beta Was this translation helpful? Give feedback.
-
After few hours debug, here is my solution with SQLite, hope it will help. db.rs
main.rs
|
Beta Was this translation helpful? Give feedback.
hey guy, I have solved this problem. here is my solution, hope it can help someone in need 😄
The dotenv crate reads the file dynamically at runtime. So it's reasonable that this does not work in production, as there is not dotenv file. So I declared variable 'DATABASE_URL' in
db.rs
which value is relative to the tauri context's path.Then do migrations at compile time, here is the detail