Skip to content

Remove the id column from version_downloads #1694

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

Merged
merged 2 commits into from
Apr 1, 2019
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
28 changes: 28 additions & 0 deletions migrations/2019-03-22-174825_remove_version_downloads_pk/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- Diesel always runs migrations in a transaction, and we can't create indexes
-- concurrently in a transaction. We can't block crate downloads on this, so
-- just raise if we try to run this in production
DO $$
DECLARE
tmp_index_exists boolean;
BEGIN
tmp_index_exists := (SELECT EXISTS (
SELECT * FROM pg_index
INNER JOIN pg_class
ON pg_class.oid = pg_index.indexrelid
WHERE relname = 'version_downloads_tmp'
));
IF NOT tmp_index_exists THEN
IF (SELECT COUNT(*) FROM version_downloads) > 1000 THEN
RAISE EXCEPTION 'Indexes need to be created concurrently in production, manually create them and try again';
ELSE
ALTER TABLE version_downloads ADD COLUMN id SERIAL;
CREATE UNIQUE INDEX version_downloads_tmp ON version_downloads (id);
CREATE UNIQUE INDEX version_downloads_unique ON version_downloads (version_id, date);
CREATE UNIQUE INDEX index_version_downloads_not_processed ON version_downloads (id) WHERE processed = false;
END IF;
END IF;
END $$;

ALTER TABLE version_downloads
DROP CONSTRAINT version_downloads_pkey,
ADD CONSTRAINT version_downloads_pkey PRIMARY KEY USING INDEX version_downloads_tmp;
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- ALERT! DESTRUCTIVE MIGRATION
-- DO NOT DEPLOY UNTIL PREVIOUS COMMIT IS DEPLOYED
ALTER TABLE version_downloads
DROP CONSTRAINT version_downloads_pkey,
ADD CONSTRAINT version_downloads_pkey PRIMARY KEY USING INDEX version_downloads_unique,
DROP COLUMN id;
3 changes: 0 additions & 3 deletions mirage/fixtures/version-downloads.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@ export default [
{
date: '2017-02-10T00:00:00Z',
downloads: 2,
id: 3023900,
version: 40905,
},
{
date: '2017-02-10T00:00:00Z',
downloads: 1,
id: 3024236,
version: 18445,
},
{
date: '2017-02-11T00:00:00Z',
downloads: 1,
id: 3027018,
version: 40905,
},
];
21 changes: 6 additions & 15 deletions src/bin/update-downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ use cargo_registry::{

use diesel::prelude::*;

static LIMIT: i64 = 1000;

fn main() -> CargoResult<()> {
let conn = db::connect_now()?;
update(&conn)?;
Expand All @@ -25,18 +23,11 @@ fn update(conn: &PgConnection) -> QueryResult<()> {
use diesel::dsl::now;
use diesel::select;

let mut max = Some(0);
while let Some(m) = max {
let rows = version_downloads
.filter(processed.eq(false))
.filter(id.gt(m))
.filter(downloads.ne(counted))
.order(id)
.limit(LIMIT)
.load(conn)?;
collect(conn, &rows)?;
max = rows.last().map(|d| d.id);
}
let rows = version_downloads
.filter(processed.eq(false))
.filter(downloads.ne(counted))
.load(conn)?;
collect(conn, &rows)?;

// Anything older than 24 hours ago will be frozen and will not be queried
// against again.
Expand All @@ -62,7 +53,7 @@ fn collect(conn: &PgConnection, rows: &[VersionDownload]) -> QueryResult<()> {

conn.transaction::<_, diesel::result::Error, _>(|| {
// increment the number of counted downloads
update(version_downloads::table.find(download.id))
update(version_downloads::table.find(download.id()))
.set(version_downloads::counted.eq(version_downloads::counted + amt))
.execute(conn)?;

Expand Down
3 changes: 1 addition & 2 deletions src/models/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::views::EncodableVersionDownload;

#[derive(Queryable, Identifiable, Associations, Debug, Clone, Copy)]
#[belongs_to(Version)]
#[primary_key(version_id, date)]
pub struct VersionDownload {
pub id: i32,
pub version_id: i32,
pub downloads: i32,
pub counted: i32,
Expand All @@ -34,7 +34,6 @@ impl VersionDownload {

pub fn encodable(self) -> EncodableVersionDownload {
EncodableVersionDownload {
id: self.id,
version: self.version_id,
downloads: self.downloads,
date: self.date.to_string(),
Expand Down
8 changes: 1 addition & 7 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -815,13 +815,7 @@ table! {
/// Representation of the `version_downloads` table.
///
/// (Automatically generated by Diesel.)
version_downloads (id) {
/// The `id` column of the `version_downloads` table.
///
/// Its SQL type is `Int4`.
///
/// (Automatically generated by Diesel.)
id -> Int4,
version_downloads (version_id, date) {
/// The `version_id` column of the `version_downloads` table.
///
/// Its SQL type is `Int4`.
Expand Down
1 change: 0 additions & 1 deletion src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ pub struct EncodableDependency {

#[derive(Serialize, Deserialize, Debug)]
pub struct EncodableVersionDownload {
pub id: i32,
pub version: i32,
pub downloads: i32,
pub date: String,
Expand Down