Skip to content

Commit c4bcccd

Browse files
Igoranzepboers1988
andauthored
Replace inspector with query to check if column exists in DB (#962)
* Replace inspector with query to check if column exists in DB * Add test and updated check for query * Fix linting' * Delete orchestrator/config/settings.py * Update test_migration_does_column_exist.py * fix linting --------- Co-authored-by: Peter Boers <peter.boers@surf.nl>
1 parent c3e4da3 commit c4bcccd

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

.bumpversion.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 4.0.3
2+
current_version = 4.0.4
33
commit = False
44
tag = False
55
parse = (?P<major>\d+)\.(?P<minor>\d+)\.(?P<patch>\d+)(rc(?P<build>\d+))?

orchestrator/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
"""This is the orchestrator workflow engine."""
1515

16-
__version__ = "4.0.3"
16+
__version__ = "4.0.4"
1717

1818
from orchestrator.app import OrchestratorCore
1919
from orchestrator.settings import app_settings

orchestrator/migrations/helpers.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,20 @@ def has_table_column(table_name: str, column_name: str, conn: sa.engine.Connecti
3535
:param conn: SQLAlchemy database Connection
3636
:return: True if the column exists, False otherwise
3737
"""
38-
inspector = sa.inspect(conn.engine)
39-
try:
40-
columns = inspector.get_columns(table_name)
41-
return any(col["name"] == column_name for col in columns)
42-
except sa.exc.NoSuchTableError:
43-
# On some migrations the table might not exist yet, so we catch the exception
44-
logger.warning(f"Table {table_name} does not exist.")
45-
return False
38+
result = conn.execute(
39+
sa.text(
40+
"""
41+
SELECT column_name
42+
FROM information_schema.columns
43+
WHERE table_name = :table_name and column_name = :column_name
44+
"""
45+
),
46+
{
47+
"table_name": table_name,
48+
"column_name": column_name,
49+
},
50+
)
51+
return result.first() is not None
4652

4753

4854
def get_resource_type_id_by_name(conn: sa.engine.Connection, name: str) -> UUID:
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from orchestrator.db import db
2+
from orchestrator.migrations.helpers import has_table_column
3+
4+
5+
def test_select_from_table():
6+
# Testing if Table Workflows exist with column is_task
7+
# it should because the db.session depends on the session where all migrations are already run
8+
session = db.session
9+
result = has_table_column(table_name="workflows", column_name="is_task", conn=session)
10+
assert result is True, "Column 'is_task' does not exist in 'workflows' table"

0 commit comments

Comments
 (0)