pushToSchema method

Future<PostgresqlMigrationResult> pushToSchema({
  1. required SessionExecutor executor,
  2. required SchemaDocument target,
  3. bool allowWarnings = false,
})

Applies the schema diff to target without writing migration history.

Implementation

Future<PostgresqlMigrationResult> pushToSchema({
  required pg.SessionExecutor executor,
  required SchemaDocument target,
  bool allowWarnings = false,
}) async {
  final filteredFrom = filterSchemaForUserModels(
    await planner.schemaIntrospector.introspect(executor),
    historyTableName: historyTableName,
  );
  final plan = _mergeRiskWarnings(
    planner.plan(from: filteredFrom, to: target),
    detectPotentialDataLossWarnings(from: filteredFrom, to: target),
  );
  if (plan.warnings.isNotEmpty && !allowWarnings) {
    throw StateError(
      'Migration plan contains warnings: ${plan.warnings.join(' | ')}',
    );
  }

  if (!plan.requiresRebuild && plan.statements.isEmpty) {
    return PostgresqlMigrationResult(plan: plan, applied: false);
  }

  await executor.runTx((session) async {
    if (plan.requiresRebuild) {
      await _rebuildDatabase(
        session: session,
        source: filteredFrom,
        target: target,
      );
    } else {
      for (final statement in plan.statements) {
        await session.execute(statement, ignoreRows: true);
      }
    }
  });

  return PostgresqlMigrationResult(
    plan: plan,
    applied: plan.requiresRebuild || plan.statements.isNotEmpty,
  );
}