File tree Expand file tree Collapse file tree 2 files changed +19
-0
lines changed
migrations/2024-03-04-092044_crate-downloads-table Expand file tree Collapse file tree 2 files changed +19
-0
lines changed Original file line number Diff line number Diff line change
1
+ drop trigger insert_crate_downloads_row on crates;
2
+ drop function insert_crate_downloads_row;
1
3
drop table crate_downloads;
Original file line number Diff line number Diff line change
1
+ -- Create the `crate_downloads` table.
2
+
1
3
create table crate_downloads
2
4
(
3
5
crate_id integer not null
@@ -13,6 +15,21 @@ comment on table crate_downloads is 'Number of downloads per crate. This was ext
13
15
comment on column crate_downloads.crate_id is ' Reference to the crate that this row belongs to.' ;
14
16
comment on column crate_downloads.downloads is ' The total number of downloads for this crate.' ;
15
17
18
+ -- Create a trigger to automatically add a row to `crate_downloads` when a new
19
+ -- crate is inserted into the `crates` table.
20
+
21
+ create or replace function insert_crate_downloads_row () returns trigger as $$
22
+ begin
23
+ insert into crate_downloads(crate_id) values (new .id );
24
+ return new;
25
+ end;
26
+ $$ language plpgsql;
27
+
28
+ create trigger insert_crate_downloads_row
29
+ after insert on crates
30
+ for each row
31
+ execute function insert_crate_downloads_row();
32
+
16
33
-- The following query can take a couple of seconds so it should be run manually
17
34
-- outside of the migration to prevent the server from taking a long time to
18
35
-- start up while waiting for the migration to complete.
You can’t perform that action at this time.
0 commit comments