rename method Null safety
- String newStoreName
Rename the store directory asynchronously
The old StoreDirectory
will still retain it's link to the old store, so
always use the new returned value instead: returns a new StoreDirectory
after a successful renaming operation.
This method requires the store to be ready, else an FMTCStoreNotReady error will be raised.
Implementation
Future<StoreDirectory> rename(String newStoreName) async {
_ensureReadyStatus();
// Unregister old database without deleting it
final oldStore = _registry.storeDatabases.remove(_id);
if (oldStore!.isOpen) await oldStore.close();
final newId = DatabaseTools.hash(newStoreName);
// Manually change the database's filename
await (_rootDirectory >>> '$_id.isar').rename(
(_rootDirectory >>> '$newId.isar').path,
);
// Register the new database (it will be re-opened)
final newStore = StoreDirectory._(newStoreName);
await newStore.manage.createAsync();
// Update the name stored inside the database
await _registry.storeDatabases[newId]?.writeTxn(
() => _registry.storeDatabases[newId]!.storeDescriptor
.put(DbStoreDescriptor(name: newStoreName)),
);
return newStore;
}