Fix sqlite error
This commit is contained in:
parent
c54cdc98ba
commit
ee2bfbb769
8 changed files with 29 additions and 4 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1 +1,3 @@
|
|||
/target
|
||||
db/
|
||||
!db/.gitkeep
|
||||
|
|
|
@ -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"
|
||||
diesel = { version = "1.4.0", features = ["sqlite", "r2d2"] }
|
||||
diesel_migrations = "1.4.0"
|
BIN
db/dev.db
BIN
db/dev.db
Binary file not shown.
0
migrations/.gitkeep
Normal file
0
migrations/.gitkeep
Normal file
1
migrations/20210708082032_create_tasks_table/down.sql
Normal file
1
migrations/20210708082032_create_tasks_table/down.sql
Normal file
|
@ -0,0 +1 @@
|
|||
DROP TABLE tasks
|
8
migrations/20210708082032_create_tasks_table/up.sql
Normal file
8
migrations/20210708082032_create_tasks_table/up.sql
Normal file
|
@ -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");
|
16
src/main.rs
16
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<Build>) -> Rocket<Build> {
|
||||
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))
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
||||
|
|
Loading…
Reference in a new issue