Skip to content

Commit 5370ebc

Browse files
authored
Merge branch 'master' into master
2 parents 28fa26f + e776bb7 commit 5370ebc

File tree

6 files changed

+44
-12
lines changed

6 files changed

+44
-12
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ repos:
1616
- id: trailing-whitespace
1717

1818
- repo: https://github.com/astral-sh/ruff-pre-commit
19-
rev: v0.8.4
19+
rev: v0.9.2
2020
hooks:
2121
- id: ruff
2222
args: [--fix, --show-fixes]
2323
- id: ruff-format
2424

2525
- repo: https://github.com/pre-commit/mirrors-mypy
26-
rev: v1.14.0
26+
rev: v1.14.1
2727
hooks:
2828
- id: mypy
2929
additional_dependencies:

README.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ values must be delimited by commas, e.g. ``--options noconstraints,nobidi``):
117117

118118
* all the options from ``declarative``
119119

120-
* ``sqlmodel``
120+
* ``sqlmodels``
121121

122122
* all the options from ``declarative``
123123

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,13 @@ dynamic = ["version"]
4141

4242
[project.optional-dependencies]
4343
test = [
44+
"sqlacodegen[sqlmodel]",
4445
"pytest >= 7.4",
4546
"coverage >= 7",
4647
"psycopg2-binary",
4748
"mysql-connector-python",
4849
]
49-
sqlmodel = ["sqlmodel >= 0.0.12"]
50+
sqlmodel = ["sqlmodel >= 0.0.22"]
5051
citext = ["sqlalchemy-citext >= 1.7.0"]
5152
geoalchemy2 = ["geoalchemy2 >= 0.11.1"]
5253
pgvector = ["pgvector >= 0.2.4"]

src/sqlacodegen/cli.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ def main() -> None:
5353
parser.add_argument(
5454
"--tables", help="tables to process (comma-delimited, default: all)"
5555
)
56-
parser.add_argument("--noviews", action="store_true", help="ignore views")
56+
parser.add_argument(
57+
"--noviews",
58+
action="store_true",
59+
help="ignore views (always true for sqlmodels generator)",
60+
)
5761
parser.add_argument("--outfile", help="file to write output to (default: stdout)")
5862
args = parser.parse_args()
5963

@@ -81,13 +85,23 @@ def main() -> None:
8185
tables = args.tables.split(",") if args.tables else None
8286
schemas = args.schemas.split(",") if args.schemas else [None]
8387
options = set(args.options.split(",")) if args.options else set()
84-
for schema in schemas:
85-
metadata.reflect(engine, schema, not args.noviews, tables)
8688

8789
# Instantiate the generator
8890
generator_class = generators[args.generator].load()
8991
generator = generator_class(metadata, engine, options)
9092

93+
if not generator.views_supported:
94+
name = generator_class.__name__
95+
print(
96+
f"VIEW models will not be generated when using the '{name}' generator",
97+
file=sys.stderr,
98+
)
99+
100+
for schema in schemas:
101+
metadata.reflect(
102+
engine, schema, (generator.views_supported and not args.noviews), tables
103+
)
104+
91105
# Open the target file (if given)
92106
with ExitStack() as stack:
93107
outfile: TextIO

src/sqlacodegen/generators.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ def __init__(
106106
if invalid_options:
107107
raise ValueError("Unrecognized options: " + ", ".join(invalid_options))
108108

109+
@property
110+
@abstractmethod
111+
def views_supported(self) -> bool:
112+
pass
113+
109114
@abstractmethod
110115
def generate(self) -> str:
111116
"""
@@ -134,6 +139,10 @@ def __init__(
134139
self.imports: dict[str, set[str]] = defaultdict(set)
135140
self.module_imports: set[str] = set()
136141

142+
@property
143+
def views_supported(self) -> bool:
144+
return True
145+
137146
def generate_base(self) -> None:
138147
self.base = Base(
139148
literal_imports=[LiteralImport("sqlalchemy", "MetaData")],
@@ -482,6 +491,9 @@ def render_column(
482491
if comment:
483492
kwargs["comment"] = repr(comment)
484493

494+
return self.render_column_callable(is_table, *args, **kwargs)
495+
496+
def render_column_callable(self, is_table: bool, *args: Any, **kwargs: Any) -> str:
485497
if is_table:
486498
self.add_import(Column)
487499
return render_callable("Column", *args, kwargs=kwargs)
@@ -1331,10 +1343,7 @@ def generate_base(self) -> None:
13311343
LiteralImport("sqlalchemy.orm", "MappedAsDataclass"),
13321344
],
13331345
declarations=[
1334-
(
1335-
f"class {self.base_class_name}(MappedAsDataclass, "
1336-
"DeclarativeBase):"
1337-
),
1346+
(f"class {self.base_class_name}(MappedAsDataclass, DeclarativeBase):"),
13381347
f"{self.indentation}pass",
13391348
],
13401349
metadata_ref=f"{self.base_class_name}.metadata",
@@ -1359,6 +1368,14 @@ def __init__(
13591368
base_class_name=base_class_name,
13601369
)
13611370

1371+
@property
1372+
def views_supported(self) -> bool:
1373+
return False
1374+
1375+
def render_column_callable(self, is_table: bool, *args: Any, **kwargs: Any) -> str:
1376+
self.add_import(Column)
1377+
return render_callable("Column", *args, kwargs=kwargs)
1378+
13621379
def generate_base(self) -> None:
13631380
self.base = Base(
13641381
literal_imports=[],

tests/test_cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def test_cli_sqlmodels(db_path: Path, tmp_path: Path) -> None:
145145
class Foo(SQLModel, table=True):
146146
id: Optional[int] = Field(default=None, sa_column=Column('id', Integer, \
147147
primary_key=True))
148-
name: str = Field(sa_column=Column('name', Text, nullable=False))
148+
name: str = Field(sa_column=Column('name', Text))
149149
"""
150150
)
151151

0 commit comments

Comments
 (0)