## lamindb_setup.evolve

Manage schema evolutions for datasets.

Import plan declarations before connecting to an instance:

 from lamindb_setup.evolve import create_plan

Use the "lamin evolve" CLI to validate, preview, and apply the plan.

Internal subpackages may change without notice.

# Plans

Define a "Plan" and declare its reusable outputs with "PlanExports".

| --- | --- |
| "create_p | Turn a declaration function into a "Plan". |
| lan"(func) |
| --- | --- |

* Plans

  * Define a plan

 * "create_plan()"

 * "Plan"

 * "Plan.defining_module"

 * "Plan.ctx"

 * "Plan.exports"

 * "Plan.dependencies"

 * "Plan.own_dependencies"

 * "Plan.inherited_dependencies"

 * "Plan.inherited_moves"

 * "Plan.depends_on"

 * "Plan.active()"

 * "Plan.run()"

 * "Plan.add_dependency()"

 * "Plan.add_plan_dependency()"

 * "Plan.finalize()"

 * "Plan.reset()"

 * "PlanContext"

 * "PlanContext.migrations"

 * "PlanContext.moves"

 * "PlanContext.migrate()"

 * "PlanContext.moved()"

 * "PlanExports"

 * "PlanDeclarationError"

  * Plan result types

 * "SpaceUID"

 * "SpaceUID.registry_name"

 * "FeatureUID"

 * "FeatureUID.registry_name"

 * "RecordUID"

 * "RecordUID.registry_name"

 * "SchemaUID"

 * "SchemaUID.registry_name"

 * "Promise"

 * "Promise.label"

 * "Promise.resolve()"

 * "Promise.get_arg()"

 * "Dependency"

 * "DependencyDAG"

 * "DependencyDAG.ordered()"

 * "Moved"

 * "Migrated"

 * "WriteStrategy"

 * "WriteStrategy.BATCHED_LINKS"

 * "WriteStrategy.ADD_VALUES"

| --- | --- |
| "Plan"(na | Collect metadata declarations for validation, preview, and apply. |
| me) |
| --- | --- |
| "PlanCont | Mutable context passed to "Plan.run()" for plan-wide settings. |
| ext"() |
| --- | --- |
| "PlanExpo | Base class for declaring the reusable outputs of a "Plan". |
| rts"() |
| --- | --- |
| "PlanDecl | A plan declares something that can never do what it says. |
| arationEr |
| ror" |
| --- | --- |

# Declarations

Declare features and group them into a "Schema", "RecordType", or
"Sheet".

| --- | --- |
| "Feature" | Declare a feature and its validation settings. |
| (name, *, |
| dtype[, |
| type, |
| synonyms, |
| ...]) |
| --- | --- |

* Declarations

  * Features

 * "FeatureType"

 * "FeatureType.uid"

 * "FeatureType.get()"

 * "FeatureType.stage()"

 * "Feature"

 * "Feature.uid"

 * "Feature.get()"

 * "Feature.stage()"

  * Schemas

 * "SchemaType"

 * "SchemaType.uid"

 * "SchemaType.get()"

 * "SchemaType.stage()"

 * "Schema"

 * "Schema.itype"

 * "Schema.dtype"

 * "Schema.uid"

 * "Schema.get()"

 * "Schema.stage()"

  * Records and sheets

 * "RecordType"

 * "RecordType.uid"

 * "RecordType.get()"

 * "RecordType.stage()"

 * "Sheet"

 * "Sheet.stage()"

 * "Record"

 * "Record.uid"

 * "Record.get()"

 * "Record.stage()"

| --- | --- |
| "FeatureT | Declare a type that groups related features. |
| ype"(name, |
| *[, type, |
| space_uid |
| ]) |
| --- | --- |
| "SchemaTy | Declare a type that groups related schemas. |
| pe"(name, |
| *[, descr |
| iption, |
| type, |
| ...]) |
| --- | --- |
| "Schema"( | Declare validation rules for a dataset or a sheet's records. |
| [name, |
| index, |
| optionals, |
| ...]) |
| --- | --- |
| "RecordTy | Declare a type that classifies records ("is_type=True"). |
| pe"(name, |
| *[, descr |
| iption, |
| type, |
| ...]) |
| --- | --- |
| "Sheet"(n | Declare a record type whose records must satisfy "schema". |
| ame, *, |
| schema[, |
| descripti |
| on, type, |
| ...]) |
| --- | --- |
| "Record"( | Seed a named record, optionally with a type and feature values. |
| [name, |
| type, |
| features, |
| descripti |
| on, ...]) |
| --- | --- |

# Typed models and dtypes

Use Python type annotations and "feature()" to describe schema fields.

| --- | --- |
| "SchemaMo | Declarative base class for schemas (see the module docstring). |
| del"() |
| --- | --- |

* Typed models and dtypes

  * Schema models

 * "SchemaModel"

 * "SchemaConfig"

  * Feature dtypes

 * "LaminDType"

 * "LaminDType.dtype"

 * "LaminDType.dtype_str"

 * "DType"

 * "ListDType"

 * "CatDType"

 * "DEFAULT_NAME_FIELD"

 * "RecordCat"

 * "RecordCat.name_field"

 * "RecordCat.schema__uid"

 * "RecordCat.is_type"

 * "ULabelCat"

 * "ULabelCat.name_field"

 * "ULabelCat.type__uid"

 * "ResolvedDType"

 * "ResolvedDType.dtype_str"

 * "get_dtype()"

 * "inline_cat_filters()"

  * Field declarations

 * "feature()"

| --- | --- |
| "SchemaCo | Schema-level configuration for a "SchemaModel". |
| nfig" |
| --- | --- |
| "LaminDTy | Legacy dtype wrapper exposing an object "dtype" + "cat_filters" dict. |
| pe"() |
| --- | --- |
| "DType"(* | A simple, non-categorical dtype (annotation only, e.g. "x: DType[int]"). |
| args, |
| **kwargs) |
| --- | --- |
| "ListDTyp | A list dtype (annotation only, e.g. "x: ListDType[int]"). |
| e"(*args, |
| **kwargs) |
| --- | --- |
| "CatDType" | Annotate a registry type with the filters for a categorical feature. |
| --- | --- |
| "DEFAULT_ | Default lookup field for each categorical registry. |
| NAME_FIEL |
| D" |
| --- | --- |
| "RecordCa | Filters for "cat[Record[...]]" features. |
| t"(*, typ |
| e__uid[, |
| name_fiel |
| d, ...]) |
| --- | --- |
| "ULabelCa | Filters for "cat[ULabel]" features. |
| t"(*[, na |
| me_field, |
| type__uid |
| ]) |
| --- | --- |
| "Resolved | lamindb's canonical dtype string + "cat_filters" for an annotation. |
| DType"(dt |
| ype, cat_ |
| filters) |
| --- | --- |
| "get_dtyp | Resolve a dtype annotation into lamindb's canonical dtype string. |
| e"(annota |
| tion) |
| --- | --- |
| "inline_c | Fold "cat_filters" into a "list[cat[...]]" dtype string. |
| at_filter |
| s"(dtype_ |
| str, cat_ |
| filters) |
| --- | --- |
| "feature" | Declare per-field feature options on a "SchemaModel" field. |
| (*[, name, |
| dtype, |
| type, |
| synonyms, |
| ...]) |
| --- | --- |

# Data migrations

Use "fill_nulls()", "map_values()", and other helpers to migrate
existing data.

| --- | --- |
| "DataMigr | A narrowing data migration attached to a plan object. |
| ation"(*, |
| name, |
| migrate[, |
| reason, |
| ...]) |
| --- | --- |

* Data migrations

  * Migration types

 * "DataMigration"

 * "DataMigration.targets"

 * "DataMigration.run()"

 * "MigrationReport"

 * "MigrationReport.kept"

 * "MigrationReport.matched"

 * "MigrationReport.mapped"

 * "MigrationReport.transformed"

 * "MigrationReport.nulled"

 * "MigrationReport.dropped"

 * "MigrationReport.updated"

 * "MigrationReport.relocated"

 * "MigrationReport.write_strategy"

 * "MigrationReport.note()"

 * "MigrationReport.summary()"

 * "MigrationError"

  * Fill and map values

 * "fill_nulls()"

 * "fill_missing()"

 * "map_values()"

 * "map_to_records()"

 * "map_to_str()"

  * Remove and revalidate data

 * "drop_values()"

 * "drop_rows()"

 * "revalidate_against()"

 * "revalidate_rows()"

 * "reexport_sheets()"

  * Accept schema changes

 * "accept_non_null()"

 * "accept_index_change()"

 * "accept_flexible_change()"

  * Handle existing metadata

 * "drop_member_values()"

 * "keep_member_values()"

 * "drop_nonmember_annotations()"

 * "keep_stale_exports()"

 * "keep_values()"

 * "keep_validated()"

 * "keep_rows()"

  * Migrate records

 * "move_rows()"

 * "adopt_children()"

 * "update_record()"

 * "drop_record()"

  * Fill values

 * "NULL"

 * "DEFAULT"

| --- | --- |
| "Migratio | Per-value outcome counters a migration reports back to the caller. |
| nReport"( |
| [kept, |
| matched, |
| mapped, |
| ...]) |
| --- | --- |
| "Migratio | Raised when a data migration fails to relocate every value. |
| nError" |
| --- | --- |
| "fill_nul | A migration giving a value to every row that is null for a feature made non-nullable. |
| ls"([valu |
| e, name, |
| reason]) |
| --- | --- |
| "fill_mis | A migration giving every row that omits a newly-required member a value. |
| sing"([va |
| lue, |
| feature, |
| name, |
| reason]) |
| --- | --- |
| "map_valu | A migration rewriting scalar values for a narrowed scalar dtype. |
| es"(*[, |
| mapping, |
| transform, |
| ...]) |
| --- | --- |
| "map_to_r | A migration relocating scalar values onto existing records. |
| ecords"(* |
| [, |
| mapping, |
| transform, |
| ...]) |
| --- | --- |
| "map_to_s | A migration rendering categorical values back to strings. |
| tr"(*[, |
| mapping, |
| transform, |
| field, |
| ...]) |
| --- | --- |
| "drop_val | A migration deleting every value stored for the feature. |
| ues"(*[, |
| name, |
| reason]) |
| --- | --- |
| "drop_row | A migration trashing the records of a record type the plan deletes. |
| s"(*[, |
| name, |
| reason]) |
| --- | --- |
| "revalida | A declaration pointing the datasets a deleted schema validated at another. |
| te_agains |
| t"(*, |
| schema[, |
| reason, |
| ...]) |
| --- | --- |
| "revalida | A declaration asking the apply to check a sheet's rows against its new schema. |
| te_rows"( |
| *[, |
| reason, |
| name]) |
| --- | --- |
| "reexport | A migration re-exporting the sheet snapshots a schema change invalidated. |
| _sheets"( |
| *[, sheet, |
| name, |
| reason]) |
| --- | --- |
| "accept_n | A declaration vouching that datasets validated with a feature hold no nulls for it. |
| on_null"( |
| *[, |
| reason, |
| name]) |
| --- | --- |
| "accept_i | A migration accepting a change to a schema's "index" despite validated data. |
| ndex_chan |
| ge"(*[, |
| reason, |
| ...]) |
| --- | --- |
| "accept_f | A migration accepting that a schema's "flexible" goes off despite validated data. |
| lexible_c |
| hange"(*[, |
| reason, |
| name]) |
| --- | --- |
| "drop_mem | A migration deleting the values a removed schema member still holds. |
| ber_value |
| s"(*[, |
| feature, |
| name, |
| reason]) |
| --- | --- |
| "keep_mem | A migration that retires a schema member and *keeps* its values. |
| ber_value |
| s"(*[, |
| feature, |
| name, |
| reason]) |
| --- | --- |
| "drop_non | A migration removing what the flexible match annotated, as "flexible" goes off. |
| member_an |
| notations |
| "(*[, |
| name, |
| reason]) |
| --- | --- |
| "keep_sta | A migration that leaves an invalidated sheet export as it is. |
| le_export |
| s"(*[, |
| sheet, |
| name, |
| reason]) |
| --- | --- |
| "keep_val | A migration that deletes a feature and *keeps* its stored values. |
| ues"(*[, |
| name, |
| reason]) |
| --- | --- |
| "keep_val | A declaration vouching for the datasets a schema the plan deletes validated. |
| idated"(* |
| [, reason, |
| name]) |
| --- | --- |
| "keep_row | A declaration keeping the records of a record type the plan deletes. |
| s"(*[, |
| type, |
| reason, |
| name]) |
| --- | --- |
| "move_row | A declaration moving the records of a record type the plan deletes. |
| s"(*, to[, |
| reason, |
| ...]) |
| --- | --- |
| "adopt_ch | A declaration re-parenting the children of a type the plan deletes. |
| ildren"(* |
| [, to, |
| reason, |
| name]) |
| --- | --- |
| "update_r | A declaration opting a seeded record into the plan's edits. |
| ecord"(*[, |
| values, d |
| escriptio |
| n, ...]) |
| --- | --- |
| "drop_rec | A declaration trashing a seeded record the plan no longer declares. |
| ord"(*[, |
| relocate_ |
| to, name, |
| reason]) |
| --- | --- |
| "NULL" | Sentinel for "this value is null". |
| --- | --- |
| "DEFAULT" | Sentinel for "write the feature's "default_value"". |
| --- | --- |

# Custom migrations

Write custom migration logic with helpers such as
"backfill_members()".

| --- | --- |
| "Migratio | What a "DataMigration" callable receives when it runs. |
| nContext" |
| (feature, |
| desired_d |
| type, ...) |
| --- | --- |

* Custom migrations

  * Migration contexts

 * "MigrationContext"

 * "MigrationContext.target_registry"

 * "MigrationContext.cat_filters"

 * "DeclarationTarget"

 * "DeclarationTarget.by_key"

 * "DeclarationTarget.describe()"

 * "MemberMigrationContext"

 * "MemberMigrationContext.accept_null()"

 * "MemberMigrationContext.missing()"

 * "MemberRemovalContext"

 * "MemberRemovalContext.keep()"

 * "MemberRemovalContext.carrying()"

 * "IndexChangeContext"

 * "FlexibleChangeContext"

 * "FlexibleChangeContext.nonmember_columns()"

 * "FeatureRemovalContext"

 * "RecordTypeRemovalContext"

 * "RecordTypeRemovalContext.rows()"

 * "SchemaRemovalContext"

 * "SheetExportContext"

 * "SheetExportContext.branch_factory"

 * "SheetExportContext.branch"

 * "SheetExportContext.write()"

 * "RecordUpdateContext"

 * "RecordRemovalContext"

 * "RecordRemovalContext.relocate_to"

 * "RecordRemovalContext.links()"

  * Migration helpers

 * "backfill_members()"

 * "clear_values()"

 * "clear_member_values()"

 * "clear_nonmember_annotations()"

 * "relocate_to_records()"

 * "relocate_to_str()"

 * "rewrite_values()"

| --- | --- |
| "Declarat | One kind of target a "DataMigration" can be declared for. |
| ionTarget |
| "(kind, |
| answers[, |
| by_key]) |
| --- | --- |
| "MemberMi | What a "DataMigration" attached to a *schema* receives. |
| grationCo |
| ntext"(sc |
| hema, |
| features[, |
| ...]) |
| --- | --- |
| "MemberRe | What a "DataMigration" attached to a schema *losing* a member receives. |
| movalCont |
| ext"(sche |
| ma, |
| features[, |
| ...]) |
| --- | --- |
| "IndexCha | What a "DataMigration" attached to a schema whose *index* changes receives. |
| ngeContex |
| t"(schema, |
| previous, |
| index) |
| --- | --- |
| "Flexible | What a "DataMigration" attached to a schema turning "flexible" off receives. |
| ChangeCon |
| text"(sch |
| ema, |
| previous, |
| flexible) |
| --- | --- |
| "FeatureR | What a "DataMigration" attached to a *deleted* feature receives. |
| emovalCon |
| text"(fea |
| ture[, |
| report]) |
| --- | --- |
| "RecordTy | What a "DataMigration" attached to a *deleted* record type receives. |
| peRemoval |
| Context"( |
| record_ty |
| pe[, |
| report]) |
| --- | --- |
| "SchemaRe | What a "DataMigration" attached to a *deleted* schema receives. |
| movalCont |
| ext"(sche |
| ma[, |
| report]) |
| --- | --- |
| "SheetExp | Context for rewriting stale sheet exports after a schema migration. |
| ortContex |
| t"(schema, |
| exports[, |
| ...]) |
| --- | --- |
| "RecordUp | What a "DataMigration" attached to a *seeded* record receives. |
| dateConte |
| xt"(recor |
| d[, |
| values, |
| ...]) |
| --- | --- |
| "RecordRe | What a "DataMigration" declared for a record the plan *drops* receives. |
| movalCont |
| ext"(reco |
| rd[, relo |
| cate_to, |
| ...]) |
| --- | --- |
| "backfill | Annotate every row that omits one of the schema's newly-required members. |
| _members" |
| (context, |
| /[, value, |
| feature]) |
| --- | --- |
| "clear_va | Delete every value stored for the feature, scalar and categorical alike. |
| lues"(con |
| text, /) |
| --- | --- |
| "clear_me | Delete the values a schema's removed members hold on the rows it validates. |
| mber_valu |
| es"(conte |
| xt, /, *[, |
| feature]) |
| --- | --- |
| "clear_no | Remove what the flexible match annotated on the artifacts a schema curated. |
| nmember_a |
| nnotation |
| s"(contex |
| t, /) |
| --- | --- |
| "relocate | Relocate a feature's scalar values onto existing records ("str" -> "cat"). |
| _to_recor |
| ds"(conte |
| xt, /, *[, |
| ...]) |
| --- | --- |
| "relocate | Relocate a feature's categorical values to strings ("cat" -> "str"). |
| _to_str"( |
| context, |
| /, *[, |
| mapping, |
| ...]) |
| --- | --- |
| "rewrite_ | Rewrite a feature's scalar values for a narrowed scalar dtype. |
| values"(c |
| ontext, /, |
| *[, |
| mapping, |
| ...]) |
| --- | --- |

# Plan result types

| --- | --- |
| "SpaceUID | UID subclass for Space registry, enforcing 12-character length. |
| "(value, |
| *[, |
| registry]) |
| --- | --- |
| "FeatureU | UID subclass for Feature registry, enforcing 12-character length. |
| ID"(value, |
| *[, |
| registry]) |
| --- | --- |
| "RecordUI | UID subclass for Record registry, enforcing 16-character length. |
| D"(value, |
| *[, |
| registry]) |
| --- | --- |
| "SchemaUI | UID subclass for Schema registry, enforcing 16-character length. |
| D"(value, |
| *[, |
| registry]) |
| --- | --- |
| "Promise" | A deferred call. |
| (arg, |
| func, *[, |
| label]) |
| --- | --- |
| "Dependen | Represents a dependency on another object that needs to be materialized first. |
| cy"(obj, |
| *[, |
| requires]) |
| --- | --- |
| "Dependen | Dependencies ordered so that required objects are created first. |
| cyDAG"(de |
| pendencie |
| s) |
| --- | --- |
| "Moved"(o | A declared address change: "obj" was previously known as "from_". |
| bj, from_) |
| --- | --- |
| "Migrated | A migration declared for "obj" via "migrate()". |
| "(obj, |
| migration) |
| --- | --- |
| "WriteStr | How a relocation wrote the replacement annotations. |
| ategy"(va |
| lue[, |
| names, |
| module, |
| ...]) |
| --- | --- |

# Environment variables

Configure tracking before applying plans. Explicit nonblank
"track_prefix" and "track_space" settings in the instance's CI
configuration take precedence over the environment.

| --- | --- | --- |
| Variable | Value and purpose | Default |
| ======================================== | ============================================ | ====================== |
| "LAMIN_SCHEMA_EVOLUTION_TRACK_PREFIX" | String prefix for tracked transform keys. | "schema_evolution" |
| --- | --- | --- |
| "LAMIN_SCHEMA_EVOLUTION_TRACK_SPACE" | Space name or UID for tracked runs, | "all" |
| transforms, and outputs. |
| --- | --- | --- |
| "LAMIN_SCHEMA_EVOLUTION_TRACEBACK" | Any nonempty value enables tracebacks for | Unset |
| handled command-line errors. It does not |
| change Python API exceptions. |
| --- | --- | --- |

"PLAN_TRANSFORM_STEM_UID" is a variable in each plan file, not an
environment variable. Keep it stable when editing the plan.