purge function

Future purge(
  1. Connection conn,
  2. Map<String, Map<String, SqlType>> tables,
  3. Map<String, IndexInfo> indexes,
  4. Map<String, RuleInfo> rules,
)

Purges all data and schemas.

Implementation

Future purge(Connection conn,
    Map<String, Map<String, SqlType>> tables,
    Map<String, IndexInfo> indexes, Map<String, RuleInfo> rules) {

  return Future.forEach(rules.keys, (name) async {
    try {
      return await conn.execute('drop rule "$name" on "${rules[name]!.table}"');
    } catch (ex) {
      if (!_isUndefined(ex)) rethrow;
    }
  })
  .then((_) => Future.forEach(indexes.keys.toList().reversed, (name) async {
    try {
      return await conn.execute('drop index "$name"');
    } catch (ex) {
      if (!_isUndefined(ex)) rethrow;
    }
  }))
  .then((_) {
    final tblsGened = HashSet<String>();
    final refsDeferred = <_DeferredRef>[];
    for (final otype in tables.keys)
      _scanDeferredRefs(otype, tables[otype]!, tblsGened, refsDeferred);
    return Future.forEach(refsDeferred,
        (_DeferredRef defRef) => defRef.drop(conn)
        .catchError((ex) {}, test: _isUndefined));
  })
  .then((_) => Future.forEach(tables.keys.toList().reversed, (name) async {
    try {
      return await conn.execute('drop table "$name"');
    } catch (ex) {
      if (!_isUndefined(ex)) rethrow;
    }
  }));
}