Skip to content

Commit

Permalink
fix nullable fields when seeding data (#845)
Browse files Browse the repository at this point in the history
* fix nullable fields when seeding data
---------

Co-authored-by: Dotan J. Nahum <[email protected]>
  • Loading branch information
kaplanelad and jondot authored Oct 20, 2024
1 parent 5ddf7e7 commit 13bc8c2
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 15 deletions.
2 changes: 0 additions & 2 deletions examples/demo/src/fixtures/notes.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
---
- id: 1
title: Loco note 1
content: content 1
created_at: "2023-11-12T12:34:56.789"
updated_at: "2023-11-12T12:34:56.789"
- id: 2
title: Loco note 2
content: content 2
created_at: "2023-11-12T12:34:56.789"
updated_at: "2023-11-12T12:34:56.789"
- id: 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ expression: "(add_note_request.status_code(), add_note_request.text())"
---
(
200,
"{\"created_at\":\"DATE\",\"updated_at\":\"DATE\",\"id\":ID,\"title\":\"Loco note 1\",\"content\":\"content 1\"}",
"{\"created_at\":\"DATE\",\"updated_at\":\"DATE\",\"id\":ID,\"title\":null,\"content\":\"content 1\"}",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ expression: "(notes.status_code(), notes.text())"
---
(
200,
"{\"results\":[{\"id\":ID,\"title\":\"Loco note 1\",\"content\":\"content 1\"},{\"id\":ID,\"title\":\"Loco note 2\",\"content\":\"content 2\"},{\"id\":ID,\"title\":\"Loco note 3\",\"content\":\"content 3\"},{\"id\":ID,\"title\":\"Loco note 4\",\"content\":\"content 4\"},{\"id\":ID,\"title\":\"Loco note 5\",\"content\":\"content 5\"},{\"id\":ID,\"title\":\"Loco 6\",\"content\":\"content 6\"},{\"id\":ID,\"title\":\"Loco 7\",\"content\":\"content 7\"},{\"id\":ID,\"title\":\"Loco 8\",\"content\":\"content 8\"},{\"id\":ID,\"title\":\"Loco 9\",\"content\":\"content 9\"},{\"id\":ID,\"title\":\"Loco 10\",\"content\":\"content 10\"},{\"id\":ID,\"title\":\"Loco 11\",\"content\":\"content 11\"}],\"pagination\":{\"page\":1,\"page_size\":25,\"total_pages\":1}}",
"{\"results\":[{\"id\":ID,\"title\":null,\"content\":\"content 1\"},{\"id\":ID,\"title\":\"Loco note 2\",\"content\":null},{\"id\":ID,\"title\":\"Loco note 3\",\"content\":\"content 3\"},{\"id\":ID,\"title\":\"Loco note 4\",\"content\":\"content 4\"},{\"id\":ID,\"title\":\"Loco note 5\",\"content\":\"content 5\"},{\"id\":ID,\"title\":\"Loco 6\",\"content\":\"content 6\"},{\"id\":ID,\"title\":\"Loco 7\",\"content\":\"content 7\"},{\"id\":ID,\"title\":\"Loco 8\",\"content\":\"content 8\"},{\"id\":ID,\"title\":\"Loco 9\",\"content\":\"content 9\"},{\"id\":ID,\"title\":\"Loco 10\",\"content\":\"content 10\"},{\"id\":ID,\"title\":\"Loco 11\",\"content\":\"content 11\"}],\"pagination\":{\"page\":1,\"page_size\":25,\"total_pages\":1}}",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ expression: "(notes.status_code(), notes.text())"
---
(
200,
"{\"results\":[{\"id\":ID,\"title\":\"Loco note 1\",\"content\":\"content 1\"},{\"id\":ID,\"title\":\"Loco note 2\",\"content\":\"content 2\"}],\"pagination\":{\"page\":1,\"page_size\":2,\"total_pages\":3}}",
"{\"results\":[{\"id\":ID,\"title\":\"Loco note 2\",\"content\":null},{\"id\":ID,\"title\":\"Loco note 3\",\"content\":\"content 3\"}],\"pagination\":{\"page\":1,\"page_size\":2,\"total_pages\":2}}",
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ expression: "(notes.status_code(), notes.text())"
---
(
200,
"{\"results\":[{\"id\":ID,\"title\":\"Loco note 1\",\"content\":\"content 1\"}],\"pagination\":{\"page\":1,\"page_size\":1,\"total_pages\":11}}",
"{\"results\":[{\"id\":ID,\"title\":null,\"content\":\"content 1\"}],\"pagination\":{\"page\":1,\"page_size\":1,\"total_pages\":11}}",
)
17 changes: 8 additions & 9 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,17 +248,16 @@ where
A: sea_orm::ActiveModelTrait + Send + Sync,
sea_orm::Insert<A>: Send + Sync, // Add this Send bound
{
let loader: Vec<serde_json::Value> = serde_yaml::from_reader(File::open(path)?)?;

let mut users: Vec<A> = vec![];
for user in loader {
users.push(A::from_json(user)?);
let seed_data: Vec<serde_json::Value> = serde_yaml::from_reader(File::open(path)?)?;

for row in seed_data {
let mut model = <A as ActiveModelTrait>::default();
model.set_from_json(row)?;
<A as ActiveModelTrait>::Entity::insert(model)
.exec(db)
.await?;
}

<A as ActiveModelTrait>::Entity::insert_many(users)
.exec(db)
.await?;

Ok(())
}

Expand Down

0 comments on commit 13bc8c2

Please sign in to comment.