Skip to content

Commit

Permalink
Better emoji migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Feb 5, 2024
1 parent 2c3a7c2 commit 1d0ab6c
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/models/zap_config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::models::schema::zap_configs::dsl;
use crate::utils::map_emoji;
use crate::DEFAULT_AUTH_RELAY;
use bitcoin::bip32::{ChildNumber, ExtendedPrivKey};
use bitcoin::key::XOnlyPublicKey;
Expand Down Expand Up @@ -166,26 +167,28 @@ impl ZapConfig {
}

pub fn migrate_emojis(conn: &mut PgConnection) -> anyhow::Result<usize> {
let mut count = diesel::update(zap_configs::table)
.filter(zap_configs::emoji.eq("⚡️"))
.set(zap_configs::emoji.eq("⚡"))
.execute(conn)?;

count += diesel::update(zap_configs::table)
.filter(zap_configs::emoji.eq(""))
.set(zap_configs::emoji.eq("❤️"))
.execute(conn)?;

count += diesel::update(zap_configs::table)
.filter(zap_configs::emoji.eq("+"))
.set(zap_configs::emoji.eq("❤️"))
.execute(conn)?;

count += diesel::update(zap_configs::table)
.filter(zap_configs::emoji.eq("❤"))
.set(zap_configs::emoji.eq("❤️"))
.execute(conn)?;
conn.transaction(|conn| {
let all = zap_configs::table.load::<Self>(conn)?;

let mut count: usize = 0;
for z in all {
if let Some(emoji) = map_emoji(&z.emoji) {
if emoji != z.emoji {
let updated = diesel::update(zap_configs::table)
.filter(zap_configs::id.eq(z.id))
.set(zap_configs::emoji.eq(emoji))
.execute(conn)?;

if updated > 1 {
anyhow::bail!("Updated more rows than expected: {updated}");
}

count += updated;
}
}
}

Ok(count)
Ok(count)
})
}
}

0 comments on commit 1d0ab6c

Please sign in to comment.