Couchers-org / couchers

The next-generation couch surfing platform. Free forever. Community‑led. Non‑profit. Modern. Chuck us a star :)
https://couchers.org
MIT License
388 stars 79 forks source link

Tests failed to catch migration error #1605

Open aapeliv opened 3 years ago

aapeliv commented 3 years ago

See hotfix: https://github.com/Couchers-org/couchers/commit/162c8fd95f42811ad5d7c73b34e3c78a1eeccd44

2021-07-09 19:42:53,773: __main__: Checking DB connection
2021-07-09 19:42:53,789: __main__: Running DB migrations
2021-07-09 19:42:53,804: alembic.runtime.migration: Context impl PostgresqlImpl.
2021-07-09 19:42:53,804: alembic.runtime.migration: Will assume transactional DDL.
2021-07-09 19:42:53,814: alembic.runtime.migration: Running upgrade 1c7784767710 -> 62fcd41e4dcd, Implement signup flow v2
2021-07-09 19:42:53,820: __main__: Unhandled exception
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1770, in _execute_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 717, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.DuplicateObject: type "hostingstatus" already exists

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

Traceback (most recent call last):
  File "src/app.py", line 50, in <module>
    apply_migrations()
  File "/app/src/couchers/db.py", line 28, in apply_migrations
    command.upgrade(alembic_cfg, "head")
  File "/usr/local/lib/python3.8/site-packages/alembic/command.py", line 294, in upgrade
    script.run_env()
  File "/usr/local/lib/python3.8/site-packages/alembic/script/base.py", line 490, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/usr/local/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 97, in load_python_file
    module = load_module_py(module_id, path)
  File "/usr/local/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 843, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "src/couchers/migrations/env.py", line 89, in <module>
    run_migrations_online()
  File "src/couchers/migrations/env.py", line 83, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/usr/local/lib/python3.8/site-packages/alembic/runtime/environment.py", line 813, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/usr/local/lib/python3.8/site-packages/alembic/runtime/migration.py", line 561, in run_migrations
    step.migration_fn(**kw)
  File "/app/src/couchers/migrations/versions/62fcd41e4dcd_implement_signup_flow_v2.py", line 21, in upgrade
    op.create_table(
  File "<string>", line 8, in create_table
  File "<string>", line 3, in create_table
  File "/usr/local/lib/python3.8/site-packages/alembic/operations/ops.py", line 1087, in create_table
    return operations.invoke(op)
  File "/usr/local/lib/python3.8/site-packages/alembic/operations/base.py", line 354, in invoke
    return fn(self, operation)
  File "/usr/local/lib/python3.8/site-packages/alembic/operations/toimpl.py", line 101, in create_table
    operations.impl.create_table(table)
  File "/usr/local/lib/python3.8/site-packages/alembic/ddl/impl.py", line 274, in create_table
    table.dispatch.before_create(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/event/attr.py", line 343, in __call__
    fn(*args, **kw)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 875, in __call__
    return getattr(self.target, self.name)(*arg, **kw)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/sql/sqltypes.py", line 1194, in _on_table_create
    t._on_table_create(target, bind, **kw)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/base.py", line 2003, in _on_table_create
    self.create(bind=bind, checkfirst=checkfirst)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/base.py", line 1908, in create
    bind._run_ddl_visitor(self.EnumGenerator, self, checkfirst=checkfirst)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 2081, in _run_ddl_visitor
    visitorcallable(self.dialect, self, **kwargs).traverse_single(element)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/sql/visitors.py", line 520, in traverse_single
    return meth(obj, **kw)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/base.py", line 1949, in visit_enum
    self.connection.execute(CreateEnumType(enum))
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1262, in execute
    return meth(self, multiparams, params, _EMPTY_EXECUTION_OPTS)
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/sql/ddl.py", line 77, in _execute_on_connection
    return connection._execute_ddl(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1352, in _execute_ddl
    ret = self._execute_context(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1813, in _execute_context
    self._handle_dbapi_exception(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1994, in _handle_dbapi_exception
    util.raise_(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/util/compat.py", line 207, in raise_
    raise exception
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/base.py", line 1770, in _execute_context
    self.dialect.do_execute(
  File "/usr/local/lib/python3.8/site-packages/sqlalchemy/engine/default.py", line 717, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.DuplicateObject) type "hostingstatus" already exists

[SQL: CREATE TYPE hostingstatus AS ENUM ('can_host', 'maybe', 'cant_host')]
(Background on this error at: http://sqlalche.me/e/14/f405)
aapeliv commented 3 years ago

@kalvdans: do you have any ideas for this? Otherwise I'll close it as unactionable and we'll catch similar ones when we deploy staging as with this one.

kalvdans commented 3 years ago

Strange, the migration test should compare all defined types as well. Leave the bug open until I have had a chance to look closer.

aapeliv commented 3 years ago

I can't remember how this happened, but I think this was caught locally and caused me to fix it in an earlier migration for that same PR that introduced this (https://github.com/Couchers-org/couchers/pull/1324). I remember spending a few moments on it when I initially created the migration at https://github.com/Couchers-org/couchers/pull/1324/commits/e974b8adcf5ed755226733456b84673c7ef153a9