orso 0.0.2

ORSO - A ORm for turSO and SQLite with zero-loss migrations
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
// Migration system with zero-loss schema changes
use crate::{Orso, database::Database, error::Error, traits::FieldType};
// use chrono::{DateTime, Utc}; // Reserved for future migration timestamp features
// use serde::{Deserialize, Serialize}; // Reserved for future migration serialization
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub struct MigrationConfig {
    max_backups_per_table: Option<u8>,
    backup_retention_days: Option<u8>,
    backup_suffix: Option<String>,
}

impl Default for MigrationConfig {
    fn default() -> Self {
        Self {
            max_backups_per_table: Some(5),
            backup_retention_days: Some(30),
            backup_suffix: Some("migration".to_string()),
        }
    }
}

impl MigrationConfig {
    // Direct getters with built-in defaults
    pub fn max_backups(&self) -> u8 {
        self.max_backups_per_table.unwrap_or(5)
    }

    pub fn retention_days(&self) -> u8 {
        self.backup_retention_days.unwrap_or(30)
    }

    pub fn suffix(&self) -> &str {
        self.backup_suffix.as_deref().unwrap_or("migration")
    }
}

pub struct Migrations;

impl Migrations {
    /// Initialize database with migrations using default config
    /// Usage: Migrations::init(&db, &[migration!(User), migration!(Product)]).await?
    pub async fn init(
        db: &Database,
        migrations: &[Box<dyn MigrationTrait>],
    ) -> Result<Vec<MigrationResult>, Error> {
        Self::init_with_config(db, migrations, &MigrationConfig::default()).await
    }

    /// Initialize database with migrations and custom config
    /// Usage: Migrations::init_with_config(&db, &[migration!(User)], &config).await?
    pub async fn init_with_config(
        db: &Database,
        migrations: &[Box<dyn MigrationTrait>],
        config: &MigrationConfig,
    ) -> Result<Vec<MigrationResult>, Error> {
        let mut results = Vec::new();

        for migration in migrations {
            let result = migration.run_migration(db, config).await?;
            results.push(result);
        }

        Ok(results)
    }
}

// Trait for migrations to avoid generic constraints
#[async_trait::async_trait]
pub trait MigrationTrait: Send + Sync {
    async fn run_migration(
        &self,
        db: &Database,
        config: &MigrationConfig,
    ) -> Result<MigrationResult, Error>;
}

// Migration entry for the init system
pub struct MigrationEntry<T: Orso + Default> {
    _phantom: std::marker::PhantomData<T>,
    custom_table_name: Option<String>,
}

impl<T: Orso + Default> MigrationEntry<T> {
    pub fn new() -> Self {
        Self {
            _phantom: std::marker::PhantomData,
            custom_table_name: None,
        }
    }

    pub fn with_custom_name(table_name: String) -> Self {
        Self {
            _phantom: std::marker::PhantomData,
            custom_table_name: Some(table_name),
        }
    }
}

#[async_trait::async_trait]
impl<T: Orso + Default + Send + Sync> MigrationTrait for MigrationEntry<T> {
    async fn run_migration(
        &self,
        db: &Database,
        config: &MigrationConfig,
    ) -> Result<MigrationResult, Error> {
        if let Some(custom_name) = &self.custom_table_name {
            ensure_table_with_name::<T>(db, custom_name, config).await
        } else {
            ensure_table::<T>(db, config).await
        }
    }
}

// migration! macro creates boxed MigrationEntry
#[macro_export]
macro_rules! migration {
    ($model:ty) => {
        Box::new($crate::migrations::MigrationEntry::<$model>::new())
            as Box<dyn $crate::migrations::MigrationTrait>
    };
    ($model:ty, $custom_name:expr) => {
        Box::new(
            $crate::migrations::MigrationEntry::<$model>::with_custom_name(
                $custom_name.to_string(),
            ),
        ) as Box<dyn $crate::migrations::MigrationTrait>
    };
}

#[derive(Debug, Clone)]
pub struct ColumnInfo {
    pub name: String,
    pub sql_type: String,
    pub nullable: bool,
    pub position: i32,
}

#[derive(Debug, Clone)]
pub struct SchemaComparison {
    pub needs_migration: bool,
    pub changes: Vec<String>,
    pub current_columns: Vec<ColumnInfo>,
    pub expected_columns: Vec<ColumnInfo>,
}

#[derive(Debug, Clone)]
pub enum MigrationAction {
    TableCreated,
    SchemaMatched,
    DataMigrated { from: String, to: String },
}

#[derive(Debug, Clone)]
pub struct MigrationResult {
    pub action: MigrationAction,
    pub backup_table: Option<String>,
    pub rows_migrated: Option<u64>,
    pub schema_changes: Vec<String>,
}

pub async fn ensure_table<T>(
    db: &Database,
    config: &MigrationConfig,
) -> Result<MigrationResult, Error>
where
    T: Orso + Default,
{
    let table_name = T::table_name();
    ensure_table_with_name::<T>(db, table_name, config).await
}

pub async fn ensure_table_with_name<T>(
    db: &Database,
    table_name: &str,
    config: &MigrationConfig,
) -> Result<MigrationResult, Error>
where
    T: Orso + Default,
{
    // Step 1: Infer expected schema from Orso trait
    let expected_schema = infer_schema_from_orso::<T>()?;

    // Step 2: Check if table exists
    let table_exists = check_table_exists(db, table_name).await?;

    if !table_exists {
        // Enable foreign key constraints for SQLite
        db.conn
            .execute("PRAGMA foreign_keys = ON", ())
            .await
            .map_err(|e| Error::DatabaseError(format!("Failed to enable foreign keys: {}", e)))?;

        // Create new table using custom SQL generation with table name override
        let create_sql = generate_migration_sql_with_custom_name::<T>(table_name);

        db.conn
            .execute(&create_sql, ())
            .await
            .map_err(|e| Error::DatabaseError(format!("Failed to create table: {}", e)))?;

        return Ok(MigrationResult {
            action: MigrationAction::TableCreated,
            backup_table: None,
            rows_migrated: None,
            schema_changes: vec![format!("Created table {} from schema", table_name)],
        });
    }

    // Step 3: Compare current vs expected schema
    let current_schema = get_current_table_schema(db, table_name).await?;
    let comparison = compare_schemas(&current_schema, &expected_schema);

    if !comparison.needs_migration {
        return Ok(MigrationResult {
            action: MigrationAction::SchemaMatched,
            backup_table: None,
            rows_migrated: None,
            schema_changes: vec![],
        });
    }

    // Step 4: Perform zero-loss migration using proven algorithm
    perform_zero_loss_migration(db, table_name, &comparison, config).await
}

fn generate_migration_sql_with_custom_name<T>(table_name: &str) -> String
where
    T: Orso,
{
    // Get the original migration SQL and replace the table name
    let original_sql = T::migration_sql();
    let original_table_name = T::table_name();

    // Replace the table name in the SQL
    // Handle both quoted and unquoted table names
    let replacements = [
        (
            format!("CREATE TABLE {}", original_table_name),
            format!("CREATE TABLE {}", table_name),
        ),
        (
            format!("CREATE TABLE \"{}\"", original_table_name),
            format!("CREATE TABLE \"{}\"", table_name),
        ),
        (
            format!("CREATE TABLE IF NOT EXISTS {}", original_table_name),
            format!("CREATE TABLE IF NOT EXISTS {}", table_name),
        ),
        (
            format!("CREATE TABLE IF NOT EXISTS \"{}\"", original_table_name),
            format!("CREATE TABLE IF NOT EXISTS \"{}\"", table_name),
        ),
    ];

    let mut modified_sql = original_sql;
    for (from, to) in replacements {
        modified_sql = modified_sql.replace(&from, &to);
    }

    modified_sql
}

fn infer_schema_from_orso<T>() -> Result<Vec<ColumnInfo>, Error>
where
    T: Orso,
{
    let mut columns = Vec::new();

    // Only add columns that actually exist in the struct
    let field_names = T::field_names();
    let field_types = T::field_types();
    let field_nullable = T::field_nullable();

    if field_names.len() != field_types.len() || field_names.len() != field_nullable.len() {
        return Err(Error::DatabaseError(
            "Mismatched field arrays in Orso implementation".to_string(),
        ));
    }

    for (i, ((name, field_type), nullable)) in field_names
        .iter()
        .zip(field_types.iter())
        .zip(field_nullable.iter())
        .enumerate()
    {
        columns.push(ColumnInfo {
            name: name.to_string(),
            sql_type: field_type_to_sqlite_type(field_type),
            nullable: *nullable,
            position: i as i32,
        });
    }

    Ok(columns)
}

fn field_type_to_sqlite_type(field_type: &FieldType) -> String {
    match field_type {
        FieldType::Text => "TEXT".to_string(),
        FieldType::Integer => "INTEGER".to_string(),
        FieldType::BigInt => "INTEGER".to_string(),
        FieldType::Numeric => "REAL".to_string(),
        FieldType::Boolean => "INTEGER".to_string(),
        FieldType::JsonB => "TEXT".to_string(),
        FieldType::Timestamp => "TEXT".to_string(),
    }
}

async fn check_table_exists(db: &Database, table_name: &str) -> Result<bool, Error> {
    let query = format!(
        "SELECT name FROM sqlite_master WHERE type='table' AND name='{}'",
        table_name
    );

    let mut rows = db
        .conn
        .query(&query, ())
        .await
        .map_err(|e| Error::DatabaseError(format!("Failed to check table existence: {}", e)))?;

    match rows
        .next()
        .await
        .map_err(|e| Error::DatabaseError(e.to_string()))?
    {
        Some(_) => Ok(true),
        None => Ok(false),
    }
}

async fn get_current_table_schema(
    db: &Database,
    table_name: &str,
) -> Result<Vec<ColumnInfo>, Error> {
    let query = format!("PRAGMA table_info({})", table_name);

    let mut rows = db
        .conn
        .query(&query, ())
        .await
        .map_err(|e| Error::DatabaseError(format!("Failed to get table info: {}", e)))?;

    let mut columns = Vec::new();

    while let Some(row) = rows
        .next()
        .await
        .map_err(|e| Error::DatabaseError(e.to_string()))?
    {
        let cid: i32 = row
            .get(0)
            .map_err(|e| Error::DatabaseError(e.to_string()))?;
        let name: String = row
            .get(1)
            .map_err(|e| Error::DatabaseError(e.to_string()))?;
        let type_name: String = row
            .get(2)
            .map_err(|e| Error::DatabaseError(e.to_string()))?;
        let not_null: i32 = row
            .get(3)
            .map_err(|e| Error::DatabaseError(e.to_string()))?;

        columns.push(ColumnInfo {
            name,
            sql_type: type_name.to_uppercase(),
            nullable: not_null == 0,
            position: cid,
        });
    }

    // Sort by position to maintain order
    columns.sort_by_key(|c| c.position);

    Ok(columns)
}

fn compare_schemas(current: &[ColumnInfo], expected: &[ColumnInfo]) -> SchemaComparison {
    let mut changes = Vec::new();
    let mut needs_migration = false;

    // Check if schemas are identical
    if current.len() != expected.len() {
        changes.push(format!(
            "Column count differs: {} vs {}",
            current.len(),
            expected.len()
        ));
        needs_migration = true;
    }

    // Create maps for easier comparison
    let current_map: HashMap<String, &ColumnInfo> =
        current.iter().map(|c| (c.name.clone(), c)).collect();
    let expected_map: HashMap<String, &ColumnInfo> =
        expected.iter().map(|c| (c.name.clone(), c)).collect();

    // Check for missing columns
    for expected_col in expected {
        match current_map.get(&expected_col.name) {
            Some(current_col) => {
                if current_col.sql_type != expected_col.sql_type {
                    changes.push(format!(
                        "Type mismatch for {}: {} vs {}",
                        expected_col.name, current_col.sql_type, expected_col.sql_type
                    ));
                    needs_migration = true;
                }
                if current_col.nullable != expected_col.nullable {
                    changes.push(format!(
                        "Nullability mismatch for {}: {} vs {}",
                        expected_col.name, current_col.nullable, expected_col.nullable
                    ));
                    needs_migration = true;
                }
                if current_col.position != expected_col.position {
                    changes.push(format!(
                        "Position mismatch for {}: {} vs {}",
                        expected_col.name, current_col.position, expected_col.position
                    ));
                    needs_migration = true;
                }
            }
            None => {
                changes.push(format!("Missing column: {}", expected_col.name));
                needs_migration = true;
            }
        }
    }

    // Check for extra columns
    for current_col in current {
        if !expected_map.contains_key(&current_col.name) {
            changes.push(format!("Extra column: {}", current_col.name));
            needs_migration = true;
        }
    }

    SchemaComparison {
        needs_migration,
        changes,
        current_columns: current.to_vec(),
        expected_columns: expected.to_vec(),
    }
}

async fn perform_zero_loss_migration(
    db: &Database,
    table_name: &str,
    comparison: &SchemaComparison,
    config: &MigrationConfig,
) -> Result<MigrationResult, Error> {
    // Generate unique backup table name with timestamp hash
    let timestamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs();
    let backup_name = format!("{}_{}_{}", table_name, config.suffix(), timestamp);

    // Step 1: Create new table with correct schema
    let temp_table_name = format!("{}_temp_{}", table_name, timestamp);
    let create_sql = generate_create_table_sql(&temp_table_name, &comparison.expected_columns);

    db.conn
        .execute(&create_sql, ())
        .await
        .map_err(|e| Error::DatabaseError(format!("Failed to create temp table: {}", e)))?;

    // Step 2: Copy data from old table to new table (preserving row order)
    let copy_sql = generate_data_migration_sql(
        table_name,
        &temp_table_name,
        &comparison.current_columns,
        &comparison.expected_columns,
    );

    let _rows_affected = db
        .conn
        .execute(&copy_sql, ())
        .await
        .map_err(|e| Error::DatabaseError(format!("Failed to migrate data: {}", e)))?;

    // Step 3: Rename original table to backup
    let rename_to_backup = format!("ALTER TABLE {} RENAME TO {}", table_name, backup_name);
    db.conn
        .execute(&rename_to_backup, ())
        .await
        .map_err(|e| Error::DatabaseError(format!("Failed to create backup: {}", e)))?;

    // Step 4: Rename new table to original name
    let rename_to_original = format!("ALTER TABLE {} RENAME TO {}", temp_table_name, table_name);
    db.conn
        .execute(&rename_to_original, ())
        .await
        .map_err(|e| Error::DatabaseError(format!("Failed to rename new table: {}", e)))?;

    // Step 5: Verify migration success
    let verification_sql = format!("SELECT COUNT(*) FROM {}", table_name);
    let mut rows = db
        .conn
        .query(&verification_sql, ())
        .await
        .map_err(|e| Error::DatabaseError(format!("Failed to verify migration: {}", e)))?;

    let row_count: i64 = if let Some(row) = rows
        .next()
        .await
        .map_err(|e| Error::DatabaseError(e.to_string()))?
    {
        row.get(0)
            .map_err(|e| Error::DatabaseError(e.to_string()))?
    } else {
        0
    };

    check_backups_retention(db, table_name, config).await?;

    Ok(MigrationResult {
        action: MigrationAction::DataMigrated {
            from: backup_name.clone(),
            to: table_name.to_string(),
        },
        backup_table: Some(backup_name),
        rows_migrated: Some(row_count as u64),
        schema_changes: comparison.changes.clone(),
    })
}

fn generate_create_table_sql(table_name: &str, columns: &[ColumnInfo]) -> String {
    let mut column_defs = Vec::new();

    for column in columns {
        let mut def = format!("\"{}\" {}", column.name, column.sql_type);

        if !column.nullable {
            def.push_str(" NOT NULL");
        }

        // Column defaults are now handled by the macro's column definition

        column_defs.push(def);
    }

    format!(
        "CREATE TABLE IF NOT EXISTS \"{}\" (\n  {}\n)",
        table_name,
        column_defs.join(",\n  ")
    )
}

fn generate_data_migration_sql(
    source_table: &str,
    target_table: &str,
    source_columns: &[ColumnInfo],
    target_columns: &[ColumnInfo],
) -> String {
    // Create maps for column matching
    let source_map: HashMap<String, &ColumnInfo> =
        source_columns.iter().map(|c| (c.name.clone(), c)).collect();

    let mut select_columns = Vec::new();

    for target_col in target_columns {
        if let Some(_source_col) = source_map.get(&target_col.name) {
            // Column exists in both, copy directly
            select_columns.push(format!("\"{}\"", target_col.name));
        } else {
            // Column doesn't exist in source, use NULL or appropriate default
            if target_col.nullable {
                select_columns.push("NULL".to_string());
            } else {
                // Provide default values for NOT NULL columns based on type
                match target_col.sql_type.as_str() {
                    "TEXT" => select_columns.push("''".to_string()),
                    "INTEGER" => select_columns.push("0".to_string()),
                    "REAL" => select_columns.push("0.0".to_string()),
                    _ => select_columns.push("NULL".to_string()),
                }
            }
        }
    }

    let target_column_names: Vec<String> = target_columns
        .iter()
        .map(|c| format!("\"{}\"", c.name))
        .collect();

    format!(
        "INSERT INTO \"{}\" ({}) SELECT {} FROM \"{}\" ORDER BY rowid",
        target_table,
        target_column_names.join(", "),
        select_columns.join(", "),
        source_table
    )
}

async fn check_backups_retention(
    db: &Database,
    table_name: &str,
    config: &MigrationConfig,
) -> Result<(), Error> {
    // Get all migration tables for this base table
    let migration_tables = get_all_migration_tables(db, table_name, config.suffix()).await?;

    let current_time = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap()
        .as_secs();

    // Sort by timestamp (newest first)
    let mut sorted_tables = migration_tables;
    sorted_tables.sort_by(|a, b| b.timestamp.cmp(&a.timestamp));

    // Delete tables that exceed the maximum count OR are too old
    for (index, old_table) in sorted_tables.iter().enumerate() {
        let age_seconds = current_time - old_table.timestamp;
        let age_days = age_seconds / 86400; // seconds to days

        let should_delete =
            // Delete if we exceed max backups (keep only the most recent ones)
            index >= config.max_backups() as usize ||
            // OR delete if older than retention policy
            age_days > config.retention_days() as u64;

        if should_delete {
            let drop_sql = format!("DROP TABLE IF EXISTS \"{}\"", old_table.name);
            db.conn.execute(&drop_sql, ()).await.map_err(|e| {
                Error::DatabaseError(format!("Failed to drop old migration table: {}", e))
            })?;

            tracing::info!(
                "Cleaned up old migration table: {} (age: {} days, index: {})",
                old_table.name,
                age_days,
                index
            );
        }
    }

    Ok(())
}

#[derive(Debug)]
struct MigrationTableInfo {
    name: String,
    timestamp: u64,
}

async fn get_all_migration_tables(
    db: &Database,
    base_table: &str,
    suffix: &str,
) -> Result<Vec<MigrationTableInfo>, Error> {
    let pattern = format!("{}_{}_", base_table, suffix);
    let query = format!(
        "SELECT name FROM sqlite_master WHERE type='table' AND name LIKE '{}%'",
        pattern
    );

    let mut rows =
        db.conn.query(&query, ()).await.map_err(|e| {
            Error::DatabaseError(format!("Failed to query migration tables: {}", e))
        })?;

    let mut migration_tables = Vec::new();

    while let Some(row) = rows
        .next()
        .await
        .map_err(|e| Error::DatabaseError(e.to_string()))?
    {
        let table_name: String = row
            .get(0)
            .map_err(|e| Error::DatabaseError(e.to_string()))?;

        // Extract timestamp from table name like "table_migration_1234567890"
        let suffix_pattern = format!("_{}_", suffix);
        if let Some(timestamp_str) = table_name.split(&suffix_pattern).nth(1) {
            if let Ok(timestamp) = timestamp_str.parse::<u64>() {
                migration_tables.push(MigrationTableInfo {
                    name: table_name,
                    timestamp,
                });
            }
        }
    }

    Ok(migration_tables)
}

impl std::fmt::Display for MigrationAction {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MigrationAction::TableCreated => write!(f, "TableCreated"),
            MigrationAction::SchemaMatched => write!(f, "SchemaMatched"),
            MigrationAction::DataMigrated { from, to } => {
                write!(f, "DataMigrated from {} to {}", from, to)
            }
        }
    }
}