Skip to content

← Tutorials overview

13. Migration manifest

Reference

The reference for this topic is Migration manifest.

A feature store built in DEV eventually lands in STAGING and PROD. The migration manifest turns the lineage graph of module 12 into a JSON document: a portable bill of materials of the views, tables and dependencies of a dataset.

import dataclasses
import getpass
import os

import tdfs4ds                      # importing it first also loads a .env file, if you have one
import teradataml as tdml

# 1. Open the Teradata session. Host and user come from the environment (TDFS4DS_TD_HOST,
#    TDFS4DS_TD_USERNAME), the password from TDFS4DS_TD_PASSWORD or a prompt: nothing secret
#    is written on this page.
host = os.environ["TDFS4DS_TD_HOST"]
user = os.environ["TDFS4DS_TD_USERNAME"]
password = os.environ.get("TDFS4DS_TD_PASSWORD") or getpass.getpass("Teradata password: ")
tdml.create_context(host=host, username=user, password=password)

# 2. Pick your industry. This is the ONLY line to change: retail, banking, healthcare, pharma,
#    manufacturing, assembly, energy or transportation. Every name below comes from S.
S = tdfs4ds.tutorial_scenario("retail")

# 3. Connect to the feature store (created when missing) and choose a data domain. disposable=True
#    records that this domain is an experiment, so the last page removes it in one call.
DATABASE = os.environ.get("TDFS4DS_TD_DATABASE") or user
tdfs4ds.connect(database=DATABASE, create_if_missing=True)
tdfs4ds.create_data_domain(f"TUTORIAL_{S.name.upper()}", disposable=True)   # an experiment: removable in one call

# 4. The tutorial data: three tables written once; a second run keeps them.
try:
    S = tdfs4ds.create_tutorial_data(S.name, DATABASE)
except ValueError:
    S = dataclasses.replace(S, database=DATABASE)


def register_entity_features():
    """Page 1 in one call: (re)register the entity features and return the process id."""
    sql = f"""
        SELECT {S.entity}, COUNT(*) AS nb_events, SUM({S.amount}) AS total_amount,
               AVG({S.amount}) AS avg_amount, COUNT(DISTINCT {S.partner}) AS nb_partners
        FROM {S.table('events')}
        GROUP BY 1"""
    return tdfs4ds.upload_features(
        df=None, sql_query=sql, target_view_name=f"{S.database}.{S.name}_entity_features",
        entity_id=[S.entity], feature_names=["nb_events", "total_amount", "avg_amount", "nb_partners"],
        replace=True, document=False, optimize_query=False, return_process_id=True)

process_id = register_entity_features()
dataset = tdfs4ds.build_dataset(
    entity_id=[S.entity],
    selected_features=["nb_events", "total_amount", "avg_amount", "nb_partners"],
    view_name=f"{S.database}.{S.name}_entity_dataset",
)

You should see a process id and the dataset built.

1 · Build the manifest

import json

from tdfs4ds.lineage import build_teradata_dependency_graph, graph_to_migration_manifest
from tdfs4ds.utils.show_text import show_text

dataset_sql = show_text(tdml.execute_sql(
    f"SHOW VIEW {S.database}.{S.name}_entity_dataset").fetchall()).replace("\r", "\n")
graph = build_teradata_dependency_graph(sql_query=dataset_sql)
manifest = graph_to_migration_manifest(graph, filter_database=DATABASE)
print({section: len(items) for section, items in manifest.items()})

Output with scenario='retail': the number of views, tables and edges of the manifest: the dataset and process views, the events table (with its primary index and partition columns) and the edges between them.

{'views': 2, 'tables': 1, 'edges': 2}
Section Content
views Every view's name, schema, DDL and dependency list
tables Source tables with primary index and partition columns
edges The directed dependency edges, restricted to the target database

filter_database keeps the objects of one schema and drops the edges that cross the boundary, so the manifest is a self-contained slice.

2 · Save it

with open(f"{S.name}_manifest.json", "w") as handle:
    json.dump(manifest, handle, indent=2, default=str)

You should see the file <flavor>_manifest.json: plain JSON, so a text diff between two environments shows a missing view, a stale partition clause or an extra dependency:

diff dev-manifest.json prod-manifest.json

3 · Promote it for real

The manifest describes; migrating is done by plan_migration (a read-only plan listing every object, manager and conflict, for approval) and migrate_objects (the only writer), which re-register each process with its own id in the target store and rebuild the datasets there:

plan = tdfs4ds.plan_migration(["<process or dataset>"], source="<lab database>")   # read-only
tdfs4ds.migrate_objects(plan, approvals={...})                                       # the only writer

The plan needs a second store (the source being promoted into the one you are connected to), so this tutorial does not run it. The target store rebuilds its own history with roll_out. See From prototype to production and the Migration manifest reference.

Composing with the rest of the tutorials

  • Module 8 › Partitioning: the manifest keeps primary_index and partition_columns, so PROD is partitioned like DEV.
  • Module 10 › Catalogs and datasets: after a migration the target catalogs hold the registered processes and datasets, which keeps lineage and documentation consistent.
  • Module 11 › Logs: the follow-up table is per environment, so each replay writes its own RUN_ID rows in PROD.

Clean up: remove everything the tutorials created

The tutorial domain was created disposable (create_data_domain(..., disposable=True) in the first block of every page), so one call removes everything the feature store holds for it: the processes and their views, the datasets, the FilterManager and TimeManager (views, tables of dates, registry rows), the features, their stored values and the storage tables left empty. Read the plan first; nothing is removed by the dry run:

plan = tdfs4ds.remove_data_domain(f"TUTORIAL_{S.name.upper()}")      # dry run: prints the plan

Output with scenario='retail' after modules 6 to 11 (43 steps, shortened): the steps in order (datasets, processes, features, value purges, managers, storage tables, the domain's settings and its disposable mark last) and, under Kept, anything shared with another domain, with the reason.

Removal of the data domain TUTORIAL_RETAIL: 43 step(s).
    1. remove_dataset         MY_DATABASE.RETAIL_MONTHLY_HISTORY  <- reads nb_events_to_date, total_amount_to_date computed by MY_DATABASE.RETAIL_MONTHLY_FEATURES (<process id>)
    ...
    6. remove_process         MY_DATABASE.RETAIL_MONTHLY_FEATURES (<process id>)  <- requested
    7. remove_feature         nb_events_to_date (TUTORIAL_RETAIL, CustomerID)  <- computed by MY_DATABASE.RETAIL_MONTHLY_FEATURES (<process id>)
    ...
   33. drop_time_manager      MY_DATABASE.RETAIL_MONTH_END and MY_DATABASE.RETAIL_MONTH_END_HIDDEN  <- TimeManager of <process id>: nothing else reads
   ...
   43. delete_domain_settings disposable mark of TUTORIAL_RETAIL  <- data domain removal, last step
Source tables are never removed.

Then remove it, and drop the three tutorial tables (they are your source data: the domain removal never touches source tables):

tdfs4ds.remove_data_domain(f"TUTORIAL_{S.name.upper()}", dry_run=False, refresh_index=False)
print(tdfs4ds.drop_tutorial_data(S.name, DATABASE))
tdml.remove_context()

Output: the statements that dropped the three tutorial tables:

['DROP TABLE my_database.retail_customers', 'DROP TABLE my_database.retail_merchants', 'DROP TABLE my_database.retail_transactions']

Nothing named <flavor>_... is left, and tdfs4ds.get_data_domains(verbose=False) no longer lists the tutorial domain. remove_data_domain refuses a plan the store changed under since it was read (plan again), and one that would break an object of another domain (it is listed as BLOCKING). To keep your tutorial store instead, call tdfs4ds.set_data_domain_disposable(f"TUTORIAL_{S.name.upper()}", False): the one-call removal then refuses it.


That is the end of the tutorials. The Concepts section explains the architecture, Configuration covers every setting, and the API reference documents every public function.

Learn more