Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
initial links push
Browse files Browse the repository at this point in the history
  • Loading branch information
thesuzerain committed Nov 26, 2023
1 parent 172b93d commit d4c9f84
Show file tree
Hide file tree
Showing 17 changed files with 779 additions and 541 deletions.
76 changes: 76 additions & 0 deletions migrations/20231117073600_links_overhaul.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
CREATE TABLE link_platforms (
id SERIAL PRIMARY KEY,
name VARCHAR(16) UNIQUE NOT NULL,

-- Used for v2 conversion
donation BOOLEAN NOT NULL DEFAULT false,
-- Will be removed at the end of the migration
donation_platform_id INTEGER REFERENCES donation_platforms (id)
);

INSERT INTO link_platforms (donation_platform_id, name, donation)
SELECT id, short, true FROM donation_platforms;

INSERT INTO link_platforms (name, donation) VALUES ('issues', false);
INSERT INTO link_platforms (name, donation) VALUES ('wiki', false);
INSERT INTO link_platforms (name, donation) VALUES ('discord', false);
INSERT INTO link_platforms (name, donation) VALUES ('source', false);
INSERT INTO link_platforms (name, donation) VALUES ('site', false);

CREATE TABLE mods_links (
id SERIAL PRIMARY KEY,
joining_mod_id BIGINT NOT NULL REFERENCES mods (id),
joining_platform_id INTEGER NOT NULL REFERENCES link_platforms (id),
url VARCHAR(255) NOT NULL
);

CREATE TABLE organization_links (
id SERIAL PRIMARY KEY,
joining_organization_id BIGINT NOT NULL REFERENCES organizations (id),
joining_platform_id INTEGER NOT NULL REFERENCES link_platforms (id),
url VARCHAR(255) NOT NULL
);

INSERT INTO mods_links (joining_mod_id, joining_platform_id, url)
SELECT m.id, lp.id, md.url
FROM mods m
LEFT JOIN mods_donations md ON m.id = md.joining_mod_id
LEFT JOIN donation_platforms dp ON dp.id = md.joining_platform_id
LEFT JOIN link_platforms lp ON lp.donation_platform_id = dp.id;

INSERT INTO mods_links (joining_mod_id, joining_platform_id, url)
SELECT m.id, lp.id, issues_url
FROM mods m
CROSS JOIN link_platforms lp
WHERE issues_url IS NOT NULL
AND lp.name = 'issues';

INSERT INTO mods_links (joining_mod_id, joining_platform_id, url)
SELECT m.id, lp.id, wiki_url
FROM mods m
CROSS JOIN link_platforms lp
WHERE wiki_url IS NOT NULL
AND lp.name = 'wiki';

INSERT INTO mods_links (joining_mod_id, joining_platform_id, url)
SELECT m.id, lp.id, discord_url
FROM mods m
CROSS JOIN link_platforms lp
WHERE discord_url IS NOT NULL
AND lp.name = 'discord';

INSERT INTO mods_links (joining_mod_id, joining_platform_id, url)
SELECT m.id, lp.id, source_url
FROM mods m
CROSS JOIN link_platforms lp
WHERE source_url IS NOT NULL
AND lp.name = 'source';

ALTER TABLE mods DROP COLUMN issues_url;
ALTER TABLE mods DROP COLUMN wiki_url;
ALTER TABLE mods DROP COLUMN discord_url;
ALTER TABLE mods DROP COLUMN source_url;

ALTER TABLE link_platforms DROP COLUMN donation_platform_id;
DROP TABLE mods_donations;
DROP TABLE donation_platforms;
34 changes: 17 additions & 17 deletions src/database/models/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ pub struct ReportType {
}

#[derive(Serialize, Deserialize)]
pub struct DonationPlatform {
pub id: DonationPlatformId,
pub short: String,
pub struct LinkPlatform {
pub id: LinkPlatformId,
pub name: String,
pub donation: bool,
}

impl Category {
Expand Down Expand Up @@ -129,38 +129,38 @@ impl Category {
}
}

impl DonationPlatform {
impl LinkPlatform {
pub async fn get_id<'a, E>(
id: &str,
exec: E,
) -> Result<Option<DonationPlatformId>, DatabaseError>
) -> Result<Option<LinkPlatformId>, DatabaseError>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
let result = sqlx::query!(
"
SELECT id FROM donation_platforms
WHERE short = $1
SELECT id FROM link_platforms
WHERE name = $1
",
id
)
.fetch_optional(exec)
.await?;

Ok(result.map(|r| DonationPlatformId(r.id)))
Ok(result.map(|r| LinkPlatformId(r.id)))
}

pub async fn list<'a, E>(
exec: E,
redis: &RedisPool,
) -> Result<Vec<DonationPlatform>, DatabaseError>
) -> Result<Vec<LinkPlatform>, DatabaseError>
where
E: sqlx::Executor<'a, Database = sqlx::Postgres>,
{
let mut redis = redis.connect().await?;

let res: Option<Vec<DonationPlatform>> = redis
.get_deserialized_from_json(TAGS_NAMESPACE, "donation_platform")
let res: Option<Vec<LinkPlatform>> = redis
.get_deserialized_from_json(TAGS_NAMESPACE, "link_platform")
.await?;

if let Some(res) = res {
Expand All @@ -169,22 +169,22 @@ impl DonationPlatform {

let result = sqlx::query!(
"
SELECT id, short, name FROM donation_platforms
SELECT id, name, donation FROM link_platforms
"
)
.fetch_many(exec)
.try_filter_map(|e| async {
Ok(e.right().map(|c| DonationPlatform {
id: DonationPlatformId(c.id),
short: c.short,
Ok(e.right().map(|c| LinkPlatform {
id: LinkPlatformId(c.id),
name: c.name,
donation: c.donation,
}))
})
.try_collect::<Vec<DonationPlatform>>()
.try_collect::<Vec<LinkPlatform>>()
.await?;

redis
.set_serialized_to_json(TAGS_NAMESPACE, "donation_platform", &result, None)
.set_serialized_to_json(TAGS_NAMESPACE, "link_platform", &result, None)
.await?;

Ok(result)
Expand Down
4 changes: 2 additions & 2 deletions src/database/models/ids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,9 @@ pub struct SideTypeId(pub i32);
#[derive(Copy, Clone, Debug, Type, Serialize, Deserialize)]
#[sqlx(transparent)]
pub struct GameId(pub i32);
#[derive(Copy, Clone, Debug, Type, Serialize, Deserialize)]
#[derive(Copy, Clone, Debug, Type, Serialize, Deserialize, PartialEq, Eq, Hash)]
#[sqlx(transparent)]
pub struct DonationPlatformId(pub i32);
pub struct LinkPlatformId(pub i32);

#[derive(Copy, Clone, Debug, Type, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[sqlx(transparent)]
Expand Down
70 changes: 24 additions & 46 deletions src/database/models/project_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ pub const PROJECTS_SLUGS_NAMESPACE: &str = "projects_slugs";
const PROJECTS_DEPENDENCIES_NAMESPACE: &str = "projects_dependencies";

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DonationUrl {
pub platform_id: DonationPlatformId,
pub platform_short: String,
pub struct LinkUrl {
pub platform_id: LinkPlatformId,
pub platform_name: String,
pub url: String,
pub donation: bool, // Is this a donation link
}

impl DonationUrl {
impl LinkUrl {
pub async fn insert_many_projects(
donation_urls: Vec<Self>,
links: Vec<Self>,
project_id: ProjectId,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), sqlx::error::Error> {
let (project_ids, platform_ids, urls): (Vec<_>, Vec<_>, Vec<_>) = donation_urls
let (project_ids, platform_ids, urls): (Vec<_>, Vec<_>, Vec<_>) = links
.into_iter()
.map(|url| (project_id.0, url.platform_id.0, url.url))
.multiunzip();
sqlx::query!(
"
INSERT INTO mods_donations (
INSERT INTO mods_links (
joining_mod_id, joining_platform_id, url
)
SELECT * FROM UNNEST($1::bigint[], $2::int[], $3::varchar[])
Expand Down Expand Up @@ -147,19 +147,15 @@ pub struct ProjectBuilder {
pub description: String,
pub body: String,
pub icon_url: Option<String>,
pub issues_url: Option<String>,
pub source_url: Option<String>,
pub wiki_url: Option<String>,
pub license_url: Option<String>,
pub discord_url: Option<String>,
pub categories: Vec<CategoryId>,
pub additional_categories: Vec<CategoryId>,
pub initial_versions: Vec<super::version_item::VersionBuilder>,
pub status: ProjectStatus,
pub requested_status: Option<ProjectStatus>,
pub license: String,
pub slug: Option<String>,
pub donation_urls: Vec<DonationUrl>,
pub link_urls: Vec<LinkUrl>,
pub gallery_items: Vec<GalleryItem>,
pub color: Option<u32>,
pub monetization_status: MonetizationStatus,
Expand Down Expand Up @@ -191,11 +187,7 @@ impl ProjectBuilder {
downloads: 0,
follows: 0,
icon_url: self.icon_url,
issues_url: self.issues_url,
source_url: self.source_url,
wiki_url: self.wiki_url,
license_url: self.license_url,
discord_url: self.discord_url,
license: self.license,
slug: self.slug,
moderation_message: None,
Expand All @@ -208,7 +200,7 @@ impl ProjectBuilder {
project_struct.insert(&mut *transaction).await?;

let ProjectBuilder {
donation_urls,
link_urls,
gallery_items,
categories,
additional_categories,
Expand All @@ -220,7 +212,7 @@ impl ProjectBuilder {
version.insert(&mut *transaction).await?;
}

DonationUrl::insert_many_projects(donation_urls, self.project_id, &mut *transaction)
LinkUrl::insert_many_projects(link_urls, self.project_id, &mut *transaction)
.await?;

GalleryItem::insert_many(gallery_items, self.project_id, &mut *transaction).await?;
Expand Down Expand Up @@ -258,11 +250,7 @@ pub struct Project {
pub downloads: i32,
pub follows: i32,
pub icon_url: Option<String>,
pub issues_url: Option<String>,
pub source_url: Option<String>,
pub wiki_url: Option<String>,
pub license_url: Option<String>,
pub discord_url: Option<String>,
pub license: String,
pub slug: Option<String>,
pub moderation_message: Option<String>,
Expand All @@ -282,17 +270,15 @@ impl Project {
"
INSERT INTO mods (
id, team_id, title, description, body,
published, downloads, icon_url, issues_url,
source_url, wiki_url, status, requested_status, discord_url,
published, downloads, icon_url, status, requested_status,
license_url, license,
slug, color, monetization_status
)
VALUES (
$1, $2, $3, $4, $5,
$6, $7, $8, $9,
$10, $11, $12, $13, $14,
$15, $16,
LOWER($17), $18, $19
$1, $2, $3, $4, $5, $6,
$7, $8, $9, $10,
$11, $12,
LOWER($13), $14, $15
)
",
self.id as ProjectId,
Expand All @@ -303,12 +289,8 @@ impl Project {
self.published,
self.downloads,
self.icon_url.as_ref(),
self.issues_url.as_ref(),
self.source_url.as_ref(),
self.wiki_url.as_ref(),
self.status.as_str(),
self.requested_status.map(|x| x.as_str()),
self.discord_url.as_ref(),
self.license_url.as_ref(),
&self.license,
self.slug.as_ref(),
Expand Down Expand Up @@ -383,7 +365,7 @@ impl Project {

sqlx::query!(
"
DELETE FROM mods_donations
DELETE FROM mods_links
WHERE joining_mod_id = $1
",
id as ProjectId,
Expand Down Expand Up @@ -570,7 +552,7 @@ impl Project {
SELECT m.id id, m.title title, m.description description, m.downloads downloads, m.follows follows,
m.icon_url icon_url, m.body body, m.published published,
m.updated updated, m.approved approved, m.queued, m.status status, m.requested_status requested_status,
m.issues_url issues_url, m.source_url source_url, m.wiki_url wiki_url, m.discord_url discord_url, m.license_url license_url,
m.license_url license_url,
m.team_id team_id, m.organization_id organization_id, m.license license, m.slug slug, m.moderation_message moderation_message, m.moderation_message_body moderation_message_body,
m.webhook_sent, m.color,
t.id thread_id, m.monetization_status monetization_status,
Expand All @@ -581,12 +563,12 @@ impl Project {
ARRAY_AGG(DISTINCT c.category) filter (where c.category is not null and mc.is_additional is true) additional_categories,
JSONB_AGG(DISTINCT jsonb_build_object('id', v.id, 'date_published', v.date_published)) filter (where v.id is not null) versions,
JSONB_AGG(DISTINCT jsonb_build_object('image_url', mg.image_url, 'featured', mg.featured, 'title', mg.title, 'description', mg.description, 'created', mg.created, 'ordering', mg.ordering)) filter (where mg.image_url is not null) gallery,
JSONB_AGG(DISTINCT jsonb_build_object('platform_id', md.joining_platform_id, 'platform_short', dp.short, 'platform_name', dp.name,'url', md.url)) filter (where md.joining_platform_id is not null) donations
JSONB_AGG(DISTINCT jsonb_build_object('platform_id', ml.joining_platform_id, 'platform_name', lp.name,'url', ml.url, 'donation', lp.donation)) filter (where ml.joining_platform_id is not null) links
FROM mods m
INNER JOIN threads t ON t.mod_id = m.id
LEFT JOIN mods_gallery mg ON mg.mod_id = m.id
LEFT JOIN mods_donations md ON md.joining_mod_id = m.id
LEFT JOIN donation_platforms dp ON md.joining_platform_id = dp.id
LEFT JOIN mods_links ml ON ml.joining_mod_id = m.id
LEFT JOIN link_platforms lp ON ml.joining_platform_id = lp.id
LEFT JOIN mods_categories mc ON mc.joining_mod_id = m.id
LEFT JOIN categories c ON mc.joining_category_id = c.id
LEFT JOIN versions v ON v.mod_id = m.id AND v.status = ANY($3)
Expand Down Expand Up @@ -619,11 +601,7 @@ impl Project {
icon_url: m.icon_url.clone(),
published: m.published,
updated: m.updated,
issues_url: m.issues_url.clone(),
source_url: m.source_url.clone(),
wiki_url: m.wiki_url.clone(),
license_url: m.license_url.clone(),
discord_url: m.discord_url.clone(),
status: ProjectStatus::from_string(
&m.status,
),
Expand Down Expand Up @@ -674,9 +652,9 @@ impl Project {

gallery
},
donation_urls: serde_json::from_value(
m.donations.unwrap_or_default(),
).ok().unwrap_or_default(),
urls: serde_json::from_value(
m.links.unwrap_or_default(),
).unwrap_or_default(),
thread_id: ThreadId(m.thread_id),
}}))
})
Expand Down Expand Up @@ -793,7 +771,7 @@ pub struct QueryProject {
pub versions: Vec<VersionId>,
pub project_types: Vec<String>,
pub games: Vec<String>,
pub donation_urls: Vec<DonationUrl>,
pub urls: Vec<LinkUrl>,
pub gallery_items: Vec<GalleryItem>,
pub thread_id: ThreadId,
}
Loading

0 comments on commit d4c9f84

Please sign in to comment.