Skip to content

Reset schema when validateDatabaseSchema() fails #2684

Answered by simolus3
hiroshihorie asked this question in Q&A
Discussion options

You must be logged in to vote

Something like this in the MigrationStrategy should work:

}, beforeOpen: (details) async {
  try {
    await validateDatabaseSchema();
  } on SchemaMismatch {
    // Delete all databases and re-create
    final migrator = createMigrator();
    final reversedEntities = allSchemaEntities.toList().reversed;

    for (final entity in reversedEntities) {
      await migrator.drop(entity);
    }

    await migrator.createAll();
  }
});

You don't need an onUpgrade callback at all in that case.

Replies: 1 comment 7 replies

Comment options

You must be logged in to vote
7 replies
@hiroshihorie
Comment options

@albertmoravec
Comment options

@simolus3
Comment options

@albertmoravec
Comment options

@simolus3
Comment options

Answer selected by hiroshihorie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants