Rename thread artifacts to attachments in the state database (#43927)

## What changed

Add a migration that renames `thread_artifacts` to `thread_attachments` and
`artifact_type` to `attachment_type`, and recreates the thread lookup index
under the attachment name.

## Testing

Extend the migration test to verify that existing attachment data survives the
rename, section metadata is preserved, and the version 50 migrator tolerates
the updated database with `ignore_missing` enabled.

GitOrigin-RevId: 43955c4f69b91f92217efc1dc8e6a551d0716059
This commit is contained in:
joeytrasatti-openai authored and copyberry committed 2026-09-08 22:02:55 +00:00
1 parent 82d4a98912
commit 9d88e9ae08
2 files changed
+41 -9

No files matched your search

@@ -0,0 +1,6 @@
ALTER TABLE thread_artifacts RENAME TO thread_attachments;
ALTER TABLE thread_attachments RENAME COLUMN artifact_type TO attachment_type;
DROP INDEX idx_thread_artifacts_thread_created_id;
CREATE INDEX idx_thread_attachments_thread_created_id
ON thread_attachments(thread_id, created_at, id);
+35 -9
View File
@@ -189,7 +189,7 @@ INSERT INTO threads (
} }
#[tokio::test] #[tokio::test]
async fn thread_artifact_migration_preserves_existing_section_metadata() { async fn thread_attachment_migration_preserves_existing_data() {
let sqlite_home = crate::runtime::test_support::unique_temp_dir(); let sqlite_home = crate::runtime::test_support::unique_temp_dir();
tokio::fs::create_dir_all(&sqlite_home) tokio::fs::create_dir_all(&sqlite_home)
.await .await
@@ -202,7 +202,7 @@ async fn thread_artifact_migration_preserves_existing_section_metadata() {
.open_read_write_pool(&sqlite.state_db_path()) .open_read_write_pool(&sqlite.state_db_path())
.await .await
.expect("sqlite database should open"); .expect("sqlite database should open");
migrator_through(/*version*/ 50) migrator_through(/*version*/ 51)
.run(&pool) .run(&pool)
.await .await
.expect("released thread migrations should apply"); .expect("released thread migrations should apply");
@@ -213,10 +213,26 @@ async fn thread_artifact_migration_preserves_existing_section_metadata() {
.await .await
.expect("released section appearance should remain writable"); .expect("released section appearance should remain writable");
let thread_id = "00000000-0000-0000-0000-000000000051";
sqlx::query(
"INSERT INTO threads (id, rollout_path, created_at, updated_at, source, model_provider, cwd, title, sandbox_policy, approval_mode) VALUES (?, 'rollout.jsonl', 1, 1, 'cli', 'openai', '/tmp', '', 'read-only', 'on-request')",
)
.bind(thread_id)
.execute(&pool)
.await
.expect("existing thread should be inserted");
sqlx::query(
"INSERT INTO thread_artifacts (id, thread_id, artifact_type, identity_key, payload, created_at) VALUES ('attachment-1', ?, 'pull_request', 'pr-123', '{}', 1)",
)
.bind(thread_id)
.execute(&pool)
.await
.expect("existing attachment should be inserted using the released schema");
STATE_MIGRATOR STATE_MIGRATOR
.run(&pool) .run(&pool)
.await .await
.expect("artifact migration should apply without rewriting released migrations"); .expect("attachment migration should apply without rewriting released migrations");
let section = sqlx::query_as::<_, (String, String, Option<String>)>( let section = sqlx::query_as::<_, (String, String, Option<String>)>(
"SELECT id, name, appearance FROM thread_sections WHERE id = ?", "SELECT id, name, appearance FROM thread_sections WHERE id = ?",
) )
@@ -233,20 +249,30 @@ async fn thread_artifact_migration_preserves_existing_section_metadata() {
) )
); );
let artifact_tables = sqlx::query_scalar::<_, String>( let attachment = sqlx::query_as::<_, (String, String, String, String, String, i64)>(
"SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'thread_artifacts'", "SELECT id, thread_id, attachment_type, identity_key, payload, created_at FROM thread_attachments",
) )
.fetch_all(&pool) .fetch_one(&pool)
.await .await
.expect("artifact table should exist"); .expect("existing attachment should remain available under the renamed table and column");
assert_eq!(artifact_tables, vec!["thread_artifacts"]); assert_eq!(
attachment,
(
"attachment-1".to_string(),
thread_id.to_string(),
"pull_request".to_string(),
"pr-123".to_string(),
"{}".to_string(),
1,
)
);
let mut released_migrator = migrator_through(/*version*/ 50); let mut released_migrator = migrator_through(/*version*/ 50);
released_migrator.ignore_missing = true; released_migrator.ignore_missing = true;
released_migrator released_migrator
.run(&pool) .run(&pool)
.await .await
.expect("released binaries should tolerate the additive artifact migration"); .expect("released binaries should tolerate the attachment rename migration");
} }
#[tokio::test] #[tokio::test]