makeMigration function

Migration makeMigration(
  1. MetaData migration,
  2. QueryBuilder builder
)

Takes a migration's meta data and returns a migration. The returned migration contains all DDL statements as well as the necessary triggers. migration The migration's meta data. @returns The corresponding migration.

Implementation

Migration makeMigration(MetaData migration, QueryBuilder builder) {
  final statements = migration.ops
      .map((op) => op.stmts.map((stmt) => stmt.sql))
      .expand((l) => l);

  final tablesI = migration.ops
      // if the operation did not change any table
      // then ignore it as we don't have to build triggers for it
      .where((op) => op.hasTable())
      .map((op) => op.table)
      .toList();

  // remove duplicate tables
  final tables = tablesI.whereIndexed((idx, tbl) {
    return tablesI.indexWhere((t) => t.name == tbl.name) == idx;
  });

  final triggers = tables
      .map((tbl) => generateTriggersForTable(tbl, builder))
      .expand((l) => l)
      .map((stmt) => stmt.sql);

  return Migration(
    statements: [...statements, ...triggers],
    version: migration.version,
  );
}