Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide retired packages #24

Merged
merged 13 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions sql/get_package.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
with
retired_package_ids as (
-- A package is retired if its latest release is retired
select package_id from (
select
package_id
, retirement_reason
from
releases
group by
package_id
having
max(inserted_in_hex_at)
)
where
retirement_reason is not null
)
ffigiel marked this conversation as resolved.
Show resolved Hide resolved
select
name
, description
Expand All @@ -9,4 +26,5 @@ from
packages
where
id = $1
and id not in retired_package_ids
limit 1;
21 changes: 20 additions & 1 deletion sql/get_total_package_count.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
with
retired_package_ids as (
-- A package is retired if its latest release is retired
select package_id from (
select
package_id
, retirement_reason
from
releases
group by
package_id
having
max(inserted_in_hex_at)
)
where
retirement_reason is not null
)
select
count(1)
from
packages;
packages
where
id not in retired_package_ids;
18 changes: 18 additions & 0 deletions sql/search_packages.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
with
retired_package_ids as (
-- A package is retired if its latest release is retired
select package_id from (
select
package_id
, retirement_reason
from
releases
group by
package_id
having
max(inserted_in_hex_at)
)
where
retirement_reason is not null
)
select
id
, name
Expand All @@ -21,6 +38,7 @@ where
from hidden_packages
where hidden_packages.name = packages.name
)
and id not in retired_package_ids
group by
packages.id
order by
Expand Down
181 changes: 118 additions & 63 deletions src/packages/generated/sql.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// THIS FILE IS GENERATED. DO NOT EDIT.
// THIS FILE IS GENERATED. DO NOT EDIT.
// Regenerate with `gleam run -m codegen`

import sqlight
Expand All @@ -9,21 +9,35 @@ import packages/error.{type Error}
pub type QueryResult(t) =
Result(List(t), Error)

pub fn get_most_recent_releases(
pub fn get_total_package_count(
db: sqlight.Connection,
arguments: List(sqlight.Value),
decoder: dynamic.Decoder(a),
) -> QueryResult(a) {
let query =
"select
version
"with
retired_package_ids as (
-- A package is retired if its latest release is retired
select package_id from (
select
package_id
, retirement_reason
from
releases
group by
package_id
having
max(inserted_in_hex_at)
)
where
retirement_reason is not null
)
select
count(1)
from
releases
packages
where
package_id = $1
order by
inserted_in_hex_at desc
limit 5;
id not in retired_package_ids;
"
sqlight.query(query, db, arguments, decoder)
|> result.map_error(error.DatabaseError)
Expand Down Expand Up @@ -65,19 +79,42 @@ returning
|> result.map_error(error.DatabaseError)
}

pub fn upsert_most_recent_hex_timestamp(
pub fn get_package(
db: sqlight.Connection,
arguments: List(sqlight.Value),
decoder: dynamic.Decoder(a),
) -> QueryResult(a) {
let query =
"insert into most_recent_hex_timestamp
(id, unix_timestamp)
values
(1, $1)
on conflict (id) do update
set
unix_timestamp = $1;
"with
retired_package_ids as (
-- A package is retired if its latest release is retired
select package_id from (
select
package_id
, retirement_reason
from
releases
group by
package_id
having
max(inserted_in_hex_at)
)
where
retirement_reason is not null
)
select
name
, description
, docs_url
, links
, inserted_in_hex_at
, updated_in_hex_at
from
packages
where
id = $1
and id not in retired_package_ids
limit 1;
"
sqlight.query(query, db, arguments, decoder)
|> result.map_error(error.DatabaseError)
Expand Down Expand Up @@ -106,24 +143,19 @@ returning
|> result.map_error(error.DatabaseError)
}

pub fn get_package(
pub fn upsert_most_recent_hex_timestamp(
db: sqlight.Connection,
arguments: List(sqlight.Value),
decoder: dynamic.Decoder(a),
) -> QueryResult(a) {
let query =
"select
name
, description
, docs_url
, links
, inserted_in_hex_at
, updated_in_hex_at
from
packages
where
id = $1
limit 1;
"insert into most_recent_hex_timestamp
(id, unix_timestamp)
values
(1, $1)
on conflict (id) do update
set
unix_timestamp = $1;
"
sqlight.query(query, db, arguments, decoder)
|> result.map_error(error.DatabaseError)
Expand All @@ -144,55 +176,35 @@ limit 1
|> result.map_error(error.DatabaseError)
}

pub fn get_total_package_count(
pub fn json_dump(
db: sqlight.Connection,
arguments: List(sqlight.Value),
decoder: dynamic.Decoder(a),
) -> QueryResult(a) {
let query =
"select
count(1)
from
packages;
json_agg(row_to_json(packages))
from packages;
"
sqlight.query(query, db, arguments, decoder)
|> result.map_error(error.DatabaseError)
}

pub fn search_packages(
pub fn get_most_recent_releases(
db: sqlight.Connection,
arguments: List(sqlight.Value),
decoder: dynamic.Decoder(a),
) -> QueryResult(a) {
let query =
"select
id
, name
, description
, docs_url
, links
, updated_in_hex_at
version
from
packages
releases
where
(
$1 = ''
or rowid in (
select rowid
from packages_fts
where packages_fts match $1
)
)
and not exists (
select 1
from hidden_packages
where hidden_packages.name = packages.name
)
group by
packages.id
package_id = $1
order by
packages.updated_in_hex_at desc
limit 1000;
inserted_in_hex_at desc
limit 5;
"
sqlight.query(query, db, arguments, decoder)
|> result.map_error(error.DatabaseError)
Expand Down Expand Up @@ -221,15 +233,58 @@ limit 1;
|> result.map_error(error.DatabaseError)
}

pub fn json_dump(
pub fn search_packages(
db: sqlight.Connection,
arguments: List(sqlight.Value),
decoder: dynamic.Decoder(a),
) -> QueryResult(a) {
let query =
"select
json_agg(row_to_json(packages))
from packages;
"with
retired_package_ids as (
-- A package is retired if its latest release is retired
select package_id from (
select
package_id
, retirement_reason
from
releases
group by
package_id
having
max(inserted_in_hex_at)
)
where
retirement_reason is not null
)
select
id
, name
, description
, docs_url
, links
, updated_in_hex_at
from
packages
where
(
$1 = ''
or rowid in (
select rowid
from packages_fts
where packages_fts match $1
)
)
and not exists (
select 1
from hidden_packages
where hidden_packages.name = packages.name
)
and id not in retired_package_ids
group by
packages.id
order by
packages.updated_in_hex_at desc
limit 1000;
"
sqlight.query(query, db, arguments, decoder)
|> result.map_error(error.DatabaseError)
Expand Down
2 changes: 1 addition & 1 deletion src/packages/index.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ create table if not exists most_recent_hex_timestamp (
-- we use a constraint to enforce that the id is always the value `1` so
-- now this table can only hold one row.
check (id == 1),

unix_timestamp integer not null
) strict;

Expand Down
4 changes: 2 additions & 2 deletions test/codegen.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn main() {
let Nil = generate_sql_queries_module()
}

const module_header = "// THIS FILE IS GENERATED. DO NOT EDIT.
const module_header = "// THIS FILE IS GENERATED. DO NOT EDIT.
// Regenerate with `gleam run -m codegen`"

fn generate_sql_queries_module() -> Nil {
Expand All @@ -17,7 +17,7 @@ fn generate_sql_queries_module() -> Nil {

let imports = [
"import sqlight", "import gleam/result", "import gleam/dynamic",
"import packages/error.{Error}",
"import packages/error.{type Error}",
]
let module =
string.join(
Expand Down
Loading
Loading