Avoid insert ... on conflict
on startup (#16045)
These queries advance the id sequence even when there's nothing to insert Release Notes: - N/A Co-authored-by: Marshall <marshall@zed.dev>
This commit is contained in:
parent
b1c69c2178
commit
d96afde5bf
1 changed files with 19 additions and 7 deletions
|
@ -5,15 +5,27 @@ use util::ResultExt;
|
||||||
impl Database {
|
impl Database {
|
||||||
/// Initializes the different kinds of notifications by upserting records for them.
|
/// Initializes the different kinds of notifications by upserting records for them.
|
||||||
pub async fn initialize_notification_kinds(&mut self) -> Result<()> {
|
pub async fn initialize_notification_kinds(&mut self) -> Result<()> {
|
||||||
notification_kind::Entity::insert_many(Notification::all_variant_names().iter().map(
|
let all_kinds = Notification::all_variant_names();
|
||||||
|kind| notification_kind::ActiveModel {
|
let existing_kinds = notification_kind::Entity::find().all(&self.pool).await?;
|
||||||
|
|
||||||
|
let kinds_to_create: Vec<_> = all_kinds
|
||||||
|
.iter()
|
||||||
|
.filter(|&kind| {
|
||||||
|
!existing_kinds
|
||||||
|
.iter()
|
||||||
|
.any(|existing| existing.name == **kind)
|
||||||
|
})
|
||||||
|
.map(|kind| notification_kind::ActiveModel {
|
||||||
name: ActiveValue::Set(kind.to_string()),
|
name: ActiveValue::Set(kind.to_string()),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
})
|
||||||
))
|
.collect();
|
||||||
.on_conflict(OnConflict::new().do_nothing().to_owned())
|
|
||||||
|
if !kinds_to_create.is_empty() {
|
||||||
|
notification_kind::Entity::insert_many(kinds_to_create)
|
||||||
.exec_without_returning(&self.pool)
|
.exec_without_returning(&self.pool)
|
||||||
.await?;
|
.await?;
|
||||||
|
}
|
||||||
|
|
||||||
let mut rows = notification_kind::Entity::find().stream(&self.pool).await?;
|
let mut rows = notification_kind::Entity::find().stream(&self.pool).await?;
|
||||||
while let Some(row) = rows.next().await {
|
while let Some(row) = rows.next().await {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue