tinyetl 0.10.0

Fast, zero-config ETL in a single binary for transforming data between formats and databases
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
use indicatif::{ProgressBar, ProgressStyle};
use std::time::Instant;
use tracing::{info, warn};

use crate::{
    config::Config,
    connectors::{Source, Target},
    schema::{Schema, SchemaFile},
    transformer::Transformer,
    Result,
};

pub struct TransferEngine;

pub struct TransferStats {
    pub total_rows: usize,
    pub total_time: std::time::Duration,
    pub rows_per_second: f64,
    pub batches_processed: usize,
}

impl TransferEngine {
    pub async fn execute(
        config: &Config,
        mut source: Box<dyn Source>,
        mut target: Box<dyn Target>,
    ) -> Result<TransferStats> {
        let start_time = Instant::now();

        // Step 1: Connect to source and target
        info!("→ Connecting to source: {}", config.source);
        source.connect().await?;

        info!("→ Connecting to target: {}", config.target);
        target.connect().await?;

        // Step 2: Infer or load schema
        let schema = if let Some(schema_file_path) = &config.schema_file {
            info!("→ Loading schema from file: {}", schema_file_path);
            let schema_file = SchemaFile::from_file(schema_file_path)?;
            // Even when using a schema file, we need to initialize the source
            // (e.g., for pagination state in SQLite)
            let _ = source.infer_schema(1).await?;
            schema_file.to_schema()?
        } else {
            info!("→ Inferring schema from source...");
            source.infer_schema(1000).await?
        };
        info!("→ {} columns detected", schema.columns.len());

        // Step 3: Handle preview mode
        if let Some(preview_rows) = config.preview {
            return Self::handle_preview(source, &schema, preview_rows, config).await;
        }

        // Step 4: Handle dry run mode
        if config.dry_run {
            return Self::handle_dry_run(source, target, &schema, config).await;
        }

        // Step 5: Initialize transformer and determine final schema
        let mut transformer = Transformer::new(&config.transform)?;
        let final_schema = if transformer.is_enabled() {
            info!("→ Transformation enabled");

            // Read a small sample to infer the transformed schema
            source.reset().await?;
            let sample_batch = source.read_batch(1).await?;

            if !sample_batch.is_empty() {
                transformer.transform_batch(&sample_batch)?;
                if let Some(transform_schema) = transformer.get_inferred_schema() {
                    info!(
                        "→ Schema updated by transformations: {} columns",
                        transform_schema.columns.len()
                    );
                    transform_schema.clone()
                } else {
                    schema.clone()
                }
            } else {
                schema.clone()
            }
        } else {
            schema.clone()
        };

        // Step 6: Extract table name from target
        let table_name = Self::extract_table_name(&config.target);

        // Step 7: Handle append-first logic or truncate mode
        let table_exists = target.exists(&table_name).await?;

        if table_exists {
            if config.truncate {
                info!("→ Truncating existing target: {}", table_name);
                target.truncate(&table_name).await?;
                // After truncating, we need to create the table again
                target.create_table(&table_name, &final_schema).await?;
            } else if target.supports_append() {
                info!("→ Appending to existing target: {}", table_name);
                // For append mode, we still need to set the schema for the target
                target.create_table(&table_name, &final_schema).await?;
            } else {
                // Target exists but doesn't support append - must truncate
                info!(
                    "→ Target exists but doesn't support append, truncating: {}",
                    table_name
                );
                target.truncate(&table_name).await?;
                // After truncating, we need to create the table again
                target.create_table(&table_name, &final_schema).await?;
            }
        } else {
            // Step 8: Create target table with final schema if it doesn't exist
            info!("→ Creating target table: {}", table_name);
            target.create_table(&table_name, &final_schema).await?;
        }

        // Step 9: Transfer data
        let estimated_rows = source.estimated_row_count().await?.unwrap_or(0);
        info!("→ Copying {} rows", estimated_rows);

        let progress_bar = if estimated_rows > 0 {
            let pb = ProgressBar::new(estimated_rows as u64);
            let style = ProgressStyle::default_bar()
                .template("{bar:40.cyan/blue} {percent}% ({pos}/{len}) {msg}")
                .expect("Failed to create progress bar template")
                .progress_chars("█▇▆▅▄▃▂▁  ");
            pb.set_style(style);
            Some(pb)
        } else {
            None
        };

        // Load schema file for validation if provided
        let schema_file = if let Some(schema_file_path) = &config.schema_file {
            Some(SchemaFile::from_file(schema_file_path)?)
        } else {
            None
        };

        let mut total_rows = 0;
        let mut batches_processed = 0;

        source.reset().await?;

        while source.has_more() {
            let mut batch = source.read_batch(config.batch_size).await?;
            if batch.is_empty() {
                break;
            }

            // Apply schema validation and defaults if schema file is provided
            if let Some(ref schema_file) = schema_file {
                for row in &mut batch {
                    schema_file.validate_and_transform_row(row)?;
                }
            }

            // Apply transformations
            let processed_batch = if transformer.is_enabled() {
                transformer.transform_batch(&batch)?
            } else {
                batch
            };

            let written = target.write_batch(&processed_batch).await?;
            total_rows += written;
            batches_processed += 1;

            if let Some(ref pb) = progress_bar {
                pb.set_position(total_rows as u64);
                pb.set_message(format!(
                    "{}k rows/sec",
                    (total_rows as f64 / start_time.elapsed().as_secs_f64() / 1000.0) as u64
                ));
            }
        }

        if let Some(pb) = progress_bar {
            pb.finish_with_message("Complete");
        }

        // Step 10: Finalize
        target.finalize().await?;

        let total_time = start_time.elapsed();
        let rows_per_second = total_rows as f64 / total_time.as_secs_f64();

        info!("→ Done in {:.1}s", total_time.as_secs_f64());

        Ok(TransferStats {
            total_rows,
            total_time,
            rows_per_second,
            batches_processed,
        })
    }

    async fn handle_preview(
        mut source: Box<dyn Source>,
        schema: &Schema,
        preview_rows: usize,
        config: &Config,
    ) -> Result<TransferStats> {
        // Initialize transformer for preview
        let mut transformer = Transformer::new(&config.transform)?;

        println!("\nOriginal Schema Preview:");
        Self::print_schema(schema);

        source.reset().await?;
        let sample_data = source.read_batch(preview_rows).await?;

        let (_final_schema, final_data) = if transformer.is_enabled() && !sample_data.is_empty() {
            println!("\nApplying transformations...");
            let transformed_data = transformer.transform_batch(&sample_data)?;

            if let Some(transform_schema) = transformer.get_inferred_schema() {
                println!("\nTransformed Schema Preview:");
                Self::print_schema(transform_schema);
                (transform_schema, transformed_data)
            } else {
                (schema, sample_data)
            }
        } else {
            (schema, sample_data)
        };

        println!("\nData Preview ({} rows):", preview_rows);
        Self::print_data_table(&final_data);

        Ok(TransferStats {
            total_rows: 0,
            total_time: std::time::Duration::from_secs(0),
            rows_per_second: 0.0,
            batches_processed: 0,
        })
    }

    fn print_schema(schema: &Schema) {
        println!("┌─────────────────────┬───────────────┬──────────┐");
        println!("│ Column              │ Type          │ Nullable │");
        println!("├─────────────────────┼───────────────┼──────────┤");

        for column in &schema.columns {
            println!(
                "│ {:<19} │ {:<13} │ {:<8} │",
                column.name,
                format!("{}", column.data_type),
                format!("{}", column.nullable)
            );
        }
        println!("└─────────────────────┴───────────────┴──────────┘");
    }

    fn print_data_table(data: &[crate::schema::Row]) {
        if data.is_empty() {
            println!("No data to display");
            return;
        }

        // Print column headers
        let headers: Vec<&String> = data[0].keys().collect();

        // Print top border
        print!("┌");
        for i in 0..headers.len() {
            print!("─────────────────");
            if i < headers.len() - 1 {
                print!("┬");
            }
        }
        println!("┐");

        // Print headers
        print!("│");
        for header in &headers {
            print!(" {:<15} │", header);
        }
        println!();

        // Print separator
        print!("├");
        for i in 0..headers.len() {
            print!("─────────────────");
            if i < headers.len() - 1 {
                print!("┼");
            }
        }
        println!("┤");

        // Print data rows
        for row in data {
            print!("│");
            for header in &headers {
                let value_str = match row.get(*header) {
                    Some(value) => format!("{:?}", value).chars().take(15).collect(),
                    None => "NULL".to_string(),
                };
                print!(" {:<15} │", value_str);
            }
            println!();
        }

        // Print bottom border
        print!("└");
        for i in 0..headers.len() {
            print!("─────────────────");
            if i < headers.len() - 1 {
                print!("┴");
            }
        }
        println!("┘");
    }

    async fn handle_dry_run(
        mut source: Box<dyn Source>,
        target: Box<dyn Target>,
        schema: &Schema,
        config: &Config,
    ) -> Result<TransferStats> {
        info!("Dry run mode - validating connections and schema");

        let estimated_rows = source.estimated_row_count().await?.unwrap_or(0);
        info!("Source connection validated");
        info!("Schema inferred: {} columns", schema.columns.len());
        info!("Estimated rows: {}", estimated_rows);

        // Test transformations if enabled
        let mut transformer = Transformer::new(&config.transform)?;
        if transformer.is_enabled() {
            info!("Testing transformations...");
            source.reset().await?;
            let test_batch = source.read_batch(10).await?; // Small sample for testing

            if !test_batch.is_empty() {
                let _transformed = transformer.transform_batch(&test_batch)?;
                if let Some(transform_schema) = transformer.get_inferred_schema() {
                    info!(
                        "Transformation successful: {} output columns",
                        transform_schema.columns.len()
                    );
                } else {
                    info!("Transformation successful: schema unchanged");
                }
            }
        }

        let table_name = Self::extract_table_name(&config.target);
        let table_exists = target.exists(&table_name).await?;

        if table_exists {
            warn!("Target table '{}' already exists", table_name);
        } else {
            info!("Target table '{}' will be created", table_name);
        }

        info!("Dry run completed successfully");

        Ok(TransferStats {
            total_rows: 0,
            total_time: std::time::Duration::from_secs(0),
            rows_per_second: 0.0,
            batches_processed: 0,
        })
    }

    fn extract_table_name(target: &str) -> String {
        if target.contains('#') {
            target.split('#').nth(1).unwrap_or("data").to_string()
        } else {
            // Extract filename without extension for file targets
            std::path::Path::new(target)
                .file_stem()
                .and_then(|s| s.to_str())
                .unwrap_or("data")
                .to_string()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::schema::{Column, DataType, Row, Value};
    use async_trait::async_trait;
    use std::collections::HashMap;

    // Mock source for testing
    struct MockSource {
        data: Vec<Row>,
        position: usize,
        connected: bool,
    }

    impl MockSource {
        fn new(data: Vec<Row>) -> Self {
            Self {
                data,
                position: 0,
                connected: false,
            }
        }
    }

    #[async_trait]
    impl Source for MockSource {
        async fn connect(&mut self) -> Result<()> {
            self.connected = true;
            Ok(())
        }

        async fn infer_schema(&mut self, _sample_size: usize) -> Result<Schema> {
            Ok(Schema {
                columns: vec![
                    Column {
                        name: "id".to_string(),
                        data_type: DataType::Integer,
                        nullable: false,
                    },
                    Column {
                        name: "name".to_string(),
                        data_type: DataType::String,
                        nullable: false,
                    },
                ],
                estimated_rows: Some(self.data.len()),
                primary_key_candidate: None,
            })
        }

        async fn read_batch(&mut self, batch_size: usize) -> Result<Vec<Row>> {
            let end = std::cmp::min(self.position + batch_size, self.data.len());
            let batch = self.data[self.position..end].to_vec();
            self.position = end;
            Ok(batch)
        }

        async fn estimated_row_count(&self) -> Result<Option<usize>> {
            Ok(Some(self.data.len()))
        }

        async fn reset(&mut self) -> Result<()> {
            self.position = 0;
            Ok(())
        }

        fn has_more(&self) -> bool {
            self.position < self.data.len()
        }
    }

    // Mock target for testing
    struct MockTarget {
        written_rows: Vec<Row>,
        connected: bool,
        table_created: bool,
    }

    impl MockTarget {
        fn new() -> Self {
            Self {
                written_rows: Vec::new(),
                connected: false,
                table_created: false,
            }
        }
    }

    #[async_trait]
    impl Target for MockTarget {
        async fn connect(&mut self) -> Result<()> {
            self.connected = true;
            Ok(())
        }

        async fn create_table(&mut self, _table_name: &str, _schema: &Schema) -> Result<()> {
            self.table_created = true;
            Ok(())
        }

        async fn write_batch(&mut self, rows: &[Row]) -> Result<usize> {
            self.written_rows.extend_from_slice(rows);
            Ok(rows.len())
        }

        async fn finalize(&mut self) -> Result<()> {
            Ok(())
        }

        async fn exists(&self, _table_name: &str) -> Result<bool> {
            Ok(false)
        }

        async fn truncate(&mut self, _table_name: &str) -> Result<()> {
            self.written_rows.clear();
            Ok(())
        }

        fn supports_append(&self) -> bool {
            true
        }
    }

    #[tokio::test]
    async fn test_transfer_execution() {
        let mut test_data = Vec::new();
        let mut row1 = HashMap::new();
        row1.insert("id".to_string(), Value::Integer(1));
        row1.insert("name".to_string(), Value::String("Alice".to_string()));
        test_data.push(row1);

        let mut row2 = HashMap::new();
        row2.insert("id".to_string(), Value::Integer(2));
        row2.insert("name".to_string(), Value::String("Bob".to_string()));
        test_data.push(row2);

        let source = MockSource::new(test_data);
        let target = MockTarget::new();

        let config = Config {
            source: "test.csv".to_string(),
            target: "test.db#users".to_string(),
            batch_size: 10,
            ..Default::default()
        };

        let stats = TransferEngine::execute(&config, Box::new(source), Box::new(target))
            .await
            .unwrap();

        assert_eq!(stats.total_rows, 2);
        assert_eq!(stats.batches_processed, 1);
    }

    #[test]
    fn test_extract_table_name() {
        assert_eq!(TransferEngine::extract_table_name("test.db#users"), "users");
        assert_eq!(TransferEngine::extract_table_name("output.csv"), "output");
        assert_eq!(TransferEngine::extract_table_name("data.json"), "data");
    }

    #[tokio::test]
    async fn test_transfer_with_preview() {
        let test_data = vec![{
            let mut row = HashMap::new();
            row.insert("id".to_string(), Value::Integer(1));
            row.insert("name".to_string(), Value::String("Alice".to_string()));
            row
        }];

        let source = MockSource::new(test_data);
        let target = MockTarget::new();

        let config = Config {
            source: "test.csv".to_string(),
            target: "test.db#users".to_string(),
            preview: Some(5),
            ..Default::default()
        };

        let stats = TransferEngine::execute(&config, Box::new(source), Box::new(target))
            .await
            .unwrap();

        // Preview mode should not transfer data
        assert_eq!(stats.total_rows, 0);
    }

    #[tokio::test]
    async fn test_transfer_with_dry_run() {
        let test_data = vec![{
            let mut row = HashMap::new();
            row.insert("id".to_string(), Value::Integer(1));
            row.insert("name".to_string(), Value::String("Alice".to_string()));
            row
        }];

        let source = MockSource::new(test_data);
        let target = MockTarget::new();

        let config = Config {
            source: "test.csv".to_string(),
            target: "test.db#users".to_string(),
            dry_run: true,
            ..Default::default()
        };

        let stats = TransferEngine::execute(&config, Box::new(source), Box::new(target))
            .await
            .unwrap();

        // Dry run should not transfer data
        assert_eq!(stats.total_rows, 0);
    }
}