Skip to content

Commit 91bc58b

Browse files
authored
fix: cleanup stale db entry on rename instance file (#6549)
fix: race on renaming instance file
1 parent 38d757b commit 91bc58b

6 files changed

Lines changed: 136 additions & 21 deletions

packages/app-lib/.sqlx/query-028eb6e4c6e87388ea7a371bd7b41e2b10c828ab93140f6d9091fbc76e03cdec.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/app-lib/.sqlx/query-5363aa346fdead6a331a5a209d0505315b9cbb5b842216db5585cc2f7f739180.json

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/app-lib/.sqlx/query-7a912eb3fd9b7da37cce1d8153241aba899e679a6248c8a1de21b7b65155843e.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/app-lib/.sqlx/query-b4885df7ebd5300533179c0ed98cbca8ca982489597f413638c6af8ccbbe2bfa.json

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/app-lib/.sqlx/query-edab0097706c394608dbc8c65670159a6d568cb4d408876d874d218d83bc8caf.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/app-lib/src/state/instances/adapters/sqlite/content_rows.rs

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,83 @@ pub(crate) async fn rename_instance_file(
619619
) -> crate::Result<Option<InstanceFile>> {
620620
let enabled = i64::from(enabled);
621621
let modified_at = Utc::now().timestamp();
622+
let mut tx = pool.begin().await?;
623+
624+
let source_id = sqlx::query_scalar!(
625+
"
626+
SELECT id
627+
FROM instance_files
628+
WHERE instance_id = ? AND relative_path = ?
629+
",
630+
instance_id,
631+
old_relative_path,
632+
)
633+
.fetch_optional(&mut *tx)
634+
.await?;
635+
let target_id = sqlx::query_scalar!(
636+
"
637+
SELECT id
638+
FROM instance_files
639+
WHERE instance_id = ? AND relative_path = ?
640+
",
641+
instance_id,
642+
new_relative_path,
643+
)
644+
.fetch_optional(&mut *tx)
645+
.await?;
646+
647+
if let (Some(source_id), Some(target_id)) =
648+
(source_id.as_deref(), target_id.as_deref())
649+
&& source_id != target_id
650+
{
651+
sqlx::query!(
652+
"
653+
DELETE FROM instance_content_entries
654+
WHERE id IN (
655+
SELECT target_entry.id
656+
FROM instance_content_entries target_entry
657+
WHERE target_entry.instance_id = ?
658+
AND target_entry.file_id = ?
659+
AND EXISTS (
660+
SELECT 1
661+
FROM instance_content_entries source_entry
662+
WHERE source_entry.instance_id = target_entry.instance_id
663+
AND source_entry.content_set_id = target_entry.content_set_id
664+
AND source_entry.file_id = ?
665+
)
666+
)
667+
",
668+
instance_id,
669+
target_id,
670+
source_id,
671+
)
672+
.execute(&mut *tx)
673+
.await?;
674+
675+
sqlx::query!(
676+
"
677+
UPDATE instance_content_entries
678+
SET file_id = ?, modified_at = ?
679+
WHERE instance_id = ? AND file_id = ?
680+
",
681+
source_id,
682+
modified_at,
683+
instance_id,
684+
target_id,
685+
)
686+
.execute(&mut *tx)
687+
.await?;
688+
689+
sqlx::query!(
690+
"
691+
DELETE FROM instance_files
692+
WHERE id = ?
693+
",
694+
target_id,
695+
)
696+
.execute(&mut *tx)
697+
.await?;
698+
}
622699

623700
sqlx::query!(
624701
"
@@ -638,9 +715,11 @@ pub(crate) async fn rename_instance_file(
638715
instance_id,
639716
old_relative_path,
640717
)
641-
.execute(pool)
718+
.execute(&mut *tx)
642719
.await?;
643720

721+
tx.commit().await?;
722+
644723
get_instance_file_by_relative_path(instance_id, new_relative_path, pool)
645724
.await
646725
}

0 commit comments

Comments
 (0)