Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions aws_advanced_python_wrapper/django/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
13 changes: 13 additions & 0 deletions aws_advanced_python_wrapper/django/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from typing import Any

import mysql.connector
import mysql.connector.django.base as base
from django.utils.asyncio import async_unsafe
from django.utils.functional import cached_property
from django.utils.regex_helper import _lazy_re_compile

from aws_advanced_python_wrapper import AwsWrapperConnection

# This should match the numerical portion of the version numbers (we can treat
# versions like 5.0.24 and 5.0.24a as the same).
server_version_re = _lazy_re_compile(r"(\d{1,2})\.(\d{1,2})\.(\d{1,2})")


class DatabaseWrapper(base.DatabaseWrapper):
"""Custom MySQL Connector backend for Django"""

def __init__(self, *args: Any, **kwargs: Any) -> None:
super().__init__(*args, **kwargs)
self._read_only = False

@async_unsafe
def get_new_connection(self, conn_params):
if "converter_class" not in conn_params:
conn_params["converter_class"] = base.DjangoMySQLConverter
conn = AwsWrapperConnection.connect(
mysql.connector.Connect,
**conn_params
)

if not self._read_only:
return conn
else:
conn.read_only = True
return conn

def get_connection_params(self):
kwargs = super().get_connection_params()
self._read_only = kwargs.pop("read_only", False)
return kwargs

@cached_property
def mysql_server_info(self):
return self.mysql_server_data["version"]

@cached_property
def mysql_version(self):
match = server_version_re.match(self.mysql_server_info)
if not match:
raise Exception(
"Unable to determine MySQL version from version string %r"
% self.mysql_server_info
)
return tuple(int(x) for x in match.groups())

@cached_property
def mysql_is_mariadb(self):
return "mariadb" in self.mysql_server_info.lower()

@cached_property
def sql_mode(self):
sql_mode = self.mysql_server_data["sql_mode"]
return set(sql_mode.split(",") if sql_mode else ())
7 changes: 7 additions & 0 deletions aws_advanced_python_wrapper/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ def rowcount(self) -> int:
def arraysize(self) -> int:
return self.target_cursor.arraysize

# Optional for PEP249
@property
def lastrowid(self) -> int:
if hasattr(self.target_cursor, 'lastrowid'):
return self.target_cursor.lastrowid
raise AttributeError("'Cursor' object has no attribute 'lastrowid'")

def close(self) -> None:
self._plugin_manager.execute(self.target_cursor, DbApiMethod.CURSOR_CLOSE,
lambda: self.target_cursor.close())
Expand Down
109 changes: 108 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ SQLAlchemy = "^2.0.30"
psycopg = "^3.3.1"
psycopg-binary = "^3.3.1"
mysql-connector-python = "^9.5.0"
django = "^5.0"
django-stubs = "^5.2.8"

[tool.poetry.group.test.dependencies]
boto3 = "^1.34.111"
Expand Down
7 changes: 6 additions & 1 deletion tests/integration/container/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,12 @@ def pytest_runtest_setup(item):
logger.warning("conftest.ExceptionWhileObtainingInstanceIDs", ex)
instances = list()

sleep(5)
# Only sleep if condition is still not met
if (len(instances) < request.get_num_of_instances()
or len(instances) == 0
or not rds_utility.is_db_instance_writer(instances[0])) and (
timeit.default_timer() - start_time) < 300:
sleep(5)

assert len(instances) > 0
current_writer = instances[0]
Expand Down
13 changes: 13 additions & 0 deletions tests/integration/container/django/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License").
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Loading