restore method

Future<CliResult> restore({
  1. required String user,
  2. required String database,
  3. required String backupFilepath,
  4. String? host,
  5. int? port,
  6. String? password,
})

Implementation

Future<CliResult> restore({
  required String user,
  required String database,
  required String backupFilepath,
  String? host,
  int? port,
  String? password,
}) async {
  final backupFile =
      await checkFileExists(backupFilepath, "backupFilepath").resolveUri();

  final lines = await backupFile.readAsLines();

// -- MariaDB dump 10.19  Distrib 10.11.4-MariaDB, for debian-linux-gnu (x86_64)
// --
// -- Host: localhost    Database: dev_database
// -- ------------------------------------------------------
  if (!lines[0].startsWith("-- MariaDB dump")) {
    throw ArgumentError.value(
      backupFile.path,
      "backupFilepath",
      "The backup file must be a MariaDB dump file",
    );
  }

  final l = lines[2];
  final hostMatch =
      RegExp("Host: (?<host>[A-z0-9.]*)").firstMatch(l)?.namedGroup("host");
  final dbNameMatch = RegExp("Database: (?<dbName>[A-z0-9.]*)")
      .firstMatch(l)
      ?.namedGroup("dbName");

  if (null == hostMatch) {
    throw ArgumentError.value(
      backupFile.path,
      "backupFilepath",
      "The backup file must be a MariaDB dump file that has "
          "host value",
    );
  }

  if (null == dbNameMatch) {
    throw ArgumentError.value(
      backupFile.path,
      "backupFilepath",
      "The backup file must be a MariaDB dump file that has "
          "database value",
    );
  }

  if (database != dbNameMatch) {
    throw ArgumentError.value(
      backupFile.path,
      "backupFilepath",
      "The backup file is not a backup of the database '$database' but of "
          "another database called '$dbNameMatch'",
    );
  }

  return runSqlScript(
    host: host,
    port: port,
    user: user,
    database: database,
    scriptFilepath: backupFilepath,
    password: password,
  );
}