pushToSchema method

SqliteMigrationResult pushToSchema({
  1. required Database database,
  2. required SchemaDocument target,
  3. bool allowWarnings = false,
})

Applies the schema diff to target without writing migration history.

Implementation

SqliteMigrationResult pushToSchema({
  required sqlite.Database database,
  required SchemaDocument target,
  bool allowWarnings = false,
}) {
  final filteredFrom = _currentSourceSchema(database);
  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 SqliteMigrationResult(plan: plan, applied: false);
  }

  if (plan.requiresRebuild) {
    _rebuildDatabase(
      database: database,
      source: filteredFrom,
      target: target,
    );
    return SqliteMigrationResult(plan: plan, applied: true);
  }

  database.execute('BEGIN');
  try {
    for (final statement in plan.statements) {
      database.execute(statement);
    }
    database.execute('COMMIT');
    return SqliteMigrationResult(plan: plan, applied: true);
  } catch (_) {
    database.execute('ROLLBACK');
    rethrow;
  }
}