Skip to content

Commit c1bb604

Browse files
committed
Include downloads
1 parent 5f1a179 commit c1bb604

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

sql/upsert_package.sql

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ insert into packages
66
, links
77
, inserted_in_hex_at
88
, updated_in_hex_at
9+
, downloads_all
10+
, downloads_recent
11+
, downloads_week
12+
, downloads_day
913
)
1014
values
1115
( $1
@@ -14,12 +18,20 @@ values
1418
, $4
1519
, $5
1620
, $6
21+
, $7
22+
, $8
23+
, $9
24+
, $10
1725
)
1826
on conflict (name) do update
1927
set
2028
updated_in_hex_at = excluded.updated_in_hex_at
2129
, description = excluded.description
2230
, docs_url = excluded.docs_url
2331
, links = excluded.links
32+
, downloads_all = excluded.downloads_all
33+
, downloads_recent = excluded.downloads_recent
34+
, downloads_week = excluded.downloads_week
35+
, downloads_day = excluded.downloads_day
2436
returning
2537
id

src/packages/generated/sql.gleam

+14-2
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,8 @@ from
177177
where
178178
(
179179
$1 = ''
180-
or instr(lower(name), $2) > 0
181-
or instr(lower(description), $2) > 0
180+
or instr(lower(name), lower($2)) > 0
181+
or instr(lower(description), lower($2)) > 0
182182
or id in (
183183
select rowid
184184
from packages_fts
@@ -250,6 +250,10 @@ insert into packages
250250
, links
251251
, inserted_in_hex_at
252252
, updated_in_hex_at
253+
, downloads_all
254+
, downloads_recent
255+
, downloads_week
256+
, downloads_day
253257
)
254258
values
255259
( $1
@@ -258,13 +262,21 @@ values
258262
, $4
259263
, $5
260264
, $6
265+
, $7
266+
, $8
267+
, $9
268+
, $10
261269
)
262270
on conflict (name) do update
263271
set
264272
updated_in_hex_at = excluded.updated_in_hex_at
265273
, description = excluded.description
266274
, docs_url = excluded.docs_url
267275
, links = excluded.links
276+
, downloads_all = excluded.downloads_all
277+
, downloads_recent = excluded.downloads_recent
278+
, downloads_week = excluded.downloads_week
279+
, downloads_day = excluded.downloads_day
268280
returning
269281
id
270282
"

src/packages/index.gleam

+19
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ create table if not exists packages (
7070
inserted_in_hex_at integer not null,
7171
updated_in_hex_at integer not null,
7272
docs_url text,
73+
downloads_all integer not null default 0,
74+
downloads_recent integer not null default 0,
75+
downloads_week integer not null default 0,
76+
downloads_day integer not null default 0,
7377
7478
links text not null default '{}'
7579
check (json(links) not null)
@@ -187,6 +191,17 @@ on conflict do nothing;
187191
pub fn connect(database: String) -> Connection {
188192
let assert Ok(db) = sqlight.open(database)
189193
let assert Ok(_) = sqlight.exec(schema, db)
194+
195+
let _ =
196+
sqlight.exec(
197+
"
198+
alter table packages add downloads_all integer not null default 0;
199+
alter table packages add downloads_recent integer not null default 0;
200+
alter table packages add downloads_week integer not null default 0;
201+
alter table packages add downloads_day integer not null default 0;
202+
",
203+
db,
204+
)
190205
Connection(db)
191206
}
192207

@@ -248,6 +263,10 @@ pub fn upsert_package(
248263
sqlight.text(links_json),
249264
sqlight.int(birl.to_unix(package.inserted_at)),
250265
sqlight.int(birl.to_unix(package.updated_at)),
266+
sqlight.int(package.downloads |> dict.get("all") |> result.unwrap(0)),
267+
sqlight.int(package.downloads |> dict.get("recent") |> result.unwrap(0)),
268+
sqlight.int(package.downloads |> dict.get("week") |> result.unwrap(0)),
269+
sqlight.int(package.downloads |> dict.get("day") |> result.unwrap(0)),
251270
]
252271
let decoder = dyn.element(0, dyn.int)
253272
use returned <- result.then(sql.upsert_package(db.inner, parameters, decoder))

0 commit comments

Comments
 (0)