createMigration method

Future<MigrationVersion?> createMigration({
  1. String? tag,
  2. required bool force,
  3. required GeneratorConfig config,
  4. bool write = true,
})

Creates a new migration version. If tag is specified, the migration will be tagged with the given name. If force is true, the migration will be created even if there are warnings. If write is false, the migration will not be written to disk.

Returns the migration version, or null if no migration was created.

Throws MigrationVersionLoadException if the a migration version could not be loaded. Throws GenerateMigrationDatabaseDefinitionException if the database definition could not be created from project models. Throws MigrationVersionAlreadyExistsException if the migration version already exists.

Implementation

Future<MigrationVersion?> createMigration({
  String? tag,
  required bool force,
  required GeneratorConfig config,
  bool write = true,
}) async {
  var migrationRegistry = MigrationRegistry.load(
    MigrationConstants.migrationsBaseDirectory(directory),
  );

  var databaseDefinitionLatest = await _getSourceDatabaseDefinition(
    projectName,
    migrationRegistry.getLatest(),
  );

  var models = await ModelHelper.loadProjectYamlModelsFromDisk(
    config,
  );
  var modelDefinitions = StatefulAnalyzer(config, models, (uri, collector) {
    collector.printErrors();

    if (collector.hasSeverErrors) {
      throw GenerateMigrationDatabaseDefinitionException();
    }
  }).validateAll();

  var databaseDefinitionProject = createDatabaseDefinitionFromModels(
    modelDefinitions,
    config.name,
    config.modulesAll,
  );

  var databaseDefinitions = await _loadModuleDatabaseDefinitions(
    config.modulesDependent,
    directory,
  );

  var versionName = createVersionName(tag);
  var nextMigrationVersion = DatabaseMigrationVersion(
    module: projectName,
    version: versionName,
  );

  var databaseDefinitionNext = _mergeDatabaseDefinitions(
    databaseDefinitionProject,
    databaseDefinitions,
    nextMigrationVersion,
  );

  var migration = generateDatabaseMigration(
    databaseSource: databaseDefinitionLatest,
    databaseTarget: databaseDefinitionNext,
  );

  var warnings = migration.warnings;
  _printWarnings(warnings);

  if (warnings.isNotEmpty && !force) {
    log.info('Migration aborted. Use --force to ignore warnings.');
    return null;
  }

  if (migration.isEmpty && !force) {
    log.info(
      'No changes detected. Use --force to create an empty migration.',
    );
    return null;
  }

  var migrationVersion = MigrationVersion(
    moduleName: projectName,
    projectDirectory: directory,
    versionName: versionName,
    migration: migration,
    databaseDefinitionProject: databaseDefinitionProject,
    databaseDefinitionFull: databaseDefinitionNext,
  );

  if (write) {
    var removedModules = _removedModulesDiff(
      databaseDefinitionLatest.installedModules,
      databaseDefinitionNext.installedModules,
    );

    await migrationVersion.write(
      installedModules: databaseDefinitionNext.installedModules,
      removedModules: removedModules,
    );
    migrationRegistry.add(versionName);
    await migrationRegistry.write();
  }

  return migrationVersion;
}