Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 63 additions & 1 deletion Cargo.lock

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

1 change: 1 addition & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ tempfile = "3"
testutils = { path = "../testutils" }
cc-common = { path = "../common" }
tonic = "0.12.3"
rusqlite = { version = "0.31", features = ["bundled"] }
83 changes: 78 additions & 5 deletions cli/tests/test_sqlite_store.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use std::fs;

#[tokio::test]
#[should_panic]
async fn test_sqlite_store_init_and_snapshot_succeeds() {
let db_dir = tempfile::tempdir().expect("Failed to create temp dir for sqlite db");
let db_path = db_dir.path().join("test_store.db");

let workspace = testutils::TestWorkspace::init_sqlite(&db_path).await;
let workspace = testutils::TestWorkspace::init_sqlite().await;
let repo_path = workspace.repo_path();

// Create a file in the working copy and snapshot it
Expand All @@ -26,3 +22,80 @@ async fn test_sqlite_store_init_and_snapshot_succeeds() {
.success()
.stdout("sqlite store test commit\n");
}

#[test]
fn test_server_fails_when_database_parent_path_is_not_a_directory() {
let temp_dir = tempfile::tempdir().unwrap();
let blocker_file = temp_dir.path().join("blocking_file");
fs::write(&blocker_file, "blocking content").unwrap();

let invalid_db_path = blocker_file.join("store.db");
let expected_io_err = fs::create_dir_all(&blocker_file).unwrap_err();
let expected_stderr = format!(
"Error: Failed to create parent directory '{}' for SQLite database: {}\n",
blocker_file.display(),
expected_io_err
);

let mut cmd = assert_cmd::Command::cargo_bin("jj-cc-server").unwrap();
cmd.args([
"--port=0",
"--store-type=sqlite",
&format!("--sqlite-path={}", invalid_db_path.display()),
]);

cmd.assert()
.failure()
.stderr(predicates::ord::eq(expected_stderr.as_str()));
}

#[test]
fn test_server_fails_when_sqlite_database_file_permission_denied() {
use std::os::unix::fs::PermissionsExt;

let temp_dir = tempfile::tempdir().unwrap();
let readonly_db = temp_dir.path().join("readonly.db");
fs::write(&readonly_db, b"").unwrap();
fs::set_permissions(&readonly_db, fs::Permissions::from_mode(0o000)).unwrap();

let expected_rusqlite_err = rusqlite::Connection::open(&readonly_db).unwrap_err();
let expected_stderr = format!(
"Error: Failed to open SQLite database at '{}': {}\n",
readonly_db.display(),
expected_rusqlite_err
);

let mut cmd = assert_cmd::Command::cargo_bin("jj-cc-server").unwrap();
cmd.args([
"--port=0",
"--store-type=sqlite",
&format!("--sqlite-path={}", readonly_db.display()),
]);

cmd.assert()
.failure()
.stderr(predicates::ord::eq(expected_stderr.as_str()));
}

#[test]
fn test_server_fails_when_home_directory_is_unwritable_for_default_sqlite_path() {
let temp_dir = tempfile::tempdir().unwrap();
let blocker_file = temp_dir.path().join("fake_home_file");
fs::write(&blocker_file, "blocking").unwrap();

let expected_default_dir = blocker_file.join(".jj-cc-server");
let expected_io_err = fs::create_dir_all(&expected_default_dir).unwrap_err();
let expected_stderr = format!(
"Error: Failed to create default SQLite directory '{}': {}\n",
expected_default_dir.display(),
expected_io_err
);

let mut cmd = assert_cmd::Command::cargo_bin("jj-cc-server").unwrap();
cmd.env("HOME", blocker_file.to_str().unwrap());
cmd.args(["--port=0", "--store-type=sqlite"]);

cmd.assert()
.failure()
.stderr(predicates::ord::eq(expected_stderr.as_str()));
}
3 changes: 3 additions & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ async-trait = "0.1"
gix = "0.68.0"
hex = "0.4"
uuid = { version = "1", features = ["v4"] }
prost = "0.13"
rusqlite = { version = "0.31", features = ["bundled"] }

[dev-dependencies]
assert_cmd = "2.0"
predicates = "3.1"
tempfile = "3"
testutils = { path = "../testutils" }
78 changes: 78 additions & 0 deletions server/db/schema_sqlite.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
-- SQLite Schema for Commit Cloud Server

-- Repository registration table for tracking active Commit Cloud repos
-- Used when running `jj cc init` to register a new remote repository
CREATE TABLE IF NOT EXISTS repos (
repo_id TEXT PRIMARY KEY,
name TEXT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Commit objects store for serialized commit metadata and history nodes
-- Used when reading and writing commits during change history operations
CREATE TABLE IF NOT EXISTS commits (
repo_id TEXT NOT NULL,
commit_id BLOB NOT NULL,
data BLOB NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (repo_id, commit_id)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is good.... ideaaaaallly the commit ID should actually be universally unique. So theoretically I think you could compact a little and not store the same commit twice. But doing it this way is probably fine? Same goes for most of these.

Anyway I don't think you have to change it, just interesting bit of trivia.

);

-- Operation objects store for Jujutsu's operation log graph entries
-- Used when recording repository state transitions and running operation log queries
CREATE TABLE IF NOT EXISTS operations (
repo_id TEXT NOT NULL,
op_id BLOB NOT NULL,
data BLOB NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (repo_id, op_id)
);

-- Operation heads tracking table for resolving the latest operation heads
-- Used during op log updates to advance the repository operation head pointers
CREATE TABLE IF NOT EXISTS op_heads (
repo_id TEXT NOT NULL,
op_id BLOB NOT NULL,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (repo_id, op_id)
);

-- Directory tree objects store for serialized directory trees and entry lists
-- Used during snapshotting and tree walking to resolve directory hierarchies
CREATE TABLE IF NOT EXISTS trees (
repo_id TEXT NOT NULL,
tree_id BLOB NOT NULL,
data BLOB NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (repo_id, tree_id)
);

-- File content / blob store for binary and text file contents
-- Used when reading and writing file contents for working copy snapshots and VFS reads
CREATE TABLE IF NOT EXISTS files (
repo_id TEXT NOT NULL,
file_id BLOB NOT NULL,
data BLOB NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (repo_id, file_id)
);

-- Symlink target metadata store for symbolic links in the repository tree
-- Used when reading and writing symlink entries in project directory trees
CREATE TABLE IF NOT EXISTS symlinks (
repo_id TEXT NOT NULL,
symlink_id BLOB NOT NULL,
target TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (repo_id, symlink_id)
);

-- View objects store for repository views (bookmarks, working copy commit IDs, remote refs)
-- Used when saving and loading repository state views associated with operations
CREATE TABLE IF NOT EXISTS views (
repo_id TEXT NOT NULL,
view_id BLOB NOT NULL,
data BLOB NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (repo_id, view_id)
);
7 changes: 4 additions & 3 deletions server/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ impl CommitCloudBackendService {
impl BackendService for CommitCloudBackendService {
async fn register_repository(
&self,
_request: tonic::Request<RegisterRepositoryRequest>,
request: tonic::Request<RegisterRepositoryRequest>,
) -> Result<tonic::Response<RegisterRepositoryResponse>, tonic::Status> {
let req = request.into_inner();
let repo_id = uuid::Uuid::new_v4().to_string();
info!("Registering repository: {}", repo_id);
self.store.register_repo(repo_id.clone()).await;
info!("Registering repository: {} (name: {:?})", repo_id, req.name);
self.store.register_repo(repo_id.clone(), req.name).await;
Ok(tonic::Response::new(RegisterRepositoryResponse { repo_id }))
}

Expand Down
Loading