Skip to content

Commit

Permalink
refactor(db): extract ee/tabby-db (#1141)
Browse files Browse the repository at this point in the history
* 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
wsxiaoys authored Dec 29, 2023
1 parent 3ca9049 commit 57d7bd7
Show file tree
Hide file tree
Showing 30 changed files with 132 additions and 109 deletions.
22 changes: 18 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"crates/aim-downloader",
"crates/juniper-axum",
"ee/tabby-webserver",
"ee/tabby-db",
]

[workspace.package]
Expand Down Expand Up @@ -41,6 +42,15 @@ utoipa = "3.3"
axum = "0.6"
hyper = "0.14"
juniper = "0.15"
chrono = "0.4"

[workspace.dependencies.uuid]
version = "1.3.3"
features = [
"v4", # Lets you generate random UUIDs
"fast-rng", # Use a faster (but still sufficiently random) RNG
"macro-diagnostics", # Enable better diagnostics for compile-time UUIDs
]

[profile.dev.package]
insta.opt-level = 3
Expand Down
11 changes: 2 additions & 9 deletions crates/tabby/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,14 @@ futures.workspace = true
async-trait.workspace = true
tabby-webserver = { path = "../../ee/tabby-webserver", optional = true }
thiserror.workspace = true
chrono = "0.4.31"
chrono.workspace = true
axum-prometheus = "0.4.0"
uuid.workspace = true

[dependencies.openssl]
optional = true
version = "*"

[dependencies.uuid]
version = "1.3.3"
features = [
"v4", # Lets you generate random UUIDs
"fast-rng", # Use a faster (but still sufficiently random) RNG
"macro-diagnostics", # Enable better diagnostics for compile-time UUIDs
]

[build-dependencies]
vergen = { version = "8.0.0", features = ["build", "git", "gitcl"] }

Expand Down
26 changes: 26 additions & 0 deletions ee/tabby-db/Cargo.toml
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"] }
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use rusqlite::{params, OptionalExtension, Row};
use uuid::Uuid;

use super::DbConn;
use crate::schema::auth;

pub struct InvitationDAO {
pub id: i32,
Expand All @@ -24,17 +23,6 @@ impl InvitationDAO {
}
}

impl From<InvitationDAO> for auth::InvitationNext {
fn from(val: InvitationDAO) -> Self {
Self {
id: juniper::ID::new(val.id.to_string()),
email: val.email,
code: val.code,
created_at: val.created_at,
}
}
}

/// db read/write operations for `invitations` table
impl DbConn {
pub async fn list_invitations_with_filter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use anyhow::Result;
use chrono::{DateTime, Utc};

use super::DbConn;
use crate::schema::job;

#[derive(Default, Clone)]
pub struct JobRunDAO {
Expand All @@ -29,20 +28,6 @@ impl JobRunDAO {
}
}

impl From<JobRunDAO> for job::JobRun {
fn from(run: JobRunDAO) -> Self {
Self {
id: juniper::ID::new(run.id.to_string()),
job_name: run.job_name,
start_time: run.start_time,
finish_time: run.finish_time,
exit_code: run.exit_code,
stdout: run.stdout,
stderr: run.stderr,
}
}
}

/// db read/write operations for `job_runs` table
impl DbConn {
pub async fn create_job_run(&self, run: JobRunDAO) -> Result<i32> {
Expand Down
38 changes: 11 additions & 27 deletions ee/tabby-webserver/src/service/db/mod.rs → ee/tabby-db/src/lib.rs
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! {
Expand All @@ -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
}

Expand Down Expand Up @@ -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() {
Expand All @@ -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)
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use rusqlite::{params, OptionalExtension, Row};
use uuid::Uuid;

use super::DbConn;
use crate::schema::auth;

#[allow(unused)]
pub struct UserDAO {
created_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,

pub id: i32,
pub email: String,
Expand Down Expand Up @@ -40,18 +39,6 @@ impl UserDAO {
}
}

impl From<UserDAO> for auth::User {
fn from(val: UserDAO) -> Self {
auth::User {
id: juniper::ID::new(val.id.to_string()),
email: val.email,
is_admin: val.is_admin,
auth_token: val.auth_token,
created_at: val.created_at,
}
}
}

/// db read/write operations for `users` table
impl DbConn {
pub async fn create_user(
Expand Down Expand Up @@ -226,7 +213,7 @@ fn generate_auth_token() -> String {
#[cfg(test)]
mod tests {
use super::*;
use crate::service::db::testutils::create_user;
use crate::testutils::create_user;

#[tokio::test]
async fn test_create_user() {
Expand Down
20 changes: 5 additions & 15 deletions ee/tabby-webserver/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,33 @@ argon2 = "0.5.1"
async-trait.workspace = true
axum = { workspace = true, features = ["ws", "headers"] }
bincode = "1.3.3"
chrono = { version = "0.4", features = ["serde"] }
chrono = { workspace = true, features = ["serde"] }
futures.workspace = true
hyper = { workspace = true, features=["client"]}
include_dir = "0.7.3"
jsonwebtoken = "9.1.0"
juniper.workspace = true
juniper-axum = { path = "../../crates/juniper-axum" }
lazy_static = "1.4.0"
lazy_static.workspace = true
mime_guess = "2.0.4"
pin-project = "1.1.3"
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"] }
rust-embed = "8.0.0"
serde.workspace = true
serde_json.workspace = true
tabby-common = { path = "../../crates/tabby-common" }
tabby-db = { path = "../../ee/tabby-db" }
tarpc = { version = "0.33.0", features = ["serde-transport"] }
thiserror.workspace = true
tokio = { workspace = true, features = ["fs"] }
tokio-cron-scheduler = "0.9.4"
tokio-rusqlite = "0.5.0"
tokio-tungstenite = "0.20.1"
tower = { version = "0.4", features = ["util"] }
tower-http = { version = "0.4.0", features = ["fs", "trace"] }
tracing.workspace = true
unicase = "2.7.0"
validator = { version = "0.16.1", features = ["derive"] }

[dependencies.uuid]
version = "1.3.3"
features = [
"v4", # Lets you generate random UUIDs
"fast-rng", # Use a faster (but still sufficiently random) RNG
"macro-diagnostics", # Enable better diagnostics for compile-time UUIDs
]
uuid.workspace = true

[dev-dependencies]
assert_matches = "1.5.0"
tokio = { workspace = true, features = ["macros", "process"] }
tabby-db = { path = "../../ee/tabby-db", features = ["testutils"] }
1 change: 0 additions & 1 deletion ee/tabby-webserver/src/lib.rs
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;
Expand Down
Loading

0 comments on commit 57d7bd7

Please sign in to comment.