From 2159fe528bea601146683b55915ea4b4c15f0dbc Mon Sep 17 00:00:00 2001 From: Jakub Kadlcik Date: Sep 19 2022 07:43:10 +0000 Subject: frontend: add migration removing %40 characters from counter_stat See PR#2274 See PR#2280 Currently, we have some stat names with %40 in the production database. select * from counter_stat where name LIKE '%\%40%'; Merges: #2313 --- diff --git a/frontend/coprs_frontend/alembic/versions/65a172e3f102_unquote_counter_stat_urls.py b/frontend/coprs_frontend/alembic/versions/65a172e3f102_unquote_counter_stat_urls.py new file mode 100644 index 0000000..4e56397 --- /dev/null +++ b/frontend/coprs_frontend/alembic/versions/65a172e3f102_unquote_counter_stat_urls.py @@ -0,0 +1,49 @@ +""" +Unquote counter_stat URLs + +Revision ID: 65a172e3f102 +Revises: 004a017535dc +Create Date: 2022-09-14 23:28:35.890110 +""" + +import sqlalchemy as sa +from alembic import op +from coprs.models import CounterStat + + +revision = '65a172e3f102' +down_revision = '004a017535dc' + + +def upgrade(): + session = sa.orm.sessionmaker(bind=op.get_bind())() + rows = (session.query(CounterStat) + .filter(CounterStat.name.like("%\%40%")) + .all()) + + for stat in rows: + # See PR#2274 and PR#2280 + name = stat.name.replace(":hset::%40", ":hset::@", 1) + + # We can't simply rename the stat and be done with it. There may already + # be a row with the unquoted name. + + existing = (session.query(CounterStat) + .filter(CounterStat.name==name) + .one_or_none()) + + if existing: + existing.counter += stat.counter + session.delete(stat) + session.add(existing) + else: + stat.name = name + session.add(stat) + + session.commit() + + +def downgrade(): + """ + There is no going back + """