feat: complete Epic 1 production foundation

This commit is contained in:
bsodfather committed 2026-08-25 01:24:11 +03:00
1 parent 767428436d
commit 182bde8ac0
298 files changed
+35711 -5291

No files matched your search

+141 -38
View File
@@ -274,9 +274,18 @@ impl PostgresRegistry {
match result {
Ok(_) => Ok(()),
Err(sqlx::Error::Database(error))
if error.constraint() == Some("platform_api_keys_workspace_name_idx") =>
if matches!(
error.constraint(),
Some(
"platform_api_keys_workspace_name_idx"
| "platform_api_keys_workspace_name_active_idx"
)
) =>
{
Err(RegistryError::Storage(sqlx::Error::Database(error)))
Err(RegistryError::PlatformApiKeyNameAlreadyExists {
workspace_id: request.api_key.workspace_id.as_str().to_owned(),
name: request.api_key.name.clone(),
})
}
Err(error) => Err(RegistryError::Storage(error)),
}
@@ -288,11 +297,22 @@ impl PostgresRegistry {
key_id: &PlatformApiKeyId,
revoked_at: &time::OffsetDateTime,
) -> Result<(), RegistryError> {
let result = sqlx::query(
"update platform_api_keys
set status = $1,
revoked_at = $2::timestamptz
where workspace_id = $3 and id = $4",
let row = sqlx::query(
"with target as (
select id
from platform_api_keys
where workspace_id = $3 and id = $4
), updated as (
update platform_api_keys
set status = $1,
revoked_at = $2::timestamptz
where workspace_id = $3
and id = $4
and status = 'active'
returning id
)
select exists(select 1 from target) as exists,
exists(select 1 from updated) as updated",
)
.bind(serialize_enum_text(
&PlatformApiKeyStatus::Revoked,
@@ -301,14 +321,19 @@ impl PostgresRegistry {
.bind(revoked_at)
.bind(workspace_id.as_str())
.bind(key_id.as_str())
.execute(&self.pool)
.fetch_one(&self.pool)
.await?;
if result.rows_affected() == 0 {
if !row.get::<bool, _>("exists") {
return Err(RegistryError::PlatformApiKeyNotFound {
key_id: key_id.as_str().to_owned(),
});
}
if !row.get::<bool, _>("updated") {
return Err(RegistryError::PlatformApiKeyInactive {
key_id: key_id.as_str().to_owned(),
});
}
Ok(())
}
@@ -320,11 +345,23 @@ impl PostgresRegistry {
key_id: &PlatformApiKeyId,
revoked_at: &time::OffsetDateTime,
) -> Result<(), RegistryError> {
let result = sqlx::query(
"update platform_api_keys
set status = $1,
revoked_at = $2::timestamptz
where workspace_id = $3 and agent_id = $4 and id = $5",
let row = sqlx::query(
"with target as (
select id
from platform_api_keys
where workspace_id = $3 and agent_id = $4 and id = $5
), updated as (
update platform_api_keys
set status = $1,
revoked_at = $2::timestamptz
where workspace_id = $3
and agent_id = $4
and id = $5
and status = 'active'
returning id
)
select exists(select 1 from target) as exists,
exists(select 1 from updated) as updated",
)
.bind(serialize_enum_text(
&PlatformApiKeyStatus::Revoked,
@@ -334,14 +371,19 @@ impl PostgresRegistry {
.bind(workspace_id.as_str())
.bind(agent_id.as_str())
.bind(key_id.as_str())
.execute(&self.pool)
.fetch_one(&self.pool)
.await?;
if result.rows_affected() == 0 {
if !row.get::<bool, _>("exists") {
return Err(RegistryError::PlatformApiKeyNotFound {
key_id: key_id.as_str().to_owned(),
});
}
if !row.get::<bool, _>("updated") {
return Err(RegistryError::PlatformApiKeyInactive {
key_id: key_id.as_str().to_owned(),
});
}
Ok(())
}
@@ -351,18 +393,42 @@ impl PostgresRegistry {
workspace_id: &WorkspaceId,
key_id: &PlatformApiKeyId,
) -> Result<(), RegistryError> {
let result =
sqlx::query("delete from platform_api_keys where workspace_id = $1 and id = $2")
.bind(workspace_id.as_str())
.bind(key_id.as_str())
.execute(&self.pool)
.await?;
let row = sqlx::query(
"with target as (
select id
from platform_api_keys
where workspace_id = $2 and id = $3
), updated as (
update platform_api_keys
set status = $1,
revoked_at = coalesce(revoked_at, now())
where workspace_id = $2
and id = $3
and status <> 'deleted'
returning id
)
select exists(select 1 from target) as exists,
exists(select 1 from updated) as updated",
)
.bind(serialize_enum_text(
&PlatformApiKeyStatus::Deleted,
"status",
)?)
.bind(workspace_id.as_str())
.bind(key_id.as_str())
.fetch_one(&self.pool)
.await?;
if result.rows_affected() == 0 {
if !row.get::<bool, _>("exists") {
return Err(RegistryError::PlatformApiKeyNotFound {
key_id: key_id.as_str().to_owned(),
});
}
if !row.get::<bool, _>("updated") {
return Err(RegistryError::PlatformApiKeyInactive {
key_id: key_id.as_str().to_owned(),
});
}
Ok(())
}
@@ -373,20 +439,44 @@ impl PostgresRegistry {
agent_id: &AgentId,
key_id: &PlatformApiKeyId,
) -> Result<(), RegistryError> {
let result = sqlx::query(
"delete from platform_api_keys where workspace_id = $1 and agent_id = $2 and id = $3",
let row = sqlx::query(
"with target as (
select id
from platform_api_keys
where workspace_id = $2 and agent_id = $3 and id = $4
), updated as (
update platform_api_keys
set status = $1,
revoked_at = coalesce(revoked_at, now())
where workspace_id = $2
and agent_id = $3
and id = $4
and status <> 'deleted'
returning id
)
select exists(select 1 from target) as exists,
exists(select 1 from updated) as updated",
)
.bind(serialize_enum_text(
&PlatformApiKeyStatus::Deleted,
"status",
)?)
.bind(workspace_id.as_str())
.bind(agent_id.as_str())
.bind(key_id.as_str())
.execute(&self.pool)
.fetch_one(&self.pool)
.await?;
if result.rows_affected() == 0 {
if !row.get::<bool, _>("exists") {
return Err(RegistryError::PlatformApiKeyNotFound {
key_id: key_id.as_str().to_owned(),
});
}
if !row.get::<bool, _>("updated") {
return Err(RegistryError::PlatformApiKeyInactive {
key_id: key_id.as_str().to_owned(),
});
}
Ok(())
}
@@ -397,23 +487,31 @@ impl PostgresRegistry {
key_id: &PlatformApiKeyId,
used_at: &time::OffsetDateTime,
) -> Result<(), RegistryError> {
let exists = sqlx::query_scalar::<_, bool>(
"with target as (
select id
let row = sqlx::query(
"with target as materialized (
select id,
last_used_at,
status = 'active'
and (expires_at is null or expires_at > now()) as active
from platform_api_keys
where workspace_id = $1 and id = $2
for update
), updated as (
update platform_api_keys
update platform_api_keys as api_key
set last_used_at = $3::timestamptz
where workspace_id = $1
and id = $2
from target
where api_key.workspace_id = $1
and api_key.id = target.id
and target.active
and (
last_used_at is null
or last_used_at < $3::timestamptz - interval '1 minute'
target.last_used_at is null
or target.last_used_at < $3::timestamptz - interval '1 minute'
)
returning id
returning api_key.id
)
select exists(select 1 from target)",
select
exists(select 1 from target) as exists,
coalesce((select active from target), false) as active",
)
.bind(workspace_id.as_str())
.bind(key_id.as_str())
@@ -421,11 +519,16 @@ impl PostgresRegistry {
.fetch_one(&self.pool)
.await?;
if !exists {
if !row.get::<bool, _>("exists") {
return Err(RegistryError::PlatformApiKeyNotFound {
key_id: key_id.as_str().to_owned(),
});
}
if !row.get::<bool, _>("active") {
return Err(RegistryError::PlatformApiKeyInactive {
key_id: key_id.as_str().to_owned(),
});
}
Ok(())
}