|
| 1 | +"""Auto-migrate command line interface""" |
| 2 | + |
| 3 | +import click |
| 4 | + |
| 5 | + |
| 6 | +@click.group() |
| 7 | +def mara_catalog(): |
| 8 | + """Commands to interact with data lakes and lakehouses""" |
| 9 | + pass |
| 10 | + |
| 11 | + |
| 12 | +@mara_catalog.command() |
| 13 | +def connect( |
| 14 | + catalog: str = None, |
| 15 | + db_alias: str = None, |
| 16 | + |
| 17 | + # from mara_pipelines.ui.cli.run_pipeline |
| 18 | + disable_colors: bool= False |
| 19 | +): |
| 20 | + """ |
| 21 | + Connects a data lake or lakehouse catalog to a database |
| 22 | +
|
| 23 | + Args: |
| 24 | + catalog: The catalog to connect to. If not set, all configured catalogs will be connected. |
| 25 | + db_alias: The db alias the catalog shall be connected to. If not set, the default db alias is taken. |
| 26 | +
|
| 27 | + disable_colors: If true, don't use escape sequences to make the log colorful (default: colorful logging) |
| 28 | + """ |
| 29 | + |
| 30 | + from mara_pipelines.pipelines import Pipeline, Task |
| 31 | + from mara_pipelines.commands.python import RunFunction |
| 32 | + import mara_pipelines.ui.cli |
| 33 | + import mara_pipelines.config |
| 34 | + from . import config |
| 35 | + from .connect import connect_catalog_mara_commands |
| 36 | + |
| 37 | + # create pipeline |
| 38 | + pipeline = Pipeline( |
| 39 | + id='_mara_catalog_connect', |
| 40 | + description="Connects a catalog with a database") |
| 41 | + |
| 42 | + def create_schema_if_not_exist(db_alias: str, schema_name: str): |
| 43 | + import sqlalchemy |
| 44 | + import sqlalchemy.schema |
| 45 | + import mara_db.sqlalchemy_engine |
| 46 | + |
| 47 | + eng = mara_db.sqlalchemy_engine.engine(db_alias) |
| 48 | + |
| 49 | + if not eng.dialect.has_schema(eng): |
| 50 | + create_schema = sqlalchemy.schema.CreateSchema(schema_name) |
| 51 | + print(create_schema) |
| 52 | + eng.execute(create_schema) |
| 53 | + |
| 54 | + for catalog_name in [catalog] or config.catalogs(): |
| 55 | + catalog_pipeline = Pipeline( |
| 56 | + id=catalog_name, |
| 57 | + description=f"Connect catalog {catalog_name}") |
| 58 | + |
| 59 | + catalog = config.catalogs()[catalog_name] |
| 60 | + |
| 61 | + if catalog.schema_name: |
| 62 | + # create schema if it does not exist |
| 63 | + catalog_pipeline.add_initial( |
| 64 | + Task(id='create_schema', |
| 65 | + description=f'Creates tthe schema {catalog.schema_name} if it does not exist', |
| 66 | + commands=[ |
| 67 | + RunFunction( |
| 68 | + function=create_schema_if_not_exist, |
| 69 | + args=[ |
| 70 | + mara_pipelines.config.default_db_alias(), |
| 71 | + catalog.schema_name |
| 72 | + ])])) |
| 73 | + |
| 74 | + for command in connect_catalog_mara_commands(catalog=catalog, |
| 75 | + db_alias=db_alias or mara_pipelines.config.default_db_alias(), |
| 76 | + or_replace=True): |
| 77 | + catalog_pipeline.add(command) |
| 78 | + |
| 79 | + pipeline.add(catalog_pipeline) |
| 80 | + |
| 81 | + # run connect pipeline |
| 82 | + mara_pipelines.ui.cli.run_pipeline(pipeline, disable_colors=disable_colors) |
0 commit comments