|
| 1 | +use clap::Parser; |
| 2 | +use serde_json::Value; |
| 3 | +use sqlite::Connection; |
| 4 | +use std::path::Path; |
| 5 | + |
| 6 | +#[derive(Parser, Debug)] |
| 7 | +#[clap(author, version, about, long_about = None)] |
| 8 | +struct Args { |
| 9 | + /// Sqlite file to migrate |
| 10 | + #[clap(short, long, value_parser)] |
| 11 | + input: String, |
| 12 | + |
| 13 | + /// Sqlite output file |
| 14 | + #[clap(short, long, value_parser)] |
| 15 | + output: String, |
| 16 | +} |
| 17 | + |
| 18 | +fn get_tables(connection: &Connection) -> Vec<String> { |
| 19 | + let cursor = connection |
| 20 | + .prepare("SELECT name FROM sqlite_schema WHERE type = ? AND name NOT LIKE ?") |
| 21 | + .unwrap() |
| 22 | + .bind(1, "table") |
| 23 | + .unwrap() |
| 24 | + .bind(2, "sqlite_%") |
| 25 | + .unwrap() |
| 26 | + .into_cursor(); |
| 27 | + |
| 28 | + cursor.map(|row| row.unwrap().get::<String, _>(0)).collect() |
| 29 | +} |
| 30 | + |
| 31 | +fn process_table(connection: &Connection, conn_out: &Connection, table: String) { |
| 32 | + println!("Processing {} table", table); |
| 33 | + |
| 34 | + conn_out |
| 35 | + .execute(format!("CREATE TABLE '{}' (ID TEXT, json TEXT)", table)) |
| 36 | + .unwrap(); |
| 37 | + |
| 38 | + let mut cursor = connection |
| 39 | + .prepare(format!("SELECT ID, json FROM '{}'", table)) |
| 40 | + .unwrap() |
| 41 | + .into_cursor(); |
| 42 | + |
| 43 | + while let Some(Ok(row)) = cursor.next() { |
| 44 | + let id = row.get::<String, _>("ID"); |
| 45 | + let json = process_row(&id, row.get("json")); |
| 46 | + conn_out |
| 47 | + .execute(format!( |
| 48 | + "INSERT INTO '{}' (ID, json) |
| 49 | + VALUES ('{}', '{}'); ", |
| 50 | + table, &id, json |
| 51 | + )) |
| 52 | + .unwrap(); |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +fn process_row(id: &str, json: String) -> String { |
| 57 | + println!("Processing row with key {}", id); |
| 58 | + |
| 59 | + // Parsing json until it matches |
| 60 | + let tmp_val = serde_json::from_str::<Value>(&json).unwrap(); |
| 61 | + let mut tmp = tmp_val.as_str().unwrap().to_owned(); |
| 62 | + loop { |
| 63 | + let current_val = serde_json::from_str::<Value>(&tmp).unwrap(); |
| 64 | + if current_val.as_str().is_none() { |
| 65 | + break; |
| 66 | + } |
| 67 | + |
| 68 | + tmp = current_val.as_str().unwrap().to_string(); |
| 69 | + } |
| 70 | + |
| 71 | + tmp |
| 72 | +} |
| 73 | + |
| 74 | +fn main() { |
| 75 | + let args = Args::parse(); |
| 76 | + if args.input == args.output { |
| 77 | + panic!("Can't have the same output for input"); |
| 78 | + } |
| 79 | + |
| 80 | + println!("Loading {} sqlite file", args.input); |
| 81 | + |
| 82 | + let connection = sqlite::open(args.input).expect("Couldn't find input sqlite file"); |
| 83 | + println!("Sqlite loaded"); |
| 84 | + println!("Creating output sqlite file"); |
| 85 | + if Path::new(&args.output).exists() { |
| 86 | + panic!("Output file already exist"); |
| 87 | + } |
| 88 | + |
| 89 | + let conn_output = sqlite::open(args.output).expect("Couldn't create output sqlite file"); |
| 90 | + println!("Getting tables"); |
| 91 | + |
| 92 | + let tables = get_tables(&connection); |
| 93 | + println!("Found tables {:?}", tables); |
| 94 | + |
| 95 | + for table in tables { |
| 96 | + process_table(&connection, &conn_output, table); |
| 97 | + } |
| 98 | +} |
0 commit comments