Running direct diesel sql query

This commit is contained in:
Benjamin Sherriff
2023-07-06 23:17:16 -04:00
parent 4a366d6237
commit 81b5cb4b32
4 changed files with 29 additions and 49 deletions

View File

@@ -27,7 +27,6 @@ services:
POSTGRES_DB: ${POSTGRES_DB} POSTGRES_DB: ${POSTGRES_DB}
volumes: volumes:
- ./data:/var/lib/postgresql/data - ./data:/var/lib/postgresql/data
- ./sql/create_tables.sql:/docker-entrypoint-initdb.d/create_tables.sql
ports: ports:
- "5432:5432" - "5432:5432"
restart: unless-stopped restart: unless-stopped

View File

@@ -1,12 +0,0 @@
CREATE TABLE IF NOT EXISTS messages (
id TEXT PRIMARY KEY NOT NULL,
guild_id BIGINT NOT NULL,
channel_id BIGINT NOT NULL,
user_id BIGINT NOT NULL,
created BIGINT NOT NULL,
model TEXT NOT NULL,
request TEXT NOT NULL,
response TEXT NOT NULL,
request_tags TEXT[] NOT NULL,
response_tags TEXT[] NOT NULL
);

View File

@@ -1,45 +1,37 @@
use std::env; use std::env;
// use std::path::Path; use std::path::Path;
use diesel::RunQueryDsl;
use diesel::r2d2::{Pool, ConnectionManager}; use diesel::r2d2::{Pool, ConnectionManager};
use diesel::pg::PgConnection; use diesel::pg::PgConnection;
use log::{error, info};
pub mod models; pub mod models;
pub mod schema; pub mod schema;
// pub fn run_migrations(pool: &Pool<ConnectionManager<PgConnection>>) { pub fn run_migrations(pool: &Pool<ConnectionManager<PgConnection>>) {
// let mut connection = pool.get().unwrap(); let mut connection = pool.get().unwrap();
// let migrations_dir = Path::new("./migrations"); let migrations_dir = Path::new("./migrations");
// let migrations = std::fs::read_dir(&migrations_dir).unwrap(); let migrations = std::fs::read_dir(&migrations_dir).unwrap();
// for migration in migrations { for migration in migrations {
// if migration.as_ref().unwrap().file_type().unwrap().is_dir() { if migration.as_ref().unwrap().file_type().unwrap().is_dir() {
// let migration_paths = std::fs::read_dir(&migration.unwrap().path()).unwrap(); let migration_paths = std::fs::read_dir(&migration.unwrap().path()).unwrap();
// for migration_path in migration_paths { for migration_path in migration_paths {
// if migration_path.as_ref().unwrap().file_name().eq_ignore_ascii_case("up.sql") { if migration_path.as_ref().unwrap().file_name().eq_ignore_ascii_case("up.sql") {
// let path = &migration_path.unwrap().path(); let path = &migration_path.unwrap().path();
// let contents = std::fs::read_to_string(path).expect("Unable to read from file"); let contents = std::fs::read_to_string(path).expect("Unable to read from file");
// connection.build_transaction() if let Err(err) = diesel::sql_query(&contents).execute(&mut connection) {
// connection.build_transaction() error!("Could not run migration: {}", err);
// .read_write() } else {
// .run(|conn| { info!("Successfully ran migration: {}", path.display());
// // let read_attempt = users.select(name).load::<String>(conn); }
// // assert!(read_attempt.is_ok()); }
}
// // let write_attempt = diesel::insert_into(users) }
// // .values(name.eq("Ruby")) }
// // .execute(conn); }
// // assert!(write_attempt.is_ok());
// // diesel::migration::CREATE_MIGRATIONS_TABLE
// // Ok(())
// });
// }
// }
// }
// }
// }
pub fn establish_connection() -> Pool<ConnectionManager<PgConnection>> { pub fn establish_connection() -> Pool<ConnectionManager<PgConnection>> {
let database_user = env::var("POSTGRES_USER").expect("Expected a user in the environment"); let database_user = env::var("POSTGRES_USER").expect("Expected a user in the environment");

View File

@@ -4,6 +4,7 @@ use std::env;
use commands::audio::create_response; use commands::audio::create_response;
use diesel::r2d2::{Pool, ConnectionManager}; use diesel::r2d2::{Pool, ConnectionManager};
use diesel::pg::PgConnection; use diesel::pg::PgConnection;
use dotenv::dotenv; use dotenv::dotenv;
use log::{error, warn, info}; use log::{error, warn, info};
use serenity::async_trait; use serenity::async_trait;
@@ -18,6 +19,8 @@ use songbird::SerenityInit;
mod commands; mod commands;
mod database; mod database;
pub const MIGRATIONS: diesel_migrations::EmbeddedMigrations = diesel_migrations::embed_migrations!("migrations");
struct Handler { struct Handler {
// Open AI Config // Open AI Config
oai: Option<commands::oai::OAI>, oai: Option<commands::oai::OAI>,
@@ -116,7 +119,7 @@ async fn main() {
}; };
let pool = database::establish_connection(); let pool = database::establish_connection();
// database::run_migrations(&pool); database::run_migrations(&pool);
let handler = match env::var("OPENAI_API_KEY") { let handler = match env::var("OPENAI_API_KEY") {
Ok(token) => { Ok(token) => {
@@ -135,9 +138,7 @@ async fn main() {
let mut client = Client::builder(token, intents) let mut client = Client::builder(token, intents)
.event_handler(handler) .event_handler(handler)
.framework(StandardFramework::new() .framework(StandardFramework::new()
.configure(|c| c .configure(|c| c.owners(owners)))
.owners(owners)
))
.register_songbird() .register_songbird()
.await .await
.expect("Error creating client"); .expect("Error creating client");