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 all 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
2 changes: 1 addition & 1 deletion sql/get_total_package_count.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
select
count(1)
from
packages;
non_retired_packages;
10 changes: 5 additions & 5 deletions sql/search_packages.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ select
, links
, updated_in_hex_at
from
packages
non_retired_packages p
where
(
$1 = ''
or rowid in (
or id in (
select rowid
from packages_fts
where packages_fts match $1
Expand All @@ -19,10 +19,10 @@ where
and not exists (
select 1
from hidden_packages
where hidden_packages.name = packages.name
where hidden_packages.name = p.name
)
group by
packages.id
p.id
order by
packages.updated_in_hex_at desc
p.updated_in_hex_at desc
limit 1000;
124 changes: 62 additions & 62 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,16 @@ 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
count(1)
from
releases
where
package_id = $1
order by
inserted_in_hex_at desc
limit 5;
non_retired_packages;
"
sqlight.query(query, db, arguments, decoder)
|> result.map_error(error.DatabaseError)
Expand Down Expand Up @@ -65,19 +60,24 @@ 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;
"select
name
, description
, docs_url
, links
, inserted_in_hex_at
, updated_in_hex_at
from
packages
where
id = $1
limit 1;
"
sqlight.query(query, db, arguments, decoder)
|> result.map_error(error.DatabaseError)
Expand Down Expand Up @@ -106,24 +106,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 +139,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 +196,40 @@ 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;
id
, name
, description
, docs_url
, links
, updated_in_hex_at
from
non_retired_packages p
where
(
$1 = ''
or id in (
select rowid
from packages_fts
where packages_fts match $1
)
)
and not exists (
select 1
from hidden_packages
where hidden_packages.name = p.name
)
group by
p.id
order by
p.updated_in_hex_at desc
limit 1000;
"
sqlight.query(query, db, arguments, decoder)
|> result.map_error(error.DatabaseError)
Expand Down
12 changes: 11 additions & 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 Expand Up @@ -141,6 +141,16 @@ create table if not exists hidden_packages (
name text primary key
) strict;

create view if not exists non_retired_packages as
-- A package is retired if all its releases are retired
select p.*
from packages p
where p.id in (
select distinct r.package_id
from releases r
where r.id is null or r.retirement_reason is null
);

-- These packages are placeholders or otherwise not useful.
insert into hidden_packages values
-- Test packages.
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