populateTables method

FutureOr<List<String>> populateTables(
  1. Object? tables
)

Implementation

FutureOr<List<String>> populateTables(Object? tables) {
  if (tables == null) {
    return <String>[];
  } else if (tables is String) {
    if (RegExp(r'^\S+\.sql$').hasMatch(tables)) {
      var apiPlatform = APIPlatform.get();

      var filePath = apiPlatform.resolveFilePath(tables);

      if (filePath == null) {
        throw StateError("Can't resolve tables file path: $tables");
      }

      _log.info('Reading $this populate tables file: $filePath');

      var fileData = apiPlatform.readFileAsString(filePath);

      if (fileData != null) {
        return fileData.resolveMapped((data) {
          if (data != null) {
            _log.info(
                'Populating $this tables [SQL length: ${data.length}]...');

            return populateTablesFromSQLs(data).resolveMapped((tables) {
              _log.info('Populate tables finished: $tables');
              return tables;
            });
          } else {
            return <String>[];
          }
        });
      }
    } else if (RegExp(r'(?:^|\s+)(?:CREATE|ALTER)\s+TABLE\s')
        .hasMatch(tables)) {
      _log.info('Populating $this tables [SQL length: ${tables.length}]...');

      return populateTablesFromSQLs(tables).resolveMapped((tables) {
        _log.info('Populate tables finished: $tables');
        return tables;
      });
    }
  }

  return <String>[];
}