Fixed dockerfile, put in temporary fix to create table in docker
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
// use std::path::Path;
|
||||
|
||||
use diesel::RunQueryDsl;
|
||||
use diesel::r2d2::{Pool, ConnectionManager};
|
||||
@@ -11,34 +11,51 @@ pub mod schema;
|
||||
|
||||
pub fn run_migrations(pool: &Pool<ConnectionManager<PgConnection>>) {
|
||||
let mut connection = pool.get().unwrap();
|
||||
let migrations_dir = Path::new("./migrations");
|
||||
let migrations = std::fs::read_dir(&migrations_dir).unwrap();
|
||||
|
||||
for migration in migrations {
|
||||
if migration.as_ref().unwrap().file_type().unwrap().is_dir() {
|
||||
let migration_paths = std::fs::read_dir(&migration.unwrap().path()).unwrap();
|
||||
|
||||
for migration_path in migration_paths {
|
||||
if migration_path.as_ref().unwrap().file_name().eq_ignore_ascii_case("up.sql") {
|
||||
let path = &migration_path.unwrap().path();
|
||||
let contents = std::fs::read_to_string(path).expect("Unable to read from file");
|
||||
if let Err(err) = diesel::sql_query(&contents).execute(&mut connection) {
|
||||
error!("Could not run migration: {}", err);
|
||||
} else {
|
||||
info!("Successfully ran migration: {}", path.display());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Err(err) = diesel::sql_query("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
|
||||
)").execute(&mut connection) {
|
||||
error!("Could not create messages table: {}", err);
|
||||
} else {
|
||||
info!("Successfully created messages table");
|
||||
}
|
||||
// let migrations_dir = Path::new("./migrations");
|
||||
// let migrations = std::fs::read_dir(&migrations_dir).unwrap();
|
||||
|
||||
// for migration in migrations {
|
||||
// if migration.as_ref().unwrap().file_type().unwrap().is_dir() {
|
||||
// let migration_paths = std::fs::read_dir(&migration.unwrap().path()).unwrap();
|
||||
|
||||
// for migration_path in migration_paths {
|
||||
// if migration_path.as_ref().unwrap().file_name().eq_ignore_ascii_case("up.sql") {
|
||||
// let path = &migration_path.unwrap().path();
|
||||
// let contents = std::fs::read_to_string(path).expect("Unable to read from file");
|
||||
// if let Err(err) = diesel::sql_query(&contents).execute(&mut connection) {
|
||||
// error!("Could not run migration: {}", err);
|
||||
// } else {
|
||||
// info!("Successfully ran migration: {}", path.display());
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
pub fn establish_connection() -> Pool<ConnectionManager<PgConnection>> {
|
||||
let database_user = env::var("POSTGRES_USER").expect("Expected a user in the environment");
|
||||
let database_password = env::var("POSTGRES_PASSWORD").expect("Expected a password in the environment");
|
||||
let database_name = env::var("POSTGRES_DB").expect("Expected a database name in the environment");
|
||||
let database_host = env::var("POSTGRES_HOST").unwrap_or("localhost".to_string());
|
||||
|
||||
let database_url = format!("postgres://{}:{}@localhost/{}", database_user, database_password, database_name);
|
||||
let database_url = format!("postgres://{}:{}@{}/{}", database_user, database_password, database_host, database_name);
|
||||
let manager = ConnectionManager::<PgConnection>::new(database_url);
|
||||
Pool::builder().build(manager).expect("Failed to create pool.")
|
||||
}
|
||||
Reference in New Issue
Block a user