Skip to content

Commit

Permalink
Add RUN_MIGRATIONS env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeMoolenaar committed Apr 4, 2024
1 parent 196a411 commit 9b1d80f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
DATABASE_URL="libsql://..."
LIBSQL_AUTH_TOKEN=""
RUN_MIGRATIONS="true"
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ axum = { version = "0.7.4", features = ["form", "macros"] }
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }
tower-http = { version = "0.5", features = ["fs","set-header"] }
serde = { version = "1.0.197", features = ["derive"] }
rand = "0.8"
chrono = "0.4.35"
dotenv = "0.15.0"
tower = "0.4.13"
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ npm run tailwind
- [x] Improve code for render_html.rs
- [x] Setup build via GH actions
- [x] Migrate from SQLX/PostgreSQL to Libsql/Turso (sqlx doesn't support turso yet... such a shame)
- [ ] Use minijinja autoreload https://docs.rs/minijinja-autoreload/latest/minijinja_autoreload/
- [ ] Rename to RATH stack, Rust Axum Turso Hhtmx

## Handy commands
Expand Down
23 changes: 14 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ async fn main() {
// Connect to DB
let db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let db_token = env::var("LIBSQL_AUTH_TOKEN").expect("TOKEN must be set");
let db_run_migrations = env::var("RUN_MIGRATIONS").unwrap_or("true".to_string());

// Setup database
let db = Builder::new_remote_replica("local.db", db_url, db_token)
Expand All @@ -45,22 +46,26 @@ async fn main() {
.await
.expect("Could not connect to database");
let conn = db.connect().unwrap();
db.sync().await.unwrap();
// db.sync().await.unwrap();

// Loop over all files in dir migrations and run them
let migrations = std::fs::read_dir("migrations").unwrap();
for migration in migrations {
let path = migration.unwrap().path();
if path.is_file() {
let content = std::fs::read_to_string(path.clone()).unwrap();
conn.execute(&content, ()).await.unwrap();
println!("Ran migration: {}", path.display());
if db_run_migrations == "true" {
let migrations = std::fs::read_dir("migrations").unwrap();
for migration in migrations {
let path = migration.unwrap().path();
if path.is_file() {
let content = std::fs::read_to_string(path.clone()).unwrap();
conn.execute(&content, ()).await.unwrap();
println!("Ran migration: {}", path.display());
}
}
}

// Setup session store
let session_store = LibsqlStore::new(conn.clone());
session_store.migrate().await.expect("Could not migrate session store");
if db_run_migrations == "true" {
session_store.migrate().await.expect("Could not migrate session store");
}
let _deletion_task = tokio::task::spawn(
session_store
.clone()
Expand Down

0 comments on commit 9b1d80f

Please sign in to comment.