diff --git a/src/apis/ap.rs b/src/apis/ap.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/apis/key.rs b/src/apis/key.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/apis/masto.rs b/src/apis/masto.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/apis/mod.rs b/src/apis/mod.rs index e69de29..141699d 100644 --- a/src/apis/mod.rs +++ b/src/apis/mod.rs @@ -0,0 +1,4 @@ +pub mod ap; +pub mod key; +pub mod oma; +pub mod masto; \ No newline at end of file diff --git a/src/apis/oma.rs b/src/apis/oma.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 43490ab..a0a5868 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -8,25 +8,57 @@ // See the Licence for the specific language governing permissions and limitations under the Licence. // -mod diesel_db; +pub mod diesel_db; use anyhow::{Error, bail}; -use diesel::{prelude::*, pg::{PgConnection, Pg}}; +use diesel::{prelude::*, pg::{PgConnection, Pg}, sqlite::Sqlite}; use diesel_migrations::{EmbeddedMigrations, embed_migrations, MigrationHarness}; - pub const POSTGRES_MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations/postgres"); -pub fn establish_connection(db_url: &str) -> Result { +pub enum DB { + Postgres(PgConnection), + Sqlite(SqliteConnection) +} + +/// Try and connect to a database at the given url +/// The url can be a path to an sqlite db file or an url to a postgres db +pub fn establish_connection(db_url: &str) -> Result { + if db_url.starts_with("postgres") { + Ok(DB::Postgres(establish_connection_postgres(db_url)?)) + } else { + Ok(DB::Sqlite(establish_connection_sqlite(db_url)?)) + } +} + +/// Try and connect to a postgres db +pub fn establish_connection_postgres(db_url: &str) -> Result { //SqliteConnection::establish(db_url).unwrap_or_else(|e| panic!("Error connecting to {}: {}", db_url, e)) let mut conn = PgConnection::establish(db_url) .unwrap_or_else(|e| panic!("Error connecting to {}: {}", db_url, e)); - run_migrations(&mut conn)?; + run_migrations_postgres(&mut conn)?; //c.run_pending_migrations(MIGRATIONS)?; Ok(conn) } -fn run_migrations(conn: &mut impl MigrationHarness) -> Result<(), Error> { +fn run_migrations_postgres(conn: &mut impl MigrationHarness) -> Result<(), Error> { + match conn.run_pending_migrations(POSTGRES_MIGRATIONS) { + Ok(_) => Ok(()), + Err(e) => bail!("Migrations failed. Error {}", e) + } +} + +/// Try and connect to a sqlite db +pub fn establish_connection_sqlite(db_url: &str) -> Result { + //SqliteConnection::establish(db_url).unwrap_or_else(|e| panic!("Error connecting to {}: {}", db_url, e)) + let mut conn = SqliteConnection::establish(db_url) + .unwrap_or_else(|e| panic!("Error connecting to {}: {}", db_url, e)); + run_migrations_sqlite(&mut conn)?; + //c.run_pending_migrations(MIGRATIONS)?; + Ok(conn) +} + +fn run_migrations_sqlite(conn: &mut impl MigrationHarness) -> Result<(), Error> { match conn.run_pending_migrations(POSTGRES_MIGRATIONS) { Ok(_) => Ok(()), Err(e) => bail!("Migrations failed. Error {}", e)