Skip to content
This repository was archived by the owner on Jan 2, 2025. It is now read-only.

Commit 477047a

Browse files
authored
Add doc entries to project_docs table in project migration (#1221)
1 parent f186798 commit 477047a

2 files changed

Lines changed: 68 additions & 24 deletions

File tree

server/bleep/sqlx-data.json

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,36 @@
224224
},
225225
"query": "SELECT pr.repo_ref, pr.branch\n FROM project_repos pr\n INNER JOIN projects p ON p.id = pr.project_id AND p.user_id = ?"
226226
},
227+
"17d2e9685f222726da591a004925941a8ef1a2d280a7e8e132005c879db20cee": {
228+
"describe": {
229+
"columns": [
230+
{
231+
"name": "context",
232+
"ordinal": 0,
233+
"type_info": "Text"
234+
},
235+
{
236+
"name": "doc_context",
237+
"ordinal": 1,
238+
"type_info": "Text"
239+
},
240+
{
241+
"name": "project_id",
242+
"ordinal": 2,
243+
"type_info": "Int64"
244+
}
245+
],
246+
"nullable": [
247+
false,
248+
false,
249+
false
250+
],
251+
"parameters": {
252+
"Right": 0
253+
}
254+
},
255+
"query": "SELECT\n context,\n doc_context,\n (SELECT project_id FROM studios WHERE studios.id = studio_snapshots.studio_id) AS project_id\n FROM studio_snapshots"
256+
},
227257
"26065ed9dd0dfa42b8b943726d85425d0b45b2cafcceb9887ea626040bef9264": {
228258
"describe": {
229259
"columns": [
@@ -542,6 +572,16 @@
542572
},
543573
"query": "UPDATE studios SET name = ? WHERE id = ?"
544574
},
575+
"4b9781f9d09ff9468d533a162da45c4a0e13d7a58ae0c2afdc590fe6b8fc431e": {
576+
"describe": {
577+
"columns": [],
578+
"nullable": [],
579+
"parameters": {
580+
"Right": 2
581+
}
582+
},
583+
"query": "INSERT INTO project_docs (project_id, doc_id)\n SELECT $1, $2\n WHERE NOT EXISTS (\n SELECT 1 FROM project_docs WHERE project_id = $1 AND doc_id = $2\n )"
584+
},
545585
"4bf8d04acb2c99669237578467e50ac6822cb46053bced5d7d7a9dc374353e0d": {
546586
"describe": {
547587
"columns": [],
@@ -580,30 +620,6 @@
580620
},
581621
"query": "INSERT INTO conversations (\n thread_id, title, exchanges, project_id, created_at\n )\n VALUES (?, ?, ?, ?, strftime('%s', 'now'))\n RETURNING id"
582622
},
583-
"4fc7072141dbc26c66bacc951f0d352ece1c8dda0289bd5c06d23fac709064b1": {
584-
"describe": {
585-
"columns": [
586-
{
587-
"name": "context",
588-
"ordinal": 0,
589-
"type_info": "Text"
590-
},
591-
{
592-
"name": "project_id",
593-
"ordinal": 1,
594-
"type_info": "Int64"
595-
}
596-
],
597-
"nullable": [
598-
false,
599-
false
600-
],
601-
"parameters": {
602-
"Right": 0
603-
}
604-
},
605-
"query": "SELECT\n context,\n (SELECT project_id FROM studios WHERE studios.id = studio_snapshots.studio_id) AS project_id\n FROM studio_snapshots"
606-
},
607623
"5128142bf657cfde043a1b53834d40980caa3e9ae5fd6f4d7f30d89be512f105": {
608624
"describe": {
609625
"columns": [],

server/bleep/src/db.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ async fn project_migration(db: &SqlitePool) -> Result<()> {
112112
let studio_snapshots = sqlx::query! {
113113
"SELECT
114114
context,
115+
doc_context,
115116
(SELECT project_id FROM studios WHERE studios.id = studio_snapshots.studio_id) AS project_id
116117
FROM studio_snapshots"
117118
}
@@ -122,6 +123,9 @@ async fn project_migration(db: &SqlitePool) -> Result<()> {
122123
let context =
123124
serde_json::from_str(&ss.context).context("did not find valid JSON in `context`")?;
124125

126+
let doc_context = serde_json::from_str(&ss.doc_context)
127+
.context("did not find valid JSON in `doc_context`")?;
128+
125129
for repo_ref in studio_context_repos(&context).context("invalid studio `context` JSON")? {
126130
sqlx::query! {
127131
"INSERT INTO project_repos (project_id, repo_ref)
@@ -135,6 +139,22 @@ async fn project_migration(db: &SqlitePool) -> Result<()> {
135139
.execute(db)
136140
.await?;
137141
}
142+
143+
for doc_id in
144+
studio_doc_context_doc_ids(&doc_context).context("invalid studio `doc_context` JSON")?
145+
{
146+
sqlx::query! {
147+
"INSERT INTO project_docs (project_id, doc_id)
148+
SELECT $1, $2
149+
WHERE NOT EXISTS (
150+
SELECT 1 FROM project_docs WHERE project_id = $1 AND doc_id = $2
151+
)",
152+
ss.project_id,
153+
doc_id,
154+
}
155+
.execute(db)
156+
.await?;
157+
}
138158
}
139159

140160
sqlx::query! { "UPDATE rust_migrations SET applied = true WHERE ref = 'project_migration'" }
@@ -215,3 +235,11 @@ fn studio_context_repos(context: &serde_json::Value) -> Option<Vec<&str>> {
215235
}
216236
Some(repos)
217237
}
238+
239+
fn studio_doc_context_doc_ids(doc_context: &serde_json::Value) -> Option<Vec<i64>> {
240+
let mut ids = Vec::new();
241+
for file in doc_context.as_array()? {
242+
ids.push(file.as_object()?.get("doc_id")?.as_i64()?);
243+
}
244+
Some(ids)
245+
}

0 commit comments

Comments
 (0)