mixtape-tools 0.4.0

Ready-to-use tool implementations for the mixtape agent framework
Documentation
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Import migrations tool

use crate::prelude::*;
use crate::sqlite::manager::with_connection;

use super::{compute_checksum, ensure_migrations_table, MIGRATIONS_TABLE};

/// A migration record to import
#[derive(Debug, Deserialize, JsonSchema)]
pub struct MigrationRecord {
    /// Version identifier (timestamp format recommended)
    pub version: String,

    /// Human-readable name/description
    pub name: String,

    /// The SQL to execute
    pub sql: String,

    /// Optional checksum for verification (computed if not provided)
    #[serde(default)]
    pub checksum: Option<String>,
}

/// Input for importing migrations
#[derive(Debug, Deserialize, JsonSchema)]
pub struct ImportMigrationsInput {
    /// Database to import migrations into (uses default if not specified)
    #[serde(default)]
    pub db_path: Option<String>,

    /// Migrations to import
    pub migrations: Vec<MigrationRecord>,

    /// How to handle migrations that already exist
    #[serde(default)]
    pub on_conflict: ConflictStrategy,
}

/// Strategy for handling migrations that already exist
#[derive(Debug, Default, Clone, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ConflictStrategy {
    /// Skip migrations that already exist (by version)
    #[default]
    Skip,
    /// Fail if any migration already exists
    Fail,
    /// Replace existing migrations (only if pending, fails for applied)
    Replace,
}

/// Result for a single migration import
#[derive(Debug, Serialize)]
struct ImportResult {
    version: String,
    name: String,
    status: &'static str,
    message: Option<String>,
}

/// Imports migrations from an export into the database as pending migrations
///
/// Use this to transfer migrations from one database to another. Imported
/// migrations are added as pending and must be applied using `sqlite_run_migrations`.
pub struct ImportMigrationsTool;

impl Tool for ImportMigrationsTool {
    type Input = ImportMigrationsInput;

    fn name(&self) -> &str {
        "sqlite_import_migrations"
    }

    fn description(&self) -> &str {
        "Import migrations into the database as pending migrations. \
         Use this to transfer migrations exported from another database via \
         sqlite_export_migrations. Imported migrations must be applied using \
         sqlite_run_migrations."
    }

    async fn execute(&self, input: Self::Input) -> Result<ToolResult, ToolError> {
        let migrations = input.migrations;
        let on_conflict = input.on_conflict;

        let (results, imported, skipped, failed) = with_connection(input.db_path, move |conn| {
            ensure_migrations_table(conn)?;

            let mut results = Vec::new();
            let mut imported = 0;
            let mut skipped = 0;
            let mut failed = 0;

            for migration in migrations {
                // Check if migration already exists
                let existing: Option<(String, Option<String>)> = conn
                    .query_row(
                        &format!(
                            "SELECT name, applied_at FROM {MIGRATIONS_TABLE} WHERE version = ?1"
                        ),
                        [&migration.version],
                        |row| Ok((row.get(0)?, row.get(1)?)),
                    )
                    .ok();

                if let Some((existing_name, applied_at)) = existing {
                    match on_conflict {
                        ConflictStrategy::Skip => {
                            results.push(ImportResult {
                                version: migration.version,
                                name: migration.name,
                                status: "skipped",
                                message: Some(format!(
                                    "Migration already exists as '{}'",
                                    existing_name
                                )),
                            });
                            skipped += 1;
                            continue;
                        }
                        ConflictStrategy::Fail => {
                            return Err(crate::sqlite::error::SqliteToolError::InvalidQuery(format!(
                                "Migration '{}' already exists. Use on_conflict: 'skip' or 'replace' to handle duplicates.",
                                migration.version
                            )));
                        }
                        ConflictStrategy::Replace => {
                            if applied_at.is_some() {
                                results.push(ImportResult {
                                    version: migration.version,
                                    name: migration.name,
                                    status: "failed",
                                    message: Some(
                                        "Cannot replace applied migration".to_string(),
                                    ),
                                });
                                failed += 1;
                                continue;
                            }
                            // Delete existing pending migration
                            conn.execute(
                                &format!("DELETE FROM {MIGRATIONS_TABLE} WHERE version = ?1"),
                                [&migration.version],
                            )?;
                        }
                    }
                }

                // Compute or verify checksum
                let computed_checksum = compute_checksum(&migration.sql);
                if let Some(provided) = &migration.checksum {
                    if provided != &computed_checksum {
                        results.push(ImportResult {
                            version: migration.version,
                            name: migration.name,
                            status: "failed",
                            message: Some(format!(
                                "Checksum mismatch: expected {}, got {}",
                                provided, computed_checksum
                            )),
                        });
                        failed += 1;
                        continue;
                    }
                }

                // Insert as pending migration
                conn.execute(
                    &format!(
                        "INSERT INTO {MIGRATIONS_TABLE} (version, name, sql, applied_at, checksum) \
                         VALUES (?1, ?2, ?3, NULL, ?4)"
                    ),
                    (
                        &migration.version,
                        &migration.name,
                        &migration.sql,
                        &computed_checksum,
                    ),
                )?;

                results.push(ImportResult {
                    version: migration.version,
                    name: migration.name,
                    status: "imported",
                    message: None,
                });
                imported += 1;
            }

            Ok((results, imported, skipped, failed))
        })
        .await?;

        Ok(ToolResult::Json(serde_json::json!({
            "status": if failed == 0 { "success" } else { "partial" },
            "imported": imported,
            "skipped": skipped,
            "failed": failed,
            "results": results
        })))
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::sqlite::migration::add::AddMigrationInput;
    use crate::sqlite::migration::export::{
        ExportFormat, ExportMigrationsInput, ExportMigrationsTool,
    };
    use crate::sqlite::migration::list::{ListMigrationsInput, ListMigrationsTool};
    use crate::sqlite::migration::run::RunMigrationsInput;
    use crate::sqlite::migration::MigrationStatusFilter;
    use crate::sqlite::migration::{AddMigrationTool, RunMigrationsTool};
    use crate::sqlite::test_utils::{unwrap_json, TestDatabase};

    #[tokio::test]
    async fn test_import_migrations() {
        let db = TestDatabase::new().await;

        let tool = ImportMigrationsTool;
        let result = tool
            .execute(ImportMigrationsInput {
                db_path: Some(db.key()),
                migrations: vec![
                    MigrationRecord {
                        version: "20240101_120000_000000".to_string(),
                        name: "create users".to_string(),
                        sql: "CREATE TABLE users (id INTEGER PRIMARY KEY);".to_string(),
                        checksum: None,
                    },
                    MigrationRecord {
                        version: "20240101_120001_000000".to_string(),
                        name: "create posts".to_string(),
                        sql: "CREATE TABLE posts (id INTEGER PRIMARY KEY);".to_string(),
                        checksum: None,
                    },
                ],
                on_conflict: ConflictStrategy::Skip,
            })
            .await
            .unwrap();

        let json = unwrap_json(result);

        assert_eq!(json["status"], "success");
        assert_eq!(json["imported"], 2);
        assert_eq!(json["skipped"], 0);
    }

    #[tokio::test]
    async fn test_import_skip_existing() {
        let db = TestDatabase::new().await;

        // Add a migration directly
        AddMigrationTool
            .execute(AddMigrationInput {
                name: "existing".to_string(),
                sql: "CREATE TABLE existing (id INTEGER);".to_string(),
                db_path: Some(db.key()),
            })
            .await
            .unwrap();

        // Get the version
        let list_result = ListMigrationsTool
            .execute(ListMigrationsInput {
                db_path: Some(db.key()),
                filter: MigrationStatusFilter::All,
            })
            .await
            .unwrap();

        let list_json = unwrap_json(list_result);
        let version = list_json["migrations"][0]["version"]
            .as_str()
            .unwrap()
            .to_string();

        // Try to import with same version
        let tool = ImportMigrationsTool;
        let result = tool
            .execute(ImportMigrationsInput {
                db_path: Some(db.key()),
                migrations: vec![MigrationRecord {
                    version,
                    name: "different name".to_string(),
                    sql: "CREATE TABLE different (id INTEGER);".to_string(),
                    checksum: None,
                }],
                on_conflict: ConflictStrategy::Skip,
            })
            .await
            .unwrap();

        let json = unwrap_json(result);

        assert_eq!(json["imported"], 0);
        assert_eq!(json["skipped"], 1);
    }

    #[tokio::test]
    async fn test_import_fail_on_conflict() {
        let db = TestDatabase::new().await;

        // Add a migration
        AddMigrationTool
            .execute(AddMigrationInput {
                name: "existing".to_string(),
                sql: "CREATE TABLE existing (id INTEGER);".to_string(),
                db_path: Some(db.key()),
            })
            .await
            .unwrap();

        let list_result = ListMigrationsTool
            .execute(ListMigrationsInput {
                db_path: Some(db.key()),
                filter: MigrationStatusFilter::All,
            })
            .await
            .unwrap();

        let list_json = unwrap_json(list_result);
        let version = list_json["migrations"][0]["version"]
            .as_str()
            .unwrap()
            .to_string();

        // Try to import with fail strategy
        let tool = ImportMigrationsTool;
        let result = tool
            .execute(ImportMigrationsInput {
                db_path: Some(db.key()),
                migrations: vec![MigrationRecord {
                    version,
                    name: "different".to_string(),
                    sql: "CREATE TABLE different (id INTEGER);".to_string(),
                    checksum: None,
                }],
                on_conflict: ConflictStrategy::Fail,
            })
            .await;

        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_import_checksum_verification() {
        let db = TestDatabase::new().await;

        let tool = ImportMigrationsTool;
        let result = tool
            .execute(ImportMigrationsInput {
                db_path: Some(db.key()),
                migrations: vec![MigrationRecord {
                    version: "20240101_120000_000000".to_string(),
                    name: "test".to_string(),
                    sql: "CREATE TABLE test (id INTEGER);".to_string(),
                    checksum: Some("invalid_checksum".to_string()),
                }],
                on_conflict: ConflictStrategy::Skip,
            })
            .await
            .unwrap();

        let json = unwrap_json(result);

        assert_eq!(json["status"], "partial");
        assert_eq!(json["imported"], 0);
        assert_eq!(json["failed"], 1);
    }

    #[tokio::test]
    async fn test_roundtrip_export_import() {
        let db1 = TestDatabase::new().await;
        let db2 = TestDatabase::new().await;

        // Add migrations to db1
        AddMigrationTool
            .execute(AddMigrationInput {
                name: "create users".to_string(),
                sql: "CREATE TABLE users (id INTEGER PRIMARY KEY);".to_string(),
                db_path: Some(db1.key()),
            })
            .await
            .unwrap();

        AddMigrationTool
            .execute(AddMigrationInput {
                name: "create posts".to_string(),
                sql: "CREATE TABLE posts (id INTEGER PRIMARY KEY);".to_string(),
                db_path: Some(db1.key()),
            })
            .await
            .unwrap();

        // Export from db1
        let export_result = ExportMigrationsTool
            .execute(ExportMigrationsInput {
                db_path: Some(db1.key()),
                filter: MigrationStatusFilter::All,
                format: ExportFormat::Json,
            })
            .await
            .unwrap();

        let export_json = unwrap_json(export_result);
        let exported = export_json["migrations"].as_array().unwrap();

        // Convert to import format
        let migrations: Vec<MigrationRecord> = exported
            .iter()
            .map(|m| MigrationRecord {
                version: m["version"].as_str().unwrap().to_string(),
                name: m["name"].as_str().unwrap().to_string(),
                sql: m["sql"].as_str().unwrap().to_string(),
                checksum: Some(m["checksum"].as_str().unwrap().to_string()),
            })
            .collect();

        // Import to db2
        let import_result = ImportMigrationsTool
            .execute(ImportMigrationsInput {
                db_path: Some(db2.key()),
                migrations,
                on_conflict: ConflictStrategy::Skip,
            })
            .await
            .unwrap();

        let json = unwrap_json(import_result);

        assert_eq!(json["status"], "success");
        assert_eq!(json["imported"], 2);

        // Run migrations on db2
        RunMigrationsTool
            .execute(RunMigrationsInput {
                db_path: Some(db2.key()),
            })
            .await
            .unwrap();

        // Verify tables exist in db2
        let rows = db2.query(
            "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name IN ('users', 'posts')",
        );
        assert_eq!(rows[0][0], 2);
    }
}