Skip to content

Commit

Permalink
SQLAlchemy 1.4 support + Move CI to GitHub workflows (#506)
Browse files Browse the repository at this point in the history
* fix database existence check

follow up to #372

* always dispose engine after db existence check

* add docs for postgres_db parameter

* fix dialect_name

Co-authored-by: Nicola Soranzo <nicola.soranzo@gmail.com>

* optimize execution order

Co-authored-by: Nicola Soranzo <nicola.soranzo@gmail.com>

* database_exists fix return

- postgres: return for the first positive test
- use immutable for default argument

* use None as default

Co-authored-by: Nicola Soranzo <nicola.soranzo@gmail.com>

* break if successful

Co-authored-by: Nicola Soranzo <nicola.soranzo@gmail.com>

* dispose only for exception

* use Null connection pool and close connections

- use a connection (which is closed automatically) for
  data base existence check
- explicitely use Null connection pool

already with the 1st change disposal of the engine (which closes all
open connections) is not necessary anymore. with the second change
we are completely sure.

* fix isort call in tox.ini and import order

* rename parameter to databases

* move functions to module level

* Add support for SQLAlchemy 1.4

Import _ColumnEntity from sqlalchemy.orm.context if importing from
.query fails.

And while checking if database_exists, use url.set() as URL object is
now immutable.

* Fixed errors related to URL.database not being directly settable.

* Removed the try..except constructs to set the database. Now checking *once* the version of sqlalchemy in use and deciding *once* how to change the database address. Using a wrapping function ('set_database_from_url') to simplify the code.

* Cleanups

* Use `execution_options()` method of `Connection`

to set the transaction isolation level to autocommit for PostgreSQL.

* Lint fixes

* Move CI lint job to GitHub workflows

* Testing: drop Python 3.5 and Python 3.9

* Use `with engine.connect()` context manager

Consolidate autocommit across drivers that support it, xref.
#494 and
fix #486 .

* Move CI tests from TravisCI to GitHub workflows

* Add SQLAlchemy 1.3 on Python 3.6 to the test matrix

* Update tests for changes in `create_database`

to use `with engine.connect()` context manager (commit
4f52578 )

* Install the version of pg8000 recommended by SQLAlchemy 1.3

when testing.

xref. #500

Fix the following exception:

```
_____________________________________________________________________________ TestDatabasePostgresPg8000.test_create_and_drop _____________________________________________________________________________

self = <tests.functions.test_database.TestDatabasePostgresPg8000 object at 0x7f3b3da76490>, dsn = 'postgresql+pg8000://postgres:postgres@localhost/db_to_test_create_and_drop_via_pg8000_driver'

    def test_create_and_drop(self, dsn):
>       assert not database_exists(dsn)

tests/functions/test_database.py:15:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
sqlalchemy_utils/functions/database.py:488: in database_exists
    return bool(_get_scalar_result(engine, text))
sqlalchemy_utils/functions/database.py:443: in _get_scalar_result
    with engine.connect() as conn:
.venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2263: in connect
    return self._connection_cls(self, **kwargs)
.venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py:104: in __init__
    else engine.raw_connection()
.venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2369: in raw_connection
    return self._wrap_pool_connect(
.venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py:2336: in _wrap_pool_connect
    return fn()
.venv/lib/python3.8/site-packages/sqlalchemy/pool/base.py:304: in unique_connection
    return _ConnectionFairy._checkout(self)
.venv/lib/python3.8/site-packages/sqlalchemy/pool/base.py:778: in _checkout
    fairy = _ConnectionRecord.checkout(pool)
.venv/lib/python3.8/site-packages/sqlalchemy/pool/base.py:495: in checkout
    rec = pool._do_get()
.venv/lib/python3.8/site-packages/sqlalchemy/pool/impl.py:241: in _do_get
    return self._create_connection()
.venv/lib/python3.8/site-packages/sqlalchemy/pool/base.py:309: in _create_connection
    return _ConnectionRecord(self)
.venv/lib/python3.8/site-packages/sqlalchemy/pool/base.py:440: in __init__
    self.__connect(first_connect_check=True)
.venv/lib/python3.8/site-packages/sqlalchemy/pool/base.py:664: in __connect
    pool.dispatch.first_connect.for_modify(
.venv/lib/python3.8/site-packages/sqlalchemy/event/attr.py:314: in exec_once_unless_exception
    self._exec_once_impl(True, *args, **kw)
.venv/lib/python3.8/site-packages/sqlalchemy/event/attr.py:285: in _exec_once_impl
    self(*args, **kw)
.venv/lib/python3.8/site-packages/sqlalchemy/event/attr.py:322: in __call__
    fn(*args, **kw)
.venv/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py:1406: in go
    return once_fn(*arg, **kw)
.venv/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py:199: in first_connect
    dialect.initialize(c)
.venv/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/pg8000.py:215: in initialize
    super(PGDialect_pg8000, self).initialize(connection)
.venv/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/base.py:2624: in initialize
    super(PGDialect, self).initialize(connection)
.venv/lib/python3.8/site-packages/sqlalchemy/engine/default.py:311: in initialize
    self.server_version_info = self._get_server_version_info(
.venv/lib/python3.8/site-packages/sqlalchemy/dialects/postgresql/base.py:2869: in _get_server_version_info
    v = connection.execute("select version()").scalar()
.venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py:1003: in execute
    return self._execute_text(object_, multiparams, params)
.venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py:1172: in _execute_text
    ret = self._execute_context(
.venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py:1316: in _execute_context
    self._handle_dbapi_exception(
.venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py:1514: in _handle_dbapi_exception
    util.raise_(exc_info[1], with_traceback=exc_info[2])
.venv/lib/python3.8/site-packages/sqlalchemy/util/compat.py:182: in raise_
    raise exception
.venv/lib/python3.8/site-packages/sqlalchemy/engine/base.py:1294: in _execute_context
    result = context._setup_crud_result_proxy()
.venv/lib/python3.8/site-packages/sqlalchemy/engine/default.py:1258: in _setup_crud_result_proxy
    result = self.get_result_proxy()
.venv/lib/python3.8/site-packages/sqlalchemy/engine/default.py:1233: in get_result_proxy
    return result.ResultProxy(self)
.venv/lib/python3.8/site-packages/sqlalchemy/engine/result.py:775: in __init__
    self._init_metadata()
.venv/lib/python3.8/site-packages/sqlalchemy/engine/result.py:807: in _init_metadata
    self._metadata = ResultMetaData(self, cursor_description)
.venv/lib/python3.8/site-packages/sqlalchemy/engine/result.py:290: in __init__
    raw = self._merge_cursor_description(
.venv/lib/python3.8/site-packages/sqlalchemy/engine/result.py:496: in _merge_cursor_description
    return [
.venv/lib/python3.8/site-packages/sqlalchemy/engine/result.py:496: in <listcomp>
    return [
.venv/lib/python3.8/site-packages/sqlalchemy/engine/result.py:616: in _merge_cols_by_none
    for (
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <sqlalchemy.engine.result.ResultMetaData object at 0x7f3b3daa4a90>, context = <sqlalchemy.dialects.postgresql.pg8000.PGExecutionContext_pg8000 object at 0x7f3b3dda7100>
cursor_description = [('version', 25, None, None, None, None, ...)]

    def _colnames_from_description(self, context, cursor_description):
        """Extract column names and data types from a cursor.description.

        Applies unicode decoding, column translation, "normalization",
        and case sensitivity rules to the names based on the dialect.

        """

        dialect = context.dialect
        case_sensitive = dialect.case_sensitive
        translate_colname = context._translate_colname
        description_decoder = (
            dialect._description_decoder
            if dialect.description_encoding
            else None
        )
        normalize_name = (
            dialect.normalize_name if dialect.requires_name_normalize else None
        )
        untranslated = None

        self.keys = []

        for idx, rec in enumerate(cursor_description):
            colname = rec[0]
            coltype = rec[1]

            if description_decoder:
>               colname = description_decoder(colname)
E               TypeError: expected bytes, str found

.venv/lib/python3.8/site-packages/sqlalchemy/engine/result.py:545: TypeError
```

* Fix another import for SQLAlchemy 1.4

* Fix test failing on SQLAlchemy 1.4

* Fix `AttributeError: 'Query' object has no attribute '_entities'`

* Fix `AttributeError: type object 'User' has no attribute '_decl_class_registry'`

and similar messages for other objects.

* Extend linting to all Python files

* Fix `AttributeError: 'Query' object has no attribute '_mapper_zero'`

* Fix `AttributeError: module 'sqlalchemy.orm.mapper' has no attribute '_mapper_registry'`

* Fix `make_order_by_deterministic` for SQLAlchemy 1.4

Fix errors:

```
    def make_order_by_deterministic(query):
        """

        order_by_func = sa.asc

>       if not query._order_by:
E       AttributeError: 'Query' object has no attribute '_order_by'

```

```
tests/functions/test_make_order_by_deterministic.py:74:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

query = <sqlalchemy.orm.query.Query object at 0x7f03d5af64f0>

    def make_order_by_deterministic(query):

                else:
                    order_by_func = sa.asc
>               column = order_by.get_children()[0]
E               TypeError: 'itertools.chain' object is not subscriptable
```

* Add message to assert

* Debug test failures under SQLAlchemy 1.4

* Don't mask potential `AttributeError` exceptions in `sa.engine.URL.create()`

* Fix wrong `dialect_name` check

Co-authored-by: Nicola Soranzo <nicola.soranzo@gmail.com>

* Remove `u` unnecessary on Python 3

* Drop unnecessary `alias()` call

This returns a `Subquery` object which is not executable in SQLAlchemy 1.4:

```
tests/relationships/test_select_correlated_expression.py:399:
    assert session.execute(aggregate)
.venv/lib/python3.8/site-packages/sqlalchemy/orm/session.py:1587: in execute
    statement = coercions.expect(roles.StatementRole, statement)
.venv/lib/python3.8/site-packages/sqlalchemy/sql/coercions.py:200: in expect
    return impl._implicit_coercions(
.venv/lib/python3.8/site-packages/sqlalchemy/sql/coercions.py:836: in _implicit_coercions
    return super(StatementImpl, self)._implicit_coercions(
.venv/lib/python3.8/site-packages/sqlalchemy/sql/coercions.py:242: in _implicit_coercions
    self._raise_for_expected(element, argname, resolved)
.venv/lib/python3.8/site-packages/sqlalchemy/sql/coercions.py:270: in _raise_for_expected
    util.raise_(exc.ArgumentError(msg, code=code), replace_context=err)

E           sqlalchemy.exc.ArgumentError: Executable SQL or text() construct expected, got <sqlalchemy.sql.selectable.Subquery at 0x7fb34b5e1880; test>.
```

* fix get_columns tests

* fix instant defaults listener

* remove sort_query and get_query_entities

The sort_query and get_query_entities functions never worked fully as intended and
contained lots of quirky  edge cases:

1. sort_query function was dangerous in a sense that it could be used for really
inefficient queries (sorting by non-indexed column).
2. The entity string label introspection in both functions relied on SQLAlchemy internals
which were drastically changed in SA 1.4. Relying on those was never a good idea in
the first place.

* Remove also `get_query_entity_by_alias()`

which depends on `get_query_entities()` which was removed in commit
460e1da .

* Fix linting errors

* fix scalar_subquery warnings

* Update sqlalchemy_utils/compat.py

Co-authored-by: Nicola Soranzo <nicola.soranzo@gmail.com>

* fix selectable columns warning

* Add health checks for SQL Server docker service

Remove `!` from password so `SA_PASSWORD` doesn't need extra quotes.

* Fix install_mssql.sh commands

* Use `isolation_level` argument also for `mssql`

* Restore `engine.dispose()`

* Add `coverage.xml` to `.gitignore`

* change tests to use non-deprecated class

Change EncryptedType tests to use StringEncryptedType

* use scalar subquery for sqlalchemy 1.4

* use create_mock_engine for SA 1.4

* isort linting fixes

Co-authored-by: Matthias Bernt <m.bernt@ufz.de>
Co-authored-by: Srinivasan R <srinivasanr@gmail.com>
Co-authored-by: Luca Venturini <luca.venturini@earlham.ac.uk>
Co-authored-by: Braden Mars <bradenmars@bradenmars.me>
Co-authored-by: Konsta Vesterinen <konsta.vesterinen@gmail.com>
  • Loading branch information
6 people committed Apr 12, 2021
1 parent 2e8ee00 commit a4154bd
Show file tree
Hide file tree
Showing 40 changed files with 447 additions and 1,092 deletions.
9 changes: 9 additions & 0 deletions .ci/install_mssql.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh

if [ ! -f /etc/apt/sources.list.d/microsoft-prod.list ]; then
curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -
sudo sh -c "curl https://packages.microsoft.com/config/ubuntu/$(lsb_release -r -s)/prod.list > /etc/apt/sources.list.d/mssql-release.list"
fi

sudo apt-get update
sudo ACCEPT_EULA=Y apt-get -y install msodbcsql17 unixodbc
18 changes: 18 additions & 0 deletions .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Python linting
on: [push, pull_request]
jobs:
test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.6', '3.9']
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install tox
run: pip install tox
- name: Run linting
run: tox -e lint
68 changes: 68 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ['3.6', '3.7', '3.8', '3.9']
tox_env: ['sqlalchemy14']
include:
- python-version: '3.6'
tox_env: 'sqlalchemy13'
services:
postgres:
image: postgres:latest
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: sqlalchemy_utils_test
# Set health checks to wait until PostgreSQL has started
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432
mysql:
image: mysql:latest
env:
MYSQL_ALLOW_EMPTY_PASSWORD: yes
MYSQL_DATABASE: sqlalchemy_utils_test
ports:
- 3306:3306
mssql:
image: mcr.microsoft.com/mssql/server:2017-latest
env:
ACCEPT_EULA: Y
SA_PASSWORD: Strong_Passw0rd
# Set health checks to wait until SQL Server has started
options: >-
--health-cmd "/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P ${SA_PASSWORD} -Q 'SELECT 1;' -b"
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 1433:1433
steps:
- uses: actions/checkout@v2
- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install MS SQL stuff
run: bash .ci/install_mssql.sh
- name: Add hstore extension to the sqlalchemy_utils_test database
env:
PGHOST: localhost
PGPASSWORD: postgres
PGPORT: 5432
run: psql -U postgres -d sqlalchemy_utils_test -c 'CREATE EXTENSION hstore;'
- name: Install tox
run: pip install tox
- name: Run tests
env:
SQLALCHEMY_UTILS_TEST_POSTGRESQL_PASSWORD: postgres
run: tox -e ${{matrix.tox_env }}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pip-log.txt
# Unit test / coverage reports
.coverage
.tox
coverage.xml
nosetests.xml

# Translations
Expand Down
1 change: 0 additions & 1 deletion .isort.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@ known_first_party=sqlalchemy_utils
known_third_party=flexmock
line_length=79
multi_line_output=3
not_skip=__init__.py
order_by_type=false
40 changes: 0 additions & 40 deletions .travis.yml

This file was deleted.

23 changes: 0 additions & 23 deletions .travis/install_mssql.sh

This file was deleted.

20 changes: 15 additions & 5 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.session import close_all_sessions

from sqlalchemy_utils import (
aggregates,
coercion_listener,
i18n,
InstrumentedList
)

from sqlalchemy_utils.functions.orm import _get_class_registry
from sqlalchemy_utils.types.pg_composite import remove_composite_listeners


Expand Down Expand Up @@ -48,14 +49,23 @@ def postgresql_db_user():
return os.environ.get('SQLALCHEMY_UTILS_TEST_POSTGRESQL_USER', 'postgres')


@pytest.fixture(scope='session')
def postgresql_db_password():
return os.environ.get('SQLALCHEMY_UTILS_TEST_POSTGRESQL_PASSWORD', '')


@pytest.fixture(scope='session')
def mysql_db_user():
return os.environ.get('SQLALCHEMY_UTILS_TEST_MYSQL_USER', 'root')


@pytest.fixture
def postgresql_dsn(postgresql_db_user, db_name):
return 'postgresql://{0}@localhost/{1}'.format(postgresql_db_user, db_name)
def postgresql_dsn(postgresql_db_user, postgresql_db_password, db_name):
return 'postgresql://{0}:{1}@localhost/{2}'.format(
postgresql_db_user,
postgresql_db_password,
db_name
)


@pytest.fixture
Expand Down Expand Up @@ -86,7 +96,7 @@ def mssql_db_user():
@pytest.fixture
def mssql_db_password():
return os.environ.get('SQLALCHEMY_UTILS_TEST_MSSQL_PASSWORD',
'Strong!Passw0rd')
'Strong_Passw0rd')


@pytest.fixture
Expand Down Expand Up @@ -166,7 +176,7 @@ def articles_count(self):

@articles_count.expression
def articles_count(cls):
Article = Base._decl_class_registry['Article']
Article = _get_class_registry(Base)['Article']
return (
sa.select([sa.func.count(Article.id)])
.where(Article.category_id == cls.id)
Expand Down
3 changes: 2 additions & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
# All configuration values have a default; values that are commented out
# serve to show the default.

import sys, os
import os
import sys

# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
Expand Down
9 changes: 9 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,11 @@
[bdist_wheel]
universal = 1

[flake8]
exclude =
.eggs
.git
.tox
.venv
build
docs/conf.py
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
Various utility functions and custom data types for SQLAlchemy.
"""
from setuptools import setup, find_packages
import os
import re
import sys

from setuptools import find_packages, setup

HERE = os.path.dirname(os.path.abspath(__file__))
PY3 = sys.version_info[0] == 3
Expand Down Expand Up @@ -58,9 +58,9 @@ def get_version():

# Add all optional dependencies to testing requirements.
test_all = []
for name, requirements in sorted(extras_require.items()):
for requirements in extras_require.values():
test_all += requirements
extras_require['test_all'] = test_all
extras_require['test_all'] = sorted(test_all)


setup(
Expand Down
3 changes: 1 addition & 2 deletions sqlalchemy_utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
get_hybrid_properties,
get_mapper,
get_primary_keys,
get_query_entities,
get_referencing_foreign_keys,
get_tables,
get_type,
Expand All @@ -42,7 +41,6 @@
naturally_equivalent,
render_expression,
render_statement,
sort_query,
table_name
)
from .generic import generic_relationship # noqa
Expand Down Expand Up @@ -91,6 +89,7 @@
remove_composite_listeners,
ScalarListException,
ScalarListType,
StringEncryptedType,
TimezoneType,
TSVectorType,
URLType,
Expand Down
3 changes: 2 additions & 1 deletion sqlalchemy_utils/aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,7 @@ class Rating(Base):
from sqlalchemy.ext.declarative import declared_attr
from sqlalchemy.sql.functions import _FunctionGenerator

from .compat import get_scalar_subquery
from .functions.orm import get_column_key
from .relationships import (
chained_join,
Expand Down Expand Up @@ -452,7 +453,7 @@ def aggregate_query(self):
self.relationships[0].mapper.class_
)

return query.as_scalar()
return get_scalar_subquery(query)

def update_query(self, objects):
table = self.class_.__table__
Expand Down
5 changes: 5 additions & 0 deletions sqlalchemy_utils/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def get_scalar_subquery(query):
try:
return query.scalar_subquery()
except AttributeError: # SQLAlchemy <1.4
return query.as_scalar()
7 changes: 1 addition & 6 deletions sqlalchemy_utils/functions/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
get_hybrid_properties,
get_mapper,
get_primary_keys,
get_query_entities,
get_tables,
get_type,
getdotattr,
Expand All @@ -40,8 +39,4 @@
table_name
)
from .render import render_expression, render_statement # noqa
from .sort_query import ( # noqa
make_order_by_deterministic,
QuerySorterException,
sort_query
)
from .sort_query import make_order_by_deterministic # noqa
Loading

0 comments on commit a4154bd

Please sign in to comment.