-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(db): extract ee/tabby-db (#1141)
* refactor(db): extract tabby-db * add tabby-db * refactor: extract uuid package to workspace * refactor: move is_admin_initialized unit test to auth.rs * remove services/db * fix workspace deps * fix deps
- Loading branch information
Showing
30 changed files
with
132 additions
and
109 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[package] | ||
name = "tabby-db" | ||
version.workspace = true | ||
edition.workspace = true | ||
authors.workspace = true | ||
homepage.workspace = true | ||
|
||
[features] | ||
testutils = [] | ||
|
||
[dependencies] | ||
anyhow.workspace = true | ||
chrono = { workspace = true, features = ["serde"] } | ||
include_dir = "0.7.3" | ||
lazy_static = "1.4.0" | ||
rusqlite = { version = "0.30.0", features = ["bundled", "chrono"] } | ||
# `alpha-async-tokio-rusqlite` is only available from 1.1.0-alpha.2, will bump up version when it's stable | ||
rusqlite_migration = { version = "1.1.0-beta.1", features = ["alpha-async-tokio-rusqlite", "from-directory"] } | ||
tabby-common = { path = "../../crates/tabby-common" } | ||
tokio = { workspace = true, features = ["fs"] } | ||
tokio-rusqlite = "0.5.0" | ||
uuid.workspace = true | ||
|
||
[dev-dependencies] | ||
assert_matches = "1.5.0" | ||
tokio = { workspace = true, features = ["macros", "process"] } |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
pub use invitations::InvitationDAO; | ||
pub use job_runs::JobRunDAO; | ||
pub use users::UserDAO; | ||
|
||
mod invitations; | ||
mod job_runs; | ||
mod path; | ||
mod refresh_tokens; | ||
mod users; | ||
|
||
use anyhow::Result; | ||
use include_dir::{include_dir, Dir}; | ||
pub use job_runs::JobRunDAO; | ||
use lazy_static::lazy_static; | ||
use rusqlite::params; | ||
use rusqlite_migration::AsyncMigrations; | ||
use tokio_rusqlite::Connection; | ||
|
||
use crate::path::db_file; | ||
|
||
static MIGRATIONS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/migrations"); | ||
|
||
lazy_static! { | ||
|
@@ -26,15 +28,15 @@ pub struct DbConn { | |
} | ||
|
||
impl DbConn { | ||
#[cfg(test)] | ||
#[cfg(any(test, feature = "testutils"))] | ||
pub async fn new_in_memory() -> Result<Self> { | ||
let conn = Connection::open_in_memory().await?; | ||
DbConn::init_db(conn).await | ||
} | ||
|
||
pub async fn new() -> Result<Self> { | ||
tokio::fs::create_dir_all(db_file().parent().unwrap()).await?; | ||
let conn = Connection::open(db_file()).await?; | ||
tokio::fs::create_dir_all(path::db_file().parent().unwrap()).await?; | ||
let conn = Connection::open(path::db_file()).await?; | ||
Self::init_db(conn).await | ||
} | ||
|
||
|
@@ -136,15 +138,6 @@ impl DbConn { | |
mod tests { | ||
|
||
use super::*; | ||
use crate::schema::auth::AuthenticationService; | ||
|
||
async fn create_user(conn: &DbConn) -> i32 { | ||
let email: &str = "[email protected]"; | ||
let password: &str = "123456789"; | ||
conn.create_user(email.to_string(), password.to_string(), true) | ||
.await | ||
.unwrap() | ||
} | ||
|
||
#[tokio::test] | ||
async fn migrations_test() { | ||
|
@@ -168,22 +161,13 @@ mod tests { | |
assert_eq!(new_token.len(), 36); | ||
assert_ne!(old_token, new_token); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_is_admin_initialized() { | ||
let conn = DbConn::new_in_memory().await.unwrap(); | ||
|
||
assert!(!conn.is_admin_initialized().await.unwrap()); | ||
create_user(&conn).await; | ||
assert!(conn.is_admin_initialized().await.unwrap()); | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod testutils { | ||
#[cfg(any(test, feature = "testutils"))] | ||
pub mod testutils { | ||
use super::*; | ||
|
||
pub(crate) async fn create_user(conn: &DbConn) -> i32 { | ||
pub async fn create_user(conn: &DbConn) -> i32 { | ||
let email: &str = "[email protected]"; | ||
let password: &str = "123456789"; | ||
conn.create_user(email.to_string(), password.to_string(), true) | ||
|
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
mod handler; | ||
mod hub; | ||
mod path; | ||
mod repositories; | ||
mod schema; | ||
mod service; | ||
|
Oops, something went wrong.