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
container_name: postgres-sb-mirror
shm_size: 1g
user: 0:0
# ports:
# - 5432:5432
volumes:

View File

@ -95,21 +95,26 @@ fn rocket() -> Rocket<Build> {
let start = Instant::now();
println!("Importing database...");
// 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;");
if result.is_err() {
c.batch_execute("ROLLBACK;").unwrap();
eprintln!("Failed to import database: {}", result.err().unwrap());
return false;
}
println!("Imported database in {}ms", start.elapsed().as_millis());
// Vacuum the database
let result = c.batch_execute("VACUUM \"sponsorTimes\";");
if result.is_err() {
eprintln!("Failed to vacuum database: {}", result.err().unwrap());
return false;
}
true
}).await;
unsafe {
LAST_UPDATE = Some(last_modified);
if res {
unsafe { LAST_UPDATE = Some(last_modified) };
}
}