feat: database migrations
This commit is contained in:
parent
0b10885015
commit
04e268a17c
9 changed files with 108 additions and 28 deletions
3
src/build.rs
Normal file
3
src/build.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fn main() {
|
||||
println!("cargo:rerun-if-changed=migrations");
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
use crate::db::users::{User, NewUser};
|
||||
use crate::db::users::{NewUser, User};
|
||||
use crate::{Context, Error};
|
||||
use diesel::RunQueryDsl;
|
||||
|
||||
|
|
@ -22,8 +22,11 @@ pub async fn register(
|
|||
let mut conn = ctx.data().pool.get()?;
|
||||
new_user.insert(&mut conn);
|
||||
}
|
||||
|
||||
ctx.say("You have been registered.").await?;
|
||||
} else {
|
||||
ctx.say("You have to send this message from a guild.").await?;
|
||||
ctx.say("You have to send this message from a guild.")
|
||||
.await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -1,23 +1,46 @@
|
|||
mod schema;
|
||||
pub mod users;
|
||||
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use diesel::connection::SimpleConnection;
|
||||
use diesel::QueryResult;
|
||||
use diesel::r2d2::{ConnectionManager, Pool};
|
||||
use diesel::sqlite::{Sqlite, SqliteConnection};
|
||||
use std::error::Error;
|
||||
|
||||
fn initialize_db(conn: &mut SqliteConnection) -> QueryResult<()> {
|
||||
// Enable WAL mode and enforce foreign keys
|
||||
conn.batch_execute("PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL; PRAGMA foreign_keys = ON;")
|
||||
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
|
||||
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("migrations");
|
||||
|
||||
type DbError = Box<dyn Error + Send + Sync + 'static>;
|
||||
|
||||
fn run_migrations(connection: &mut impl MigrationHarness<Sqlite>) -> Result<(), DbError> {
|
||||
// This will run the necessary migrations.
|
||||
//
|
||||
// See the documentation for `MigrationHarness` for
|
||||
// all available methods.
|
||||
connection.run_pending_migrations(MIGRATIONS)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn initialize_pool(url: &str) -> Pool<ConnectionManager<SqliteConnection>> {
|
||||
fn initialize_db(conn: &mut SqliteConnection) -> Result<(), DbError> {
|
||||
// Enable WAL mode and enforce foreign keys
|
||||
conn.batch_execute(
|
||||
"PRAGMA journal_mode = WAL; PRAGMA synchronous = NORMAL; PRAGMA foreign_keys = ON;",
|
||||
)?;
|
||||
run_migrations(conn)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn initialize_pool(url: &str) -> Result<Pool<ConnectionManager<SqliteConnection>>, DbError> {
|
||||
let manager = ConnectionManager::new(url);
|
||||
|
||||
let pool = Pool::builder().test_on_check_out(true).build(manager).expect("oops");
|
||||
let pool = Pool::builder()
|
||||
.test_on_check_out(true)
|
||||
.build(manager)
|
||||
.expect("oops");
|
||||
|
||||
let mut conn = pool.get().unwrap();
|
||||
initialize_db(&mut conn).unwrap();
|
||||
let mut conn = pool.get()?;
|
||||
initialize_db(&mut conn)?;
|
||||
|
||||
pool
|
||||
Ok(pool)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
use super::schema::users::{self, dsl::*};
|
||||
use diesel::prelude::*;
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use diesel::dsl::{AsSelect, Select};
|
||||
use diesel::sqlite::Sqlite;
|
||||
use diesel::dsl::Eq;
|
||||
use diesel::helper_types::Filter;
|
||||
use diesel::sql_types::BigInt;
|
||||
use diesel::dsl::{AsSelect, Select};
|
||||
use diesel::expression::AsExpression;
|
||||
use diesel::helper_types::Filter;
|
||||
use diesel::prelude::*;
|
||||
use diesel::sql_types::BigInt;
|
||||
use diesel::sqlite::Sqlite;
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
|
||||
#[derive(Queryable, Selectable)]
|
||||
#[diesel(table_name = users)]
|
||||
|
|
@ -44,13 +44,19 @@ impl User {
|
|||
// Self::all().filter(guild_id.eq(guild_id_))
|
||||
// }
|
||||
|
||||
pub fn by_guild_id(guild_id_: i64) -> ByGuild<i64>
|
||||
{
|
||||
pub fn by_guild_id(guild_id_: i64) -> ByGuild<i64> {
|
||||
Self::all().filter(guild_id.eq(guild_id_))
|
||||
}
|
||||
|
||||
pub fn get(conn: &mut SqliteConnection, guild_id_: i64, discord_id_: i64) -> Result<User, diesel::result::Error> {
|
||||
Self::all().filter(guild_id.eq(guild_id_)).filter(discord_id.eq(discord_id_)).first(conn)
|
||||
|
||||
pub fn get(
|
||||
conn: &mut SqliteConnection,
|
||||
guild_id_: i64,
|
||||
discord_id_: i64,
|
||||
) -> Result<User, diesel::result::Error> {
|
||||
Self::all()
|
||||
.filter(guild_id.eq(guild_id_))
|
||||
.filter(discord_id.eq(discord_id_))
|
||||
.first(conn)
|
||||
}
|
||||
|
||||
pub fn get_by_id(conn: &mut SqliteConnection, id_: i32) -> Result<User, diesel::result::Error> {
|
||||
|
|
|
|||
10
src/main.rs
10
src/main.rs
|
|
@ -2,10 +2,10 @@ mod commands;
|
|||
mod db;
|
||||
|
||||
use affluences_api::AffluencesClient;
|
||||
use poise::serenity_prelude as serenity;
|
||||
use std::{env::var, time::Duration};
|
||||
use diesel::r2d2::{ConnectionManager, Pool};
|
||||
use diesel::sqlite::SqliteConnection;
|
||||
use poise::serenity_prelude as serenity;
|
||||
use std::{env::var, time::Duration};
|
||||
|
||||
// Types used by all command functions
|
||||
type Error = Box<dyn std::error::Error + Send + Sync>;
|
||||
|
|
@ -14,7 +14,7 @@ type Context<'a> = poise::Context<'a, Data, Error>;
|
|||
// Custom user data passed to all command functions
|
||||
pub struct Data {
|
||||
client: AffluencesClient,
|
||||
pool: Pool<ConnectionManager<SqliteConnection>>
|
||||
pool: Pool<ConnectionManager<SqliteConnection>>,
|
||||
}
|
||||
|
||||
async fn on_error(error: poise::FrameworkError<'_, Data, Error>) {
|
||||
|
|
@ -84,6 +84,8 @@ async fn main() {
|
|||
..Default::default()
|
||||
};
|
||||
|
||||
let pool = db::initialize_pool("affy.db").unwrap();
|
||||
|
||||
poise::Framework::builder()
|
||||
.token(
|
||||
var("DISCORD_TOKEN")
|
||||
|
|
@ -95,7 +97,7 @@ async fn main() {
|
|||
poise::builtins::register_globally(ctx, &framework.options().commands).await?;
|
||||
Ok(Data {
|
||||
client: AffluencesClient::new(),
|
||||
pool: db::initialize_pool("affy.db")
|
||||
pool,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue