ensureTursoSharedDatabase function
Creates the shared Turso database database in group if it does not exist.
An existing database in another group throws StateError because the region Worker rejects a group mismatch. HTTP 401/403 and other failures throw StateError. Returns true if the database was created.
共用Turso DB databaseが存在しなければgroupに作成します。
別groupに既存DBがある場合はregion Workerが拒否するためStateErrorを投げます。 HTTP 401/403とその他の失敗はStateErrorを投げます。作成した場合はtrueを返します。
Implementation
Future<bool> ensureTursoSharedDatabase({
required TursoPlatformApi api,
required String organization,
required String database,
required String group,
}) async {
const tokenHint =
"A Turso Platform API token that can read and create databases in the organization is required. "
"Set it to [cloudflare]->[turso]->[platform_api_token]. "
"organizationのDBを参照・作成できるTurso Platform API tokenが必要です。 "
"[cloudflare]->[turso]->[platform_api_token]に設定してください。";
try {
final existing = await api.getDatabase(organization, name: database);
if (existing != null) {
if (existing.group != null &&
existing.group!.isNotEmpty &&
existing.group != group) {
throw StateError(
"The shared Turso database `$database` already exists in group `${existing.group}` "
"(katana.yaml: `$group`). Fix [cloudflare]->[turso]->[shared]->[group]. "
"共用Turso DB `$database` は既にgroup `${existing.group}` に存在します(katana.yaml: `$group`)。 "
"[cloudflare]->[turso]->[shared]->[group]を修正してください。",
);
}
return false;
}
label("Create shared Turso database: $database ($group)");
return await api.createDatabase(
organization,
name: database,
group: group,
);
} on TursoPlatformApiException catch (e) {
if (e.isUnauthorized) {
throw StateError("$e\n$tokenHint");
}
throw StateError(
"$e\nCheck that the group `$group` exists, or list it in [cloudflare]->[turso]->[groups] with a location. "
"group `$group` が存在するか、[cloudflare]->[turso]->[groups]にlocation付きで記載されているか確認してください。",
);
}
}