handle case pg_trgm can't be dropped when running test

This commit is contained in:
Son Nguyen Kim 2021-09-20 12:28:12 +02:00
parent 153831ed1a
commit 0aa3dff38b
1 changed files with 11 additions and 2 deletions

View File

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