Skip to content

Commit

Permalink
update migration
Browse files Browse the repository at this point in the history
  • Loading branch information
deeper-x committed Aug 9, 2024
1 parent 2f16a3c commit 9a17440
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,19 @@ Using a different terminal send an HTTP POST request to the running server:
```shell
http http://127.0.0.1:8080/ping/get
```

Migrations list:
```sh
http http://127.0.0.1:8080/migration/all
```

Migration info:
```sh
http http://127.0.0.1:8080/migration/details/14
```

Update migration:

```sh
echo '{"id": 14, "query": "demo query edited from cli"}' | http -f --json --print hb http://127.0.0.1:8080/migration/update
```
14 changes: 14 additions & 0 deletions src/db/dml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ pub async fn add_migration_record(
client.query_one(&stmt, &[&migration.query]).await
}

pub async fn update_migration_record(
client: &Client,
migration: Migration,
) -> Result<tokio_postgres::Row, tokio_postgres::Error> {
let stmt = client
.prepare("UPDATE migrations set query = $1 where id = $2 RETURNING id;")
.await
.unwrap();

client
.query_one(&stmt, &[&migration.query, &migration.id])
.await
}

// add ping record
pub async fn add_ping_record(client: &Client, ping_info: Ping) -> Result<Ping, MyError> {
let _stmt = include_str!("./sql/ping/add_record.sql");
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ async fn main() -> std::io::Result<()> {
.route(
"/details/{id_migration}",
web::get().to(server::router::get_migration_details),
),
)
.route("/update", web::post().to(server::router::update_migration)),
)
})
.bind(config.server_addr.clone())?
Expand Down
26 changes: 26 additions & 0 deletions src/server/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@ pub async fn get_migration_details(
}
}

pub async fn update_migration(
migration: web::Json<models::Migration>,
db_pool: web::Data<Pool>,
) -> Result<HttpResponse, Error> {
let migration_info = migration.into_inner();

let client = db_pool
.get()
.await
.map_err(settings::errors::MyError::PoolError)?;

let updated_migration = dml::update_migration_record(&client, migration_info).await;

match updated_migration {
Ok(it) => {
let id: i64 = it.get(0);
Ok(HttpResponse::Ok().json(id))
}

Err(err) => {
println!("Error on updating migration: {}", err);
Ok(HttpResponse::InternalServerError().json("err"))
}
}
}

pub async fn add_migration_record(
migration: web::Json<models::Migration>,
db_pool: web::Data<Pool>,
Expand Down

0 comments on commit 9a17440

Please sign in to comment.