This repository was archived by the owner on Apr 11, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_shim.rs
More file actions
363 lines (297 loc) · 11.8 KB
/
Copy pathdb_shim.rs
File metadata and controls
363 lines (297 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//! Sync shim over `flowctl-db` (async libSQL) providing the same API
//! surface as the deprecated `flowctl-db` (rusqlite) crate.
//!
//! Every sync method spins up a per-call `tokio::runtime::Builder::
//! new_current_thread` runtime, which is cheap for CLI command invocation.
//! The shim exists so the many sync CLI call sites can stay as-is while
//! the underlying storage is async libSQL.
//!
//! This module is the canonical CLI entry point: `crate::commands::db_shim
//! as flowctl_db` (glob-style) is the migration pattern. Do not add
//! long-lived futures or background tasks here.
#![allow(dead_code)]
use std::path::{Path, PathBuf};
pub use flowctl_db::{DbError, ReindexResult};
pub use flowctl_db::metrics::{
Bottleneck, DoraMetrics, EpicStats, Summary, TokenBreakdown, WeeklyTrend,
};
/// Wrapped libSQL connection. Produced by [`open`]; passed by reference to
/// the repos mirroring the old rusqlite API.
#[derive(Clone)]
pub struct Connection {
conn: libsql::Connection,
}
impl Connection {
fn inner(&self) -> libsql::Connection {
self.conn.clone()
}
}
fn block_on<F: std::future::Future>(fut: F) -> F::Output {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.expect("failed to create tokio runtime")
.block_on(fut)
}
// ── Pool functions ──────────────────────────────────────────────────
pub fn resolve_state_dir(working_dir: &Path) -> Result<PathBuf, DbError> {
flowctl_db::resolve_state_dir(working_dir)
}
pub fn resolve_db_path(working_dir: &Path) -> Result<PathBuf, DbError> {
flowctl_db::resolve_db_path(working_dir)
}
pub fn open(working_dir: &Path) -> Result<Connection, DbError> {
block_on(async {
let db = flowctl_db::open_async(working_dir).await?;
let conn = db.connect()?;
// Leak the Database handle to keep it alive for the process lifetime.
// (libsql Database drop closes the file.)
std::mem::forget(db);
Ok(Connection { conn })
})
}
pub fn cleanup(conn: &Connection) -> Result<u64, DbError> {
block_on(flowctl_db::cleanup(&conn.inner()))
}
pub fn reindex(
conn: &Connection,
flow_dir: &Path,
state_dir: Option<&Path>,
) -> Result<ReindexResult, DbError> {
block_on(flowctl_db::reindex(&conn.inner(), flow_dir, state_dir))
}
// ── Epic repository ────────────────────────────────────────────────
pub struct EpicRepo(libsql::Connection);
impl EpicRepo {
pub fn new(conn: &Connection) -> Self {
Self(conn.inner())
}
pub fn get(&self, id: &str) -> Result<flowctl_core::types::Epic, DbError> {
block_on(flowctl_db::EpicRepo::new(self.0.clone()).get(id))
}
pub fn get_with_body(
&self,
id: &str,
) -> Result<(flowctl_core::types::Epic, String), DbError> {
block_on(flowctl_db::EpicRepo::new(self.0.clone()).get_with_body(id))
}
pub fn list(
&self,
status: Option<&str>,
) -> Result<Vec<flowctl_core::types::Epic>, DbError> {
block_on(flowctl_db::EpicRepo::new(self.0.clone()).list(status))
}
pub fn upsert(&self, epic: &flowctl_core::types::Epic) -> Result<(), DbError> {
block_on(flowctl_db::EpicRepo::new(self.0.clone()).upsert(epic))
}
pub fn upsert_with_body(
&self,
epic: &flowctl_core::types::Epic,
body: &str,
) -> Result<(), DbError> {
block_on(flowctl_db::EpicRepo::new(self.0.clone()).upsert_with_body(epic, body))
}
pub fn update_status(
&self,
id: &str,
status: flowctl_core::types::EpicStatus,
) -> Result<(), DbError> {
block_on(flowctl_db::EpicRepo::new(self.0.clone()).update_status(id, status))
}
}
// ── Task repository ────────────────────────────────────────────────
pub struct TaskRepo(libsql::Connection);
impl TaskRepo {
pub fn new(conn: &Connection) -> Self {
Self(conn.inner())
}
pub fn get(&self, id: &str) -> Result<flowctl_core::types::Task, DbError> {
block_on(flowctl_db::TaskRepo::new(self.0.clone()).get(id))
}
pub fn get_with_body(
&self,
id: &str,
) -> Result<(flowctl_core::types::Task, String), DbError> {
block_on(flowctl_db::TaskRepo::new(self.0.clone()).get_with_body(id))
}
pub fn list_by_epic(
&self,
epic_id: &str,
) -> Result<Vec<flowctl_core::types::Task>, DbError> {
block_on(flowctl_db::TaskRepo::new(self.0.clone()).list_by_epic(epic_id))
}
pub fn list_all(
&self,
status: Option<&str>,
domain: Option<&str>,
) -> Result<Vec<flowctl_core::types::Task>, DbError> {
block_on(flowctl_db::TaskRepo::new(self.0.clone()).list_all(status, domain))
}
pub fn upsert(&self, task: &flowctl_core::types::Task) -> Result<(), DbError> {
block_on(flowctl_db::TaskRepo::new(self.0.clone()).upsert(task))
}
pub fn upsert_with_body(
&self,
task: &flowctl_core::types::Task,
body: &str,
) -> Result<(), DbError> {
block_on(flowctl_db::TaskRepo::new(self.0.clone()).upsert_with_body(task, body))
}
pub fn update_status(
&self,
id: &str,
status: flowctl_core::state_machine::Status,
) -> Result<(), DbError> {
block_on(flowctl_db::TaskRepo::new(self.0.clone()).update_status(id, status))
}
}
// ── Dep repository ─────────────────────────────────────────────────
pub struct DepRepo(libsql::Connection);
impl DepRepo {
pub fn new(conn: &Connection) -> Self {
Self(conn.inner())
}
pub fn add_task_dep(&self, task_id: &str, depends_on: &str) -> Result<(), DbError> {
block_on(
flowctl_db::DepRepo::new(self.0.clone()).add_task_dep(task_id, depends_on),
)
}
pub fn remove_task_dep(&self, task_id: &str, depends_on: &str) -> Result<(), DbError> {
block_on(
flowctl_db::DepRepo::new(self.0.clone()).remove_task_dep(task_id, depends_on),
)
}
pub fn list_task_deps(&self, task_id: &str) -> Result<Vec<String>, DbError> {
block_on(flowctl_db::DepRepo::new(self.0.clone()).list_task_deps(task_id))
}
/// Replace all deps for a task (delete-all + insert each).
pub fn replace_task_deps(&self, task_id: &str, deps: &[String]) -> Result<(), DbError> {
let inner = self.0.clone();
block_on(async move {
inner
.execute(
"DELETE FROM task_deps WHERE task_id = ?1",
libsql::params![task_id.to_string()],
)
.await?;
for d in deps {
inner
.execute(
"INSERT INTO task_deps (task_id, depends_on) VALUES (?1, ?2)",
libsql::params![task_id.to_string(), d.to_string()],
)
.await?;
}
Ok::<(), DbError>(())
})
}
}
// ── Runtime repository ─────────────────────────────────────────────
pub struct RuntimeRepo(libsql::Connection);
impl RuntimeRepo {
pub fn new(conn: &Connection) -> Self {
Self(conn.inner())
}
pub fn get(
&self,
task_id: &str,
) -> Result<Option<flowctl_core::types::RuntimeState>, DbError> {
block_on(flowctl_db::RuntimeRepo::new(self.0.clone()).get(task_id))
}
pub fn upsert(
&self,
state: &flowctl_core::types::RuntimeState,
) -> Result<(), DbError> {
block_on(flowctl_db::RuntimeRepo::new(self.0.clone()).upsert(state))
}
}
// ── File lock repository ───────────────────────────────────────────
pub struct FileLockRepo(libsql::Connection);
impl FileLockRepo {
pub fn new(conn: &Connection) -> Self {
Self(conn.inner())
}
pub fn acquire(&self, file_path: &str, task_id: &str) -> Result<(), DbError> {
block_on(
flowctl_db::FileLockRepo::new(self.0.clone()).acquire(file_path, task_id),
)
}
pub fn release_for_task(&self, task_id: &str) -> Result<u64, DbError> {
block_on(flowctl_db::FileLockRepo::new(self.0.clone()).release_for_task(task_id))
}
pub fn release_all(&self) -> Result<u64, DbError> {
block_on(flowctl_db::FileLockRepo::new(self.0.clone()).release_all())
}
pub fn check(&self, file_path: &str) -> Result<Option<String>, DbError> {
block_on(flowctl_db::FileLockRepo::new(self.0.clone()).check(file_path))
}
/// List all active locks: (file_path, task_id, locked_at).
pub fn list_all(&self) -> Result<Vec<(String, String, String)>, DbError> {
let inner = self.0.clone();
block_on(async move {
let mut rows = inner
.query(
"SELECT file_path, task_id, locked_at FROM file_locks ORDER BY file_path",
(),
)
.await?;
let mut out = Vec::new();
while let Some(row) = rows.next().await? {
out.push((
row.get::<String>(0)?,
row.get::<String>(1)?,
row.get::<String>(2)?,
));
}
Ok(out)
})
}
}
// ── Phase progress repository ──────────────────────────────────────
pub struct PhaseProgressRepo(libsql::Connection);
impl PhaseProgressRepo {
pub fn new(conn: &Connection) -> Self {
Self(conn.inner())
}
pub fn get_completed(&self, task_id: &str) -> Result<Vec<String>, DbError> {
block_on(
flowctl_db::PhaseProgressRepo::new(self.0.clone()).get_completed(task_id),
)
}
pub fn mark_done(&self, task_id: &str, phase: &str) -> Result<(), DbError> {
block_on(
flowctl_db::PhaseProgressRepo::new(self.0.clone()).mark_done(task_id, phase),
)
}
}
// ── Stats query ────────────────────────────────────────────────────
pub struct StatsQuery(libsql::Connection);
impl StatsQuery {
pub fn new(conn: &Connection) -> Self {
Self(conn.inner())
}
pub fn summary(&self) -> Result<Summary, DbError> {
block_on(flowctl_db::StatsQuery::new(self.0.clone()).summary())
}
pub fn per_epic(&self, epic_id: Option<&str>) -> Result<Vec<EpicStats>, DbError> {
block_on(flowctl_db::StatsQuery::new(self.0.clone()).epic_stats(epic_id))
}
pub fn weekly_trends(&self, weeks: u32) -> Result<Vec<WeeklyTrend>, DbError> {
block_on(flowctl_db::StatsQuery::new(self.0.clone()).weekly_trends(weeks))
}
pub fn token_breakdown(
&self,
epic_id: Option<&str>,
) -> Result<Vec<TokenBreakdown>, DbError> {
block_on(flowctl_db::StatsQuery::new(self.0.clone()).token_breakdown(epic_id))
}
pub fn bottlenecks(&self, limit: usize) -> Result<Vec<Bottleneck>, DbError> {
block_on(flowctl_db::StatsQuery::new(self.0.clone()).bottlenecks(limit))
}
pub fn dora_metrics(&self) -> Result<DoraMetrics, DbError> {
block_on(flowctl_db::StatsQuery::new(self.0.clone()).dora_metrics())
}
pub fn generate_monthly_rollups(&self) -> Result<u64, DbError> {
block_on(flowctl_db::StatsQuery::new(self.0.clone()).generate_monthly_rollups())
}
}