lahwaacz / wiki-scripts

Framework for writing bots, maintenance scripts or performing data analysis on wikis powered by MediaWiki
http://lahwaacz.github.io/wiki-scripts/
GNU General Public License v3.0
27 stars 12 forks source link

Key (user_name)=(Anonymous) already exists #63

Closed kynikos closed 4 years ago

kynikos commented 4 years ago

Ouch, User:Anonymous had to create some problem somehow ^^ (on commit a04f1f529395a58b61ef11246f0efea9d19bd52f)

% python statistics.py -s
ERROR    The wiki-scripts database is not up to date. Please run `alembic upgrade head` in the wiki-scripts repository to execute pending migrations.
% alembic upgrade head
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade c82c483221d6 -> 1124ae67cc01, rename dummy user to Anonymous
Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1283, in _execute_context
    self.dialect.do_execute(
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 590, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "user_name"
DETAIL:  Key (user_name)=(Anonymous) already exists.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/bin/alembic", line 11, in <module>
    load_entry_point('alembic==1.4.2', 'console_scripts', 'alembic')()
  File "/usr/lib/python3.8/site-packages/alembic/config.py", line 577, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/usr/lib/python3.8/site-packages/alembic/config.py", line 571, in main
    self.run_cmd(cfg, options)
  File "/usr/lib/python3.8/site-packages/alembic/config.py", line 548, in run_cmd
    fn(
  File "/usr/lib/python3.8/site-packages/alembic/command.py", line 298, in upgrade
    script.run_env()
  File "/usr/lib/python3.8/site-packages/alembic/script/base.py", line 489, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/usr/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 98, in load_python_file
    module = load_module_py(module_id, path)
  File "/usr/lib/python3.8/site-packages/alembic/util/compat.py", line 184, in load_module_py
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "ws/db/migrations/env.py", line 110, in <module>
    run_migrations_online()
  File "ws/db/migrations/env.py", line 105, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/usr/lib/python3.8/site-packages/alembic/runtime/environment.py", line 846, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/usr/lib/python3.8/site-packages/alembic/runtime/migration.py", line 520, in run_migrations
    step.migration_fn(**kw)
  File "/home/dario/dev/arch/wiki-scripts/ws/db/migrations/versions/1124ae67cc01_rename_dummy_user_to_anonymous.py", line 36, in upgrade
    op.execute(
  File "<string>", line 8, in execute
  File "<string>", line 3, in execute
  File "/usr/lib/python3.8/site-packages/alembic/operations/ops.py", line 2260, in execute
    return operations.invoke(op)
  File "/usr/lib/python3.8/site-packages/alembic/operations/base.py", line 374, in invoke
    return fn(self, operation)
  File "/usr/lib/python3.8/site-packages/alembic/operations/toimpl.py", line 177, in execute_sql
    operations.migration_context.impl.execute(
  File "/usr/lib/python3.8/site-packages/alembic/ddl/impl.py", line 143, in execute
    self._exec(sql, execution_options)
  File "/usr/lib/python3.8/site-packages/alembic/ddl/impl.py", line 140, in _exec
    return conn.execute(construct, *multiparams, **params)
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1020, in execute
    return meth(self, multiparams, params)
  File "/usr/lib/python3.8/site-packages/sqlalchemy/sql/elements.py", line 298, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1133, in _execute_clauseelement
    ret = self._execute_context(
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1323, in _execute_context
    self._handle_dbapi_exception(
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1517, in _handle_dbapi_exception
    util.raise_(
  File "/usr/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 178, in raise_
    raise exception
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1283, in _execute_context
    self.dialect.do_execute(
  File "/usr/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 590, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.IntegrityError: (psycopg2.errors.UniqueViolation) duplicate key value violates unique constraint "user_name"
DETAIL:  Key (user_name)=(Anonymous) already exists.

[SQL: UPDATE "user" SET user_name=%(user_name)s WHERE "user".user_id = %(user_id_1)s]
[parameters: {'user_name': 'Anonymous', 'user_id_1': 0}]
(Background on this error at: http://sqlalche.me/e/gkpj)

What do I do?

lahwaacz commented 4 years ago

Ouch, I wrote the last migration after I synchronized the changes we made with the Renameuser and UserMerge extensions, so at that time there was no "Anonymous" user...

The easiest way would be if you did the same - run the script at 1a913bacba600db45be5f1283761aea64bdbb390 which will do the initial synchronization, then checkout master again and alembic upgrade head should work. Otherwise I can try to fix the migration script...

kynikos commented 4 years ago

Thanks, I did that, I think it worked!

lahwaacz commented 4 years ago

Great! It would be good to compare the output of checkdb.py again, but I still have some issues myself (e.g. #62) so I'll let you know when it's fixed.