kellnr-db 6.5.2

Kellnr is a self-hosted registry for Rust crates with support for rustdocs and crates.io caching.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Test utilities for database testing.
//!
//! These functions provide convenient ways to set up test data
//! in the database for integration and unit tests.
//!
//! # Builder Pattern
//!
//! For ergonomic test setup, use [`TestCrateBuilder`]:
//!
//! ```ignore
//! TestCrateBuilder::new(test_db)
//!     .name("mycrate")
//!     .owner("admin")
//!     .version("1.0.0")
//!     .build()
//!     .await
//!     .unwrap();
//! ```

use std::collections::BTreeMap;

use chrono::{DateTime, TimeZone, Utc};
use kellnr_common::index_metadata::IndexMetadata;
use kellnr_common::original_name::OriginalName;
use kellnr_common::prefetch::Prefetch;
use kellnr_common::publish_metadata::PublishMetadata;
use kellnr_common::version::Version;
use kellnr_entity::{crate_index, crate_meta, cratesio_crate, krate, session, user};
use sea_orm::sea_query::Expr;
use sea_orm::{
    ActiveModelTrait, ActiveValue, ColumnTrait, EntityTrait, ExprTrait, QueryFilter, Set,
};

use super::{DB_DATE_FORMAT, Database, parse_db_version};
use crate::CrateMeta;
use crate::error::DbError;
use crate::provider::{DbProvider, DbResult};

/// Returns a standard test date for consistent test data.
///
/// The returned date is `2020-10-07 13:18:00 UTC`, which is commonly
/// used across the test suite.
#[must_use]
pub fn default_created() -> DateTime<Utc> {
    Utc.with_ymd_and_hms(2020, 10, 7, 13, 18, 0).unwrap()
}

/// Add multiple versions of a crate at once.
///
/// This is a convenience function for tests that need to set up a crate
/// with multiple versions quickly.
///
/// # Arguments
///
/// * `db` - The database connection
/// * `name` - The crate name
/// * `owner` - The owner username
/// * `versions` - Slice of version strings to add (e.g., `["1.0.0", "2.0.0"]`)
///
/// # Returns
///
/// A vector of crate IDs for each version added.
///
/// # Example
///
/// ```ignore
/// let ids = add_multiple_versions(test_db, "mycrate", "admin", &["1.0.0", "2.0.0", "3.0.0"])
///     .await
///     .unwrap();
/// ```
pub async fn add_multiple_versions(
    db: &Database,
    name: &str,
    owner: &str,
    versions: &[&str],
) -> DbResult<Vec<i64>> {
    let created = default_created();
    let mut ids = Vec::with_capacity(versions.len());
    for version in versions {
        let id = test_add_crate(db, name, owner, &parse_db_version(version)?, &created).await?;
        ids.push(id);
    }
    Ok(ids)
}

/// A builder for creating test crates with a fluent API.
///
/// # Example
///
/// ```ignore
/// // Minimal usage with defaults
/// TestCrateBuilder::new(test_db)
///     .name("mycrate")
///     .build()
///     .await
///     .unwrap();
///
/// // Full customization
/// let crate_id = TestCrateBuilder::new(test_db)
///     .name("mycrate")
///     .owner("testuser")
///     .version("2.0.0")
///     .created(Utc::now())
///     .downloads(100)
///     .build()
///     .await
///     .unwrap();
/// ```
pub struct TestCrateBuilder<'a> {
    db: &'a Database,
    name: Option<&'a str>,
    owner: &'a str,
    version: &'a str,
    created: Option<DateTime<Utc>>,
    downloads: Option<i64>,
}

impl<'a> TestCrateBuilder<'a> {
    /// Create a new builder with default values.
    ///
    /// Defaults:
    /// - `owner`: "admin"
    /// - `version`: "1.0.0"
    /// - `created`: [`default_created()`]
    /// - `downloads`: None (0)
    #[must_use]
    pub fn new(db: &'a Database) -> Self {
        Self {
            db,
            name: None,
            owner: "admin",
            version: "1.0.0",
            created: None,
            downloads: None,
        }
    }

    /// Set the crate name (required).
    #[must_use]
    pub fn name(mut self, name: &'a str) -> Self {
        self.name = Some(name);
        self
    }

    /// Set the owner username. Defaults to "admin".
    #[must_use]
    pub fn owner(mut self, owner: &'a str) -> Self {
        self.owner = owner;
        self
    }

    /// Set the version string. Defaults to "1.0.0".
    #[must_use]
    pub fn version(mut self, version: &'a str) -> Self {
        self.version = version;
        self
    }

    /// Set the creation date. Defaults to [`default_created()`].
    #[must_use]
    pub fn created(mut self, created: DateTime<Utc>) -> Self {
        self.created = Some(created);
        self
    }

    /// Set the download count. If not set, downloads will be 0.
    #[must_use]
    pub fn downloads(mut self, downloads: i64) -> Self {
        self.downloads = Some(downloads);
        self
    }

    /// Build and insert the crate into the database.
    ///
    /// # Errors
    ///
    /// Returns an error if:
    /// - The crate name was not set
    /// - The version string is invalid
    /// - Database insertion fails
    ///
    /// # Returns
    ///
    /// The crate ID on success.
    pub async fn build(self) -> DbResult<i64> {
        let name = self.name.ok_or_else(|| {
            DbError::InvalidCrateName("TestCrateBuilder: name is required".to_string())
        })?;
        let created = self.created.unwrap_or_else(default_created);
        let version = Version::try_from(self.version)
            .map_err(|_| DbError::InvalidVersion(self.version.to_string()))?;

        if let Some(downloads) = self.downloads {
            test_add_crate_with_downloads(
                self.db,
                name,
                self.owner,
                &version,
                &created,
                Some(downloads),
            )
            .await
        } else {
            test_add_crate(self.db, name, self.owner, &version, &created).await
        }
    }
}

/// Add a cached crate with a specified download count.
pub async fn test_add_cached_crate_with_downloads(
    db: &Database,
    name: &str,
    version: &str,
    downloads: u64,
) -> DbResult<()> {
    let _ = test_add_cached_crate(db, name, version).await?;

    let krate = cratesio_crate::Entity::find()
        .filter(cratesio_crate::Column::Name.eq(name))
        .one(&db.db_con)
        .await?
        .ok_or_else(|| DbError::CrateNotFound(name.to_string()))?;

    let total_downloads = krate.total_downloads as u64;

    let mut krate: cratesio_crate::ActiveModel = krate.into();
    krate.total_downloads = Set((total_downloads + downloads) as i64);
    krate.update(&db.db_con).await?;

    Ok(())
}

/// Add a cached crate for testing.
pub async fn test_add_cached_crate(db: &Database, name: &str, version: &str) -> DbResult<Prefetch> {
    let etag = "etag";
    let last_modified = "last_modified";
    let description = Some(String::from("description"));
    let indices = vec![IndexMetadata {
        name: name.to_string(),
        vers: version.to_string(),
        deps: vec![],
        cksum: "cksum".to_string(),
        features: BTreeMap::new(),
        features2: None,
        pubtime: None,
        yanked: false,
        links: None,
        v: Some(1),
    }];

    db.add_cratesio_prefetch_data(
        &OriginalName::from_unchecked(name.to_string()),
        etag,
        last_modified,
        description,
        &indices,
    )
    .await
}

/// Add a test crate.
pub async fn test_add_crate(
    db: &Database,
    name: &str,
    owner: &str,
    version: &Version,
    created: &DateTime<Utc>,
) -> DbResult<i64> {
    let pm = PublishMetadata {
        name: name.to_string(),
        vers: version.to_string(),
        ..PublishMetadata::default()
    };
    let user = user::Entity::find()
        .filter(user::Column::Name.eq(owner))
        .one(&db.db_con)
        .await?;
    if user.is_none() {
        db.add_user(name, "pwd", "salt", false, false).await?;
    }

    db.add_crate(&pm, "cksum", created, owner).await
}

/// Add a test crate with a specified download count.
pub async fn test_add_crate_with_downloads(
    db: &Database,
    name: &str,
    owner: &str,
    version: &Version,
    created: &DateTime<Utc>,
    downloads: Option<i64>,
) -> DbResult<i64> {
    let pm = PublishMetadata {
        name: name.to_string(),
        vers: version.to_string(),
        ..PublishMetadata::default()
    };
    let user = user::Entity::find()
        .filter(user::Column::Name.eq(owner))
        .one(&db.db_con)
        .await?;
    if user.is_none() {
        db.add_user(name, "pwd", "salt", false, false).await?;
    }

    db.add_crate(&pm, "cksum", created, owner).await?;
    let (cm, krate) = crate_meta::Entity::find()
        .find_also_related(krate::Entity)
        .filter(krate::Column::Name.eq(name))
        .filter(crate_meta::Column::Version.eq(version))
        .one(&db.db_con)
        .await?
        .ok_or_else(|| DbError::CrateNotFound(name.to_string()))?;
    let mut cm: crate_meta::ActiveModel = cm.into();

    let current_downloads = krate.as_ref().unwrap().total_downloads;
    let crate_id = krate.as_ref().unwrap().id;

    let mut krate: krate::ActiveModel = krate.unwrap().into();
    krate.total_downloads = Set(current_downloads + downloads.unwrap_or(0));
    krate.update(&db.db_con).await?;
    cm.downloads = Set(downloads.unwrap_or_default());
    cm.update(&db.db_con).await?;
    Ok(crate_id)
}

/// Add test crate metadata.
pub async fn test_add_crate_meta(
    db: &Database,
    crate_id: i64,
    version: &str,
    created: &DateTime<Utc>,
    downloads: Option<i64>,
) -> DbResult<()> {
    let cm = crate_meta::ActiveModel {
        id: ActiveValue::default(),
        version: Set(version.to_string()),
        created: Set(created.to_string()),
        downloads: Set(downloads.unwrap_or_default()),
        crate_fk: Set(crate_id),
        ..Default::default()
    };

    cm.insert(&db.db_con).await?;

    Ok(())
}

/// Delete crate index entries for testing.
pub async fn test_delete_crate_index(db: &Database, crate_id: i64) -> DbResult<()> {
    crate_index::Entity::delete_many()
        .filter(crate_index::Column::CrateFk.eq(crate_id))
        .exec(&db.db_con)
        .await?;
    Ok(())
}

/// Clean database by removing old sessions.
pub async fn clean_db(db: &Database, session_age: std::time::Duration) -> DbResult<()> {
    let session_age = chrono::Duration::from_std(session_age).unwrap();
    let now = std::ops::Add::add(Utc::now(), session_age)
        .format(DB_DATE_FORMAT)
        .to_string();

    session::Entity::delete_many()
        .filter(Expr::col(session::Column::Created).lt(now))
        .exec(&db.db_con)
        .await?;

    Ok(())
}

/// Insert a session whose `created` timestamp is `age` in the past.
///
/// Used to test session-expiry behavior without sleeping: pass an `age`
/// larger than the configured session age to create an already-expired
/// session, or a small one to create a still-valid session.
pub async fn add_aged_session(
    db: &Database,
    user_name: &str,
    token: &str,
    age: std::time::Duration,
) -> DbResult<()> {
    let user = user::Entity::find()
        .filter(user::Column::Name.eq(user_name))
        .one(&db.db_con)
        .await?
        .ok_or_else(|| DbError::UserNotFound(user_name.to_owned()))?;

    let age = chrono::Duration::from_std(age).expect("session age out of range");
    let created = (Utc::now() - age).format(DB_DATE_FORMAT).to_string();
    let s = session::ActiveModel {
        token: Set(token.to_owned()),
        created: Set(created),
        user_fk: Set(user.id),
        ..Default::default()
    };
    s.insert(&db.db_con).await?;
    Ok(())
}

/// Get crate meta list by crate ID.
pub async fn get_crate_meta_list(db: &Database, crate_id: i64) -> DbResult<Vec<CrateMeta>> {
    let cm: Vec<(crate_meta::Model, Option<krate::Model>)> = crate_meta::Entity::find()
        .find_also_related(krate::Entity)
        .filter(crate_meta::Column::CrateFk.eq(crate_id))
        .all(&db.db_con)
        .await?;

    let crate_metas: Vec<CrateMeta> = cm
        .into_iter()
        .map(|(m, c)| CrateMeta {
            name: c.unwrap().name, // Unwarp is ok, as a relation always exists
            id: m.id,
            version: m.version,
            created: m.created,
            downloads: m.downloads,
            crate_fk: m.crate_fk,
        })
        .collect();

    Ok(crate_metas)
}