#### Sync data across databases [image: .md][image]

This guide shows how to sync objects from a source database to your
default database.

We need a target database:

 !lamin init --storage ./test-sync --modules bionty

Import "lamindb" and optionally run "ln.track()":

 import lamindb as ln

 ln.track()

Syncing works for any object type ("Artifact", "Record", "Transform",
"ULabel", etc.). Let's sync an artifact to our current default
database:

 db = ln.DB("laminlabs/lamindata")
 # query the artifact on the source database
 artifact = db.Artifact.get(key="example_datasets/mini_immuno/dataset1.h5ad")
 # sync the artifact to the current database
 artifact.save()

If you also want to sync feature & label annotations, pass
"transfer="annotations"":

 # query again so that `artifact` holds the object on the source database
 artifact = db.Artifact.get(key="example_datasets/mini_immuno/dataset1.h5ad")
 # sync the artifact to the current database, including transfer of annotations where necessary
 artifact.save(transfer="annotations")

The artifact now has all feature & label annotations:

 artifact.describe()

The sync is zero-copy, which means that the data itself remained in
the original storage location:

 artifact.path

Data lineage indicates the source database of the sync:

 artifact.view_lineage()

The run that initiated the sync is linked via "initiated_by_run":

 artifact.run.initiated_by_run.transform

Upon calling ".save()" again, "lamindb" identifies that the object
already exists in the target database and simply maps it:

 artifact = db.Artifact.get(key="example_datasets/mini_immuno/dataset1.h5ad")
 artifact.save()

-[ How do I know if an object is in the default database or elsewhere?
]-

Every "SQLRecord" object has an attribute "._state.db" which can take
the following values:

* "None": the object has not yet been saved to any database

* ""default"": the object is saved on the default database instance

* ""account/name"": the object is saved on a non-default database
  instance referenced by "account/name" (e.g., "laminlabs/lamindata")

 # test the last 3 cells here
 assert artifact.transform.description == "Transfer from `laminlabs/lamindata`"
 assert artifact.transform.key == "__lamindb_transfer__/4XIuR0tvaiXM"
 assert artifact.transform.uid == "4XIuR0tvaiXM0000"
 assert artifact.run.initiated_by_run.transform.description.startswith("Sync data")