Beispiel #1
0
 private void migrateData(
     List<SchemaVersion> pending, UpdateUI ui, CurrentSchemaVersion curr, ReviewDb db)
     throws OrmException, SQLException {
   for (SchemaVersion v : pending) {
     ui.message(String.format("Migrating data to schema %d ...", v.getVersionNbr()));
     v.migrateData(db, ui);
     v.finish(curr, db);
   }
 }
  /** Runs check on the prior schema version, and then upgrades. */
  protected void upgradeFrom(
      UpdateUI ui, CurrentSchemaVersion curr, ReviewDb db, boolean toTargetVersion)
      throws OrmException, SQLException {
    final JdbcSchema s = (JdbcSchema) db;

    if (curr.versionNbr > versionNbr) {
      throw new OrmException(
          "Cannot downgrade database schema from version "
              + curr.versionNbr
              + " to "
              + versionNbr
              + ".");
    }

    prior.get().check(ui, curr, db, false);

    ui.message(
        "Upgrading database schema from version " + curr.versionNbr + " to " + versionNbr + " ...");

    preUpdateSchema(db);
    final JdbcExecutor e = new JdbcExecutor(s);
    try {
      s.updateSchema(e);
      migrateData(db, ui);

      if (toTargetVersion) {
        final List<String> pruneList = new ArrayList<String>();
        s.pruneSchema(
            new StatementExecutor() {
              public void execute(String sql) {
                pruneList.add(sql);
              }
            });

        if (!pruneList.isEmpty()) {
          ui.pruneSchema(e, pruneList);
        }
      }
    } finally {
      e.close();
    }
    finish(curr, db);
  }