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:
Max Brunsfeld 2024-08-09 12:32:11 -07:00 committed by GitHub
parent b1c69c2178
commit d96afde5bf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,15 +5,27 @@ use util::ResultExt;
impl Database {
/// Initializes the different kinds of notifications by upserting records for them.
pub async fn initialize_notification_kinds(&mut self) -> Result<()> {
notification_kind::Entity::insert_many(Notification::all_variant_names().iter().map(
|kind| notification_kind::ActiveModel {
let all_kinds = Notification::all_variant_names();
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()),
..Default::default()
},
))
.on_conflict(OnConflict::new().do_nothing().to_owned())
.exec_without_returning(&self.pool)
.await?;
})
.collect();
if !kinds_to_create.is_empty() {
notification_kind::Entity::insert_many(kinds_to_create)
.exec_without_returning(&self.pool)
.await?;
}
let mut rows = notification_kind::Entity::find().stream(&self.pool).await?;
while let Some(row) = rows.next().await {