Skip to content

Commit

Permalink
Update data_migration.md
Browse files Browse the repository at this point in the history
  • Loading branch information
letyletylety committed Feb 12, 2023
1 parent 26f29f8 commit 45ab90e
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions docs/docs/ko/recipes/data_migration.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
---
title: Data migration
title: 데이터 마이그레이션 (Data migration)
---

# Data Migration
# 데이터 마이그레이션

Isar automatically migrates your database schemas if you add or remove collections, fields, or indexes. Sometimes you might want to migrate your data as well. Isar does not offer a built-in solution because it would impose arbitrary migration restrictions. It is easy to implement migration logic that fits your needs.
Isar 는 컬렉션, 속성, 인덱스를 추가하거나 삭제하면 데이터베이스 스키마를 자동으로 마이그레이션합니다. 가끔은 데이터도 마이그레이션해야 할 수 있습니다. Isar 는 임의 마이그레이션 제한을 적용하기 때문에 기본 제공 솔루션을 제공하지는 않습니다. 사용자의 요구사항에 맞는 마이그레이션 로직을 쉽게 구현할 수 있습니다.

We want to use a single version for the entire database in this example. We use shared preferences to store the current version and compare it to the version we want to migrate to. If the versions do not match, we migrate the data and update the version.
이 예시에서는 전체 데이터베이스에서 하나의 버전을 이용하려고 합니다. shared preferences 를 사용해서 현재 버전을 저장하고 마이그레이션하려는 버전과 비교합니다. 버전이 일치하지 않으면 데이터를 마이그레이션하고 버전을 업데이트 합니다.

:::tip
You could also give each collection its own version and migrate them individually.
각 컬렉션에 자체 버전을 지정하고 개별적으로 마이그레이션 할 수 있습니다.
:::

Imagine we have a user collection with a birthday field. In version 2 of our app, we need an additional birth year field to query users based on age.
생일 필드가 있는 사용자 컬렉션이 있다고 상상해 보십시오. 우리 앱의 버전 2에서는 나이를 기준으로 사용자를 조회할 수 있는 추가 출생 연도 필드가 필요합니다.

버전 1:

Version 1:
```dart
@collection
class User {
Expand All @@ -26,7 +27,8 @@ class User {
}
```

Version 2:
버전 2:

```dart
@collection
class User {
Expand All @@ -40,7 +42,7 @@ class User {
}
```

The problem is the existing user models will have an empty `birthYear` field because it did not exist in version 1. We need to migrate the data to set the `birthYear` field.
문제는 기존에 있던 사용자 모델들은 버전 1에서 `birthYear` 가 없었기 때문에 비어있는 `birthYear` 를 가지게 된다는 것입니다. 우리는 `birthYear` 필드를 설정하기 위해서 데이터를 마이그레이션 해야 합니다.

```dart
import 'package:isar/isar.dart';
Expand All @@ -62,30 +64,30 @@ Future<void> performMigrationIfNeeded(Isar isar) async {
await migrateV1ToV2(isar);
break;
case 2:
// If the version is not set (new installation) or already 2, we do not need to migrate
// 버전이 설정되지 않았거나(새로 설치한 경우), 이미 2인 경우 마이그레이션할 필요가 없습니다.
return;
default:
throw Exception('Unknown version: $currentVersion');
}
// Update version
// 버전 업데이트
await prefs.setInt('version', 2);
}
Future<void> migrateV1ToV2(Isar isar) async {
final userCount = await isar.users.count();
// We paginate through the users to avoid loading all users into memory at once
// 모든 사용자를 한 번에 메모리에 로드하지 않도록 사용자를 페이지 분할합니다.
for (var i = 0; i < userCount; i += 50) {
final users = await isar.users.where().offset(i).limit(50).findAll();
await isar.writeTxn((isar) async {
// We don't need to update anything since the birthYear getter is used
// 생년월일 게터를 사용하기 때문에 업데이트할 필요가 없습니다.
await isar.users.putAll(users);
});
}
}
```

:::warning
If you have to migrate a lot of data, consider using a background isolate to prevent strain on the UI thread.
많은 데이터를 마이그레이션 해야 하는 경우 UI 스레드에 부담이 가지 않도록 백그라운드 isolate 를 사용하는 것이 좋습니다.
:::

0 comments on commit 45ab90e

Please sign in to comment.