Skip to content
Open
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
10 changes: 9 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ on:

jobs:
test:
name: Python ${{ matrix.python-version }} / SQLAlchemy ${{ matrix.sqlalchemy }}
strategy:
fail-fast: false
matrix:
python-version: ["3.10", "3.11", "3.12", "3.13", "3.14"]
sqlalchemy: [">=2.0.29,<2.1", ">=2.1,<2.2"]
exclude:
# SQLAlchemy 2.1 requires Python 3.11 or later.
- python-version: "3.10"
sqlalchemy: ">=2.1,<2.2"
Comment on lines +15 to +19

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives us 9 CI jobs. Do we need all of them? We could just add the SQLA 2.0 jobs as inclusions (instructions here).

runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v7
Expand All @@ -22,7 +28,9 @@ jobs:
cache: pip
cache-dependency-path: pyproject.toml
- name: Install dependencies
run: pip install --group test -e .[sqlmodel,citext,geoalchemy2,pgvector]
run: pip install --group test -e .[sqlmodel,citext,geoalchemy2,pgvector] "SQLAlchemy${{ matrix.sqlalchemy }}"
- name: Show dependency version
run: python -c "import sqlalchemy; print(sqlalchemy.__version__)"
Comment on lines +32 to +33

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we need this for? The previous step already shows clearly which version is installed.

- name: Test with pytest
run: coverage run -m pytest
- name: Upload Coverage
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ Version history
their relationships no longer pass ``secondary`` via ``sa_relationship_kwargs``.
Code importing those variables will break when models are regenerated. Use the
``nolinktables`` option to keep the previous output.
- Added SQLAlchemy 2.1 support
(`#489 <https://github.com/agronholm/sqlacodegen/pull/489>`_; PR by @Claire-ovo,
reworked by @sheinbergon)
- Added support for many-to-many link models in the SQLModel generator: association
tables with a primary key are now rendered as ``SQLModel`` classes and referenced via
``Relationship(link_model=...)``
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ classifiers = [
]
requires-python = ">=3.10"
dependencies = [
"SQLAlchemy >= 2.0.29, < 2.1",
"SQLAlchemy >= 2.0.29",
"inflect >= 4.0.0",
]
dynamic = ["version"]
Expand Down
5 changes: 5 additions & 0 deletions src/sqlacodegen/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,11 @@ def _collect_inherited_init_kwargs(
if default is not Parameter.empty and value == default:
continue

# SQLAlchemy 2.1 attaches the table's metadata to named types and
# uses a sentinel as the default for "schema"
if value is None or isinstance(value, MetaData):
continue

inherited_kwargs[super_param.name] = self._render_column_type_value(
value
)
Expand Down
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
from textwrap import dedent

import pytest
import sqlalchemy
from pytest import FixtureRequest
from sqlalchemy.engine import Engine, create_engine
from sqlalchemy.orm import clear_mappers, configure_mappers
from sqlalchemy.schema import MetaData


@pytest.fixture
def sqlalchemy_version(request: FixtureRequest) -> None:
installed = tuple(map(int, sqlalchemy.__version__.split(".")[:2]))
if installed != request.param:
pytest.skip(f"Requires SQLAlchemy {request.param}; installed {installed}")


@pytest.fixture
def engine(request: FixtureRequest) -> Engine:
dialect = getattr(request, "param", None)
Expand Down
155 changes: 147 additions & 8 deletions tests/test_generator_declarative.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
from __future__ import annotations

import sys

import pytest
from _pytest.fixtures import FixtureRequest
from geoalchemy2 import Geography, Geometry
Expand Down Expand Up @@ -2442,8 +2440,11 @@ class WithItems(Base):
)


@pytest.mark.parametrize(
"sqlalchemy_version", [(2, 0)], indirect=True, ids=["sqlalchemy20"]
)
@pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"])
def test_domain_json(generator: CodeGenerator) -> None:
def test_domain_json_sa20(sqlalchemy_version: None, generator: CodeGenerator) -> None:
Table(
"test_domain_json",
generator.metadata,
Expand Down Expand Up @@ -2481,11 +2482,57 @@ class TestDomainJson(Base):
)


@pytest.mark.parametrize(
"sqlalchemy_version", [(2, 1)], indirect=True, ids=["sqlalchemy21"]
)
@pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"])
def test_domain_json_sa21(sqlalchemy_version: None, generator: CodeGenerator) -> None:
Table(
"test_domain_json",
generator.metadata,
Column("id", BIGINT, primary_key=True),
Column(
"foo",
postgresql.DOMAIN(
"domain_json",
JSON,
not_null=False,
),
nullable=True,
),
)

validate_code(
generator.generate(),
"""\
from typing import Optional

from sqlalchemy import BigInteger
from sqlalchemy.dialects.postgresql import DOMAIN, JSON
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

class Base(DeclarativeBase):
pass


class TestDomainJson(Base):
__tablename__ = 'test_domain_json'

id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
foo: Mapped[Optional[object]] = mapped_column(DOMAIN('domain_json', JSON(), not_null=False))
""",
)


@pytest.mark.parametrize(
"sqlalchemy_version", [(2, 0)], indirect=True, ids=["sqlalchemy20"]
)
@pytest.mark.parametrize(
"domain_type",
[JSONB, JSON],
)
def test_domain_non_default_json(
def test_domain_non_default_json_sa20(
sqlalchemy_version: None,
generator: CodeGenerator,
domain_type: type[JSON] | type[JSONB],
) -> None:
Expand Down Expand Up @@ -2526,12 +2573,62 @@ class TestDomainJson(Base):
)


@pytest.mark.skipif(
sys.version_info < (3, 10),
reason="This test assumes GeoAlchemy2 0.18.x and above, which does not support python 3.9",
@pytest.mark.parametrize(
"sqlalchemy_version", [(2, 1)], indirect=True, ids=["sqlalchemy21"]
)
@pytest.mark.parametrize(
"domain_type",
[JSONB, JSON],
)
def test_domain_non_default_json_sa21(
sqlalchemy_version: None,
generator: CodeGenerator,
domain_type: type[JSON] | type[JSONB],
) -> None:
Table(
"test_domain_json",
generator.metadata,
Column("id", BIGINT, primary_key=True),
Column(
"foo",
postgresql.DOMAIN(
"domain_json",
domain_type(astext_type=Text(128)),
not_null=False,
),
nullable=True,
),
)

validate_code(
generator.generate(),
f"""\
from typing import Optional

from sqlalchemy import BigInteger, Text
from sqlalchemy.dialects.postgresql import DOMAIN, {domain_type.__name__}
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

class Base(DeclarativeBase):
pass


class TestDomainJson(Base):
__tablename__ = 'test_domain_json'

id: Mapped[int] = mapped_column(BigInteger, primary_key=True)
foo: Mapped[Optional[object]] = mapped_column(DOMAIN('domain_json', {domain_type.__name__}(astext_type=Text(length=128)), not_null=False))
""",
)


@pytest.mark.parametrize(
"sqlalchemy_version", [(2, 0)], indirect=True, ids=["sqlalchemy20"]
)
@pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"])
def test_geoalchemy2_types(generator: CodeGenerator) -> None:
def test_geoalchemy2_types_sa20(
sqlalchemy_version: None, generator: CodeGenerator
) -> None:
Table(
"spatial_table",
generator.metadata,
Expand Down Expand Up @@ -2567,6 +2664,48 @@ class SpatialTable(Base):
)


@pytest.mark.parametrize(
"sqlalchemy_version", [(2, 1)], indirect=True, ids=["sqlalchemy21"]
)
@pytest.mark.parametrize("engine", ["postgresql"], indirect=["engine"])
def test_geoalchemy2_types_sa21(
sqlalchemy_version: None, generator: CodeGenerator
) -> None:
Table(
"spatial_table",
generator.metadata,
Column("id", INTEGER, primary_key=True),
Column("geom", Geometry("POINT", srid=4326, dimension=2), nullable=False),
Column("geog", Geography("POLYGON", dimension=2)),
)

validate_code(
generator.generate(),
"""\
from typing import Optional

from geoalchemy2.types import Geography, Geometry
from sqlalchemy import Index, Integer
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column

class Base(DeclarativeBase):
pass


class SpatialTable(Base):
__tablename__ = 'spatial_table'
__table_args__ = (
Index('idx_spatial_table_geog', 'geog', postgresql_using='gist'),
Index('idx_spatial_table_geom', 'geom', postgresql_using='gist')
)

id: Mapped[int] = mapped_column(Integer, primary_key=True)
geom: Mapped[object] = mapped_column(Geometry('POINT', 4326, 2, from_text='ST_GeomFromEWKT', name='geometry', nullable=False), nullable=False)
geog: Mapped[Optional[object]] = mapped_column(Geography('POLYGON', dimension=2, from_text='ST_GeogFromText', name='geography'))
""",
)


def test_enum_nonativeenums_option(generator: CodeGenerator) -> None:
Table(
"users",
Expand Down