restoreDatabase function

Future<void> restoreDatabase({
  1. required String databaseName,
  2. required String pathToBackup,
  3. required bool thisWillDestroyMyDb,
})

Restores a database from a .sql file created by mysqldump.

WARNING: calling this method will DROP your existing datbase.

if thisWillDestroyMyDb is not true then a MySqlORMException will be thrown.

Implementation

Future<void> restoreDatabase(
    {required String databaseName,
    required String pathToBackup,
    required bool thisWillDestroyMyDb}) async {
  if (thisWillDestroyMyDb != true) {
    throw MySqlORMException(
        'You must call this function with thisWillDestroyMyDb = true');
  }
  await dropDatabase(databaseName);

  final schemaScript = (read(pathToBackup).toList()
        ..removeWhere((line) => line.startsWith('--'))
        ..removeWhere((line) => line.startsWith('/*'))
        ..removeWhere((line) => line.isEmpty))
      .join('\n');

  final statements = schemaScript.split(';');

  await withNoConstraints(action: () async {
    for (final statement in statements) {
      if (statement.isNotEmpty) {
        await Transaction.current.db.query(statement);
      }
    }
  });
}