Exemple #1
0
  /** Runs check on the prior schema version, and then upgrades. */
  private void upgradeFrom(UpdateUI ui, CurrentSchemaVersion curr, ReviewDb db)
      throws OrmException, SQLException {
    List<SchemaVersion> pending = pending(curr.versionNbr);
    updateSchema(pending, ui, db);
    migrateData(pending, ui, curr, db);

    JdbcSchema s = (JdbcSchema) db;
    final List<String> pruneList = new ArrayList<>();
    s.pruneSchema(
        new StatementExecutor() {
          @Override
          public void execute(String sql) {
            pruneList.add(sql);
          }

          @Override
          public void close() {
            // Do nothing.
          }
        });

    try (JdbcExecutor e = new JdbcExecutor(s)) {
      if (!pruneList.isEmpty()) {
        ui.pruneSchema(e, pruneList);
      }
    }
  }
Exemple #2
0
 /** Rename an existing column. */
 protected static void renameColumn(ReviewDb db, String table, String from, String to)
     throws OrmException {
   JdbcSchema s = (JdbcSchema) db;
   try (JdbcExecutor e = new JdbcExecutor(s)) {
     s.renameColumn(e, table, from, to);
   }
 }
  @Test
  public void testGetCauses_CreateSchema() throws OrmException, SQLException, IOException {
    // Initially the schema should be empty.
    //
    {
      final JdbcSchema d = (JdbcSchema) db.open();
      try {
        final String[] types = {"TABLE", "VIEW"};
        final ResultSet rs = d.getConnection().getMetaData().getTables(null, null, null, types);
        try {
          assertFalse(rs.next());
        } finally {
          rs.close();
        }
      } finally {
        d.close();
      }
    }

    // Create the schema using the current schema version.
    //
    db.create();
    db.assertSchemaVersion();

    // By default sitePath is set to the current working directory.
    //
    File sitePath = new File(".").getAbsoluteFile();
    if (sitePath.getName().equals(".")) {
      sitePath = sitePath.getParentFile();
    }
    assertEquals(sitePath.getCanonicalPath(), db.getSystemConfig().sitePath);
  }
 /** Rename an existing column. */
 protected void renameColumn(ReviewDb db, String table, String from, String to)
     throws OrmException {
   final JdbcSchema s = (JdbcSchema) db;
   final JdbcExecutor e = new JdbcExecutor(s);
   try {
     s.renameField(e, table, from, to);
   } finally {
     e.close();
   }
 }
Exemple #5
0
  private void updateSchema(List<SchemaVersion> pending, UpdateUI ui, ReviewDb db)
      throws OrmException, SQLException {
    for (SchemaVersion v : pending) {
      ui.message(String.format("Upgrading schema to %d ...", v.getVersionNbr()));
      v.preUpdateSchema(db);
    }

    JdbcSchema s = (JdbcSchema) db;
    try (JdbcExecutor e = new JdbcExecutor(s)) {
      s.updateSchema(e);
    }
  }
  /** 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);
  }