Skip to content

Commit

Permalink
Add load-tap-schema command to CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyMcCormick committed Aug 29, 2024
1 parent a26a5b8 commit 54a4f55
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions python/felis/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down Expand Up @@ -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(

Check warning on line 396 in python/felis/cli.py

View check run for this annotation

Codecov / codecov/patch

python/felis/cli.py#L395-L396

Added lines #L395 - L396 were not covered by tests
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(

Check warning on line 403 in python/felis/cli.py

View check run for this annotation

Codecov / codecov/patch

python/felis/cli.py#L403

Added line #L403 was not covered by tests
yaml.load(file, Loader=yaml.SafeLoader), context={"generate_ids": ctx.obj["id_generation"]}
)

DataLoader(

Check warning on line 407 in python/felis/cli.py

View check run for this annotation

Codecov / codecov/patch

python/felis/cli.py#L407

Added line #L407 was not covered by tests
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
Expand Down

0 comments on commit 54a4f55

Please sign in to comment.