Db working & Migrations

This commit is contained in:
Julia Lange 2025-06-13 10:02:01 -07:00
parent 9d8fb730ba
commit 1abdb7f133
Signed by: Julia
SSH key fingerprint: SHA256:5DJcfxa5/fKCYn57dcabJa2vN2e6eT0pBerYi5SUbto
7 changed files with 65 additions and 35 deletions

View file

@ -0,0 +1,45 @@
-- Add migration script here
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE TABLE actor (
did VARCHAR PRIMARY KEY,
handle VARCHAR UNIQUE,
indexed_at VARCHAR NOT NULL
);
CREATE INDEX actor_handle_trgm_idx ON actor USING gist (handle gist_trgm_ops);
CREATE TABLE session (
uri VARCHAR PRIMARY KEY,
cid VARCHAR NOT NULL,
owner VARCHAR NOT NULL,
content VARCHAR NOT NULL,
contentcid VARCHAR NOT NULL,
label VARCHAR,
-- Participants in participant
created_at VARCHAR NOT NULL,
indexed_at VARCHAR NOT NULL,
sort_at VARCHAR GENERATED ALWAYS AS (LEAST(created_at,indexed_at)) STORED NOT NULL
);
CREATE TABLE activity (
uri VARCHAR PRIMARY KEY,
cid VARCHAR NOT NULL,
session VARCHAR,
sessioncid VARCHAR,
-- Progress in progress
performed_at VARCHAR,
created_at VARCHAR NOT NULL,
indexed_at VARCHAR NOT NULL,
sort_at VARCHAR GENERATED ALWAYS AS (LEAST(created_at,indexed_at)) STORED NOT NULL
);
CREATE TABLE participant (
participantdid VARCHAR NOT NULL,
session VARCHAR NOT NULL,
role VARCHAR NOT NULL
);

View file

@ -1,15 +1,3 @@
use sqlx::{
query,
Database,
Pool,
Postgres,
pool::PoolOptions,
postgres::{
PgConnectOptions,
PgSslMode,
},
Result,
};
use std::string::ToString; use std::string::ToString;
pub struct Db<Db: Database> { pub struct Db<Db: Database> {
@ -39,13 +27,6 @@ pub struct Session {
impl Db<Postgres> { impl Db<Postgres> {
async fn connect() -> Result<Self> { async fn connect() -> Result<Self> {
let conn = PgConnectOptions::new()
.host("localhost")
.port(5432)
.username("postgres")
.password("062217")
.database("anisky")
.ssl_mode(PgSslMode::Disable);
let pool = match PoolOptions::new().connect_with(conn).await { let pool = match PoolOptions::new().connect_with(conn).await {
Ok(p) => p, Ok(p) => p,

8
db/src/error.rs Normal file
View file

@ -0,0 +1,8 @@
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("Database Implementation Error: {0}")]
Backend(#[from] sqlx::Error),
}
pub type Result<T> = std::result::Result<T, Error>;

View file

@ -1,4 +1,4 @@
use crate::DbError; use crate::Error;
use atproto::{ use atproto::{
Cid, Cid,
Uri, Uri,
@ -56,15 +56,15 @@ pub enum Progress {
pub async fn ingest_session( pub async fn ingest_session(
db: PgPool, session: Session db: PgPool, session: Session
) -> Result<(), DbError> { ) -> Result<(), Error> {
let mut transaction = db.begin().await?; let mut transaction = db.begin().await?;
write_session(&mut transaction, session).await?; write_session(&mut transaction, session).await?;
transaction.commit().await.map_err(DbError::Backend) transaction.commit().await.map_err(Error::Backend)
} }
async fn write_session( async fn write_session(
tr: &mut PgTransaction<'_>, session: Session tr: &mut PgTransaction<'_>, session: Session
) -> Result<(), DbError> { ) -> Result<(), Error> {
let (contenturi, contentcid): (Option<Content>, String) = let (contenturi, contentcid): (Option<Content>, String) =
match session.content { match session.content {
@ -94,7 +94,7 @@ async fn write_session(
async fn write_participant( async fn write_participant(
tr: &mut PgTransaction<'_>, participant: Participant, sessionuri: Uri tr: &mut PgTransaction<'_>, participant: Participant, sessionuri: Uri
) -> Result<(), DbError> { ) -> Result<(), Error> {
let (participantType, user): (String, User) = match participant { let (participantType, user): (String, User) = match participant {
Participant::Owner(user) => ("Owner".to_string(), user), Participant::Owner(user) => ("Owner".to_string(), user),
Participant::Added(user) => ("Participant".to_string(), user), Participant::Added(user) => ("Participant".to_string(), user),

View file

@ -1,12 +1,7 @@
use thiserror::Error;
pub mod interfaces; pub mod interfaces;
pub mod error;
pub use crate::error::Error;
#[non_exhaustive]
#[derive(Debug, Error)]
pub enum DbError {
#[error("Database Implementation Error: {0}")]
Backend(#[from] sqlx::Error),
}
// pub struct db; // pub struct db;

6
flake.lock generated
View file

@ -41,11 +41,11 @@
"nixpkgs": "nixpkgs_2" "nixpkgs": "nixpkgs_2"
}, },
"locked": { "locked": {
"lastModified": 1746585402, "lastModified": 1749695868,
"narHash": "sha256-Pf+ufu6bYNA1+KQKHnGMNEfTwpD9ZIcAeLoE2yPWIP0=", "narHash": "sha256-debjTLOyqqsYOUuUGQsAHskFXH5+Kx2t3dOo/FCoNRA=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "72dd969389583664f87aa348b3458f2813693617", "rev": "55f914d5228b5c8120e9e0f9698ed5b7214d09cd",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

@ -42,6 +42,7 @@
packages = (with pkgs; [ packages = (with pkgs; [
# The package provided by our custom overlay. Includes cargo, Clippy, cargo-fmt, # The package provided by our custom overlay. Includes cargo, Clippy, cargo-fmt,
# rustdoc, rustfmt, and other tools. # rustdoc, rustfmt, and other tools.
sqlx-cli
rustToolchain rustToolchain
]) ++ pkgs.lib.optionals pkgs.stdenv.isDarwin (with pkgs; [ libiconv ]); ]) ++ pkgs.lib.optionals pkgs.stdenv.isDarwin (with pkgs; [ libiconv ]);
}; };