From 0aa3dff38bd915f227cb961cf39b204109492bc1 Mon Sep 17 00:00:00 2001 From: Son Nguyen Kim Date: Mon, 20 Sep 2021 12:28:12 +0200 Subject: [PATCH] handle case pg_trgm can't be dropped when running test --- tests/conftest.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 97d24f61..3966ee7c 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,10 +2,14 @@ import os # use the tests/test.env config fle # flake8: noqa: E402 +import sqlalchemy + os.environ["CONFIG"] = os.path.abspath( os.path.join(os.path.dirname(os.path.dirname(__file__)), "tests/test.env") ) +from psycopg2 import errors +from psycopg2.errorcodes import DEPENDENT_OBJECTS_STILL_EXIST import pytest @@ -21,8 +25,13 @@ app.config["SERVER_NAME"] = "sl.test" with app.app_context(): # enable pg_trgm extension with db.engine.connect() as conn: - conn.execute("DROP EXTENSION if exists pg_trgm") - conn.execute("CREATE EXTENSION pg_trgm") + try: + conn.execute("DROP EXTENSION if exists pg_trgm") + conn.execute("CREATE EXTENSION pg_trgm") + except sqlalchemy.exc.InternalError as e: + if isinstance(e.orig, errors.lookup(DEPENDENT_OBJECTS_STILL_EXIST)): + print(">>> pg_trgm can't be dropped, ignore") + conn.execute("Rollback") db.create_all()