db+adapter, add sqlx + schema & add adapter
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:
parent
a42853ac5a
commit
674079769d
7 changed files with 1514 additions and 14 deletions
8
flake.lock
generated
8
flake.lock
generated
|
|
@ -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"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
1
koucha/.gitignore
vendored
|
|
@ -1 +1,2 @@
|
||||||
|
*.db
|
||||||
/target
|
/target
|
||||||
|
|
|
||||||
1419
koucha/Cargo.lock
generated
1419
koucha/Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -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"
|
||||||
|
|
|
||||||
62
koucha/migrations/20260115003047_initial_schema.sql
Normal file
62
koucha/migrations/20260115003047_initial_schema.sql
Normal 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);
|
||||||
|
|
@ -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 }
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue