fix(postgres): Fix sqlx's misinterpretation of Ids, update sqlx (#46)

This commit is contained in:
Aeledfyr
2020-07-30 22:45:22 -05:00
committed by GitHub
parent ff28ea8fa8
commit c05ae6e94c
9 changed files with 539 additions and 135 deletions

View File

@@ -73,28 +73,38 @@ generate_ids!(
);
#[derive(Copy, Clone, Debug, Type)]
#[sqlx(transparent)]
pub struct UserId(pub i64);
#[derive(Copy, Clone, Debug, Type)]
#[sqlx(transparent)]
pub struct TeamId(pub i64);
#[derive(Copy, Clone, Debug, Type)]
#[sqlx(transparent)]
pub struct TeamMemberId(pub i64);
#[derive(Copy, Clone, Debug, Type)]
#[sqlx(transparent)]
pub struct ModId(pub i64);
#[derive(Copy, Clone, Debug, Type)]
#[sqlx(transparent)]
pub struct VersionId(pub i64);
#[derive(Copy, Clone, Debug, Type)]
pub struct ChannelId(pub i64);
#[sqlx(transparent)]
pub struct ChannelId(pub i32);
#[derive(Copy, Clone, Debug, Type)]
#[sqlx(transparent)]
pub struct GameVersionId(pub i32);
#[derive(Copy, Clone, Debug, Type)]
#[sqlx(transparent)]
pub struct LoaderId(pub i32);
#[derive(Copy, Clone, Debug, Type)]
#[sqlx(transparent)]
pub struct CategoryId(pub i32);
#[derive(Copy, Clone, Debug, Type)]
#[sqlx(transparent)]
pub struct FileId(pub i64);
use crate::models::ids;

View File

@@ -40,14 +40,14 @@ impl ModBuilder {
}
for category in self.categories {
sqlx::query(
sqlx::query!(
"
INSERT INTO mod_categories (joining_mod_id, joining_category_id)
INSERT INTO mods_categories (joining_mod_id, joining_category_id)
VALUES ($1, $2)
",
self.mod_id as ModId,
category as CategoryId,
)
.bind(self.mod_id)
.bind(category)
.execute(&mut *transaction)
.await?;
}
@@ -75,7 +75,7 @@ impl Mod {
&self,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), sqlx::error::Error> {
sqlx::query(
sqlx::query!(
"
INSERT INTO mods (
id, team_id, title, description, body_url,
@@ -88,18 +88,18 @@ impl Mod {
$10, $11
)
",
self.id as ModId,
self.team_id as TeamId,
&self.title,
&self.description,
&self.body_url,
self.published,
self.downloads,
self.icon_url.as_ref(),
self.issues_url.as_ref(),
self.source_url.as_ref(),
self.wiki_url.as_ref(),
)
.bind(self.id)
.bind(self.team_id)
.bind(&self.title)
.bind(&self.description)
.bind(&self.body_url)
.bind(self.published)
.bind(self.downloads)
.bind(self.icon_url.as_ref())
.bind(self.issues_url.as_ref())
.bind(self.source_url.as_ref())
.bind(self.wiki_url.as_ref())
.execute(&mut *transaction)
.await?;

View File

@@ -18,13 +18,13 @@ impl TeamBuilder {
let team = Team { id: team_id };
sqlx::query(
sqlx::query!(
"
INSERT INTO teams (id)
VALUES ($1)
",
team.id as TeamId,
)
.bind(team.id)
.execute(&mut *transaction)
.await?;
@@ -38,17 +38,17 @@ impl TeamBuilder {
role: member.role,
};
sqlx::query(
sqlx::query!(
"
INSERT INTO team_members (id, team_id, user_id, name, role)
VALUES ($1, $2)
INSERT INTO team_members (id, team_id, user_id, member_name, role)
VALUES ($1, $2, $3, $4, $5)
",
team_member.id as TeamMemberId,
team_member.team_id as TeamId,
team_member.user_id as UserId,
team_member.name,
team_member.role,
)
.bind(team_member.id)
.bind(team_member.team_id)
.bind(team_member.user_id)
.bind(team_member.name)
.bind(team_member.role)
.execute(&mut *transaction)
.await?;
}

View File

@@ -45,69 +45,69 @@ impl VersionBuilder {
for file in self.files {
let file_id = generate_file_id(&mut *transaction).await?;
sqlx::query(
sqlx::query!(
"
INSERT INTO files (id, version_id, url, filename)
VALUES ($1, $2, $3, $4)
",
file_id as FileId,
self.version_id as VersionId,
file.url,
file.filename,
)
.bind(file_id)
.bind(self.version_id)
.bind(file.url)
.bind(file.filename)
.execute(&mut *transaction)
.await?;
for hash in file.hashes {
sqlx::query(
sqlx::query!(
"
INSERT INTO hashes (file_id, algorithm, hash)
VALUES ($1, $2, $3)
",
file_id as FileId,
hash.algorithm,
hash.hash,
)
.bind(file_id)
.bind(hash.algorithm)
.bind(hash.hash)
.execute(&mut *transaction)
.await?;
}
}
for dependency in self.dependencies {
sqlx::query(
sqlx::query!(
"
INSERT INTO dependencies (dependent_id, dependency_id)
VALUES ($1, $2)
",
self.version_id as VersionId,
dependency as VersionId,
)
.bind(self.version_id)
.bind(dependency)
.execute(&mut *transaction)
.await?;
}
for loader in self.loaders {
sqlx::query(
sqlx::query!(
"
INSERT INTO dependencies (loader_id, version_id)
INSERT INTO loaders_versions (loader_id, version_id)
VALUES ($1, $2)
",
loader as LoaderId,
self.version_id as VersionId,
)
.bind(loader)
.bind(self.version_id)
.execute(&mut *transaction)
.await?;
}
for game_version in self.game_versions {
sqlx::query(
sqlx::query!(
"
INSERT INTO dependencies (game_version_id, joining_version_id)
INSERT INTO game_versions_versions (game_version_id, joining_version_id)
VALUES ($1, $2)
",
game_version as GameVersionId,
self.version_id as VersionId,
)
.bind(game_version)
.bind(self.version_id)
.execute(&mut *transaction)
.await?;
}
@@ -132,7 +132,7 @@ impl Version {
&self,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), sqlx::error::Error> {
sqlx::query(
sqlx::query!(
"
INSERT INTO versions (
id, mod_id, name, version_number,
@@ -145,15 +145,15 @@ impl Version {
$7, $8
)
",
self.id as VersionId,
self.mod_id as ModId,
&self.name,
&self.version_number,
self.changelog_url.as_ref(),
self.date_published,
self.downloads,
self.release_channel as ChannelId,
)
.bind(self.id)
.bind(self.mod_id)
.bind(&self.name)
.bind(&self.version_number)
.bind(self.changelog_url.as_ref())
.bind(self.date_published)
.bind(self.downloads)
.bind(self.release_channel)
.execute(&mut *transaction)
.await?;
@@ -166,16 +166,15 @@ impl Version {
{
use futures::stream::TryStreamExt;
let vec = sqlx::query_as::<_, (VersionId,)>(
let vec = sqlx::query!(
"
SELECT id FROM versions v
INNER JOIN dependencies d ON d.dependency_id = v.id
WHERE d.dependent_id = $1
SELECT dependency_id id FROM dependencies
WHERE dependent_id = $1
",
self.id as VersionId,
)
.bind(self.id)
.fetch_many(exec)
.try_filter_map(|e| async { Ok(e.right().map(|(v,)| v)) })
.try_filter_map(|e| async { Ok(e.right().map(|v| VersionId(v.id))) })
.try_collect::<Vec<VersionId>>()
.await?;