PersistentStore persistentStore

Source

PersistentStore get persistentStore {
  if (_persistentStore != null) {
    return _persistentStore;
  }

  if (values["flavor"] == null) {
    throw new CLIException("No database flavor selected. See --flavor.");
  }

  if (databaseFlavor == FlavorPostgreSQL) {
    connectedDatabase = new DatabaseConnectionConfiguration();
    if (databaseConnectionString != null) {
      try {
        connectedDatabase.decode(databaseConnectionString);
      } catch (_) {
        throw new CLIException("Invalid database configuration.",
            instructions: [
              "Invalid connection string was: $databaseConnectionString",
              "Expected format:               database://user:password@host:port/databaseName"
            ]);
      }
    } else {
      if (!databaseConfigurationFile.existsSync()) {
        throw new CLIException("No database configuration file found.",
            instructions: [
              "Expected file at: ${databaseConfigurationFile.path}.",
              "See --connect and --database-config. If not using --connect, "
                  "this tool expects a YAML configuration file with the following format:\n$_dbConfigFormat"
            ]);
      }

      try {
        var contents = databaseConfigurationFile.readAsStringSync();
        var yaml = loadYaml(contents);
        connectedDatabase.readFromMap(yaml);
      } catch (_) {
        throw new CLIException("Invalid database configuration.",
            instructions: [
              "File located at ${databaseConfigurationFile.path}.",
              "See --connect and --database-config. If not using --connect, "
                  "this tool expects a YAML configuration file with the following format:\n$_dbConfigFormat"
            ]);
      }
    }

    _persistentStore = new PostgreSQLPersistentStore.fromConnectionInfo(
        connectedDatabase.username,
        connectedDatabase.password,
        connectedDatabase.host,
        connectedDatabase.port,
        connectedDatabase.databaseName,
        useSSL: useSSL);
    return _persistentStore;
  }

  throw new CLIException("Invalid flavor $databaseFlavor");
}