Skip to content

Commit

Permalink
fix(db): change synced_at column to synced using a bool (TabbyML#2169)
Browse files Browse the repository at this point in the history
* fix(db): change synced_at column to synced using a bool

* Update method name

* [autofix.ci] apply automated fixes

* Fix tests

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
boxbeam and autofix-ci[bot] authored May 20, 2024
1 parent 107cdca commit c7ebed9
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion ee/tabby-db/migrations/0029_merged-provider-tables.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CREATE TABLE integrations(
error TEXT,
created_at TIMESTAMP NOT NULL DEFAULT (DATETIME('now')),
updated_at TIMESTAMP NOT NULL DEFAULT (DATETIME('now')),
synced_at TIMESTAMP NOT NULL DEFAULT (DATETIME('now'))
synced BOOLEAN NOT NULL DEFAULT FALSE
);

CREATE TABLE provided_repositories(
Expand Down
Binary file modified ee/tabby-db/schema.sqlite
Binary file not shown.
2 changes: 1 addition & 1 deletion ee/tabby-db/schema/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ CREATE TABLE integrations(
error TEXT,
created_at TIMESTAMP NOT NULL DEFAULT(DATETIME('now')),
updated_at TIMESTAMP NOT NULL DEFAULT(DATETIME('now')),
synced_at TIMESTAMP NOT NULL DEFAULT(DATETIME('now'))
synced BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE TABLE provided_repositories(
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
Expand Down
2 changes: 1 addition & 1 deletion ee/tabby-db/schema/schema.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 5 additions & 5 deletions ee/tabby-db/src/integrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct IntegrationDAO {
pub access_token: String,
pub created_at: DateTimeUtc,
pub updated_at: DateTimeUtc,
pub synced_at: DateTimeUtc,
pub synced: bool,
}

impl DbConn {
Expand Down Expand Up @@ -45,7 +45,7 @@ impl DbConn {
access_token,
updated_at,
created_at,
synced_at
synced
FROM integrations WHERE id = ?;"#,
id
)
Expand Down Expand Up @@ -81,7 +81,7 @@ impl DbConn {
};

let res = query!(
"UPDATE integrations SET display_name = ?, access_token = ?, updated_at = DATETIME('now') WHERE id = ? AND kind = ?;",
"UPDATE integrations SET display_name = ?, access_token = ?, updated_at = DATETIME('now'), synced = false WHERE id = ? AND kind = ?;",
display_name,
access_token,
id,
Expand All @@ -101,7 +101,7 @@ impl DbConn {

pub async fn update_integration_error(&self, id: i64, error: Option<String>) -> Result<()> {
query!(
"UPDATE integrations SET synced_at = DATETIME('now'), error = ? WHERE id = ?",
"UPDATE integrations SET synced = true, error = ? WHERE id = ?",
error,
id
)
Expand Down Expand Up @@ -146,7 +146,7 @@ impl DbConn {
"access_token",
"created_at",
"updated_at",
"synced_at"
"synced"
],
limit,
skip_id,
Expand Down
7 changes: 3 additions & 4 deletions ee/tabby-schema/src/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,19 @@ impl From<ServerSettingDAO> for NetworkSetting {
impl TryFrom<IntegrationDAO> for Integration {
type Error = anyhow::Error;
fn try_from(value: IntegrationDAO) -> anyhow::Result<Self> {
let status = if *value.synced_at <= *value.updated_at {
IntegrationStatus::Pending
let status = if value.synced && value.error.is_none() {
IntegrationStatus::Ready
} else if value.error.is_some() {
IntegrationStatus::Failed
} else {
IntegrationStatus::Ready
IntegrationStatus::Pending
};
Ok(Self {
id: value.id.as_id(),
kind: IntegrationKind::from_enum_str(&value.kind)?,
display_name: value.display_name,
access_token: value.access_token,
created_at: *value.created_at,
synced_at: *value.synced_at,
updated_at: *value.updated_at,
status,
})
Expand Down
3 changes: 1 addition & 2 deletions ee/tabby-schema/src/schema/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ pub struct Integration {
pub access_token: String,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub synced_at: DateTime<Utc>,
pub status: IntegrationStatus,
}

Expand Down Expand Up @@ -59,5 +58,5 @@ pub trait IntegrationService: Send + Sync {
) -> Result<Vec<Integration>>;

async fn get_integration(&self, id: ID) -> Result<Integration>;
async fn update_integration_error(&self, id: ID, error: Option<String>) -> Result<()>;
async fn update_integration_sync_status(&self, id: ID, error: Option<String>) -> Result<()>;
}
6 changes: 3 additions & 3 deletions ee/tabby-webserver/src/service/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ impl IntegrationService for IntegrationServiceImpl {
Ok(self.db.get_integration(id.as_rowid()?).await?.try_into()?)
}

async fn update_integration_error(&self, id: ID, error: Option<String>) -> Result<()> {
async fn update_integration_sync_status(&self, id: ID, error: Option<String>) -> Result<()> {
self.db
.update_integration_error(id.as_rowid()?, error)
.await?;
Expand Down Expand Up @@ -150,7 +150,7 @@ mod tests {
// Test updating error status for gitlab provider
tokio::time::sleep(Duration::from_secs(1)).await;
integration
.update_integration_error(id.clone(), Some("error".into()))
.update_integration_sync_status(id.clone(), Some("error".into()))
.await
.unwrap();

Expand All @@ -159,7 +159,7 @@ mod tests {

// Test successful status (no error)
integration
.update_integration_error(id.clone(), None)
.update_integration_sync_status(id.clone(), None)
.await
.unwrap();

Expand Down
4 changes: 2 additions & 2 deletions ee/tabby-webserver/src/service/repository/third_party.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl ThirdPartyRepositoryService for ThirdPartyRepositoryServiceImpl {
Ok(repos) => repos,
Err((e, true)) => {
self.integration
.update_integration_error(provider.id.clone(), Some("".into()))
.update_integration_sync_status(provider.id.clone(), Some("".into()))
.await?;
error!(
"Credentials for integration {} are expired or invalid",
Expand Down Expand Up @@ -233,7 +233,7 @@ async fn refresh_repositories_for_provider(
}

integration
.update_integration_error(provider.id.clone(), None)
.update_integration_sync_status(provider.id.clone(), None)
.await?;
let num_removed = repository
.delete_outdated_repositories(provider.id, start)
Expand Down

0 comments on commit c7ebed9

Please sign in to comment.