#2313 frontend: add migration removing %40 characters from counter_stat
Merged 2 years ago by praiskup. Opened 2 years ago by frostyx.
copr/ frostyx/copr unquote-hitcounter-names  into  main

@@ -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)

why do we need the :hset:: part here? If needed, shouldn't we have the same pattern in the query above?

We don't need it, replace("%40", "@") would work as well. This too

from requests.utils import unquote
unquote(stat.name)

I added the :hset:: to be extra sure that I am not replacing anything that I am not aware of. But I think it's not needed.

+ 

+         # 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

+     """

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%';

Build failed. More information on how to proceed and troubleshoot errors available at https://fedoraproject.org/wiki/Zuul-based-ci

why do we need the :hset:: part here? If needed, shouldn't we have the same pattern in the query above?

We don't need it, replace("%40", "@") would work as well. This too

from requests.utils import unquote
unquote(stat.name)

I added the :hset:: to be extra sure that I am not replacing anything that I am not aware of. But I think it's not needed.

Commit 2159fe5 fixes this pull-request

Pull-Request has been merged by praiskup

2 years ago