plan static method

Future<MigrationPlan> plan({
  1. required DatabaseExecutor db,
  2. required Schema targetSchema,
})

Inspects live DB structure against targetSchema and returns proposed SQL steps WITHOUT executing any mutations on the database.

Implementation

static Future<MigrationPlan> plan({
  required DatabaseExecutor db,
  required Schema targetSchema,
}) async {
  // 1. Fast version gatekeeper check
  final versionResult = await db.rawQuery('PRAGMA user_version;');
  final currentVersion = (versionResult.first['user_version'] as int?) ?? 0;

  if (currentVersion == targetSchema.version) {
    return MigrationPlan(
      fromVersion: currentVersion,
      toVersion: targetSchema.version,
      steps: const [],
    );
  }

  final steps = <PlanStep>[];

  // 2. Diff Standard Tables & Indexes
  for (final table in targetSchema.tables) {
    steps.addAll(await SchemaDiffer.diffTable(db, table));
  }

  // 3. Diff Virtual Tables (FTS5, FTS4, RTree)
  if (targetSchema.virtualTables.isNotEmpty) {
    steps.addAll(
      await SchemaDiffer.diffVirtualTables(db, targetSchema.virtualTables),
    );
  }

  // 4. Diff Triggers
  if (targetSchema.triggers.isNotEmpty) {
    steps.addAll(await SchemaDiffer.diffTriggers(db, targetSchema.triggers));
  }

  return MigrationPlan(
    fromVersion: currentVersion,
    toVersion: targetSchema.version,
    steps: steps,
  );
}