From ef056edf92b678265a4666e1f9405e3b0ce66a42 Mon Sep 17 00:00:00 2001 From: Nathan Perry Date: Fri, 16 Aug 2024 21:14:30 -0400 Subject: repo: overhaul for multitenancy --- src/db/manual_migrate.rs | 62 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 src/db/manual_migrate.rs (limited to 'src/db/manual_migrate.rs') diff --git a/src/db/manual_migrate.rs b/src/db/manual_migrate.rs new file mode 100644 index 0000000..851f0d8 --- /dev/null +++ b/src/db/manual_migrate.rs @@ -0,0 +1,62 @@ +use crate::{ + db::{ + do_migrate, + schema::*, + POOL, + }, + guild_id, +}; +use anyhow::anyhow; +use diesel::{ + associations::HasTable, + backend::Backend, + pg::Pg, + query_builder::AsQuery, + ExpressionMethods, +}; +use diesel_async::{ + scoped_futures::ScopedFutureExt, + AsyncConnection, + AsyncPgConnection, + RunQueryDsl, +}; + +#[inline] +pub async fn connection_no_migrate() +-> anyhow::Result> { + let conn = super::POOL.get().await?; + Ok(conn) +} + +pub async fn set_default_guild(mut conn: Conn, guild_id: u64) -> anyhow::Result<()> +where + Conn: AsyncConnection + 'static, +{ + conn.transaction::<_, anyhow::Error, _>(|tx| { + (async move { + diesel::update(memes::table) + .filter(memes::guild.is_null()) + .set(memes::guild.eq(guild_id as i64)) + .execute(tx) + .await?; + + diesel::update(invocation_records::table) + .filter(invocation_records::guild.is_null()) + .set(invocation_records::guild.eq(guild_id as i64)) + .execute(tx) + .await?; + + diesel::update(tombstones::table) + .filter(tombstones::guild.is_null()) + .set(tombstones::guild.eq(guild_id as i64)) + .execute(tx) + .await?; + + Ok(()) + }) + .scope_boxed() + }) + .await?; + + Ok(()) +} -- cgit v1.3.1