Move to sqlx migrations

This commit is contained in:
Joshua Coles 2024-02-07 21:02:27 +00:00
parent 5e8890fef4
commit 97688b49c9
3 changed files with 31 additions and 25 deletions

5
build.rs Normal file
View File

@ -0,0 +1,5 @@
// generated by `sqlx migrate build-script`
fn main() {
// trigger recompilation when a new migration is added
println!("cargo:rerun-if-changed=migrations");
}

View File

@ -0,0 +1,21 @@
CREATE TABLE IF NOT EXISTS place
(
place_id UUID PRIMARY KEY not null,
json JSONB NOT NULL
);
CREATE TABLE IF NOT EXISTS timeline_item
(
item_id UUID PRIMARY KEY not null,
json JSONB NOT NULL,
place_id UUID,
end_date TIMESTAMP WITH TIME ZONE NOT NULL,
foreign key (place_id) references place (place_id)
);
CREATE TABLE IF NOT EXISTS raw_files
(
date TEXT PRIMARY KEY not null,
sha256 TEXT not null,
json JSONB NOT NULL
)

View File

@ -6,7 +6,7 @@ use std::sync::Arc;
use chrono::{DateTime, Utc};
use clap::Parser;
use itertools::Itertools;
use sqlx::{Executor, FromRow};
use sqlx::FromRow;
use rayon::prelude::*;
use serde_json::Value;
use uuid::Uuid;
@ -67,29 +67,6 @@ struct Place {
rest: HashMap<String, Value>,
}
async fn init_db(db: &sqlx::PgPool) {
let init_query = r#"
CREATE TABLE IF NOT EXISTS place (
place_id UUID PRIMARY KEY not null,
json JSONB NOT NULL
);
CREATE TABLE IF NOT EXISTS timeline_item (
item_id UUID PRIMARY KEY not null,
json JSONB NOT NULL,
place_id UUID,
end_date TIMESTAMP WITH TIME ZONE NOT NULL,
foreign key (place_id) references place (place_id)
);
CREATE TABLE IF NOT EXISTS raw_files (date TEXT PRIMARY KEY not null, sha256 TEXT not null, json JSONB NOT NULL)
"#;
db.execute(init_query)
.await
.unwrap();
}
async fn find_updated(db: &sqlx::PgPool, files: ReadDir) -> Vec<UpdatedFile> {
let date_hashes: Vec<DateHash> = sqlx::query_as("SELECT date, sha256 FROM raw_files")
.fetch_all(db)
@ -155,7 +132,10 @@ async fn main() {
.await
.unwrap();
init_db(&db).await;
sqlx::migrate!()
.run(&db)
.await
.unwrap();
let need_refresh = find_updated(&db, files).await;