Skip to content
Merged
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- **Database / Compatibility**: Support upstream database schema 27 for local startup and WebDAV/S3 restore, including snapshots from CC Switch desktop v4.0.34 through v4.0.40, fixing `Remote database schema is too new (27); this app supports up to 26`. Preserve the upstream skill repository host columns through migrations, CLI edits and sync round-trips. Addresses [#576](https://github.com/SaladDay/cc-switch-cli/issues/576).
- **Database / Compatibility**: Support upstream database schema 27 for local startup and WebDAV/S3 restore, including snapshots from CC Switch desktop v4.0.34 through v4.0.40, fixing `Remote database schema is too new (27); this app supports up to 26`. Preserve the upstream skill repository host columns through migrations, CLI edits and sync round-trips. As upstream does, adding a GitHub repository with the same owner/name as a synced non-GitHub repository replaces it, a GitHub install no longer reuses a non-GitHub skill with the same name, and synced non-GitHub repositories stop the CLI from re-adding the default repositories. Addresses [#576](https://github.com/SaladDay/cc-switch-cli/issues/576).

### Upgrade notes

Expand Down
32 changes: 28 additions & 4 deletions src-tauri/src/database/dao/skills.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,8 @@ impl Database {

/// 保存 Skill 仓库
///
/// 只写 CLI 管理的四列;上游 v27 新增的 `host`(GitLab 等非 GitHub 主机)不在此处
/// 维护,冲突时必须保留库里的值,否则 `load_index → save_index` 一次往返就会把
/// 桌面端同步过来的 GitLab 仓库改回 github.com。
/// 只写 CLI 管理的四列,冲突时保留库里的 `host`(上游 v27)。用于回写已加载的
/// 状态,不会改动已有行的主机;用户显式添加仓库请用 [`Self::add_github_skill_repo`]。
pub fn save_skill_repo(&self, repo: &SkillRepo) -> Result<(), AppError> {
let conn = lock_conn!(self.conn);
conn.execute(
Expand All @@ -266,6 +265,23 @@ impl Database {
Ok(())
}

/// 用户显式添加的 GitHub 仓库:与上游一致,同 owner/name 的已有行(包括桌面端
/// 同步来的非 GitHub 仓库)会被改为 github.com,而不是静默保留成 CLI 看不到的行。
pub fn add_github_skill_repo(&self, repo: &SkillRepo) -> Result<(), AppError> {
let conn = lock_conn!(self.conn);
conn.execute(
"INSERT INTO skill_repos (owner, name, branch, enabled, host)
VALUES (?1, ?2, ?3, ?4, 'github.com')
ON CONFLICT(owner, name) DO UPDATE SET
branch = excluded.branch,
enabled = excluded.enabled,
host = excluded.host",
params![repo.owner, repo.name, repo.branch, repo.enabled],
)
.map_err(|e| AppError::Database(e.to_string()))?;
Ok(())
}

/// 删除 Skill 仓库
pub fn delete_skill_repo(&self, owner: &str, name: &str) -> Result<(), AppError> {
let conn = lock_conn!(self.conn);
Expand Down Expand Up @@ -303,7 +319,15 @@ impl Database {
}

// 兼容升级前已经存在的用户选择,并记录初始化状态,避免以后删空后恢复默认值。
if !self.get_skill_repos()?.is_empty() {
// 与上游一致按全部行判断:只有桌面端同步来的非 GitHub 仓库时也不再补默认仓库。
let has_repos: bool = {
let conn = lock_conn!(self.conn);
conn.query_row("SELECT EXISTS(SELECT 1 FROM skill_repos)", [], |row| {
row.get(0)
})
.map_err(|e| AppError::Database(e.to_string()))?
};
if has_repos {
self.set_setting(INITIALIZED_KEY, "true")?;
return Ok(0);
}
Expand Down
47 changes: 47 additions & 0 deletions src-tauri/src/database/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2735,6 +2735,53 @@ fn skill_repo_save_preserves_upstream_host() {
assert_eq!(host, "github.com");
}

#[test]
fn adding_a_github_repo_takes_over_a_hidden_non_github_row() {
let db = Database::memory().expect("create memory db");
{
let conn = db.conn.lock().expect("lock conn");
conn.execute(
"INSERT INTO skill_repos (owner, name, branch, enabled, host)
VALUES ('acme', 'skills', 'develop', 0, 'gitlab.com')",
[],
)
.expect("seed GitLab repo");
}
assert!(db.get_skill_repos().expect("load repos").is_empty());

let repo = crate::services::skill::SkillRepo {
owner: "acme".to_string(),
name: "skills".to_string(),
branch: "main".to_string(),
enabled: true,
};
db.add_github_skill_repo(&repo).expect("add repo");

let repos = db.get_skill_repos().expect("load repos after add");
assert_eq!(repos.len(), 1, "the added repo must be visible to the CLI");
assert_eq!((repos[0].branch.as_str(), repos[0].enabled), ("main", true));
}

#[test]
fn default_skill_repos_are_not_seeded_over_non_github_repos() {
let db = Database::memory().expect("create memory db");
{
let conn = db.conn.lock().expect("lock conn");
conn.execute(
"INSERT INTO skill_repos (owner, name, branch, enabled, host)
VALUES ('acme', 'skills', 'main', 1, 'gitlab.com')",
[],
)
.expect("seed GitLab repo");
}

assert_eq!(db.init_default_skill_repos().expect("initialize repos"), 0);
assert!(db.get_skill_repos().expect("get repos").is_empty());
assert!(db
.get_bool_flag("default_skill_repos_initialized")
.expect("get initialized flag"));
}

#[test]
fn schema_create_tables_repairs_legacy_proxy_config_singleton_to_per_app() {
let conn = Connection::open_in_memory().expect("open memory db");
Expand Down
2 changes: 1 addition & 1 deletion src-tauri/src/deeplink/skill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub fn import_skill_from_deeplink(
};

// Save using Database
state.db.save_skill_repo(&repo)?;
state.db.add_github_skill_repo(&repo)?;

log::info!("Successfully added skill repo '{owner}/{name}'");

Expand Down
75 changes: 73 additions & 2 deletions src-tauri/src/services/skill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2902,7 +2902,7 @@ impl SkillService {

pub fn upsert_repo(repo: SkillRepo) -> Result<(), AppError> {
let _state_guard = skill_state_write_guard();
Database::init()?.save_skill_repo(&repo)
Database::init()?.add_github_skill_repo(&repo)
}

pub fn remove_repo(owner: &str, name: &str) -> Result<(), AppError> {
Expand Down Expand Up @@ -2963,14 +2963,17 @@ impl SkillService {
app: &AppType,
) -> Result<Option<InstalledSkill>, AppError> {
let db = Database::init()?;
let non_github = db.non_github_skill_ids()?;
for existing in db.get_all_installed_skills()?.values() {
if !existing.directory.eq_ignore_ascii_case(install_name) {
continue;
}

// CLI 只从 GitHub 安装;同 owner/name 但来自其他主机的不是同一个仓库。
let same_repo = existing.repo_owner.as_deref()
== Some(discoverable.repo_owner.as_str())
&& existing.repo_name.as_deref() == Some(discoverable.repo_name.as_str());
&& existing.repo_name.as_deref() == Some(discoverable.repo_name.as_str())
&& !non_github.contains(&existing.id);
if !same_repo {
let existing_repo = format!(
"{}/{}",
Expand Down Expand Up @@ -4700,6 +4703,74 @@ mod tests {
);
}

fn seed_gitlab_skill(db: &Database) -> InstalledSkill {
let mut skill = poisoned_skill("acme/skills:demo", "demo");
skill.name = "Demo".to_string();
skill.repo_owner = Some("acme".to_string());
skill.repo_name = Some("skills".to_string());
skill.repo_branch = Some("main".to_string());
db.save_skill(&skill).expect("save GitLab Skill");
db.conn
.lock()
.expect("lock conn")
.execute(
"UPDATE skills SET repo_host = 'gitlab.com' WHERE id = ?1",
[&skill.id],
)
.expect("mark Skill as GitLab");
skill
}

#[tokio::test(flavor = "current_thread")]
async fn update_check_and_update_skip_non_github_skills() {
let home = tempfile::tempdir().expect("create isolated home");
let _env = crate::test_support::TestEnvGuard::isolated(home.path());
let db = Database::init().expect("initialize isolated database");
let skill = seed_gitlab_skill(&db);
let service = SkillService::new().expect("create Skill service");

let check = service.check_updates().await.expect("check updates");
assert!(check.updates.is_empty() && check.failures.is_empty());

let result = service.update_skills(std::slice::from_ref(&skill.id)).await;
assert!(result.updated.is_empty());
assert_eq!(result.failures.len(), 1);
assert!(
result.failures[0].error.contains("non-GitHub"),
"unexpected error: {}",
result.failures[0].error
);
}

#[tokio::test(flavor = "current_thread")]
async fn install_does_not_reuse_a_non_github_skill_with_the_same_repo() {
let home = tempfile::tempdir().expect("create isolated home");
let _env = crate::test_support::TestEnvGuard::isolated(home.path());
let db = Database::init().expect("initialize isolated database");
for mut repo in SkillStore::default().repos {
repo.enabled = false;
db.save_skill_repo(&repo)
.expect("disable default repository");
}
let skill = seed_gitlab_skill(&db);

let error = SkillService::new()
.expect("create Skill service")
.install("acme/skills:demo", &AppType::Codex)
.await
.expect_err("a GitLab Skill must not be reused for a GitHub install");
assert!(
error.to_string().contains("SKILL_DIRECTORY_CONFLICT"),
"unexpected error: {error}"
);

let stored = db
.get_installed_skill(&skill.id)
.expect("load Skill")
.expect("find Skill");
assert!(!stored.apps.codex, "the GitLab Skill must stay untouched");
}

#[test]
fn storage_migration_rejects_a_poisoned_directory_before_moving_it() {
let home = tempfile::tempdir().expect("create isolated home");
Expand Down
Loading