Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion src/how-to/garbage-collection.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ Every managed path embeds the schema name (`{hash_prefix}/{schema}/...` and
`{schema_prefix}/{schema}/...`), so garbage collection is **per-schema**: each
schema is scanned against its own subtree. You may pass any subset of the
schemas sharing a store — a schema not passed is simply not scanned; its live
objects are never seen and never at risk.
objects are never seen and never at risk. The root-level layout
(`schema_prefix: ""`) is the exception: there, a file in a subfolder of
`{schema}/` that only rows of other schemas reference is listed as an orphan.
See [Root-Level Layout](#root-level-layout).

```python
# Clean several schemas at once...
stats = dj.gc.GarbageCollector(schema_raw, schema_processed, schema_analysis).collect(dry_run=False)

# ...or just one — safe even when others share the same store
# (except in the root-level layout; see Root-Level Layout below)
stats = dj.gc.GarbageCollector(schema_raw).collect(dry_run=False)
```

Expand Down Expand Up @@ -208,6 +212,34 @@ DataJoint uses two storage patterns:
collected
- GC never touches the store's declared `filepath_prefix` namespace
(user-managed `<filepath@>` files)
- In the root-level layout (`schema_prefix: ""`), GC keeps the files listed
below

### Root-Level Layout

Files migrated from pre-2.0 DataJoint stay where it stored them,
`{database}/...`, which is then inside the schema-addressed section, and
`<filepath@>` files may lie there too. In this layout only, GC keeps these
files:

- A file that a hash reference (such as a migrated `<blob@>` or `<attach@>`
value) or a `<filepath@>` reference points to counts as referenced.
- A file named as pre-2.0 DataJoint named blobs and attachments (32 hex digits,
optionally followed by `.` and a name) is never an orphan.
- A file directly under `{schema}/`, with no subfolder, is never an orphan.
2.0 schema-addressed paths are `{schema}/{table}/...`, so such a file is a
pre-2.0 file stored without subfolding, or a blob that pre-2.0 DataJoint had
migrated from an earlier release.
- The section of a schema that still has pre-2.0 `~external_*` tables is not
listed at all, and the report names the schema under `skipped_schemas`.

GC reads references only from the schemas it collects, in the store it
collects. A file in a subfolder of `{schema}/` that is not named as pre-2.0
DataJoint named its files, and that only rows of other schemas reference (for
example a `<filepath@>` file referenced from another schema), is still listed
as an orphan. Pass every schema whose rows reference files in the store, such
as a copy migrated with `legacy_database`, to the same collector, and preview
with `dry_run=True` before deleting. See [Migrate to 2.0](migrate-to-v20.md).

## Troubleshooting

Expand Down
82 changes: 67 additions & 15 deletions src/how-to/migrate-to-v20.md
Original file line number Diff line number Diff line change
Expand Up @@ -2155,6 +2155,23 @@ for table in tables:
print(f"{table}: copied {result['rows_copied']} rows")
```

**Tables with in-store columns:** `copy_table_data` converts `blob@store`, `attach@store`, and `filepath@store` columns as it copies them. Each must be declared with the same codec in your 2.0 code, for example `<blob@raw>` for a pre-2.0 `blob@raw` column. Files stay where they are:

- The 2.0 store named in the declaration must point to the pre-2.0 location (step 0). If your pre-2.0 configuration set `subfolding` for the store, set the same value. Otherwise leave it out or set it to `null` (as the configuration template writes it): the pre-2.0 default, `[2, 2]`, is then used to find the files. Set `[]` only if your pre-2.0 store turned subfolding off.
- Every file is looked up before anything is copied, through that store, which must be configured and reachable. If a file is missing, or has no entry in the pre-2.0 `~external_<store>` table, `copy_table_data` raises an error and copies nothing.
- A pre-2.0 in-store column copied into a column with a different codec, such as `<blob>`, is refused.
- Pre-2.0 DataJoint put blob and attachment files under the name of the database it wrote them from, `{database}/...`. `copy_table_data` looks for them under `source_schema`. If the source is itself a copy, such as one made with `create_parallel_schema`, pass the original name as `legacy_database='my_pipeline'`. Without it, the files are not found and nothing is copied; the error says to pass `legacy_database`.
- Keep the default `schema_prefix` (`_schema`) on a store that points to the pre-2.0 location. With `schema_prefix: ""` (the root-level layout), garbage collection keeps the files listed below.

With `schema_prefix: ""`, the store's schema-addressed section is `{database}/`, the folder that also holds the pre-2.0 files, and `<filepath@>` files may lie there too. In this layout only, garbage collection keeps these files:

- A file that a hash reference (such as a migrated `<blob@>` or `<attach@>` value) or a `<filepath@>` reference points to counts as referenced.
- A file named as pre-2.0 DataJoint named blobs and attachments (32 hex digits, optionally followed by `.` and a name) is never an orphan.
- A file directly under `{database}/`, with no subfolder, is never an orphan. 2.0 schema-addressed paths are `{schema}/{table}/...`, so such a file is a pre-2.0 file stored without subfolding, or a blob that pre-2.0 DataJoint had migrated from an earlier release.
- The section of a schema that still has pre-2.0 `~external_*` tables is not listed at all, and the report names the schema under `skipped_schemas`.

Garbage collection reads references only from the schemas it collects, in the store it collects. A file in a subfolder of `{database}/` that is not named as pre-2.0 DataJoint named its files, and that only rows of other schemas reference (for example a `<filepath@>` file referenced from another schema), is still listed as an orphan. Pass every schema whose rows reference files in the store, such as a copy migrated with `legacy_database`, to the same collector, and preview with `dry_run=True` before deleting. See [Clean Up Object Storage](garbage-collection.md#root-level-layout).

#### 3. Populate Computed Tables

```python
Expand All @@ -2167,35 +2184,39 @@ Analysis.populate(display_progress=True)

#### 4. Migrate In-Store Metadata

**Important:** Your code already handles in-store codecs (converted in Phase I). This step just updates metadata format.
Tables copied with `copy_table_data` in step 2 are already converted, and tables populated in step 3 store their files with 2.0. For them, no further step is needed.

If you have tables using `<blob@>`, `<attach@>`, or `<filepath@>` codecs, migrate the storage metadata from legacy BINARY(16) UUID format to 2.0 JSON format:
To convert one in-store column of a table that is still in the pre-2.0 layout, use `migrate_external_pointers_v2`. For example, use it on a copy made with `create_parallel_schema(..., copy_data=True)`, or to give a store a new 2.0 name. The copy keeps the pre-2.0 layout and its `~external_*` tables, but its files stay under the original database name, so name the original with `legacy_database`:

```python
from datajoint.migrate import migrate_external_pointers_v2
from datajoint.migrate import create_parallel_schema, migrate_external_pointers_v2

create_parallel_schema('my_pipeline', 'my_pipeline_copy', copy_data=True)

# Update metadata format (UUID → JSON)
# This does NOT move files—just updates database pointers
result = migrate_external_pointers_v2(
schema='my_pipeline_v2',
schema='my_pipeline_copy',
table='recording',
attribute='signal',
source_store='raw', # Legacy pre-2.0 store name
source_store='raw', # Pre-2.0 store name, as in the column comment
dest_store='raw', # 2.0 store name (from datajoint.json)
copy_files=False, # Keep files in place (recommended)
legacy_database='my_pipeline', # The database pre-2.0 DataJoint wrote the files from
)

print(f"Migrated {result['rows_migrated']} pointers")
```

**What this does:**

- Reads legacy BINARY(16) UUID pointers from `~external_*` hidden tables
- Creates new JSON metadata with file path, store name, hash
- Writes JSON to the `<blob@store>` column (code written in Phase I)
- Does NOT copy files (unless `copy_files=True`)
- Reads the pre-2.0 pointers in the column and looks each one up in the schema's `~external_<source_store>` table
- Finds each file where pre-2.0 DataJoint stored it, through `dest_store`, which must point to the pre-2.0 location. Blob and attachment files are under `legacy_database`, which defaults to `schema`; on a copy, pass the original name.
- Adds a JSON column with the 2.0 metadata, then renames the original column to `<name>_v1` and gives its name to the new column, with the comment `:<blob@dest_store>:` (or `attach`, `filepath`). A required column stays required.
- Does NOT copy files (`copy_files=True` is not implemented yet)

**Result:** Files stay in place, but 2.0 code can now access them via the new codec system.
Every file is looked up before anything changes. If a pointer has no entry in the hidden table, or its file is missing, the function raises an error and changes nothing. A column already declared by 2.0 holds no pre-2.0 pointers, so it is refused.

`create_parallel_schema` copies tables with foreign key checks off for its session, because it creates them in name order and the `~external_*` tables sort last. The copy's foreign keys point to the copy's own tables. To migrate a whole copy in place instead, follow Option B on it and pass `legacy_database='my_pipeline'` to `migrate_external` (`migrate_filepath` needs no such argument, because filepath paths are stored in full).

**Result:** Files stay in place, and 2.0 code reads them through the in-store codecs.

#### 5. Validate Data Integrity

Expand Down Expand Up @@ -2346,25 +2367,56 @@ print(f"Rebuilt lineage: {result['lineage_entries']} entries")

If you use `blob@store`, `attach@store`, or `filepath@store`:

Configure each store under its pre-2.0 name, with its pre-2.0 location, and keep its default `schema_prefix` (see the note on garbage collection under Option A, step 2). If your pre-2.0 configuration set `subfolding` for the store, set the same value. Otherwise leave it out or set it to `null` (as the configuration template writes it): the pre-2.0 default, `[2, 2]`, is then used to find the files. Set `[]` only if your pre-2.0 store turned subfolding off. Files stay where they are.

```python
from datajoint.migrate import migrate_external, migrate_filepath

# Preview external blob/attach migration
result = migrate_external(schema, dry_run=True)
print(f"Found {result['columns_found']} external columns")
for detail in result['details']:
for problem in detail.get('problems', []):
print(f" Cannot migrate: {problem}")

# Apply migration (adds _v2 columns with JSON metadata)
result = migrate_external(schema, dry_run=False)
print(f"Migrated {result['rows_migrated']} rows")

# Similarly for filepath columns
result = migrate_filepath(schema, dry_run=True)
print(f"Found {result['columns_found']} filepath columns")
for detail in result['details']:
for problem in detail.get('problems', []):
print(f" Cannot migrate: {problem}")

result = migrate_filepath(schema, dry_run=False)
print(f"Migrated {result['rows_migrated']} rows")

# After verification, finalize (rename columns)
result = migrate_external(schema, finalize=True)
result = migrate_filepath(schema, finalize=True)
result = migrate_external(schema, dry_run=False, finalize=True)
result = migrate_filepath(schema, dry_run=False, finalize=True)
```

**What this does:**

- Looks up every file where pre-2.0 DataJoint stored it before changing anything. If a file is missing, the migration raises an error and changes nothing. The preview lists these files.
- Blob and attachment files are under the name of the database pre-2.0 DataJoint wrote them from. If you migrate a copy of the schema rather than the schema itself, pass the original name: `migrate_external(schema, dry_run=False, legacy_database='my_pipeline')`, and the same for the preview.
- The preview looks up the files too, so every store must be configured and reachable before you run it. This is one existence check per file, which is one request per file on S3.
- Adds a `<name>_v2` column for each in-store column, with the type marker (for example `:<blob@raw>:`) in its comment. Fetch the `_v2` columns with DataJoint 2.0 to verify them before you finalize. If a step fails, the `_v2` columns the run added are dropped again, and the original columns are unchanged.
- Finalizing renames each original column to `<name>_v1` and gives its name to the migrated column. A required column stays required, except one that an earlier release already finalized (see below). It needs `dry_run=False`: with the default `dry_run=True`, it only previews. It first checks that every `_v2` column exists and reads in 2.0, and changes nothing otherwise.

**If you ran this step with an earlier DataJoint 2.x release:** those releases wrote values that 2.0 cannot read. Rerun the migration with `dry_run=False` and the current release. It rewrites `_v2` columns that do not read, and skips the ones that do; the preview shows them with the action `rewrite`. If you had already finalized, it rewrites the finalized columns from the pointers kept in `<name>_v1`, with the action `repair`. The original pointers are never changed, so no data is lost.

Repair cannot restore `NOT NULL`. The earlier release's finalize made every migrated column optional and did not record which ones were required, so a repaired column stays optional. The preview marks these columns with `nullable: True`, and the run logs a warning for each. If a column was required, restore it in SQL, keeping its comment:

```sql
ALTER TABLE `my_pipeline`.`recording`
MODIFY COLUMN `signal` JSON NOT NULL COMMENT ':<blob@raw>:raw signal';
```

`alter()` cannot make this change while the table keeps its `<name>_v1` columns, because their pre-2.0 type, `binary(16)`, is not accepted in a 2.0 definition.

#### Step 5: Verify Migration

```python
Expand Down
Loading