Move to sqlx migrations
This commit is contained in:
parent
5e8890fef4
commit
97688b49c9
5
build.rs
Normal file
5
build.rs
Normal 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");
|
||||||
|
}
|
||||||
21
migrations/20240207205957_init.sql
Normal file
21
migrations/20240207205957_init.sql
Normal 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
|
||||||
|
)
|
||||||
30
src/main.rs
30
src/main.rs
@ -6,7 +6,7 @@ use std::sync::Arc;
|
|||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use sqlx::{Executor, FromRow};
|
use sqlx::FromRow;
|
||||||
use rayon::prelude::*;
|
use rayon::prelude::*;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@ -67,29 +67,6 @@ struct Place {
|
|||||||
rest: HashMap<String, Value>,
|
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> {
|
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")
|
let date_hashes: Vec<DateHash> = sqlx::query_as("SELECT date, sha256 FROM raw_files")
|
||||||
.fetch_all(db)
|
.fetch_all(db)
|
||||||
@ -155,7 +132,10 @@ async fn main() {
|
|||||||
.await
|
.await
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
init_db(&db).await;
|
sqlx::migrate!()
|
||||||
|
.run(&db)
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
let need_refresh = find_updated(&db, files).await;
|
let need_refresh = find_updated(&db, files).await;
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user