diff options
Diffstat (limited to 'src/db/manual_migrate.rs')
| -rw-r--r-- | src/db/manual_migrate.rs | 62 |
1 files changed, 62 insertions, 0 deletions
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<diesel_async::pooled_connection::deadpool::Object<AsyncPgConnection>> { + let conn = super::POOL.get().await?; + Ok(conn) +} + +pub async fn set_default_guild<Conn>(mut conn: Conn, guild_id: u64) -> anyhow::Result<()> +where + Conn: AsyncConnection<Backend = Pg> + '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(()) +} |
