From 0e2c6ee5859bdbface7c881be4dc4bc4dabc3357 Mon Sep 17 00:00:00 2001 From: sdementen Date: Tue, 17 Oct 2023 11:33:45 +0200 Subject: [PATCH 1/6] enable finer selection on tables allow to express the tables to process via regexp both to explicitly keep tables or to explicitly exclude tables --- src/sqlacodegen/cli.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/sqlacodegen/cli.py b/src/sqlacodegen/cli.py index e176e85a..5ac13628 100644 --- a/src/sqlacodegen/cli.py +++ b/src/sqlacodegen/cli.py @@ -1,6 +1,7 @@ from __future__ import annotations import argparse +import re import sys from contextlib import ExitStack from typing import TextIO @@ -46,7 +47,10 @@ def main() -> None: help="generator class to use", ) parser.add_argument( - "--tables", help="tables to process (comma-delimited, default: all)" + "--tables", help="tables to process (comma-delimited strings or regexp, default: all)" + ) + parser.add_argument( + "--exclude-tables", help="tables to exclude (comma-delimited strings or regexp, default: none)" ) parser.add_argument("--noviews", action="store_true", help="ignore views") parser.add_argument("--outfile", help="file to write output to (default: stdout)") @@ -69,6 +73,15 @@ def main() -> None: # Use reflection to fill in the metadata engine = create_engine(args.url) metadata = MetaData() + tables = engine.table_names() + if args.tables: + # only keep the tables defined in args.tables + filter = re.compile(args.tables.replace(",","|") + tables = [t for t in tables if filter.match(t)] + if args.exclude_tables: + # exclude the tables defined in args.exclude_tables + filter = re.compile(args.exclude_tables.replace(",","|") + tables = [t for t in tables if not filter.match(t)] tables = args.tables.split(",") if args.tables else None schemas = args.schemas.split(",") if args.schemas else [None] options = set(args.options.split(",")) if args.options else set() From f9bae1e056a48499bcdd7fed99ee1e26fe20b70b Mon Sep 17 00:00:00 2001 From: sdementen Date: Tue, 17 Oct 2023 11:37:59 +0200 Subject: [PATCH 2/6] fix typo --- src/sqlacodegen/cli.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sqlacodegen/cli.py b/src/sqlacodegen/cli.py index 5ac13628..e39b8d86 100644 --- a/src/sqlacodegen/cli.py +++ b/src/sqlacodegen/cli.py @@ -76,11 +76,11 @@ def main() -> None: tables = engine.table_names() if args.tables: # only keep the tables defined in args.tables - filter = re.compile(args.tables.replace(",","|") + filter = re.compile(args.tables.replace(",","|")) tables = [t for t in tables if filter.match(t)] if args.exclude_tables: # exclude the tables defined in args.exclude_tables - filter = re.compile(args.exclude_tables.replace(",","|") + filter = re.compile(args.exclude_tables.replace(",","|")) tables = [t for t in tables if not filter.match(t)] tables = args.tables.split(",") if args.tables else None schemas = args.schemas.split(",") if args.schemas else [None] From 04d188fa0e68e8dcafeeb20083a87c40d26dceea Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 17 Oct 2023 09:38:36 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/sqlacodegen/cli.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/sqlacodegen/cli.py b/src/sqlacodegen/cli.py index e39b8d86..c4507f61 100644 --- a/src/sqlacodegen/cli.py +++ b/src/sqlacodegen/cli.py @@ -47,10 +47,12 @@ def main() -> None: help="generator class to use", ) parser.add_argument( - "--tables", help="tables to process (comma-delimited strings or regexp, default: all)" + "--tables", + help="tables to process (comma-delimited strings or regexp, default: all)", ) parser.add_argument( - "--exclude-tables", help="tables to exclude (comma-delimited strings or regexp, default: none)" + "--exclude-tables", + help="tables to exclude (comma-delimited strings or regexp, default: none)", ) parser.add_argument("--noviews", action="store_true", help="ignore views") parser.add_argument("--outfile", help="file to write output to (default: stdout)") @@ -76,11 +78,11 @@ def main() -> None: tables = engine.table_names() if args.tables: # only keep the tables defined in args.tables - filter = re.compile(args.tables.replace(",","|")) + filter = re.compile(args.tables.replace(",", "|")) tables = [t for t in tables if filter.match(t)] if args.exclude_tables: # exclude the tables defined in args.exclude_tables - filter = re.compile(args.exclude_tables.replace(",","|")) + filter = re.compile(args.exclude_tables.replace(",", "|")) tables = [t for t in tables if not filter.match(t)] tables = args.tables.split(",") if args.tables else None schemas = args.schemas.split(",") if args.schemas else [None] From f2f965d0207a426dae475db3a15af90b644113b7 Mon Sep 17 00:00:00 2001 From: sdementen Date: Tue, 17 Oct 2023 11:42:51 +0200 Subject: [PATCH 4/6] use inspect in sa 2.0 to get table names --- src/sqlacodegen/cli.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/sqlacodegen/cli.py b/src/sqlacodegen/cli.py index c4507f61..68f83ba0 100644 --- a/src/sqlacodegen/cli.py +++ b/src/sqlacodegen/cli.py @@ -6,7 +6,7 @@ from contextlib import ExitStack from typing import TextIO -from sqlalchemy.engine import create_engine +from sqlalchemy.engine import create_engine, inspect from sqlalchemy.schema import MetaData try: @@ -75,7 +75,14 @@ def main() -> None: # Use reflection to fill in the metadata engine = create_engine(args.url) metadata = MetaData() - tables = engine.table_names() + try: + # sa 1.4 + tables = engine.table_names() + except AttributeError: + # sa 2.0 + inspection = inspect(engine) + tables = inspection.get_tables_names() + if args.tables: # only keep the tables defined in args.tables filter = re.compile(args.tables.replace(",", "|")) From 49e92c427516aaa898fedaad8007c6f7dec34a21 Mon Sep 17 00:00:00 2001 From: sdementen Date: Tue, 17 Oct 2023 11:44:16 +0200 Subject: [PATCH 5/6] fix import --- src/sqlacodegen/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sqlacodegen/cli.py b/src/sqlacodegen/cli.py index 68f83ba0..6a923d1e 100644 --- a/src/sqlacodegen/cli.py +++ b/src/sqlacodegen/cli.py @@ -6,7 +6,7 @@ from contextlib import ExitStack from typing import TextIO -from sqlalchemy.engine import create_engine, inspect +from sqlalchemy import create_engine, inspect from sqlalchemy.schema import MetaData try: From 7049386b7a3a548f8b8bab4bec8e435735c094be Mon Sep 17 00:00:00 2001 From: sdementen Date: Tue, 17 Oct 2023 11:45:24 +0200 Subject: [PATCH 6/6] fix typo on get_table_names --- src/sqlacodegen/cli.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sqlacodegen/cli.py b/src/sqlacodegen/cli.py index 6a923d1e..8301cb40 100644 --- a/src/sqlacodegen/cli.py +++ b/src/sqlacodegen/cli.py @@ -81,7 +81,7 @@ def main() -> None: except AttributeError: # sa 2.0 inspection = inspect(engine) - tables = inspection.get_tables_names() + tables = inspection.get_table_names() if args.tables: # only keep the tables defined in args.tables