Search in sources :

Example 6 with JdbcSchema

use of com.google.gwtorm.jdbc.JdbcSchema in project gerrit by GerritCodeReview.

the class SchemaVersion method updateSchema.

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);
    }
}
Also used : CurrentSchemaVersion(com.google.gerrit.reviewdb.client.CurrentSchemaVersion) JdbcSchema(com.google.gwtorm.jdbc.JdbcSchema) JdbcExecutor(com.google.gwtorm.jdbc.JdbcExecutor)

Example 7 with JdbcSchema

use of com.google.gwtorm.jdbc.JdbcSchema in project gerrit by GerritCodeReview.

the class Schema_102 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
    JdbcSchema schema = (JdbcSchema) db;
    SqlDialect dialect = schema.getDialect();
    try (StatementExecutor e = newExecutor(db)) {
        // Drop left over indexes that were missed to be removed in schema 84.
        // See "Delete SQL index support" commit for more details:
        // d4ae3a16d5e1464574bd04f429a63eb9c02b3b43
        Pattern pattern = Pattern.compile("^changes_(allOpen|allClosed|byBranchClosed)$", Pattern.CASE_INSENSITIVE);
        String table = "changes";
        Set<String> listIndexes = dialect.listIndexes(schema.getConnection(), table);
        for (String index : listIndexes) {
            if (pattern.matcher(index).matches()) {
                dialect.dropIndex(e, table, index);
            }
        }
        dialect.dropIndex(e, table, "changes_byProjectOpen");
        if (dialect instanceof DialectPostgreSQL) {
            e.execute("CREATE INDEX changes_byProjectOpen" + " ON " + table + " (dest_project_name, last_updated_on)" + " WHERE open = 'Y'");
        } else {
            e.execute("CREATE INDEX changes_byProjectOpen" + " ON " + table + " (open, dest_project_name, last_updated_on)");
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) JdbcSchema(com.google.gwtorm.jdbc.JdbcSchema) DialectPostgreSQL(com.google.gwtorm.schema.sql.DialectPostgreSQL) SqlDialect(com.google.gwtorm.schema.sql.SqlDialect) StatementExecutor(com.google.gwtorm.server.StatementExecutor)

Example 8 with JdbcSchema

use of com.google.gwtorm.jdbc.JdbcSchema in project gerrit by GerritCodeReview.

the class Schema_105 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws SQLException, OrmException {
    JdbcSchema schema = (JdbcSchema) db;
    SqlDialect dialect = schema.getDialect();
    Map<String, OrmException> errors = new HashMap<>();
    try (StatementExecutor e = newExecutor(db)) {
        for (String index : listChangesIndexes(schema)) {
            ui.message("Dropping index " + index + " on table " + TABLE);
            try {
                dialect.dropIndex(e, TABLE, index);
            } catch (OrmException err) {
                errors.put(index, err);
            }
        }
    }
    for (String index : listChangesIndexes(schema)) {
        String msg = "Failed to drop index " + index;
        OrmException err = errors.get(index);
        if (err != null) {
            msg += ": " + err.getMessage();
        }
        ui.message(msg);
    }
}
Also used : JdbcSchema(com.google.gwtorm.jdbc.JdbcSchema) HashMap(java.util.HashMap) OrmException(com.google.gwtorm.server.OrmException) SqlDialect(com.google.gwtorm.schema.sql.SqlDialect) StatementExecutor(com.google.gwtorm.server.StatementExecutor)

Example 9 with JdbcSchema

use of com.google.gwtorm.jdbc.JdbcSchema 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 10 with JdbcSchema

use of com.google.gwtorm.jdbc.JdbcSchema 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

JdbcSchema (com.google.gwtorm.jdbc.JdbcSchema)11 SqlDialect (com.google.gwtorm.schema.sql.SqlDialect)5 StatementExecutor (com.google.gwtorm.server.StatementExecutor)5 OrmException (com.google.gwtorm.server.OrmException)4 CurrentSchemaVersion (com.google.gerrit.reviewdb.client.CurrentSchemaVersion)3 JdbcExecutor (com.google.gwtorm.jdbc.JdbcExecutor)3 Connection (java.sql.Connection)3 Statement (java.sql.Statement)3 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 HashMap (java.util.HashMap)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 GeneralPreferencesInfo (com.google.gerrit.extensions.client.GeneralPreferencesInfo)1 Account (com.google.gerrit.reviewdb.client.Account)1 VersionedAccountPreferences (com.google.gerrit.server.account.VersionedAccountPreferences)1 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)1 DialectPostgreSQL (com.google.gwtorm.schema.sql.DialectPostgreSQL)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1