mirror of
https://github.com/Xevion/banner.git
synced 2025-12-06 05:14:26 -06:00
feat: translate over to sqlx, remove diesel
This commit is contained in:
@@ -1,12 +1,9 @@
|
||||
//! Diesel models for the database schema.
|
||||
//! `sqlx` models for the database schema.
|
||||
|
||||
use crate::data::schema::{course_audits, course_metrics, courses, scrape_jobs};
|
||||
use chrono::{DateTime, Utc};
|
||||
use diesel::{Insertable, Queryable, QueryableByName, Selectable};
|
||||
use serde_json::Value;
|
||||
|
||||
#[derive(Queryable, Selectable)]
|
||||
#[diesel(table_name = courses)]
|
||||
#[derive(sqlx::FromRow, Debug, Clone)]
|
||||
pub struct Course {
|
||||
pub id: i32,
|
||||
pub crn: String,
|
||||
@@ -21,24 +18,7 @@ pub struct Course {
|
||||
pub last_scraped_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = courses)]
|
||||
pub struct NewCourse<'a> {
|
||||
pub crn: &'a str,
|
||||
pub subject: &'a str,
|
||||
pub course_number: &'a str,
|
||||
pub title: &'a str,
|
||||
pub term_code: &'a str,
|
||||
pub enrollment: i32,
|
||||
pub max_enrollment: i32,
|
||||
pub wait_count: i32,
|
||||
pub wait_capacity: i32,
|
||||
pub last_scraped_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Queryable, Selectable)]
|
||||
#[diesel(table_name = course_metrics)]
|
||||
#[diesel(belongs_to(Course))]
|
||||
#[derive(sqlx::FromRow, Debug, Clone)]
|
||||
pub struct CourseMetric {
|
||||
pub id: i32,
|
||||
pub course_id: i32,
|
||||
@@ -48,19 +28,7 @@ pub struct CourseMetric {
|
||||
pub seats_available: i32,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = course_metrics)]
|
||||
pub struct NewCourseMetric {
|
||||
pub course_id: i32,
|
||||
pub timestamp: DateTime<Utc>,
|
||||
pub enrollment: i32,
|
||||
pub wait_count: i32,
|
||||
pub seats_available: i32,
|
||||
}
|
||||
|
||||
#[derive(Queryable, Selectable)]
|
||||
#[diesel(table_name = course_audits)]
|
||||
#[diesel(belongs_to(Course))]
|
||||
#[derive(sqlx::FromRow, Debug, Clone)]
|
||||
pub struct CourseAudit {
|
||||
pub id: i32,
|
||||
pub course_id: i32,
|
||||
@@ -70,18 +38,9 @@ pub struct CourseAudit {
|
||||
pub new_value: String,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[diesel(table_name = course_audits)]
|
||||
pub struct NewCourseAudit<'a> {
|
||||
pub course_id: i32,
|
||||
pub timestamp: DateTime<Utc>,
|
||||
pub field_changed: &'a str,
|
||||
pub old_value: &'a str,
|
||||
pub new_value: &'a str,
|
||||
}
|
||||
|
||||
/// The priority level of a scrape job.
|
||||
#[derive(diesel_derive_enum::DbEnum, Copy, Debug, Clone)]
|
||||
#[derive(sqlx::Type, Copy, Debug, Clone)]
|
||||
#[sqlx(type_name = "scrape_priority", rename_all = "PascalCase")]
|
||||
pub enum ScrapePriority {
|
||||
Low,
|
||||
Medium,
|
||||
@@ -90,7 +49,8 @@ pub enum ScrapePriority {
|
||||
}
|
||||
|
||||
/// The type of target for a scrape job, determining how the payload is interpreted.
|
||||
#[derive(diesel_derive_enum::DbEnum, Copy, Debug, Clone)]
|
||||
#[derive(sqlx::Type, Copy, Debug, Clone)]
|
||||
#[sqlx(type_name = "target_type", rename_all = "PascalCase")]
|
||||
pub enum TargetType {
|
||||
Subject,
|
||||
CourseRange,
|
||||
@@ -99,8 +59,7 @@ pub enum TargetType {
|
||||
}
|
||||
|
||||
/// Represents a queryable job from the database.
|
||||
#[derive(Debug, Clone, Queryable, QueryableByName)]
|
||||
#[diesel(table_name = scrape_jobs)]
|
||||
#[derive(sqlx::FromRow, Debug, Clone)]
|
||||
pub struct ScrapeJob {
|
||||
pub id: i32,
|
||||
pub target_type: TargetType,
|
||||
@@ -110,14 +69,3 @@ pub struct ScrapeJob {
|
||||
pub created_at: DateTime<Utc>,
|
||||
pub locked_at: Option<DateTime<Utc>>,
|
||||
}
|
||||
|
||||
/// Represents a new job to be inserted into the database.
|
||||
#[derive(Debug, Clone, Insertable)]
|
||||
#[diesel(table_name = scrape_jobs)]
|
||||
pub struct NewScrapeJob {
|
||||
pub target_type: TargetType,
|
||||
#[diesel(sql_type = diesel::sql_types::Jsonb)]
|
||||
pub target_payload: Value,
|
||||
pub priority: ScrapePriority,
|
||||
pub execute_at: DateTime<Utc>,
|
||||
}
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
pub mod sql_types {
|
||||
#[derive(diesel::sql_types::SqlType)]
|
||||
#[diesel(postgres_type(name = "scrape_priority"))]
|
||||
pub struct ScrapePriority;
|
||||
|
||||
#[derive(diesel::sql_types::SqlType)]
|
||||
#[diesel(postgres_type(name = "target_type"))]
|
||||
pub struct TargetType;
|
||||
}
|
||||
|
||||
use super::models::{ScrapePriorityMapping, TargetTypeMapping};
|
||||
|
||||
diesel::table! {
|
||||
use diesel::sql_types::*;
|
||||
use super::{ScrapePriorityMapping, TargetTypeMapping};
|
||||
|
||||
scrape_jobs (id) {
|
||||
id -> Int4,
|
||||
target_type -> TargetTypeMapping,
|
||||
target_payload -> Jsonb,
|
||||
priority -> ScrapePriorityMapping,
|
||||
execute_at -> Timestamptz,
|
||||
created_at -> Timestamptz,
|
||||
locked_at -> Nullable<Timestamptz>,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
courses (id) {
|
||||
id -> Int4,
|
||||
crn -> Varchar,
|
||||
subject -> Varchar,
|
||||
course_number -> Varchar,
|
||||
title -> Varchar,
|
||||
term_code -> Varchar,
|
||||
enrollment -> Int4,
|
||||
max_enrollment -> Int4,
|
||||
wait_count -> Int4,
|
||||
wait_capacity -> Int4,
|
||||
last_scraped_at -> Timestamptz,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
course_metrics (id) {
|
||||
id -> Int4,
|
||||
course_id -> Int4,
|
||||
timestamp -> Timestamptz,
|
||||
enrollment -> Int4,
|
||||
wait_count -> Int4,
|
||||
seats_available -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
course_audits (id) {
|
||||
id -> Int4,
|
||||
course_id -> Int4,
|
||||
timestamp -> Timestamptz,
|
||||
field_changed -> Varchar,
|
||||
old_value -> Text,
|
||||
new_value -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::joinable!(course_metrics -> courses (course_id));
|
||||
diesel::joinable!(course_audits -> courses (course_id));
|
||||
|
||||
diesel::allow_tables_to_appear_in_same_query!(courses, course_metrics, course_audits, scrape_jobs,);
|
||||
Reference in New Issue
Block a user