Skip to content

tdfs4ds.migration.script

tdfs4ds.migration.script

Generate a standalone Python script that re-plans and executes a cross-store migration with a baked-in approval map (v0.3.1.150).

Mirrors :mod:tdfs4ds.scheduling.script: the script re-derives a fresh manifest at runtime (plan_migration) from the baked-in seeds / source / target_data_domain and then runs migrate_objects with the baked-in approvals — so the operator can review the emitted .py before executing a lab→production promotion. Each worker opens its own Teradata context from the TDFS4DS_TD_* environment via reconnect_teradata (same as the scheduler's generated script).

_TEMPLATE module-attribute

_TEMPLATE = '#!/usr/bin/env python\n"""\nAuto-generated by tdfs4ds {version} on {generated_at}.\n\nCross-store MIGRATION script (lab -> production promotion).\nSource: {source_view}  ->  primary schema (from the environment).\n{n_processes} process(es), {n_objects} object(s), {n_conflicts} conflict(s).\n\nReview the APPROVALS below before running - a `replace` promotes over an existing\nproduction object. Run:   python {basename}\n"""\nimport json\n\nSOURCE = json.loads({source_literal})\nSEEDS = json.loads({seeds_literal})\n# A domain name, or {{source domain: target domain}} (\'*\' = any other domain).\nTARGET_DATA_DOMAIN = {target_domain}\nDATA_STRATEGY = {data_strategy}\nAPPROVALS = json.loads({approvals_literal})\n# Per-table decisions taken on the reviewed plan (copy the table, or read it in\n# place). Re-applied after re-planning: the object keys are stable.\nDATA_CHOICES = json.loads({data_choices_literal})\n# Target database per kind of object (source_table, intermediate_view,\n# process_view, dataset, downstream_view); empty = the primary store\'s own databases.\nDATABASE_MAP = json.loads({database_map_literal})\n# Who runs the processes and who reads the datasets in the target store (None = the\n# session user / not named yet: the GRANTs of those groups are then placeholders).\nRUN_USER = {run_user}\nDATASET_READERS = json.loads({readers_literal})\nSANITY = {sanity}\n# The other plan_migration options of the reviewed plan (outside_objects, data_domains,\n# include_downstream); empty = their defaults.\nPLAN_OPTIONS = json.loads({plan_options_literal})\n\n\ndef _bootstrap():\n    import tdfs4ds\n    tdfs4ds.require({version_literal})   # the version that generated this script, or later\n    from tdfs4ds.utils.reconnect import reconnect_teradata\n    if not reconnect_teradata():\n        raise RuntimeError(\n            "reconnect_teradata() failed - set TDFS4DS_TD_HOST/USERNAME/PASSWORD "\n            "(and optionally _LOGMECH/_DATABASE) in the environment."\n        )\n    if {primary_schema}:\n        tdfs4ds.SCHEMA = {primary_schema}\n    if {primary_schema_view}:\n        tdfs4ds.SCHEMA_VIEW = {primary_schema_view}\n    return tdfs4ds\n\n\ndef main():\n    tdfs4ds = _bootstrap()\n    plan = tdfs4ds.plan_migration(\n        seeds=SEEDS or None, source=SOURCE,\n        target_data_domain=TARGET_DATA_DOMAIN, data_strategy=DATA_STRATEGY,\n        database_map=DATABASE_MAP or None, run_user=RUN_USER,\n        dataset_readers=DATASET_READERS or None, sanity=SANITY, **PLAN_OPTIONS)\n    for key, choice in DATA_CHOICES.items():\n        if key in plan.get("objects", {{}}):\n            plan["objects"][key]["data_choice"] = choice\n    print(tdfs4ds.render_migration_report(plan))\n    # Reports, does not stop: whether the GRANTs of the report were applied yet\n    # (run as the user that will run the processes to confirm them).\n    access = tdfs4ds.check_access(plan)\n    print("check_access:", json.dumps({{k: access.get(k) for k in ("user", "ok", "refused", "broken")}},\n                                      indent=2, default=str))\n    if plan.get("cycle"):\n        raise SystemExit("Circular dependency in the plan - aborting.")\n    report = tdfs4ds.migrate_objects(plan, approvals=APPROVALS, progress=print)\n    print(json.dumps(report, indent=2, default=str))\n\n\nif __name__ == "__main__":\n    main()\n'

_PLAN_OPTIONS module-attribute

_PLAN_OPTIONS = (('outside_objects', 'move'), ('data_domains', None), ('include_downstream', False))

_plain_map

_plain_map(dbmap) -> Dict[str, Any]

The plan's normalised map back in the argument's form ('*' alone = a name).

generate_migration_script

generate_migration_script(plan: Dict[str, Any], approvals: Optional[Dict[str, str]] = None, *, data_strategy: Optional[str] = None) -> str

Return a standalone .py (string) that re-plans and executes this migration with the given approvals baked in. Inspect before executing. The text is ASCII only, and the script calls tdfs4ds.require(<this version>) first.

write_migration_script

write_migration_script(plan: Dict[str, Any], out_path: Optional[str] = None, approvals: Optional[Dict[str, str]] = None, **kw) -> str

Write the generated migration script under tdfs4ds.SCHEDULING_SCRIPT_DIR (or out_path); return its path.