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
dac085756a
7 changed files with 1514 additions and 14 deletions
1
koucha/.gitignore
vendored
1
koucha/.gitignore
vendored
|
|
@ -1 +1,2 @@
|
|||
*.db
|
||||
/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]
|
||||
tokio = { version = "1.49.0", features = ["full"] }
|
||||
sqlx = { version = "0.8.6", features = [ "runtime-tokio", "sqlite" ] }
|
||||
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>>;
|
||||
|
||||
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