Skip to content

Commit 3258e48

Browse files
committed
fix: add an ai feature so that ai features are not compiled by default.
Fixes #1219 Signed-off-by: Hiram Chirino <[email protected]>
1 parent c10d89a commit 3258e48

File tree

18 files changed

+88
-362
lines changed

18 files changed

+88
-362
lines changed

.cargo/config.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[alias]
22
xtask = "run --package xtask --"
3+
xtask-ai = "run --features ai --package xtask --"
34

45
[env]
56
CARGO_WORKSPACE_ROOT = { value = "", relative = true }

.github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ jobs:
5353
key: ${{ runner.os }}-theseus-postgresql-${{ hashFiles('**/Cargo.lock') }}
5454

5555
- name: Test
56-
run: cargo test -- --nocapture
56+
run: cargo test --all-features -- --nocapture
5757
env:
5858
RUST_LOG: warn,sqlx=error,sea_orm=error
5959
- name: Export and Validate Generated Openapi Spec

common/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ edition.workspace = true
55
publish.workspace = true
66
license.workspace = true
77

8+
[features]
9+
ai = [ "trustify-migration/ai" ]
10+
811
[dependencies]
912
trustify-migration = { workspace = true }
1013

common/src/db/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ impl Database {
4747
pub async fn migrate(&self) -> Result<(), anyhow::Error> {
4848
log::debug!("applying migrations");
4949
Migrator::up(&self.db, None).await?;
50+
#[cfg(feature = "ai")]
51+
migration::ai::Migrator::up(&self.db, None).await?;
5052
log::debug!("applied migrations");
5153

5254
Ok(())
@@ -56,6 +58,8 @@ impl Database {
5658
pub async fn refresh(&self) -> Result<(), anyhow::Error> {
5759
log::warn!("refreshing database schema...");
5860
Migrator::refresh(&self.db).await?;
61+
#[cfg(feature = "ai")]
62+
migration::ai::Migrator::refresh(&self.db).await?;
5963
log::warn!("refreshing database schema... done!");
6064

6165
Ok(())

migration/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ license.workspace = true
99
name = "migration"
1010
path = "src/lib.rs"
1111

12+
[features]
13+
ai = ["trustify-common/ai"]
14+
1215
[dependencies]
1316
sea-orm-migration = { workspace = true, features = ["runtime-tokio-rustls", "sqlx-postgres", "with-uuid"] }
1417
serde_json = { workspace = true }

migration/src/ai.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
pub use sea_orm_migration::prelude::*;
2+
3+
pub struct Migrator;
4+
5+
#[async_trait::async_trait]
6+
impl MigratorTrait for Migrator {
7+
fn migration_table_name() -> DynIden {
8+
#[derive(DeriveIden)]
9+
enum AiTables {
10+
AiMigrations,
11+
}
12+
13+
AiTables::AiMigrations.into_iden()
14+
}
15+
16+
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
17+
vec![Box::new(crate::m0000820_create_conversation::Migration)]
18+
}
19+
}

migration/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ mod m0000830_perf_indexes;
103103
mod m0000840_add_relationship_14_15;
104104
mod m0000850_python_version;
105105

106+
#[cfg(feature = "ai")]
107+
pub mod ai;
108+
106109
pub struct Migrator;
107110

108111
#[async_trait::async_trait]
@@ -207,7 +210,6 @@ impl MigratorTrait for Migrator {
207210
Box::new(m0000790_alter_sbom_alter_document_id::Migration),
208211
Box::new(m0000800_alter_product_version_range_scheme::Migration),
209212
Box::new(m0000810_fix_get_purl::Migration),
210-
Box::new(m0000820_create_conversation::Migration),
211213
Box::new(m0000830_perf_indexes::Migration),
212214
Box::new(m0000840_add_relationship_14_15::Migration),
213215
Box::new(m0000850_python_version::Migration),

migration/tests/tests.rs

+20
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,26 @@ async fn test_migrations(ctx: TrustifyContext) -> Result<(), anyhow::Error> {
2121
Ok(())
2222
}
2323

24+
#[cfg(feature = "ai")]
25+
#[test_context(TrustifyContext, skip_teardown)]
26+
#[test(tokio::test)]
27+
async fn test_ai_migrations(ctx: TrustifyContext) -> Result<(), anyhow::Error> {
28+
let db = ctx.db;
29+
30+
let migrations = migration::ai::Migrator::get_applied_migrations(&db).await?;
31+
// 'Migrator.up' was called in bootstrap function when using TrustifyContext.
32+
// At this point we already have migrations.
33+
assert!(!migrations.is_empty());
34+
35+
db.refresh().await?;
36+
37+
let rolled_back_and_reapplied_migrations =
38+
migration::ai::Migrator::get_applied_migrations(&db).await?;
39+
assert!(!rolled_back_and_reapplied_migrations.is_empty());
40+
41+
Ok(())
42+
}
43+
2444
#[test_context(TrustifyContext, skip_teardown)]
2545
#[test(tokio::test)]
2646
async fn only_up_migration(_ctx: TrustifyContext) -> Result<(), anyhow::Error> {

modules/fundamental/Cargo.toml

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ edition.workspace = true
55
publish.workspace = true
66
license.workspace = true
77

8+
[features]
9+
default = []
10+
ai = [ "trustify-common/ai" ]
11+
812
[dependencies]
913
trustify-auth = { workspace = true }
10-
trustify-common = { workspace = true }
14+
trustify-common = { workspace = true}
1115
trustify-cvss = { workspace = true }
1216
trustify-entity = { workspace = true }
1317
trustify-module-analysis = { workspace = true }

modules/fundamental/src/ai/endpoints/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[cfg(feature = "ai")]
12
#[cfg(test)]
23
mod test;
34

modules/fundamental/src/ai/endpoints/test.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,25 @@ async fn conversation_crud(ctx: &TrustifyContext) -> anyhow::Result<()> {
173173

174174
// Verify that there are no conversations
175175
let request = TestRequest::get()
176-
.uri("/api/v1/ai/conversations")
176+
.uri("/api/v2/ai/conversations")
177177
.to_request()
178178
.test_auth("user-a");
179179

180180
let response = app.call_service(request).await;
181-
assert_eq!(response.status(), StatusCode::OK);
181+
assert_eq!(
182+
response.status(),
183+
StatusCode::OK,
184+
"response: {:?}",
185+
read_text(response).await
186+
);
182187

183188
let result: PaginatedResults<ConversationSummary> = read_body_json(response).await;
184189
assert_eq!(result.total, 0);
185190
assert_eq!(result.items.len(), 0);
186191

187192
// Create a conversation
188193
let request = TestRequest::post()
189-
.uri("/api/v1/ai/conversations")
194+
.uri("/api/v2/ai/conversations")
190195
.to_request()
191196
.test_auth("user-a");
192197

@@ -208,7 +213,7 @@ async fn conversation_crud(ctx: &TrustifyContext) -> anyhow::Result<()> {
208213
));
209214

210215
let request = TestRequest::put()
211-
.uri(format!("/api/v1/ai/conversations/{}", conversation_v1.id).as_str())
216+
.uri(format!("/api/v2/ai/conversations/{}", conversation_v1.id).as_str())
212217
.set_json(update1.clone())
213218
.to_request()
214219
.test_auth("user-a");
@@ -225,7 +230,7 @@ async fn conversation_crud(ctx: &TrustifyContext) -> anyhow::Result<()> {
225230

226231
// Verify that the conversation can be listed
227232
let request = TestRequest::get()
228-
.uri("/api/v1/ai/conversations")
233+
.uri("/api/v2/ai/conversations")
229234
.to_request()
230235
.test_auth("user-a");
231236

@@ -239,7 +244,7 @@ async fn conversation_crud(ctx: &TrustifyContext) -> anyhow::Result<()> {
239244

240245
// Verify that we can retrieve the conversation by ID
241246
let request = TestRequest::get()
242-
.uri(format!("/api/v1/ai/conversations/{}", conversation_v1.id).as_str())
247+
.uri(format!("/api/v2/ai/conversations/{}", conversation_v1.id).as_str())
243248
.to_request()
244249
.test_auth("user-a");
245250

@@ -256,7 +261,7 @@ async fn conversation_crud(ctx: &TrustifyContext) -> anyhow::Result<()> {
256261
));
257262

258263
let request = TestRequest::put()
259-
.uri(format!("/api/v1/ai/conversations/{}", conversation_v1.id).as_str())
264+
.uri(format!("/api/v2/ai/conversations/{}", conversation_v1.id).as_str())
260265
.append_header(("if-match", format!("\"{}\"", conversation_v2.seq).as_str()))
261266
.set_json(update2.clone())
262267
.to_request()
@@ -274,7 +279,7 @@ async fn conversation_crud(ctx: &TrustifyContext) -> anyhow::Result<()> {
274279

275280
// Verify that we can retrieve the updated conversation by ID
276281
let request = TestRequest::get()
277-
.uri(format!("/api/v1/ai/conversations/{}", conversation_v1.id).as_str())
282+
.uri(format!("/api/v2/ai/conversations/{}", conversation_v1.id).as_str())
278283
.to_request()
279284
.test_auth("user-a");
280285

@@ -286,7 +291,7 @@ async fn conversation_crud(ctx: &TrustifyContext) -> anyhow::Result<()> {
286291

287292
// Verify that we can delete the conversation
288293
let request = TestRequest::delete()
289-
.uri(format!("/api/v1/ai/conversations/{}", conversation_v1.id).as_str())
294+
.uri(format!("/api/v2/ai/conversations/{}", conversation_v1.id).as_str())
290295
.to_request()
291296
.test_auth("user-a");
292297

@@ -295,7 +300,7 @@ async fn conversation_crud(ctx: &TrustifyContext) -> anyhow::Result<()> {
295300

296301
// Verify that the conversation is deleted
297302
let request = TestRequest::get()
298-
.uri("/api/v1/ai/conversations")
303+
.uri("/api/v2/ai/conversations")
299304
.to_request()
300305
.test_auth("user-a");
301306

modules/fundamental/src/endpoints.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub fn configure(
2424

2525
crate::advisory::endpoints::configure(svc, db.clone(), config.advisory_upload_limit);
2626
crate::license::endpoints::configure(svc, db.clone());
27+
#[cfg(feature = "ai")]
2728
crate::ai::endpoints::configure(svc, db.clone());
2829
crate::organization::endpoints::configure(svc, db.clone());
2930
crate::purl::endpoints::configure(svc, db.clone());

modules/fundamental/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
pub mod advisory;
2+
#[cfg(feature = "ai")]
23
pub mod ai;
34
pub mod endpoints;
45
pub mod error;

0 commit comments

Comments
 (0)