diff --git a/.gitignore b/.gitignore index ea8c4bf..19cc5fd 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ /target +db/ +!db/.gitkeep diff --git a/Cargo.toml b/Cargo.toml index b70683b..8c70010 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,5 +9,5 @@ edition = "2018" rocket = { version = "0.5.0-rc.1", features = ["secrets", "tls", "json"] } rocket_codegen = "0.5.0-rc.1" rocket_sync_db_pools = { version = "0.1.0-rc.1", features = ["diesel_sqlite_pool"] } -diesel = { version = "1.3", features = ["sqlite", "r2d2"] } -diesel_migrations = "1.3" \ No newline at end of file +diesel = { version = "1.4.0", features = ["sqlite", "r2d2"] } +diesel_migrations = "1.4.0" \ No newline at end of file diff --git a/db/dev.db b/db/dev.db deleted file mode 100644 index 2de9716..0000000 Binary files a/db/dev.db and /dev/null differ diff --git a/migrations/.gitkeep b/migrations/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/migrations/20210708082032_create_tasks_table/down.sql b/migrations/20210708082032_create_tasks_table/down.sql new file mode 100644 index 0000000..efc4f71 --- /dev/null +++ b/migrations/20210708082032_create_tasks_table/down.sql @@ -0,0 +1 @@ +DROP TABLE tasks \ No newline at end of file diff --git a/migrations/20210708082032_create_tasks_table/up.sql b/migrations/20210708082032_create_tasks_table/up.sql new file mode 100644 index 0000000..ca08908 --- /dev/null +++ b/migrations/20210708082032_create_tasks_table/up.sql @@ -0,0 +1,8 @@ +CREATE TABLE tasks ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + description VARCHAR NOT NULL, + completed BOOLEAN NOT NULL DEFAULT 0 +); + +INSERT INTO tasks (description) VALUES ("demo task"); +INSERT INTO tasks (description) VALUES ("demo task2"); \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index aa310ff..bcd8c1e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ #[macro_use] extern crate diesel; +#[macro_use] extern crate diesel_migrations; use rocket::*; +use rocket::fairing::AdHoc; use rocket_sync_db_pools::{database}; mod task; @@ -19,7 +21,19 @@ async fn get_log(conn: DbConn, id: i32) -> String { format!("test {}, {:?}", id, result) } +async fn run_migrations(rocket: Rocket) -> Rocket { + embed_migrations!(); + + let conn = DbConn::get_one(&rocket).await.expect("database connection"); + conn.run(|c| embedded_migrations::run(c)).await.expect("can run migrations"); + + rocket +} + #[launch] fn rocket() -> _ { - rocket::build().mount("/", routes![index, get_log]).attach(DbConn::fairing()) + rocket::build() + .mount("/", routes![index, get_log]) + .attach(DbConn::fairing()) + .attach(AdHoc::on_ignite("Run Migrations", run_migrations)) } diff --git a/src/task.rs b/src/task.rs index f29f6f8..7731f92 100644 --- a/src/task.rs +++ b/src/task.rs @@ -12,7 +12,7 @@ mod schema { } use schema::tasks; -use schema::tasks::dsl::{tasks as all_tasks, completed as task_completed}; +use schema::tasks::dsl::{tasks as all_tasks}; use crate::DbConn;