Rollback on transaction failure, and run postgres as root.

This commit is contained in:
Kavin 2022-10-30 16:06:27 +00:00
parent 5e945d0914
commit 6608a9993a
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD
2 changed files with 9 additions and 3 deletions

View file

@ -15,6 +15,7 @@ services:
image: postgres:15-alpine image: postgres:15-alpine
container_name: postgres-sb-mirror container_name: postgres-sb-mirror
shm_size: 1g shm_size: 1g
user: 0:0
# ports: # ports:
# - 5432:5432 # - 5432:5432
volumes: volumes:

View file

@ -95,21 +95,26 @@ fn rocket() -> Rocket<Build> {
let start = Instant::now(); let start = Instant::now();
println!("Importing database..."); println!("Importing database...");
// Execute a query of some kind // Execute a query of some kind
db.run(move |c| { let res = db.run(move |c| {
let result = c.batch_execute("BEGIN; DROP TABLE IF EXISTS \"sponsorTimesTemp\"; CREATE UNLOGGED TABLE \"sponsorTimesTemp\"(LIKE \"sponsorTimes\" INCLUDING defaults INCLUDING constraints INCLUDING indexes); COPY \"sponsorTimesTemp\" FROM '/mirror/sponsorTimes.csv' DELIMITER ',' CSV HEADER; DROP TABLE \"sponsorTimes\"; ALTER TABLE \"sponsorTimesTemp\" RENAME TO \"sponsorTimes\"; COMMIT;"); let result = c.batch_execute("BEGIN; DROP TABLE IF EXISTS \"sponsorTimesTemp\"; CREATE UNLOGGED TABLE \"sponsorTimesTemp\"(LIKE \"sponsorTimes\" INCLUDING defaults INCLUDING constraints INCLUDING indexes); COPY \"sponsorTimesTemp\" FROM '/mirror/sponsorTimes.csv' DELIMITER ',' CSV HEADER; DROP TABLE \"sponsorTimes\"; ALTER TABLE \"sponsorTimesTemp\" RENAME TO \"sponsorTimes\"; COMMIT;");
if result.is_err() { if result.is_err() {
c.batch_execute("ROLLBACK;").unwrap();
eprintln!("Failed to import database: {}", result.err().unwrap()); eprintln!("Failed to import database: {}", result.err().unwrap());
return false;
} }
println!("Imported database in {}ms", start.elapsed().as_millis()); println!("Imported database in {}ms", start.elapsed().as_millis());
// Vacuum the database // Vacuum the database
let result = c.batch_execute("VACUUM \"sponsorTimes\";"); let result = c.batch_execute("VACUUM \"sponsorTimes\";");
if result.is_err() { if result.is_err() {
eprintln!("Failed to vacuum database: {}", result.err().unwrap()); eprintln!("Failed to vacuum database: {}", result.err().unwrap());
return false;
} }
true
}).await; }).await;
unsafe { if res {
LAST_UPDATE = Some(last_modified); unsafe { LAST_UPDATE = Some(last_modified) };
} }
} }