-
Notifications
You must be signed in to change notification settings - Fork 649
Extract crate_downloads
table
#8232
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
Changes from all commits
a6e4588
c128a4b
b62e1f8
fdf1407
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
drop trigger insert_crate_downloads_row on crates; | ||
drop function insert_crate_downloads_row; | ||
drop table crate_downloads; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
-- Create the `crate_downloads` table. | ||
|
||
create table crate_downloads | ||
( | ||
crate_id integer not null | ||
constraint crate_downloads_pk | ||
primary key | ||
constraint crate_downloads_crates_id_fk | ||
references crates | ||
on delete cascade, | ||
downloads bigint default 0 not null | ||
); | ||
|
||
comment on table crate_downloads is 'Number of downloads per crate. This was extracted from the `crates` table for performance reasons.'; | ||
comment on column crate_downloads.crate_id is 'Reference to the crate that this row belongs to.'; | ||
comment on column crate_downloads.downloads is 'The total number of downloads for this crate.'; | ||
|
||
-- Create a trigger to automatically add a row to `crate_downloads` when a new | ||
-- crate is inserted into the `crates` table. | ||
|
||
create or replace function insert_crate_downloads_row() returns trigger as $$ | ||
begin | ||
insert into crate_downloads(crate_id) values (new.id); | ||
return new; | ||
end; | ||
$$ language plpgsql; | ||
|
||
create trigger insert_crate_downloads_row | ||
after insert on crates | ||
for each row | ||
execute function insert_crate_downloads_row(); | ||
|
||
-- The following query can take a couple of seconds so it should be run manually | ||
-- outside of the migration to prevent the server from taking a long time to | ||
-- start up while waiting for the migration to complete. | ||
|
||
-- insert into crate_downloads (crate_id, downloads) | ||
-- select id, downloads | ||
-- from crates; |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we expand the test data for crates and their versions to ensure we are updating the correct one? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess that would generally make sense. Since that was already a problem with the existing code I don't want to conflate that with the purpose of this PR though :) |
Uh oh!
There was an error while loading. Please reload this page.