refactor: replace scootaloo_config with &str in init_db()

This commit is contained in:
VC
2022-04-24 10:02:45 +02:00
parent 13bb6d6f37
commit 26491f146f
3 changed files with 58 additions and 51 deletions

View File

@@ -34,6 +34,8 @@ Then run the command with the `init` subcommand to initiate the DB:
scootaloo init scootaloo init
``` ```
This subcommand is completely idempotent.
Then run the command with the `register` subcommand: Then run the command with the `register` subcommand:
```sh ```sh
scootaloo register --host https://m.nintendojo.fr scootaloo register --host https://m.nintendojo.fr
@@ -76,9 +78,13 @@ SUBCOMMANDS:
Scootaloo does not respect the spam limits imposed by Mastodon: it will make a 429 error if too much Tweets are converted to Toots in a short amount of time (and it will not recover from it). By default, it gets the last 200 tweets from the user timeline (which is a lot!). It is recommended to put a Tweet number into the DB file before copying an old account. Scootaloo does not respect the spam limits imposed by Mastodon: it will make a 429 error if too much Tweets are converted to Toots in a short amount of time (and it will not recover from it). By default, it gets the last 200 tweets from the user timeline (which is a lot!). It is recommended to put a Tweet number into the DB file before copying an old account.
You can can insert it like this: You can insert that Tweet number, by connecting to the DB you created:
```sh ```sh
sqlite3 /var/lib/scootaloo/scootaloo.sqlite sqlite3 /var/lib/scootaloo/scootaloo.sqlite
INSERT INTO tweet_to_toot VALUES (1383782580412030982, ""); ```
.quit
And inserting the data:
```sql
INSERT INTO tweet_to_toot VALUES (1383782580412030982, "");
``` ```

View File

@@ -54,7 +54,7 @@ fn main() {
}, },
("init", Some(sub_m)) => { ("init", Some(sub_m)) => {
let config = parse_toml(sub_m.value_of("config").unwrap_or(DEFAULT_CONFIG_PATH)); let config = parse_toml(sub_m.value_of("config").unwrap_or(DEFAULT_CONFIG_PATH));
init_db(&config.scootaloo).unwrap(); init_db(&config.scootaloo.db_path).unwrap();
return; return;
}, },
_ => (), _ => (),

View File

@@ -1,5 +1,3 @@
use crate::config::ScootalooConfig;
use std::error::Error; use std::error::Error;
use log::debug; use log::debug;
use rusqlite::{Connection, params, OptionalExtension}; use rusqlite::{Connection, params, OptionalExtension};
@@ -45,9 +43,9 @@ pub fn write_state(conn: &Connection, t: TweetToToot) -> Result<(), Box<dyn Erro
} }
/// Initiates the DB from path /// Initiates the DB from path
pub fn init_db(config: &ScootalooConfig) -> Result<(), Box<dyn Error>> { pub fn init_db(d: &str) -> Result<(), Box<dyn Error>> {
debug!("Initializing DB for Scootaloo"); debug!("Initializing DB for Scootaloo");
let conn = Connection::open(&config.db_path)?; let conn = Connection::open(d)?;
conn.execute( conn.execute(
"CREATE TABLE IF NOT EXISTS tweet_to_toot ( "CREATE TABLE IF NOT EXISTS tweet_to_toot (
@@ -70,36 +68,51 @@ mod tests {
#[test] #[test]
fn test_init_db() { fn test_init_db() {
let scootaloo_config = ScootalooConfig { let d = "/tmp/test_init_db.sqlite";
db_path: String::from("/tmp/test_init_db.sqlite"),
cache_path: String::from("/tmp/scootaloo"),
};
init_db(&scootaloo_config).unwrap(); init_db(d).unwrap();
// check that file exist // check that file exist
assert!(Path::new(&scootaloo_config.db_path).exists()); assert!(Path::new(d).exists());
// open said file // open said file
let conn = Connection::open(&scootaloo_config.db_path).unwrap(); let conn = Connection::open(d).unwrap();
conn.execute( conn.execute(
"SELECT * from tweet_to_toot;", "SELECT * from tweet_to_toot;",
[], [],
).unwrap(); ).unwrap();
remove_file(scootaloo_config.db_path).unwrap(); remove_file(d).unwrap();
}
#[test]
fn test_init_init_db() {
// init_db fn should be idempotent so lets test that
let d = "/tmp/test_init_init_db.sqlite";
init_db(d).unwrap();
let conn = Connection::open(d).unwrap();
conn.execute(
"INSERT INTO tweet_to_toot
VALUES
(100, 'A');",
[],
).unwrap();
init_db(d).unwrap();
remove_file(d).unwrap();
} }
#[test] #[test]
fn test_write_state() { fn test_write_state() {
let scootaloo_config = ScootalooConfig { let d = "/tmp/test_write_state.sqlite";
db_path: String::from("/tmp/test_write_state.sqlite"),
cache_path: String::from("/tmp/scootaloo"),
};
init_db(&scootaloo_config).unwrap(); init_db(d).unwrap();
let conn = Connection::open(&scootaloo_config.db_path).unwrap(); let conn = Connection::open(d).unwrap();
let t_in = TweetToToot { let t_in = TweetToToot {
tweet_id: 123456789, tweet_id: 123456789,
@@ -120,19 +133,16 @@ mod tests {
assert_eq!(t_out.tweet_id, 123456789); assert_eq!(t_out.tweet_id, 123456789);
assert_eq!(t_out.toot_id, String::from("987654321")); assert_eq!(t_out.toot_id, String::from("987654321"));
remove_file(&scootaloo_config.db_path).unwrap(); remove_file(d).unwrap();
} }
#[test] #[test]
fn test_none_to_tweet_id_read_state() { fn test_none_to_tweet_id_read_state() {
let scootaloo_config = ScootalooConfig { let d = "/tmp/test_none_to_tweet_id_read_state.sqlite";
db_path: String::from("/tmp/test_none_to_tweet_id_read_state.sqlite"),
cache_path: String::from("/tmp/scootaloo"),
};
init_db(&scootaloo_config).unwrap(); init_db(d).unwrap();
let conn = Connection::open(&scootaloo_config.db_path).unwrap(); let conn = Connection::open(d).unwrap();
conn.execute( conn.execute(
"INSERT INTO tweet_to_toot (tweet_id, toot_id) "INSERT INTO tweet_to_toot (tweet_id, toot_id)
@@ -144,7 +154,7 @@ mod tests {
let t_out = read_state(&conn, None).unwrap().unwrap(); let t_out = read_state(&conn, None).unwrap().unwrap();
remove_file(&scootaloo_config.db_path).unwrap(); remove_file(d).unwrap();
assert_eq!(t_out.tweet_id, 102); assert_eq!(t_out.tweet_id, 102);
assert_eq!(t_out.toot_id, "B"); assert_eq!(t_out.toot_id, "B");
@@ -152,32 +162,26 @@ mod tests {
#[test] #[test]
fn test_none_to_none_read_state() { fn test_none_to_none_read_state() {
let scootaloo_config = ScootalooConfig { let d = "/tmp/test_none_to_none_read_state.sqlite";
db_path: String::from("/tmp/test_none_to_none_read_state.sqlite"),
cache_path: String::from("/tmp/scootaloo"),
};
init_db(&scootaloo_config).unwrap(); init_db(d).unwrap();
let conn = Connection::open(&scootaloo_config.db_path).unwrap(); let conn = Connection::open(d).unwrap();
let t_out = read_state(&conn, None).unwrap(); let t_out = read_state(&conn, None).unwrap();
remove_file(&scootaloo_config.db_path).unwrap(); remove_file(d).unwrap();
assert!(t_out.is_none()); assert!(t_out.is_none());
} }
#[test] #[test]
fn test_tweet_id_to_none_read_state() { fn test_tweet_id_to_none_read_state() {
let scootaloo_config = ScootalooConfig { let d = "/tmp/test_tweet_id_to_none_read_state.sqlite";
db_path: String::from("/tmp/test_tweet_id_to_none_read_state.sqlite"),
cache_path: String::from("/tmp/scootaloo"),
};
init_db(&scootaloo_config).unwrap(); init_db(d).unwrap();
let conn = Connection::open(&scootaloo_config.db_path).unwrap(); let conn = Connection::open(d).unwrap();
conn.execute( conn.execute(
"INSERT INTO tweet_to_toot (tweet_id, toot_id) "INSERT INTO tweet_to_toot (tweet_id, toot_id)
@@ -188,21 +192,18 @@ mod tests {
let t_out = read_state(&conn, Some(101)).unwrap(); let t_out = read_state(&conn, Some(101)).unwrap();
remove_file(&scootaloo_config.db_path).unwrap(); remove_file(d).unwrap();
assert!(t_out.is_none()); assert!(t_out.is_none());
} }
#[test] #[test]
fn test_tweet_id_to_tweet_id_read_state() { fn test_tweet_id_to_tweet_id_read_state() {
let scootaloo_config = ScootalooConfig { let d = "/tmp/test_tweet_id_to_tweet_id_read_state.sqlite";
db_path: String::from("/tmp/test_tweet_id_to_tweet_id_read_state.sqlite"),
cache_path: String::from("/tmp/scootaloo"),
};
init_db(&scootaloo_config).unwrap(); init_db(d).unwrap();
let conn = Connection::open(&scootaloo_config.db_path).unwrap(); let conn = Connection::open(d).unwrap();
conn.execute( conn.execute(
"INSERT INTO tweet_to_toot (tweet_id, toot_id) "INSERT INTO tweet_to_toot (tweet_id, toot_id)
@@ -213,7 +214,7 @@ mod tests {
let t_out = read_state(&conn, Some(100)).unwrap().unwrap(); let t_out = read_state(&conn, Some(100)).unwrap().unwrap();
remove_file(&scootaloo_config.db_path).unwrap(); remove_file(d).unwrap();
assert_eq!(t_out.tweet_id, 100); assert_eq!(t_out.tweet_id, 100);
assert_eq!(t_out.toot_id, "A"); assert_eq!(t_out.toot_id, "A");