Skip to content

Commit 4ab76e1

Browse files
committed
Port bin/update-crates over to Diesel
I thought we were done, but we're not... I forgot the binaries. Sorry. :( This one was pretty straightforward. There's a bit of refactoring in here as well, and I moved some of the integrity checks that this code was doing to the database. It's worth noting that the new body of `set_updated_at` is identical to `diesel_set_updated_at`, but I can't actually call that function from the other since it's `RETURNS trigger`. Not sure if it's worth resolving, since the function is unlikely to change.
1 parent b2ee89c commit 4ab76e1

File tree

11 files changed

+326
-315
lines changed

11 files changed

+326
-315
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
CREATE OR REPLACE FUNCTION set_updated_at() RETURNS trigger AS $$
2+
BEGIN
3+
IF (NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at) THEN
4+
NEW.updated_at := current_timestamp;
5+
END IF;
6+
RETURN NEW;
7+
END
8+
$$ LANGUAGE plpgsql;
9+
10+
CREATE OR REPLACE FUNCTION set_updated_at_ignore_downloads() RETURNS trigger AS $$
11+
BEGIN
12+
IF (NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at AND
13+
NEW.downloads = OLD.downloads) THEN
14+
NEW.updated_at := current_timestamp;
15+
END IF;
16+
RETURN NEW;
17+
END
18+
$$ LANGUAGE plpgsql;
19+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
CREATE OR REPLACE FUNCTION set_updated_at() RETURNS trigger AS $$
2+
BEGIN
3+
IF (
4+
NEW IS DISTINCT FROM OLD AND
5+
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
6+
) THEN
7+
NEW.updated_at = CURRENT_TIMESTAMP;
8+
END IF;
9+
RETURN NEW;
10+
END
11+
$$ LANGUAGE plpgsql;
12+
13+
CREATE OR REPLACE FUNCTION set_updated_at_ignore_downloads() RETURNS trigger AS $$
14+
DECLARE
15+
new_downloads integer;
16+
BEGIN
17+
new_downloads := NEW.downloads;
18+
OLD.downloads := NEW.downloads;
19+
IF (
20+
NEW IS DISTINCT FROM OLD AND
21+
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
22+
) THEN
23+
NEW.updated_at = CURRENT_TIMESTAMP;
24+
END IF;
25+
NEW.downloads := new_downloads;
26+
RETURN NEW;
27+
END
28+
$$ LANGUAGE plpgsql;

src/bin/delete-crate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use cargo_registry::Crate;
1919

2020
#[allow(dead_code)]
2121
fn main() {
22-
let conn = cargo_registry::db::connect_now();
22+
let conn = cargo_registry::db::connect_now_old();
2323
{
2424
let tx = conn.transaction().unwrap();
2525
delete(&tx);

src/bin/delete-version.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use cargo_registry::{Crate, Version};
2020

2121
#[allow(dead_code)]
2222
fn main() {
23-
let conn = cargo_registry::db::connect_now();
23+
let conn = cargo_registry::db::connect_now_old();
2424
{
2525
let tx = conn.transaction().unwrap();
2626
delete(&tx);

src/bin/populate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rand::{StdRng, Rng};
1717

1818
#[allow(dead_code)]
1919
fn main() {
20-
let conn = cargo_registry::db::connect_now();
20+
let conn = cargo_registry::db::connect_now_old();
2121
{
2222
let tx = conn.transaction().unwrap();
2323
update(&tx).unwrap();

src/bin/transfer-crates.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use cargo_registry::Model;
2020

2121
#[allow(dead_code)]
2222
fn main() {
23-
let conn = cargo_registry::db::connect_now();
23+
let conn = cargo_registry::db::connect_now_old();
2424
{
2525
let tx = conn.transaction().unwrap();
2626
transfer(&tx);

0 commit comments

Comments
 (0)