You've already forked AstralRinth
forked from didirus/AstralRinth
Labrinth ID cleanup (#3681)
* Put all ID types in the labrinth::models::ids, and reduce code duplication with them * Rewrite labrinth::database::models::ids and rename most DB interface ID structs to be prefixed with DB * Run sqlx prepare --------- Co-authored-by: Alejandro González <7822554+AlexTMjugador@users.noreply.github.com>
This commit is contained in:
@@ -20,9 +20,9 @@ const VERSION_FILES_NAMESPACE: &str = "versions_files";
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct VersionBuilder {
|
||||
pub version_id: VersionId,
|
||||
pub project_id: ProjectId,
|
||||
pub author_id: UserId,
|
||||
pub version_id: DBVersionId,
|
||||
pub project_id: DBProjectId,
|
||||
pub author_id: DBUserId,
|
||||
pub name: String,
|
||||
pub version_number: String,
|
||||
pub changelog: String,
|
||||
@@ -39,8 +39,8 @@ pub struct VersionBuilder {
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct DependencyBuilder {
|
||||
pub project_id: Option<ProjectId>,
|
||||
pub version_id: Option<VersionId>,
|
||||
pub project_id: Option<DBProjectId>,
|
||||
pub version_id: Option<DBVersionId>,
|
||||
pub file_name: Option<String>,
|
||||
pub dependency_type: String,
|
||||
}
|
||||
@@ -48,7 +48,7 @@ pub struct DependencyBuilder {
|
||||
impl DependencyBuilder {
|
||||
pub async fn insert_many(
|
||||
builders: Vec<Self>,
|
||||
version_id: VersionId,
|
||||
version_id: DBVersionId,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<(), DatabaseError> {
|
||||
let mut project_ids = Vec::new();
|
||||
@@ -97,7 +97,7 @@ impl DependencyBuilder {
|
||||
async fn try_get_project_id(
|
||||
&self,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<Option<ProjectId>, DatabaseError> {
|
||||
) -> Result<Option<DBProjectId>, DatabaseError> {
|
||||
Ok(if let Some(project_id) = self.project_id {
|
||||
Some(project_id)
|
||||
} else if let Some(version_id) = self.version_id {
|
||||
@@ -105,11 +105,11 @@ impl DependencyBuilder {
|
||||
"
|
||||
SELECT mod_id FROM versions WHERE id = $1
|
||||
",
|
||||
version_id as VersionId,
|
||||
version_id as DBVersionId,
|
||||
)
|
||||
.fetch_optional(&mut **transaction)
|
||||
.await?
|
||||
.map(|x| ProjectId(x.mod_id))
|
||||
.map(|x| DBProjectId(x.mod_id))
|
||||
} else {
|
||||
None
|
||||
})
|
||||
@@ -129,9 +129,9 @@ pub struct VersionFileBuilder {
|
||||
impl VersionFileBuilder {
|
||||
pub async fn insert(
|
||||
self,
|
||||
version_id: VersionId,
|
||||
version_id: DBVersionId,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<FileId, DatabaseError> {
|
||||
) -> Result<DBFileId, DatabaseError> {
|
||||
let file_id = generate_file_id(&mut *transaction).await?;
|
||||
|
||||
sqlx::query!(
|
||||
@@ -139,8 +139,8 @@ impl VersionFileBuilder {
|
||||
INSERT INTO files (id, version_id, url, filename, is_primary, size, file_type)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
",
|
||||
file_id as FileId,
|
||||
version_id as VersionId,
|
||||
file_id as DBFileId,
|
||||
version_id as DBVersionId,
|
||||
self.url,
|
||||
self.filename,
|
||||
self.primary,
|
||||
@@ -156,7 +156,7 @@ impl VersionFileBuilder {
|
||||
INSERT INTO hashes (file_id, algorithm, hash)
|
||||
VALUES ($1, $2, $3)
|
||||
",
|
||||
file_id as FileId,
|
||||
file_id as DBFileId,
|
||||
hash.algorithm,
|
||||
hash.hash,
|
||||
)
|
||||
@@ -178,7 +178,7 @@ impl VersionBuilder {
|
||||
pub async fn insert(
|
||||
self,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<VersionId, DatabaseError> {
|
||||
) -> Result<DBVersionId, DatabaseError> {
|
||||
let version = Version {
|
||||
id: self.version_id,
|
||||
project_id: self.project_id,
|
||||
@@ -203,7 +203,7 @@ impl VersionBuilder {
|
||||
SET updated = NOW()
|
||||
WHERE id = $1
|
||||
",
|
||||
self.project_id as ProjectId,
|
||||
self.project_id as DBProjectId,
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
@@ -245,7 +245,7 @@ impl VersionBuilder {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct LoaderVersion {
|
||||
pub loader_id: LoaderId,
|
||||
pub version_id: VersionId,
|
||||
pub version_id: DBVersionId,
|
||||
}
|
||||
|
||||
impl LoaderVersion {
|
||||
@@ -274,9 +274,9 @@ impl LoaderVersion {
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize, PartialEq, Eq)]
|
||||
pub struct Version {
|
||||
pub id: VersionId,
|
||||
pub project_id: ProjectId,
|
||||
pub author_id: UserId,
|
||||
pub id: DBVersionId,
|
||||
pub project_id: DBProjectId,
|
||||
pub author_id: DBUserId,
|
||||
pub name: String,
|
||||
pub version_number: String,
|
||||
pub changelog: String,
|
||||
@@ -307,9 +307,9 @@ impl Version {
|
||||
$9, $10, $11, $12
|
||||
)
|
||||
",
|
||||
self.id as VersionId,
|
||||
self.project_id as ProjectId,
|
||||
self.author_id as UserId,
|
||||
self.id as DBVersionId,
|
||||
self.project_id as DBProjectId,
|
||||
self.author_id as DBUserId,
|
||||
&self.name,
|
||||
&self.version_number,
|
||||
self.changelog,
|
||||
@@ -327,7 +327,7 @@ impl Version {
|
||||
}
|
||||
|
||||
pub async fn remove_full(
|
||||
id: VersionId,
|
||||
id: DBVersionId,
|
||||
redis: &RedisPool,
|
||||
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
|
||||
) -> Result<Option<()>, DatabaseError> {
|
||||
@@ -347,7 +347,7 @@ impl Version {
|
||||
SET version_id = NULL
|
||||
WHERE version_id = $1
|
||||
",
|
||||
id as VersionId,
|
||||
id as DBVersionId,
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
@@ -357,7 +357,7 @@ impl Version {
|
||||
DELETE FROM version_fields vf
|
||||
WHERE vf.version_id = $1
|
||||
",
|
||||
id as VersionId,
|
||||
id as DBVersionId,
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
@@ -367,7 +367,7 @@ impl Version {
|
||||
DELETE FROM loaders_versions
|
||||
WHERE loaders_versions.version_id = $1
|
||||
",
|
||||
id as VersionId,
|
||||
id as DBVersionId,
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
@@ -381,7 +381,7 @@ impl Version {
|
||||
(hashes.file_id = files.id)
|
||||
)
|
||||
",
|
||||
id as VersionId
|
||||
id as DBVersionId
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
@@ -391,7 +391,7 @@ impl Version {
|
||||
DELETE FROM files
|
||||
WHERE files.version_id = $1
|
||||
",
|
||||
id as VersionId,
|
||||
id as DBVersionId,
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
@@ -402,7 +402,7 @@ impl Version {
|
||||
"
|
||||
SELECT mod_id FROM versions WHERE id = $1
|
||||
",
|
||||
id as VersionId,
|
||||
id as DBVersionId,
|
||||
)
|
||||
.fetch_one(&mut **transaction)
|
||||
.await?;
|
||||
@@ -413,7 +413,7 @@ impl Version {
|
||||
SET dependency_id = NULL, mod_dependency_id = $2
|
||||
WHERE dependency_id = $1
|
||||
",
|
||||
id as VersionId,
|
||||
id as DBVersionId,
|
||||
project_id.mod_id,
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
@@ -431,7 +431,7 @@ impl Version {
|
||||
"
|
||||
DELETE FROM dependencies WHERE dependent_id = $1
|
||||
",
|
||||
id as VersionId,
|
||||
id as DBVersionId,
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
@@ -442,13 +442,13 @@ impl Version {
|
||||
"
|
||||
DELETE FROM versions WHERE id = $1
|
||||
",
|
||||
id as VersionId,
|
||||
id as DBVersionId,
|
||||
)
|
||||
.execute(&mut **transaction)
|
||||
.await?;
|
||||
|
||||
crate::database::models::Project::clear_cache(
|
||||
ProjectId(project_id.mod_id),
|
||||
DBProjectId(project_id.mod_id),
|
||||
None,
|
||||
None,
|
||||
redis,
|
||||
@@ -459,7 +459,7 @@ impl Version {
|
||||
}
|
||||
|
||||
pub async fn get<'a, 'b, E>(
|
||||
id: VersionId,
|
||||
id: DBVersionId,
|
||||
executor: E,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Option<QueryVersion>, DatabaseError>
|
||||
@@ -472,7 +472,7 @@ impl Version {
|
||||
}
|
||||
|
||||
pub async fn get_many<'a, E>(
|
||||
version_ids: &[VersionId],
|
||||
version_ids: &[DBVersionId],
|
||||
exec: E,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Vec<QueryVersion>, DatabaseError>
|
||||
@@ -486,7 +486,7 @@ impl Version {
|
||||
let mut exec = exec.acquire().await?;
|
||||
|
||||
let loader_field_enum_value_ids = DashSet::new();
|
||||
let version_fields: DashMap<VersionId, Vec<QueryVersionField>> = sqlx::query!(
|
||||
let version_fields: DashMap<DBVersionId, Vec<QueryVersionField>> = sqlx::query!(
|
||||
"
|
||||
SELECT version_id, field_id, int_value, enum_value, string_value
|
||||
FROM version_fields
|
||||
@@ -497,9 +497,9 @@ impl Version {
|
||||
.fetch(&mut *exec)
|
||||
.try_fold(
|
||||
DashMap::new(),
|
||||
|acc: DashMap<VersionId, Vec<QueryVersionField>>, m| {
|
||||
|acc: DashMap<DBVersionId, Vec<QueryVersionField>>, m| {
|
||||
let qvf = QueryVersionField {
|
||||
version_id: VersionId(m.version_id),
|
||||
version_id: DBVersionId(m.version_id),
|
||||
field_id: LoaderFieldId(m.field_id),
|
||||
int_value: m.int_value,
|
||||
enum_value: if m.enum_value == -1 { None } else { Some(LoaderFieldEnumValueId(m.enum_value)) },
|
||||
@@ -510,7 +510,7 @@ impl Version {
|
||||
loader_field_enum_value_ids.insert(LoaderFieldEnumValueId(m.enum_value));
|
||||
}
|
||||
|
||||
acc.entry(VersionId(m.version_id)).or_default().push(qvf);
|
||||
acc.entry(DBVersionId(m.version_id)).or_default().push(qvf);
|
||||
async move { Ok(acc) }
|
||||
},
|
||||
)
|
||||
@@ -525,7 +525,7 @@ impl Version {
|
||||
}
|
||||
|
||||
let loader_field_ids = DashSet::new();
|
||||
let loaders_ptypes_games: DashMap<VersionId, VersionLoaderData> = sqlx::query!(
|
||||
let loaders_ptypes_games: DashMap<DBVersionId, VersionLoaderData> = sqlx::query!(
|
||||
"
|
||||
SELECT DISTINCT version_id,
|
||||
ARRAY_AGG(DISTINCT l.loader) filter (where l.loader is not null) loaders,
|
||||
@@ -546,7 +546,7 @@ impl Version {
|
||||
&version_ids
|
||||
).fetch(&mut *exec)
|
||||
.map_ok(|m| {
|
||||
let version_id = VersionId(m.version_id);
|
||||
let version_id = DBVersionId(m.version_id);
|
||||
|
||||
// Add loader fields to the set we need to fetch
|
||||
let loader_loader_field_ids = m.loader_fields.unwrap_or_default().into_iter().map(LoaderFieldId).collect::<Vec<_>>();
|
||||
@@ -614,14 +614,14 @@ impl Version {
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Hash {
|
||||
pub file_id: FileId,
|
||||
pub file_id: DBFileId,
|
||||
pub algorithm: String,
|
||||
pub hash: String,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct File {
|
||||
pub id: FileId,
|
||||
pub id: DBFileId,
|
||||
pub url: String,
|
||||
pub filename: String,
|
||||
pub primary: bool,
|
||||
@@ -631,7 +631,7 @@ impl Version {
|
||||
|
||||
let file_ids = DashSet::new();
|
||||
let reverse_file_map = DashMap::new();
|
||||
let files : DashMap<VersionId, Vec<File>> = sqlx::query!(
|
||||
let files : DashMap<DBVersionId, Vec<File>> = sqlx::query!(
|
||||
"
|
||||
SELECT DISTINCT version_id, f.id, f.url, f.filename, f.is_primary, f.size, f.file_type
|
||||
FROM files f
|
||||
@@ -639,9 +639,9 @@ impl Version {
|
||||
",
|
||||
&version_ids
|
||||
).fetch(&mut *exec)
|
||||
.try_fold(DashMap::new(), |acc : DashMap<VersionId, Vec<File>>, m| {
|
||||
.try_fold(DashMap::new(), |acc : DashMap<DBVersionId, Vec<File>>, m| {
|
||||
let file = File {
|
||||
id: FileId(m.id),
|
||||
id: DBFileId(m.id),
|
||||
url: m.url,
|
||||
filename: m.filename,
|
||||
primary: m.is_primary,
|
||||
@@ -649,17 +649,17 @@ impl Version {
|
||||
file_type: m.file_type.map(|x| FileType::from_string(&x)),
|
||||
};
|
||||
|
||||
file_ids.insert(FileId(m.id));
|
||||
reverse_file_map.insert(FileId(m.id), VersionId(m.version_id));
|
||||
file_ids.insert(DBFileId(m.id));
|
||||
reverse_file_map.insert(DBFileId(m.id), DBVersionId(m.version_id));
|
||||
|
||||
acc.entry(VersionId(m.version_id))
|
||||
acc.entry(DBVersionId(m.version_id))
|
||||
.or_default()
|
||||
.push(file);
|
||||
async move { Ok(acc) }
|
||||
}
|
||||
).await?;
|
||||
|
||||
let hashes: DashMap<VersionId, Vec<Hash>> = sqlx::query!(
|
||||
let hashes: DashMap<DBVersionId, Vec<Hash>> = sqlx::query!(
|
||||
"
|
||||
SELECT DISTINCT file_id, algorithm, encode(hash, 'escape') hash
|
||||
FROM hashes
|
||||
@@ -668,15 +668,15 @@ impl Version {
|
||||
&file_ids.iter().map(|x| x.0).collect::<Vec<_>>()
|
||||
)
|
||||
.fetch(&mut *exec)
|
||||
.try_fold(DashMap::new(), |acc: DashMap<VersionId, Vec<Hash>>, m| {
|
||||
.try_fold(DashMap::new(), |acc: DashMap<DBVersionId, Vec<Hash>>, m| {
|
||||
if let Some(found_hash) = m.hash {
|
||||
let hash = Hash {
|
||||
file_id: FileId(m.file_id),
|
||||
file_id: DBFileId(m.file_id),
|
||||
algorithm: m.algorithm,
|
||||
hash: found_hash,
|
||||
};
|
||||
|
||||
if let Some(version_id) = reverse_file_map.get(&FileId(m.file_id)) {
|
||||
if let Some(version_id) = reverse_file_map.get(&DBFileId(m.file_id)) {
|
||||
acc.entry(*version_id).or_default().push(hash);
|
||||
}
|
||||
}
|
||||
@@ -684,7 +684,7 @@ impl Version {
|
||||
})
|
||||
.await?;
|
||||
|
||||
let dependencies : DashMap<VersionId, Vec<QueryDependency>> = sqlx::query!(
|
||||
let dependencies : DashMap<DBVersionId, Vec<QueryDependency>> = sqlx::query!(
|
||||
"
|
||||
SELECT DISTINCT dependent_id as version_id, d.mod_dependency_id as dependency_project_id, d.dependency_id as dependency_version_id, d.dependency_file_name as file_name, d.dependency_type as dependency_type
|
||||
FROM dependencies d
|
||||
@@ -694,13 +694,13 @@ impl Version {
|
||||
).fetch(&mut *exec)
|
||||
.try_fold(DashMap::new(), |acc : DashMap<_,Vec<QueryDependency>>, m| {
|
||||
let dependency = QueryDependency {
|
||||
project_id: m.dependency_project_id.map(ProjectId),
|
||||
version_id: m.dependency_version_id.map(VersionId),
|
||||
project_id: m.dependency_project_id.map(DBProjectId),
|
||||
version_id: m.dependency_version_id.map(DBVersionId),
|
||||
file_name: m.file_name,
|
||||
dependency_type: m.dependency_type,
|
||||
};
|
||||
|
||||
acc.entry(VersionId(m.version_id))
|
||||
acc.entry(DBVersionId(m.version_id))
|
||||
.or_default()
|
||||
.push(dependency);
|
||||
async move { Ok(acc) }
|
||||
@@ -719,7 +719,7 @@ impl Version {
|
||||
)
|
||||
.fetch(&mut *exec)
|
||||
.try_fold(DashMap::new(), |acc, v| {
|
||||
let version_id = VersionId(v.id);
|
||||
let version_id = DBVersionId(v.id);
|
||||
let VersionLoaderData {
|
||||
loaders,
|
||||
project_types,
|
||||
@@ -737,9 +737,9 @@ impl Version {
|
||||
|
||||
let query_version = QueryVersion {
|
||||
inner: Version {
|
||||
id: VersionId(v.id),
|
||||
project_id: ProjectId(v.mod_id),
|
||||
author_id: UserId(v.author_id),
|
||||
id: DBVersionId(v.id),
|
||||
project_id: DBProjectId(v.mod_id),
|
||||
author_id: DBUserId(v.author_id),
|
||||
name: v.version_name,
|
||||
version_number: v.version_number,
|
||||
changelog: v.changelog,
|
||||
@@ -812,7 +812,7 @@ impl Version {
|
||||
pub async fn get_file_from_hash<'a, 'b, E>(
|
||||
algo: String,
|
||||
hash: String,
|
||||
version_id: Option<VersionId>,
|
||||
version_id: Option<DBVersionId>,
|
||||
executor: E,
|
||||
redis: &RedisPool,
|
||||
) -> Result<Option<SingleFile>, DatabaseError>
|
||||
@@ -873,9 +873,9 @@ impl Version {
|
||||
let key = format!("{algorithm}_{hash}");
|
||||
|
||||
let file = SingleFile {
|
||||
id: FileId(f.id),
|
||||
version_id: VersionId(f.version_id),
|
||||
project_id: ProjectId(f.mod_id),
|
||||
id: DBFileId(f.id),
|
||||
version_id: DBVersionId(f.version_id),
|
||||
project_id: DBProjectId(f.mod_id),
|
||||
url: f.url,
|
||||
filename: f.filename,
|
||||
hashes,
|
||||
@@ -940,15 +940,15 @@ pub struct QueryVersion {
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize, PartialEq, Eq)]
|
||||
pub struct QueryDependency {
|
||||
pub project_id: Option<ProjectId>,
|
||||
pub version_id: Option<VersionId>,
|
||||
pub project_id: Option<DBProjectId>,
|
||||
pub version_id: Option<DBVersionId>,
|
||||
pub file_name: Option<String>,
|
||||
pub dependency_type: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize, PartialEq, Eq)]
|
||||
pub struct QueryFile {
|
||||
pub id: FileId,
|
||||
pub id: DBFileId,
|
||||
pub url: String,
|
||||
pub filename: String,
|
||||
pub hashes: HashMap<String, String>,
|
||||
@@ -959,9 +959,9 @@ pub struct QueryFile {
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
pub struct SingleFile {
|
||||
pub id: FileId,
|
||||
pub version_id: VersionId,
|
||||
pub project_id: ProjectId,
|
||||
pub id: DBFileId,
|
||||
pub version_id: DBVersionId,
|
||||
pub project_id: DBProjectId,
|
||||
pub url: String,
|
||||
pub filename: String,
|
||||
pub hashes: HashMap<String, String>,
|
||||
@@ -1037,11 +1037,11 @@ mod tests {
|
||||
date_published: DateTime<Utc>,
|
||||
) -> Version {
|
||||
Version {
|
||||
id: VersionId(id),
|
||||
id: DBVersionId(id),
|
||||
ordering,
|
||||
date_published,
|
||||
project_id: ProjectId(0),
|
||||
author_id: UserId(0),
|
||||
project_id: DBProjectId(0),
|
||||
author_id: DBUserId(0),
|
||||
name: Default::default(),
|
||||
version_number: Default::default(),
|
||||
changelog: Default::default(),
|
||||
|
||||
Reference in New Issue
Block a user