diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..d506869 --- /dev/null +++ b/build.rs @@ -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"); +} diff --git a/migrations/20240207205957_init.sql b/migrations/20240207205957_init.sql new file mode 100644 index 0000000..fb9ba46 --- /dev/null +++ b/migrations/20240207205957_init.sql @@ -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 +) diff --git a/src/main.rs b/src/main.rs index 4fbcf96..a3240a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, } -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 { let date_hashes: Vec = 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;