From ee2bfbb7695298c53d00b49359314fb58b2367bf Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Thu, 8 Jul 2021 09:53:19 +0200 Subject: [PATCH] Fix sqlite error --- .gitignore | 2 ++ Cargo.toml | 4 ++-- db/dev.db | Bin 20480 -> 0 bytes migrations/.gitkeep | 0 .../20210708082032_create_tasks_table/down.sql | 1 + .../20210708082032_create_tasks_table/up.sql | 8 ++++++++ src/main.rs | 16 +++++++++++++++- src/task.rs | 2 +- 8 files changed, 29 insertions(+), 4 deletions(-) delete mode 100644 db/dev.db create mode 100644 migrations/.gitkeep create mode 100644 migrations/20210708082032_create_tasks_table/down.sql create mode 100644 migrations/20210708082032_create_tasks_table/up.sql 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 2de97168aa9102a966d9ac1f49572911a0d67184..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20480 zcmeI&J#U*p7zgmfI4`wC+)k+54IPY>R6t@|id3Z+_u5*;c8YP;Oo+sn1j&X3aMd;W zD&6~i(ht$OQkEXHM)APdq1OMznY())A)X&z&UpOM2{S=2;$$9Vbe}vWwHkR%DIr9a zeM9zTtI0uqIgw}O(YR`;l9xyKTJsmFYY$}SzWGgi*!fF%G{A{9v#%mw|#+}?ZU(&5OX!-?bkji1L+CZddb{IgtNja@I!W?_0Iru29; zayT273G@wr%O;LTO|4-V^-cYiu8=UAim&O-Om1EMV3Ebe+%N9bw~Euo?K@>lXH2;h=BMXZVjlSO@G=RqFpkpdNNvTTI*+!#h$NNQT2|g_o5o|HvY6du5&1ID z>ks(YV}p}2QkL19xGo=hewkv7Yn%Edt17zswtTBPmhbJi^KX?VyVCqYr$FO zi9FCC009U<00Izz00bZa0SG_<0uWdWfoH1mWc4S3{QbZD(?1#nAOHafKmY;|fB*y_ q009U<00L_)kiY-O{(p@hFiL{}1Rwwb2tWV=5P$##AOHafNP*vD1HDiH 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;