From 54a4f55c952654d8e0132fe7b84df0e993f7d603 Mon Sep 17 00:00:00 2001 From: Jeremy McCormick Date: Wed, 21 Aug 2024 15:20:44 -0500 Subject: [PATCH] Add load-tap-schema command to CLI --- python/felis/cli.py | 70 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/python/felis/cli.py b/python/felis/cli.py index 36727ebe..9f188d05 100644 --- a/python/felis/cli.py +++ b/python/felis/cli.py @@ -39,6 +39,7 @@ from .db.utils import DatabaseContext from .metadata import MetaDataBuilder from .tap import Tap11Base, TapLoadingVisitor, init_tables +from .tap_schema import DataLoader, TableManager __all__ = ["cli"] @@ -345,6 +346,75 @@ def load_tap( tap_visitor.visit_schema(schema) +@cli.command("load-tap-schema", help="Load metadata from a Felis file into a TAP_SCHEMA database") +@click.option("--engine-url", envvar="FELIS_ENGINE_URL", help="SQLAlchemy Engine URL") +@click.option("--tap-schema-name", help="Name of the TAP_SCHEMA schema in the database") +@click.option( + "--tap-tables-postfix", help="Postfix which is applied to standard TAP_SCHEMA table names", default="" +) +@click.option("--tap-schema-index", type=int, help="TAP_SCHEMA index of the schema in this environment") +@click.option("--dry-run", is_flag=True, help="Execute dry run only. Does not insert any data.") +@click.option("--echo", is_flag=True, help="Print out the generated insert statements to stdout") +@click.option("--output-file", type=click.File(mode="w"), help="Write SQL commands to a file") +@click.argument("file", type=click.File()) +def load_tap_schema( + ctx: click.Context, + engine_url: str, + tap_schema_name: str, + tap_tables_postfix: str, + tap_schema_index: int, + dry_run: bool, + echo: bool, + output_file: IO[str] | None, + file: io.TextIOBase, +) -> None: + """Load TAP metadata from a Felis file. + + Parameters + ---------- + engine_url + SQLAlchemy Engine URL. + tap_tables_postfix + Postfix which is applied to standard TAP_SCHEMA table names. + tap_schema_index + TAP_SCHEMA index of the schema in this environment. + dry_run + Execute dry run only. Does not insert any data. + echo + Print out the generated insert statements to stdout. + output_file + Output file for writing generated SQL. + file + Felis file to read. + + Notes + ----- + The TAP_SCHEMA database must already exist or the command will fail. This + command will not initialize the TAP_SCHEMA tables. + """ + engine = create_engine(engine_url) + mgr = TableManager( + engine=engine, + apply_schema_to_metadata=False if engine.dialect.name == "sqlite" else True, + schema_name=tap_schema_name, + table_name_postfix=tap_tables_postfix, + ) + + schema = Schema.model_validate( + yaml.load(file, Loader=yaml.SafeLoader), context={"generate_ids": ctx.obj["id_generation"]} + ) + + DataLoader( + schema, + mgr, + engine, + tap_schema_index=tap_schema_index, + dry_run=dry_run, + print_sql=echo, + outfile=output_file, + ).load() + + @cli.command("validate", help="Validate one or more Felis YAML files") @click.option( "--check-description", is_flag=True, help="Check that all objects have a description", default=False