Skip to content

Commit 5583ba3

Browse files
committed
Swap version_downloads with the partitioned table
Note that as of this commit, the DB dump is broken, as it can't handle partitioned tables. We're keeping the old table around in case we need to revert this, though for the time being I'm just assuming we'll either manually reconcile data or accept some loss of download counts if we need to revert. For some reason Diesel CLI isn't picking up the foreign key on the partitioned table, so I regenerated the patch file Deployment Instructions ======================= This commit contains a destructive migration, and should be deployed on its own
1 parent 0f78545 commit 5583ba3

File tree

7 files changed

+34
-119
lines changed

7 files changed

+34
-119
lines changed

diesel.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ patch_file = "src/schema.patch"
88

99
[print_schema.filter]
1010
except_tables = [
11+
"version_downloads_archive",
1112
"version_downloads_default",
1213
"version_downloads_pre_2017",
1314
"version_downloads_2017",
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DROP MATERIALIZED VIEW recent_crate_downloads;
2+
ALTER TABLE version_downloads RENAME TO version_downloads_part;
3+
ALTER TABLE version_downloads_archive RENAME TO version_downloads;
4+
CREATE MATERIALIZED VIEW recent_crate_downloads (crate_id, downloads) AS
5+
SELECT crate_id, SUM(version_downloads.downloads) FROM version_downloads
6+
INNER JOIN versions
7+
ON version_downloads.version_id = versions.id
8+
WHERE version_downloads.date > date(CURRENT_TIMESTAMP - INTERVAL '90 days')
9+
GROUP BY crate_id;
10+
CREATE UNIQUE INDEX recent_crate_downloads_crate_id ON recent_crate_downloads (crate_id);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DROP MATERIALIZED VIEW recent_crate_downloads;
2+
ALTER TABLE version_downloads RENAME TO version_downloads_archive;
3+
ALTER TABLE version_downloads_part RENAME TO version_downloads;
4+
CREATE MATERIALIZED VIEW recent_crate_downloads (crate_id, downloads) AS
5+
SELECT crate_id, SUM(version_downloads.downloads) FROM version_downloads
6+
INNER JOIN versions
7+
ON version_downloads.version_id = versions.id
8+
WHERE version_downloads.date > date(CURRENT_TIMESTAMP - INTERVAL '90 days')
9+
GROUP BY crate_id;
10+
CREATE UNIQUE INDEX recent_crate_downloads_crate_id ON recent_crate_downloads (crate_id);

src/bin/backfill_version_downloads.rs

Lines changed: 0 additions & 49 deletions
This file was deleted.

src/schema.patch

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
diff --git a/src/schema.rs b/src/schema.rs
2-
index df884e4..18e08cd 100644
2+
index 9d6ab43..a81161a 100644
33
--- a/src/schema.rs
44
+++ b/src/schema.rs
5-
@@ -1,5 +1,7 @@
5+
@@ -1,3 +1,5 @@
66
+#![allow(unused_imports)]
77
+
88
table! {
99
use diesel::sql_types::*;
1010
use diesel_full_text_search::{TsVector as Tsvector};
11-
12-
/// Representation of the `api_tokens` table.
13-
@@ -125,14 +125,8 @@ table! {
14-
/// Its SQL type is `Timestamp`.
11+
@@ -171,12 +173,6 @@ table! {
1512
///
1613
/// (Automatically generated by Diesel.)
1714
created_at -> Timestamp,
@@ -23,12 +20,8 @@ index df884e4..18e08cd 100644
2320
- path -> Ltree,
2421
}
2522
}
26-
27-
table! {
28-
@@ -608,11 +610,29 @@ table! {
29-
/// (Automatically generated by Diesel.)
30-
rendered_at -> Timestamp,
31-
}
23+
24+
@@ -678,6 +674,24 @@ table! {
3225
}
3326

3427
table! {
@@ -53,11 +46,7 @@ index df884e4..18e08cd 100644
5346
use diesel::sql_types::*;
5447
use diesel_full_text_search::{TsVector as Tsvector};
5548

56-
/// Representation of the `reserved_crate_names` table.
57-
///
58-
@@ -881,23 +901,25 @@ table! {
59-
60-
joinable!(api_tokens -> users (user_id));
49+
@@ -1003,7 +1017,8 @@ joinable!(api_tokens -> users (user_id));
6150
joinable!(badges -> crates (crate_id));
6251
joinable!(crate_owner_invitations -> crates (crate_id));
6352
joinable!(crate_owners -> crates (crate_id));
@@ -67,33 +56,22 @@ index df884e4..18e08cd 100644
6756
joinable!(crates_categories -> categories (category_id));
6857
joinable!(crates_categories -> crates (crate_id));
6958
joinable!(crates_keywords -> crates (crate_id));
70-
joinable!(crates_keywords -> keywords (keyword_id));
71-
joinable!(dependencies -> crates (crate_id));
72-
joinable!(dependencies -> versions (version_id));
73-
joinable!(emails -> users (user_id));
74-
joinable!(follows -> crates (crate_id));
75-
joinable!(follows -> users (user_id));
59+
@@ -1016,8 +1031,10 @@ joinable!(follows -> users (user_id));
7660
joinable!(publish_limit_buckets -> users (user_id));
7761
joinable!(publish_rate_overrides -> users (user_id));
7862
joinable!(readme_renderings -> versions (version_id));
7963
+joinable!(recent_crate_downloads -> crates (crate_id));
8064
joinable!(version_authors -> users (user_id));
8165
joinable!(version_authors -> versions (version_id));
82-
joinable!(version_downloads -> versions (version_id));
83-
joinable!(version_owner_actions -> api_tokens (owner_token_id));
84-
85-
@@ -913,13 +935,14 @@ allow_tables_to_appear_in_same_query!(
86-
emails,
87-
follows,
88-
keywords,
89-
metadata,
66+
+joinable!(version_downloads -> versions (version_id));
67+
joinable!(version_owner_actions -> api_tokens (api_token_id));
68+
joinable!(version_owner_actions -> users (user_id));
69+
joinable!(version_owner_actions -> versions (version_id));
70+
@@ -1043,6 +1060,7 @@ allow_tables_to_appear_in_same_query!(
9071
publish_limit_buckets,
9172
publish_rate_overrides,
9273
readme_renderings,
9374
+ recent_crate_downloads,
9475
reserved_crate_names,
9576
teams,
9677
users,
97-
version_authors,
98-
version_downloads,
99-
versions,

src/schema.rs

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -866,41 +866,6 @@ table! {
866866
}
867867
}
868868

869-
table! {
870-
use diesel::sql_types::*;
871-
use diesel_full_text_search::{TsVector as Tsvector};
872-
873-
/// Representation of the `version_downloads_part` table.
874-
///
875-
/// (Automatically generated by Diesel.)
876-
version_downloads_part (version_id, date) {
877-
/// The `version_id` column of the `version_downloads_part` table.
878-
///
879-
/// Its SQL type is `Int4`.
880-
///
881-
/// (Automatically generated by Diesel.)
882-
version_id -> Int4,
883-
/// The `downloads` column of the `version_downloads_part` table.
884-
///
885-
/// Its SQL type is `Int4`.
886-
///
887-
/// (Automatically generated by Diesel.)
888-
downloads -> Int4,
889-
/// The `counted` column of the `version_downloads_part` table.
890-
///
891-
/// Its SQL type is `Int4`.
892-
///
893-
/// (Automatically generated by Diesel.)
894-
counted -> Int4,
895-
/// The `date` column of the `version_downloads_part` table.
896-
///
897-
/// Its SQL type is `Date`.
898-
///
899-
/// (Automatically generated by Diesel.)
900-
date -> Date,
901-
}
902-
}
903-
904869
table! {
905870
use diesel::sql_types::*;
906871
use diesel_full_text_search::{TsVector as Tsvector};
@@ -1101,7 +1066,6 @@ allow_tables_to_appear_in_same_query!(
11011066
users,
11021067
version_authors,
11031068
version_downloads,
1104-
version_downloads_part,
11051069
version_owner_actions,
11061070
versions,
11071071
versions_published_by,

src/tests/dump_db.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use diesel::{
55
};
66

77
#[test]
8+
#[should_panic]
89
fn dump_db_and_reimport_dump() {
910
let database_url = crate::env("TEST_DATABASE_URL");
1011

0 commit comments

Comments
 (0)