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
use clap::Parser;
use tracing::{error, info};
use tracing_subscriber::{fmt, EnvFilter};

use tinyetl::{
    cli::Cli,
    config::Config,
    connectors::{create_source_from_url_with_type_and_options, create_target_from_url_with_options, Source, Target},
    secrets::process_connection_string,
    transfer::TransferEngine,
    yaml_config::YamlConfig,
};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cli = Cli::parse();

    // handle generate-default-config subcommand
    if cli.is_generate_default_config_mode() {
        return handle_generate_default_config();
    }

    // handle generate-config subcommand
    if cli.is_generate_config_mode() {
        return handle_generate_config(cli);
    }

    // load configuration either from YAML file or CLI arguments
    let config = load_config(cli)?;
    setup_logging(&config);

    // execute the transfer
    let (source, target) = create_connectors(&config).await?;
    execute_transfer(&config, source, target).await?;

    Ok(())
}

/// Handle the generate-config subcommand by converting CLI arguments to YAML config
fn handle_generate_config(cli: Cli) -> Result<(), Box<dyn std::error::Error>> {
    if let Some(tinyetl::cli::Commands::GenerateConfig {
        source,
        target,
        infer_schema,
        schema_file,
        batch_size,
        preview,
        dry_run,
        log_level,
        skip_existing,
        truncate,
        transform_file,
        transform,
        source_type,
        source_secret_id,
        dest_secret_id,
    }) = cli.command
    {
        let transform_config = determine_transform_config(&transform_file, &transform);

        let config = Config {
            source,
            target,
            infer_schema,
            schema_file,
            batch_size,
            preview,
            dry_run,
            log_level,
            skip_existing,
            truncate,
            transform: transform_config,
            source_type,
            source_secret_id,
            dest_secret_id,
            source_options: std::collections::HashMap::new(),
            target_options: std::collections::HashMap::new(),
        };

        let yaml_config = YamlConfig::from_config(config);
        let yaml_string = yaml_config.to_yaml_string()?;
        println!("{}", yaml_string);
    }

    Ok(())
}

/// Handle the generate-default-config subcommand by outputting the example config from README.md
fn handle_generate_default_config() -> Result<(), Box<dyn std::error::Error>> {
    let default_config = r#"# TinyETL Default Configuration Example
# -------------------------------------
#
# This is a configuration file for TinyETL.
# It is in YAML format.
# Comments start with the '#' character and are ignored.
# Blank lines are ignored.
#
# To use this configuration:
# 1. Customize this configuration as needed.
# 2. Save it as 'config.yaml' or with a similar name of your choosing.
# 3. Use the configuration by running: `tinyetl run config.yaml`.
#
# You may generate a configuration file from CLI arguments using:
# `tinyetl generate-config [OPTIONS] <SOURCE> <TARGET> > config.yaml`
#
# Use `${VAR_NAME}` syntax to insert dynamic values from environment variables.
# For example, to use a database password from an environment variable:
#   uri: "postgres://user:${DB_PASSWORD}@localhost:5432/dbname"

version: 1

source:
  uri: "employees.csv"            # or database connection string
  # options:                      # Optional source-specific options
  #   header.User-Agent: "TinyETL/0.9.0"
  #   header.Accept: "text/csv"
  #   auth.basic.username: "user"
  #   auth.basic.password: "${PASSWORD}"
  #   auth.bearer: "${BEARER_TOKEN}"

target:
  uri: "employees_output.json"    # or database connection string
  # options:                      # Optional target-specific options
  #   header.X-Custom: "value"

# The "options" key and all other keys beneath "options" can be omitted.
# Sensible default values will be used for omitted keys.

options:
  batch_size: 10000               # Number of rows per batch
  infer_schema: true              # Auto-detect column types
  schema_file: "schema path.yaml" # Override with external schema
  preview: 10                     # Show N rows without transfer
  dry_run: false                  # Validate without transferring
  log_level: info                 # info, warn, error (lowercase in YAML)
  skip_existing: false            # Skip if target exists
  source_type: "csv"              # Force source file type
  truncate: false                 # Truncate target before writing
  transform:                      # Inline Lua script transformation
    type: script
    value: |
      -- Calculate derived fields
      full_name = row.first_name .. " " .. row.last_name
      annual_salary = row.monthly_salary * 12
      hire_year = tonumber(string.sub(row.hire_date, 1, 4))

# The "transform" key can also specify a Lua script file, as follows:
#  transform:
#    type: file
#    value: "transform.lua"
"#;

    println!("{}", default_config);
    Ok(())
}

/// Determine the transformation configuration from CLI arguments
fn determine_transform_config(
    transform_file: &Option<String>,
    transform: &Option<String>,
) -> tinyetl::transformer::TransformConfig {
    match (transform_file, transform) {
        (Some(file), None) => tinyetl::transformer::TransformConfig::File(file.clone()),
        (None, Some(expressions)) => {
            tinyetl::transformer::TransformConfig::Inline(expressions.clone())
        }
        (Some(file), Some(_)) => {
            eprintln!(
                "Warning: Both --transform-file and --transform specified. Using --transform-file."
            );
            tinyetl::transformer::TransformConfig::File(file.clone())
        }
        (None, None) => tinyetl::transformer::TransformConfig::None,
    }
}

/// Load configuration from either YAML file or CLI arguments
fn load_config(cli: Cli) -> Result<Config, Box<dyn std::error::Error>> {
    if cli.is_config_mode() {
        if let Some(config_file) = cli.get_config_file() {
            let yaml_config = YamlConfig::from_file(config_file)?;
            yaml_config.into_config()
        } else {
            Err("No config file specified".into())
        }
    } else if cli.has_direct_params() {
        Ok(cli.into())
    } else {
        Err(
            "Error: SOURCE and TARGET are required.\n\nUsage: tinyetl <SOURCE> <TARGET> [OPTIONS]\n   or: tinyetl <COMMAND>\n\nFor more information, try '--help'".into(),
        )
    }
}

// Initialize logging with specific module filtering
// Respect RUST_LOG environment variable if set, otherwise use config
fn setup_logging(config: &Config) {
    let env_filter = if std::env::var("RUST_LOG").is_ok() {
        EnvFilter::from_default_env()
    } else {
        EnvFilter::new(format!(
            "sqlx=warn,tinyetl={}",
            match config.log_level {
                tinyetl::config::LogLevel::Info => "info",
                tinyetl::config::LogLevel::Warn => "warn",
                tinyetl::config::LogLevel::Error => "error",
            }
        ))
    };

    fmt().with_env_filter(env_filter).init();
}

/// Create source and target connectors with secret processing
async fn create_connectors(
    config: &Config,
) -> Result<(Box<dyn Source>, Box<dyn Target>), Box<dyn std::error::Error>> {
    let processed_source =
        process_connection_string(&config.source, config.source_secret_id.as_ref(), "source")?;

    let processed_target = process_connection_string(
        &config.target,
        config.dest_secret_id.as_ref(),
        "destination",
    )?;

    let source = create_source_from_url_with_type_and_options(
        &processed_source, 
        config.source_type.as_deref(),
        &config.source_options,
    ).await?;
    
    let target = create_target_from_url_with_options(
        &processed_target,
        &config.target_options,
    ).await?;

    Ok((source, target))
}

/// Execute the data transfer and handle results
async fn execute_transfer(
    config: &Config,
    source: Box<dyn Source>,
    target: Box<dyn Target>,
) -> Result<(), Box<dyn std::error::Error>> {
    match TransferEngine::execute(config, source, target).await {
        Ok(stats) => {
            if config.preview.is_none() && !config.dry_run {
                info!("Transfer completed successfully!");
                info!(
                    "Processed {} rows in {:.2}s ({:.0} rows/sec)",
                    stats.total_rows,
                    stats.total_time.as_secs_f64(),
                    stats.rows_per_second
                );
            }
            Ok(())
        }
        Err(e) => {
            error!("Transfer failed: {}", e);
            std::process::exit(1);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tinyetl::connectors::{create_source_from_url_with_type, create_target_from_url};

    use std::process::Command;
    use tempfile::NamedTempFile;

    #[test]
    fn test_main_function_exists() {
        // Basic test to ensure main function compiles
        assert!(true);
    }

    #[test]
    fn test_cli_parsing() {
        // Test that Cli can be created from command line args
        let cli = Cli::try_parse_from([
            "tinyetl",
            "test.csv",      // positional source
            "test.db#table", // positional target
          
        ]);
        assert!(cli.is_ok());


        let cli = cli.unwrap();
        assert_eq!(cli.source, Some("test.csv".to_string()));
        assert_eq!(cli.target, Some("test.db#table".to_string()));
    }


    #[test]
    fn test_cli_to_config_conversion() {
        let cli = Cli::try_parse_from([
            "tinyetl",
            "input.csv",   // positional source
            "output.json", // positional target
            "--batch-size",
            "100",
        ])
        .unwrap();

        let config: Config = cli.into();
        assert_eq!(config.source, "input.csv");
        assert_eq!(config.target, "output.json");
        assert_eq!(config.batch_size, 100);
    }


    #[test]
    fn test_cli_with_preview_option() {
        let cli = Cli::try_parse_from([
            "tinyetl",
            "test.csv",  // positional source
            "test.json", // positional target
            "--preview",
            "5",
        ])
        .unwrap();

        let config: Config = cli.into();
        assert_eq!(config.preview, Some(5));
    }


    #[test]
    fn test_cli_with_dry_run() {
        let cli = Cli::try_parse_from([
            "tinyetl",
            "test.csv",  // positional source
            "test.json", // positional target
            "--dry-run",
        ])
        .unwrap();

        let config: Config = cli.into();
        assert!(config.dry_run);
    }


    #[test]
    fn test_cli_with_transform() {
        let cli = Cli::try_parse_from([
            "tinyetl",
            "test.csv",  // positional source
            "test.json", // positional target
            "--transform-file",
            "transform.lua",
        ])
        .unwrap();

        let config: Config = cli.into();
        match config.transform {
            tinyetl::transformer::TransformConfig::File(path) => {
                assert_eq!(path, "transform.lua");
            }
            _ => panic!("Expected File transform config"),
        }
    }


    #[test]
    fn test_cli_missing_required_args() {
        // With new subcommand structure, CLI parsing should succeed
        // but conversion to Config should handle validation
        let result = Cli::try_parse_from(&["tinyetl", "only_target.json"]);
        assert!(result.is_ok());


        // Should succeed parsing with no args (could be subcommand)
        let result = Cli::try_parse_from(&["tinyetl"]);
        assert!(result.is_ok());


        // Test config file subcommand
        let result = Cli::try_parse_from(&["tinyetl", "run", "config.yaml"]);
        assert!(result.is_ok());
        let cli = result.unwrap();
        assert!(cli.is_config_mode());
        assert_eq!(cli.get_config_file(), Some("config.yaml"));
    }


    #[tokio::test]
    async fn test_env_filter_log_levels() {
        // Test different log level configurations
        let config_info = Config {
            source: "test.csv".to_string(),
            target: "test.json".to_string(),
            log_level: tinyetl::config::LogLevel::Info,
            ..Default::default()
        };


        let env_filter = EnvFilter::new(format!(
            "sqlx=warn,tinyetl={}",
            match config_info.log_level {
                tinyetl::config::LogLevel::Info => "info",
                tinyetl::config::LogLevel::Warn => "warn",
                tinyetl::config::LogLevel::Error => "error",
            }
        ));


        // Just verify the filter can be created without error
        assert!(env_filter.to_string().contains("sqlx=warn"));
        assert!(env_filter.to_string().contains("tinyetl=info"));


        let config_warn = Config {
            log_level: tinyetl::config::LogLevel::Warn,
            ..config_info.clone()
        };


        let env_filter_warn = EnvFilter::new(format!(
            "sqlx=warn,tinyetl={}",
            match config_warn.log_level {
                tinyetl::config::LogLevel::Info => "info",
                tinyetl::config::LogLevel::Warn => "warn",
                tinyetl::config::LogLevel::Error => "error",
            }
        ));


        assert!(env_filter_warn.to_string().contains("tinyetl=warn"));
    }


    // Integration test using the actual binary
    #[test]
    fn test_binary_help_command() {
        let output = Command::new("cargo")
            .args(["run", "--", "--help"])
            .current_dir(env!("CARGO_MANIFEST_DIR"))
            .output();


        if let Ok(output) = output {
            let stdout = String::from_utf8_lossy(&output.stdout);
            assert!(stdout.contains("Usage:") || stdout.contains("USAGE:"));
            // Since source and target are positional, check for SOURCE and TARGET in help
            assert!(stdout.contains("SOURCE") || stdout.contains("source"));
            assert!(stdout.contains("TARGET") || stdout.contains("target"));
        }
        // If cargo run fails (e.g., in CI), just pass the test
    }

    #[test]
    fn test_binary_missing_args() {
        let output = Command::new("cargo")
            .args(["run", "--"])
            .current_dir(env!("CARGO_MANIFEST_DIR"))
            .output();


        if let Ok(output) = output {
            assert!(!output.status.success());
            let stderr = String::from_utf8_lossy(&output.stderr);
            // Should contain error about missing required arguments
            assert!(stderr.contains("required") || stderr.contains("argument"));
        }
    }

    #[tokio::test]
    async fn test_source_connector_creation_nonexistent_file() {
        let result = create_source_from_url_with_type("nonexistent.csv", None).await;
        // This might succeed in creation but fail on connection - behavior depends on implementation
        // The test just verifies the connector creation doesn't panic
        let _result = result; // Use the result to avoid unused variable warning
    }


    #[tokio::test]
    async fn test_target_connector_creation() {
        let temp_file = NamedTempFile::new().unwrap();
        let file_path = temp_file.path().to_str().unwrap();


        // Test JSON target creation
        let json_target = format!("{}.json", file_path);
        let result = create_target_from_url(&json_target).await;
        assert!(result.is_ok());

        // Test CSV target creation

        // Test CSV target creation
        let csv_target = format!("{}.csv", file_path);
        let result = create_target_from_url(&csv_target).await;
        assert!(result.is_ok());
    }


    #[tokio::test]
    async fn test_config_default_values() {
        let config = Config::default();


        // Test that default values are sensible
        assert_eq!(config.batch_size, 1_000);
        assert_eq!(config.preview, None);
        assert!(!config.dry_run);
        assert_eq!(config.log_level, tinyetl::config::LogLevel::Info);
        match config.transform {
            tinyetl::transformer::TransformConfig::None => {} // Expected
            _ => panic!("Expected None transform config by default"),
        }
    }
}