From 1d0ab6cf1bf812ff1a34f51382068405bb648f0d Mon Sep 17 00:00:00 2001 From: benthecarman Date: Mon, 5 Feb 2024 21:25:54 +0000 Subject: [PATCH] Better emoji migrate --- src/models/zap_config.rs | 43 +++++++++++++++++++++------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/src/models/zap_config.rs b/src/models/zap_config.rs index 96fe900..d6a3fb2 100644 --- a/src/models/zap_config.rs +++ b/src/models/zap_config.rs @@ -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; @@ -166,26 +167,28 @@ impl ZapConfig { } pub fn migrate_emojis(conn: &mut PgConnection) -> anyhow::Result { - 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::(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) + }) } }