Search in sources :

Example 1 with CurrentSchemaVersion

use of com.google.gerrit.reviewdb.client.CurrentSchemaVersion in project gerrit by GerritCodeReview.

the class SchemaUpdater method update.

public void update(final UpdateUI ui) throws OrmException {
    try (ReviewDb db = ReviewDbUtil.unwrapDb(schema.open())) {
        final SchemaVersion u = updater.get();
        final CurrentSchemaVersion version = getSchemaVersion(db);
        if (version == null) {
            try {
                creator.create(db);
            } catch (IOException | ConfigInvalidException e) {
                throw new OrmException("Cannot initialize schema", e);
            }
        } else {
            try {
                u.check(ui, version, db);
            } catch (SQLException e) {
                throw new OrmException("Cannot upgrade schema", e);
            }
            updateSystemConfig(db);
        }
    }
}
Also used : CurrentSchemaVersion(com.google.gerrit.reviewdb.client.CurrentSchemaVersion) CurrentSchemaVersion(com.google.gerrit.reviewdb.client.CurrentSchemaVersion) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) OrmException(com.google.gwtorm.server.OrmException) SQLException(java.sql.SQLException) IOException(java.io.IOException) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 2 with CurrentSchemaVersion

use of com.google.gerrit.reviewdb.client.CurrentSchemaVersion in project gerrit by GerritCodeReview.

the class SchemaVersion method migrateData.

private void migrateData(List<SchemaVersion> pending, UpdateUI ui, CurrentSchemaVersion curr, ReviewDb db) throws OrmException, SQLException {
    for (SchemaVersion v : pending) {
        Stopwatch sw = Stopwatch.createStarted();
        ui.message(String.format("Migrating data to schema %d ...", v.getVersionNbr()));
        v.migrateData(db, ui);
        v.finish(curr, db);
        ui.message(String.format("\t> Done (%.3f s)", sw.elapsed(TimeUnit.MILLISECONDS) / 1000d));
    }
}
Also used : CurrentSchemaVersion(com.google.gerrit.reviewdb.client.CurrentSchemaVersion) Stopwatch(com.google.common.base.Stopwatch)

Example 3 with CurrentSchemaVersion

use of com.google.gerrit.reviewdb.client.CurrentSchemaVersion in project gerrit by GerritCodeReview.

the class SchemaVersionCheck method start.

@Override
public void start() {
    try (ReviewDb db = schema.open()) {
        final CurrentSchemaVersion currentVer = getSchemaVersion(db);
        final int expectedVer = SchemaVersion.getBinaryVersion();
        if (currentVer == null) {
            throw new ProvisionException("Schema not yet initialized." + "  Run init to initialize the schema:\n" + "$ java -jar gerrit.war init -d " + site.site_path.toAbsolutePath());
        }
        if (currentVer.versionNbr < expectedVer) {
            throw new ProvisionException("Unsupported schema version " + currentVer.versionNbr + "; expected schema version " + expectedVer + ".  Run init to upgrade:\n" + "$ java -jar " + site.gerrit_war.toAbsolutePath() + " init -d " + site.site_path.toAbsolutePath());
        } else if (currentVer.versionNbr > expectedVer) {
            throw new ProvisionException("Unsupported schema version " + currentVer.versionNbr + "; expected schema version " + expectedVer + ". Downgrade is not supported.");
        }
    } catch (OrmException e) {
        throw new ProvisionException("Cannot read schema_version", e);
    }
}
Also used : CurrentSchemaVersion(com.google.gerrit.reviewdb.client.CurrentSchemaVersion) ProvisionException(com.google.inject.ProvisionException) OrmException(com.google.gwtorm.server.OrmException) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 4 with CurrentSchemaVersion

use of com.google.gerrit.reviewdb.client.CurrentSchemaVersion in project gerrit by GerritCodeReview.

the class SchemaCreator method create.

public void create(final ReviewDb db) throws OrmException, IOException, ConfigInvalidException {
    final JdbcSchema jdbc = (JdbcSchema) db;
    try (JdbcExecutor e = new JdbcExecutor(jdbc)) {
        jdbc.updateSchema(e);
    }
    final CurrentSchemaVersion sVer = CurrentSchemaVersion.create();
    sVer.versionNbr = SchemaVersion.getBinaryVersion();
    db.schemaVersion().insert(Collections.singleton(sVer));
    createDefaultGroups(db);
    initSystemConfig(db);
    allProjectsCreator.setAdministrators(GroupReference.forGroup(admin)).setBatchUsers(GroupReference.forGroup(batch)).create();
    allUsersCreator.setAdministrators(GroupReference.forGroup(admin)).create();
    dataSourceType.getIndexScript().run(db);
}
Also used : CurrentSchemaVersion(com.google.gerrit.reviewdb.client.CurrentSchemaVersion) JdbcSchema(com.google.gwtorm.jdbc.JdbcSchema) JdbcExecutor(com.google.gwtorm.jdbc.JdbcExecutor)

Example 5 with CurrentSchemaVersion

use of com.google.gerrit.reviewdb.client.CurrentSchemaVersion in project gerrit by GerritCodeReview.

the class SchemaVersion method upgradeFrom.

/** 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);
        }
    }
}
Also used : CurrentSchemaVersion(com.google.gerrit.reviewdb.client.CurrentSchemaVersion) JdbcSchema(com.google.gwtorm.jdbc.JdbcSchema) ArrayList(java.util.ArrayList) JdbcExecutor(com.google.gwtorm.jdbc.JdbcExecutor) StatementExecutor(com.google.gwtorm.server.StatementExecutor)

Aggregations

CurrentSchemaVersion (com.google.gerrit.reviewdb.client.CurrentSchemaVersion)5 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)2 JdbcExecutor (com.google.gwtorm.jdbc.JdbcExecutor)2 JdbcSchema (com.google.gwtorm.jdbc.JdbcSchema)2 OrmException (com.google.gwtorm.server.OrmException)2 Stopwatch (com.google.common.base.Stopwatch)1 StatementExecutor (com.google.gwtorm.server.StatementExecutor)1 ProvisionException (com.google.inject.ProvisionException)1 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)1