Add sqlx, add Adapter, create initial schema

Adds SQLX for database management, and an Adapter interface for
interacting with it. Through the type "AdapterPool"

Creates an initial_schema with everything I think I'll need.
This commit is contained in:
Julia Lange 2026-02-05 12:23:27 -08:00
parent a42853ac5a
commit 6702f976cb
Signed by: Julia
SSH key fingerprint: SHA256:5DJcfxa5/fKCYn57dcabJa2vN2e6eT0pBerYi5SUbto
7 changed files with 1514 additions and 14 deletions

8
flake.lock generated
View file

@ -2,16 +2,16 @@
"nodes": { "nodes": {
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1768127708, "lastModified": 1769933782,
"narHash": "sha256-1Sm77VfZh3mU0F5OqKABNLWxOuDeHIlcFjsXeeiPazs=", "narHash": "sha256-GlZemJ2dxhXMMq6TNyt588OFv4/jIt3J1QVBO9MspBE=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "ffbc9f8cbaacfb331b6017d5a5abb21a492c9a38", "rev": "64728753f1a42c81c5688a136a6bee173665acc9",
"type": "github" "type": "github"
}, },
"original": { "original": {
"owner": "NixOS", "owner": "NixOS",
"ref": "nixos-unstable", "ref": "nixos-25.11-small",
"repo": "nixpkgs", "repo": "nixpkgs",
"type": "github" "type": "github"
} }

View file

@ -2,7 +2,7 @@
description = "Koucha rust flake"; description = "Koucha rust flake";
inputs = { inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.11-small";
}; };
outputs = { self, nixpkgs }: outputs = { self, nixpkgs }:
@ -24,6 +24,8 @@
rustc rustc
cargo cargo
rust-analyzer rust-analyzer
sqlx-cli
]; ];
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";

1
koucha/.gitignore vendored
View file

@ -1 +1,2 @@
*.db
/target /target

1419
koucha/Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -5,4 +5,5 @@ edition = "2024"
[dependencies] [dependencies]
tokio = { version = "1.49.0", features = ["full"] } tokio = { version = "1.49.0", features = ["full"] }
sqlx = { version = "0.8.6", features = [ "runtime-tokio", "sqlite" ] }
chrono = "0.4.43" chrono = "0.4.43"

View file

@ -0,0 +1,62 @@
-- Add migration script here
PRAGMA foreign_keys = ON;
CREATE TABLE users (
id INTEGER PRIMARY KEY,
name TEXT UNIQUE NOT NULL
);
CREATE TABLE channels (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
link TEXT UNIQUE NOT NULL,
description TEXT,
last_fetched TEXT
);
CREATE TABLE items (
id INTEGER PRIMARY KEY,
channel_id INTEGER NOT NULL,
guid TEXT NOT NULL,
fetched_at TEXT,
title TEXT,
description TEXT,
content TEXT,
UNIQUE(channel_id, guid),
FOREIGN KEY (channel_id) REFERENCES channels(id)
);
CREATE TABLE feeds (
id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL,
title TEXT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id)
);
CREATE TABLE feed_channels (
feed_id INTEGER NOT NULL,
channel_id INTEGER NOT NULL,
initial_score INTEGER,
gravity INTEGER,
boost INTEGER,
PRIMARY KEY (feed_id, channel_id),
FOREIGN KEY (feed_id) REFERENCES feeds(id),
FOREIGN KEY (channel_id) REFERENCES channels(id)
);
CREATE TABLE feed_items (
item_id INTEGER NOT NULL,
feed_id INTEGER NOT NULL,
score INTEGER NOT NULL,
last_updated TEXT NOT NULL,
boosted_at TEXT,
archived BOOLEAN DEFAULT FALSE,
PRIMARY KEY (item_id, feed_id),
FOREIGN KEY (feed_id) REFERENCES feeds(id),
FOREIGN KEY (item_id) REFERENCES items(id)
);
CREATE INDEX idx_feed_items_score
ON feed_items(feed_id, archived, score DESC);

View file

@ -3,3 +3,36 @@ use std::error::Error;
type Result<T> = std::result::Result<T, Box<dyn Error>>; type Result<T> = std::result::Result<T, Box<dyn Error>>;
pub mod score; pub mod score;
pub struct AdapterPool(sqlx::SqlitePool);
pub struct AdapterBuilder {
database_url: String,
}
impl AdapterBuilder {
pub fn new() -> Self {
Self {
database_url: "sqlite:test.db".to_string(),
}
}
pub fn database_url(mut self, url: &str) -> Self {
self.database_url = url.to_string();
self
}
pub async fn create(self) -> Result<Adapter> {
let db = sqlx::sqlite::SqlitePoolOptions::new()
.connect(&self.database_url).await?;
sqlx::migrate!().run(&db).await?;
Ok(Adapter { db: AdapterPool(db) })
}
}
pub struct Adapter {
db: AdapterPool,
}
impl Adapter {
pub fn get_pool(&self) -> &AdapterPool { &self.db }
}