Skip to content

Commit bbbdef8

Browse files
fregoguimaxime1907
andauthored
fix(sql-alchemy): update deprecated functions (#25)
* fix(sql-alchemy): update deprecated functions * fix: version and lint --------- Co-authored-by: Guillaume Fregosi <[email protected]> Co-authored-by: Maxime Leroy <[email protected]>
1 parent 8d01862 commit bbbdef8

File tree

9 files changed

+27
-13
lines changed

9 files changed

+27
-13
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## v4.3.3 (2023-07-13)
4+
### Fixes
5+
- sql-alchemy: update deprecated functions
6+
37
## v4.3.2 (2023-05-11)
48
### Fixes
59
- Do not log almbic migrations log in clear

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ The state diagram of your application while upgrading using Helm and using Chart
7575
...
7676
with context.begin_transaction():
7777
if patroni_postgresql:
78-
context.execute("SET ROLE wiremind_owner")
78+
context.execute(text("SET ROLE wiremind_owner"))
7979
context.run_migrations()
8080
...
8181
```

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.3.2
1+
4.3.3

example/alembic/env.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from logging.config import fileConfig
22

3-
from sqlalchemy import engine_from_config, pool
3+
from sqlalchemy import engine_from_config, pool, text
44

55
from alembic import context
66

@@ -69,7 +69,7 @@ def run_migrations_online() -> None:
6969
# The default privileges are set according to this user
7070
# See: https://github.com/zalando/postgres-operator/issues/1170
7171
if patroni_postgresql:
72-
context.execute("SET ROLE wiremind_owner")
72+
context.execute(text("SET ROLE wiremind_owner"))
7373
context.run_migrations()
7474

7575

pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[tool.black]
22
line-length = 120
33
target-version = ['py37']
4+
5+
[tool.isort]
6+
line_length = 120

src/chartreuse/tests/e2e_tests/conftest.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
import time
55
from typing import Generator
66

7-
import chartreuse
87
import pytest
98
import sqlalchemy
9+
from sqlalchemy import inspect
1010
from wiremind_kubernetes.kube_config import load_kubernetes_config
1111
from wiremind_kubernetes.kubernetes_helper import KubernetesDeploymentManager
1212
from wiremind_kubernetes.tests.e2e_tests.conftest import create_namespace, setUpE2E # noqa: F401
1313
from wiremind_kubernetes.utils import run_command
1414

15+
import chartreuse
16+
1517
TEST_NAMESPACE = "chartreuse-e2e-test"
1618
TEST_RELEASE = "e2e-test-release"
1719

@@ -107,11 +109,11 @@ def populate_cluster_with_chartreuse_pre_upgrade() -> Generator:
107109

108110

109111
def assert_sql_upgraded() -> None:
110-
assert sqlalchemy.create_engine(POSTGRESQL_URL).table_names() == ["alembic_version", "upgraded"]
112+
assert inspect(sqlalchemy.create_engine(POSTGRESQL_URL)).get_table_names() == ["alembic_version", "upgraded"]
111113

112114

113115
def assert_sql_not_upgraded() -> None:
114-
assert not sqlalchemy.create_engine(POSTGRESQL_URL).table_names() == ["alembic_version", "upgraded"]
116+
assert not inspect(sqlalchemy.create_engine(POSTGRESQL_URL)).get_table_names() == ["alembic_version", "upgraded"]
115117

116118

117119
def are_pods_scaled_down() -> bool:

src/chartreuse/tests/unit_tests/test_alembic.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import chartreuse.utils
21
from pytest_mock.plugin import MockerFixture
32

3+
import chartreuse.utils
4+
45

56
def test_respect_empty_database(mocker: MockerFixture) -> None:
67
"""

src/chartreuse/tests/unit_tests/test_chartreuse_upgrade.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from typing import Optional
22

3-
import chartreuse.chartreuse_upgrade
43
import pytest
5-
from chartreuse import get_version
64
from pytest_mock.plugin import MockerFixture
75

6+
import chartreuse.chartreuse_upgrade
7+
from chartreuse import get_version
8+
89
from ..conftest import configure_os_environ_mock
910
from .conftest import configure_chartreuse_mock
1011

src/chartreuse/utils/alembic_migration_helper.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import List
77

88
import sqlalchemy
9+
from sqlalchemy import inspect, text
910
from sqlalchemy.pool import NullPool
1011
from wiremind_kubernetes.utils import run_command
1112

@@ -46,7 +47,9 @@ def __init__(
4647
def _configure(self) -> None:
4748
with open("%s/%s" % (self.alembic_directory_path, self.alembic_config_file_path), "r") as f:
4849
content = f.read()
49-
content_new = re.sub("(sqlalchemy.url.*=.*){1}", r"sqlalchemy.url=%s" % self.database_url, content, flags=re.M)
50+
content_new = re.sub(
51+
"(sqlalchemy.url.*=.*){1}", r"sqlalchemy.url=%s" % self.database_url, content, flags=re.M
52+
)
5053
if content != content_new:
5154
with open("%s/%s" % (self.alembic_directory_path, self.alembic_config_file_path), "w") as f:
5255
f.write(content_new)
@@ -79,7 +82,7 @@ def _wait_postgres_is_configured(self) -> None:
7982
with engine.connect() as connection:
8083
transac = connection.begin()
8184
# TODO: Use scalar_one() once sqlachemly >= 1.4
82-
_id = connection.execute(";".join(default_privileges_checks)).scalar()
85+
_id = connection.execute(text(";".join(default_privileges_checks))).scalar()
8386
assert _id == 1
8487
transac.rollback()
8588
logger.info(
@@ -101,7 +104,7 @@ def _wait_postgres_is_configured(self) -> None:
101104
)
102105

103106
def _get_table_list(self) -> List[str]:
104-
return sqlalchemy.create_engine(self.database_url).table_names()
107+
return inspect(sqlalchemy.create_engine(self.database_url)).get_table_names()
105108

106109
def is_postgres_empty(self) -> bool:
107110
table_list = self._get_table_list()

0 commit comments

Comments
 (0)