rename method

Future<StoreDirectory> rename(
  1. 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 {
  // Unregister and close old database without deleting it
  final store = _registry.unregister(_id);
  if (store == null) {
    _registry(_name);
    throw StateError(
      'This error represents a serious internal error in FMTC. Please raise a bug report if seen in any application',
    );
  }
  await store.close();

  // Manually change the database's filename
  await (_rootDirectory >>> '$_id.isar').rename(
    (_rootDirectory >>> '${DatabaseTools.hash(newStoreName)}.isar').path,
  );

  // Register the new database (it will be re-opened)
  final newStore = StoreDirectory._(newStoreName, autoCreate: false);
  await newStore.manage.createAsync();

  // Update the name stored inside the database
  await _registry(newStoreName).writeTxn(
    () => _registry(newStoreName)
        .storeDescriptor
        .put(DbStoreDescriptor(name: newStoreName)),
  );

  return newStore;
}