diff --git a/CHANGES.rst b/CHANGES.rst index 96623d9a..0177f15b 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -1,6 +1,10 @@ Version history =============== + +**UNRELEASED** +- Add optional `--ignored-tables` argument (PR by @plaes) + **3.1.0** - Type annotations for ARRAY column attributes now include the Python type of diff --git a/src/sqlacodegen/cli.py b/src/sqlacodegen/cli.py index 07f7ea59..bfc82819 100644 --- a/src/sqlacodegen/cli.py +++ b/src/sqlacodegen/cli.py @@ -93,6 +93,13 @@ def main() -> None: ), ) parser.add_argument("--outfile", help="file to write output to (default: stdout)") + parser.add_argument( + "--ignored_tables", + default=None, + nargs="?", + type=argparse.FileType("r"), + help="file containing list of ignored table names separated by newlines (default: None)", + ) args = parser.parse_args() if args.version: @@ -121,9 +128,12 @@ def main() -> None: schemas = args.schemas.split(",") if args.schemas else [None] options = set(args.options.split(",")) if args.options else set() + # Prepare set of ignored tables + ignored = {t.strip().lower() for t in args.ignored_tables} + # Instantiate the generator generator_class = generators[args.generator].load() - generator = generator_class(metadata, engine, options) + generator = generator_class(metadata, engine, options, ignored) if not generator.views_supported: name = generator_class.__name__ diff --git a/src/sqlacodegen/generators.py b/src/sqlacodegen/generators.py index 7b4901a7..bc119129 100644 --- a/src/sqlacodegen/generators.py +++ b/src/sqlacodegen/generators.py @@ -93,11 +93,16 @@ class CodeGenerator(metaclass=ABCMeta): valid_options: ClassVar[set[str]] = set() def __init__( - self, metadata: MetaData, bind: Connection | Engine, options: Sequence[str] + self, + metadata: MetaData, + bind: Connection | Engine, + options: Sequence[str], + ignored: set[str], ): self.metadata: MetaData = metadata self.bind: Connection | Engine = bind self.options: set[str] = set(options) + self.ignored: set[str] = ignored # Validate options invalid_options = {opt for opt in options if opt not in self.valid_options} @@ -127,10 +132,11 @@ def __init__( metadata: MetaData, bind: Connection | Engine, options: Sequence[str], + ignored: str[str], *, indentation: str = " ", ): - super().__init__(metadata, bind, options) + super().__init__(metadata, bind, options, ignored) self.indentation: str = indentation self.imports: dict[str, set[str]] = defaultdict(set) self.module_imports: set[str] = set() @@ -618,7 +624,11 @@ def add_fk_options(*opts: Any) -> None: def should_ignore_table(self, table: Table) -> bool: # Support for Alembic and sqlalchemy-migrate -- never expose the schema version # tables - return table.name in ("alembic_version", "migrate_version") + if table.name in ("alembic_version", "migrate_version"): + return True + if table.name in self.ignored: + return True + return False def find_free_name( self, name: str, global_names: set[str], local_names: Collection[str] = () @@ -759,11 +769,12 @@ def __init__( metadata: MetaData, bind: Connection | Engine, options: Sequence[str], + ignored: set[str], *, indentation: str = " ", base_class_name: str = "Base", ): - super().__init__(metadata, bind, options, indentation=indentation) + super().__init__(metadata, bind, options, ignored, indentation=indentation) self.base_class_name: str = base_class_name self.inflect_engine = inflect.engine() @@ -1375,6 +1386,7 @@ def __init__( metadata: MetaData, bind: Connection | Engine, options: Sequence[str], + ignored: set[str], *, indentation: str = " ", base_class_name: str = "Base", @@ -1385,6 +1397,7 @@ def __init__( metadata, bind, options, + ignored, indentation=indentation, base_class_name=base_class_name, ) @@ -1411,6 +1424,7 @@ def __init__( metadata: MetaData, bind: Connection | Engine, options: Sequence[str], + ignored: set[str], *, indentation: str = " ", base_class_name: str = "SQLModel", @@ -1419,6 +1433,7 @@ def __init__( metadata, bind, options, + ignored, indentation=indentation, base_class_name=base_class_name, )