aboutsummaryrefslogtreecommitdiff
path: root/src/db/manual_migrate.rs
blob: 851f0d8e0555088b4060169fdb95bccabf3e9524 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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(())
}