Skip to content
Draft
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
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 11 additions & 1 deletion src/sqlacodegen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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__
Expand Down
23 changes: 19 additions & 4 deletions src/sqlacodegen/generators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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] = ()
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -1375,6 +1386,7 @@ def __init__(
metadata: MetaData,
bind: Connection | Engine,
options: Sequence[str],
ignored: set[str],
*,
indentation: str = " ",
base_class_name: str = "Base",
Expand All @@ -1385,6 +1397,7 @@ def __init__(
metadata,
bind,
options,
ignored,
indentation=indentation,
base_class_name=base_class_name,
)
Expand All @@ -1411,6 +1424,7 @@ def __init__(
metadata: MetaData,
bind: Connection | Engine,
options: Sequence[str],
ignored: set[str],
*,
indentation: str = " ",
base_class_name: str = "SQLModel",
Expand All @@ -1419,6 +1433,7 @@ def __init__(
metadata,
bind,
options,
ignored,
indentation=indentation,
base_class_name=base_class_name,
)
Expand Down
Loading