runSeedRegistryOnConnection function

Future<void> runSeedRegistryOnConnection(
  1. OrmConnection connection,
  2. List<SeederRegistration> seeds, {
  3. List<String>? names,
  4. bool pretend = false,
  5. void beforeRun(
    1. OrmConnection connection
    )?,
  6. void log(
    1. String message
    )?,
})

Run a list of registered seeders on the provided connection.

If names is provided, only seeders with matching names will be run.

pretend can be set to true to log queries instead of executing them.

beforeRun is a callback invoked before execution (e.g. to bootstrap the registry).

Implementation

Future<void> runSeedRegistryOnConnection(
  OrmConnection connection,
  List<SeederRegistration> seeds, {
  List<String>? names,
  bool pretend = false,
  void Function(OrmConnection connection)? beforeRun,
  void Function(String message)? log,
}) async {
  if (seeds.isEmpty) {
    throw StateError('No seeders registered.');
  }
  final logger = log ?? (String message) => stdout.writeln(message);
  final reporter = CliEventReporter(io: cliIO)..listenToSeeders();
  try {
    await SeederRunner().run(
      connection: connection,
      seeders: seeds,
      names: (names == null || names.isEmpty) ? null : names,
      pretend: pretend,
      beforeRun: beforeRun,
      log: null,
      // Preserve previous behavior (no default log output).
      onPretendQueries: pretend
          ? (entries) {
              for (final entry in entries) {
                logger('[pretend] ${formatPretendEntry(entry)}');
              }
            }
          : null,
    );
  } finally {
    reporter.dispose();
  }
}