Skip to content

Commit e796258

Browse files
authored
Additional changes for tests and backwards compatibility (#144)
* Additional changes for tests and backwards compat - Restore previous import path for run_transaction(): Prevent existing code from breaking on from cockroachdb.sqlalchemy import run_transaction and add deprecation warning. - Add TODO re: passing autocommit= to sessionmaker - Add README.testing.SQLAlchemy.md * Bump dialect version to 1.4
1 parent f41a217 commit e796258

File tree

8 files changed

+52
-18
lines changed

8 files changed

+52
-18
lines changed

README.testing.SQLAlchemy.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## Testing this dialect with SQLAlchemy
2+
3+
"setup.cfg" contains a default SQLAlchemy connection URI that should
4+
work if you have a local instance of CockroachDB installed:
5+
6+
[db]
7+
default=cockroachdb://root@localhost:26257/defaultdb?disable_cockroachdb_telemetry=True
8+
9+
If you want to test against a remote server (or otherwise need to tweak
10+
the connection URI) simply create a file named "test.cfg" in the same
11+
folder as "setup.cfg", copy the ``[db]`` section into it, and adjust the
12+
``default=`` URI accordingly.
13+
14+
The minimum requirements for testing are:
15+
16+
- SQLAlchemy,
17+
- pytest, and
18+
- the psycopg2 DBAPI module.
19+
20+
Install them with
21+
22+
pip install sqlalchemy pytest psycopg2-binary
23+
24+
Then, to run a complete test simply invoke
25+
26+
pytest
27+
28+
at a command prompt in your virtual environment.
29+
30+
For more detailed information see the corresponding SQLAlchemy document
31+
32+
https://github.com/sqlalchemy/sqlalchemy/blob/master/README.unittests.rst

cockroachdb/sqlalchemy/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from sqlalchemy import util
2+
from sqlalchemy_cockroachdb import run_transaction # noqa
3+
4+
util.warn_limited(
5+
"Importing ``run_transaction`` from ``cockroachdb.sqlalchemy`` is deprecated since "
6+
"version %s of this dialect. "
7+
"Please import it from ``sqlalchemy_cockroachdb`` instead.",
8+
"1.4",
9+
)

sqlalchemy_cockroachdb/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from sqlalchemy.dialects import registry as _registry
2+
from .transaction import run_transaction # noqa
23

3-
__version__ = "1.3.4.dev0"
4+
__version__ = "1.4.0.dev0"
45

56
_registry.register(
67
"cockroachdb.psycopg2",

sqlalchemy_cockroachdb/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def initialize(self, connection):
153153
"SELECT crdb_internal.increment_feature_counter"
154154
+ "('SQLAlchemy {version}')".format(version=version)
155155
)
156-
connection.execute(telemetry_query)
156+
connection.execute(text(telemetry_query))
157157

158158
def _get_server_version_info(self, conn):
159159
# PGDialect expects a postgres server version number here,

sqlalchemy_cockroachdb/transaction.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ def run_transaction(transactor, callback, max_retries=None, max_backoff=0):
3535
with transactor.connect() as connection:
3636
return _txn_retry_loop(connection, callback, max_retries, max_backoff)
3737
elif isinstance(transactor, sqlalchemy.orm.sessionmaker):
38+
# TODO: `autocommit=` is deprecated in SQLA 1.4 (to be removed in 2.0).
39+
# Keep this here for the time being (for compatibility with SQLA 1.3),
40+
# but may not be necessary for 1.4 since _txn_retry_loop uses .begin()
3841
session = transactor(autocommit=True)
3942
return _txn_retry_loop(session, callback, max_retries, max_backoff)
4043
else:

test/test_aaa_run_transaction_session.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from sqlalchemy.types import Integer
77
import threading
88

9-
from sqlalchemy_cockroachdb.transaction import run_transaction
9+
from sqlalchemy_cockroachdb import run_transaction
1010

1111
""" This file is named "test_aaa_run_transaction_session.py" to ensure that it is run before any
1212
other tests. Testing under SQLA 1.4 revealed that it ran by itself just fine, but if *any*

test/test_aab_run_transaction_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from sqlalchemy.types import Integer
55
import threading
66

7-
from sqlalchemy_cockroachdb.transaction import run_transaction
7+
from sqlalchemy_cockroachdb import run_transaction
88

99
""" This file is named "test_aab_run_transaction_core.py" to keep it close to its more
1010
temperamental "session" sibling.

test/test_suite.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,13 @@
1-
import re
2-
3-
from sqlalchemy import text
1+
from sqlalchemy import __version__ as sa_version
42
from sqlalchemy.testing.suite import * # noqa
53
from sqlalchemy.testing.suite import ComponentReflectionTest as _ComponentReflectionTest
64
from sqlalchemy.testing.suite import ExpandingBoundInTest as _ExpandingBoundInTest
75

86

97
class ComponentReflectionTest(_ComponentReflectionTest):
10-
@testing.skip("cockroachdb") # noqa
11-
def test_deprecated_get_primary_keys(self):
12-
# This is removed in the next sqlalchemy release (1.3.18)
13-
pass
14-
158
def test_get_indexes(self, connection):
16-
full_version_string = connection.execute(text("SELECT version()")).scalar()
17-
result = re.search(r" (v\d+\.\d+\.\d+) \(", full_version_string)
18-
if result:
19-
version_string = result.group(1)
20-
if version_string >= "v20.2":
21-
super().test_get_indexes(connection, None, None)
9+
if connection.dialect._is_v202plus and sa_version >= "1.4":
10+
super().test_get_indexes(connection, None, None)
2211

2312

2413
class ExpandingBoundInTest(_ExpandingBoundInTest):

0 commit comments

Comments
 (0)