Simplify database schema
This commit is contained in:
@@ -3,6 +3,9 @@ use anyhow::{
|
||||
Context,
|
||||
Result,
|
||||
};
|
||||
use chrono::{
|
||||
Utc,
|
||||
};
|
||||
use clap::{
|
||||
Parser,
|
||||
};
|
||||
@@ -167,6 +170,8 @@ struct Cli {
|
||||
async fn main() -> Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let scan_time = Utc::now().timestamp();
|
||||
|
||||
let db = SqlitePoolOptions::new().connect("sqlite://flake-tracker.db").await?;
|
||||
|
||||
let flake_metadata_raw = Command::new("nix")
|
||||
@@ -180,21 +185,21 @@ async fn main() -> Result<()> {
|
||||
let flake_metadata: FlakeMetadata = serde_json::from_slice(&flake_metadata_raw.stdout)
|
||||
.context("Failed to parse flake metadata")?;
|
||||
|
||||
sqlx::query("INSERT INTO flake_revisions (revision_uri, uri, nix_store_path, revision, nar_hash, last_modified)
|
||||
sqlx::query("INSERT INTO revisions (revision_uri, flake_uri, nix_store_path, nar_hash, last_modified, tracker_last_scanned)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(revision_uri) DO UPDATE SET
|
||||
uri=excluded.uri,
|
||||
flake_uri=excluded.flake_uri,
|
||||
nix_store_path=excluded.nix_store_path,
|
||||
revision=excluded.revision,
|
||||
nar_hash=excluded.nar_hash,
|
||||
last_modified=excluded.last_modified
|
||||
last_modified=excluded.last_modified,
|
||||
tracker_last_scanned=excluded.tracker_last_scanned
|
||||
")
|
||||
.bind(&flake_metadata.locked.flake_uri()?)
|
||||
.bind(&flake_metadata.resolved.flake_uri()?)
|
||||
.bind(&flake_metadata.path)
|
||||
.bind(&flake_metadata.revision)
|
||||
.bind(&flake_metadata.locked.narHash)
|
||||
.bind(&flake_metadata.locked.lastModified)
|
||||
.bind(&scan_time)
|
||||
.execute(&db).await?;
|
||||
|
||||
let locks_root_name = &flake_metadata.locks.root;
|
||||
@@ -207,12 +212,12 @@ async fn main() -> Result<()> {
|
||||
let locks_input_node = flake_metadata.locks.nodes.get(&locks_input_name)
|
||||
.context("Failed to find lock of input")?;
|
||||
|
||||
sqlx::query("INSERT INTO flake_revisions_inputs (flake_revision_uri, input_name, revision_uri, uri, nar_hash, last_modified)
|
||||
sqlx::query("INSERT INTO inputs (revision_uri, input_name, locked_revision_uri, locked_flake_uri, locked_nar_hash, last_modified)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(flake_revision_uri, input_name) DO UPDATE SET
|
||||
revision_uri=excluded.revision_uri,
|
||||
uri=excluded.uri,
|
||||
nar_hash=excluded.nar_hash,
|
||||
ON CONFLICT(revision_uri, input_name) DO UPDATE SET
|
||||
locked_revision_uri=excluded.locked_revision_uri,
|
||||
locked_flake_uri=excluded.locked_flake_uri,
|
||||
locked_nar_hash=excluded.locked_nar_hash,
|
||||
last_modified=excluded.last_modified
|
||||
")
|
||||
.bind(flake_metadata.locked.flake_uri()?)
|
||||
@@ -222,6 +227,14 @@ async fn main() -> Result<()> {
|
||||
.bind(locks_input_node.locked.clone().context("Unexpected missing lock")?.narHash)
|
||||
.bind(locks_input_node.locked.clone().context("Unexpected missing lock")?.lastModified)
|
||||
.execute(&db).await?;
|
||||
|
||||
sqlx::query("INSERT INTO revisions (revision_uri, flake_uri)
|
||||
VALUES (?, ?)
|
||||
ON CONFLICT(revision_uri) DO NOTHING
|
||||
")
|
||||
.bind(locks_input_node.locked.clone().context("Unexpected missing lock")?.flake_uri()?)
|
||||
.bind(locks_input_node.original.clone().context("Unexpected missing lock")?.flake_uri()?)
|
||||
.execute(&db).await?;
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user