Search in sources :

Example 1 with JdbcSchema

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

the class Schema_145 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)) {
        try {
            dialect.dropIndex(e, "account_external_ids", "account_external_ids_byEmail");
        } catch (OrmException ex) {
        // Ignore.  The index did not exist.
        }
        e.execute("CREATE INDEX account_external_ids_byEmail" + " ON account_external_ids" + " (email_address)");
    }
}
Also used : JdbcSchema(com.google.gwtorm.jdbc.JdbcSchema) OrmException(com.google.gwtorm.server.OrmException) SqlDialect(com.google.gwtorm.schema.sql.SqlDialect) StatementExecutor(com.google.gwtorm.server.StatementExecutor)

Example 2 with JdbcSchema

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

the class Schema_117 method preUpdateSchema.

@Override
protected void preUpdateSchema(ReviewDb db) throws OrmException, SQLException {
    JdbcSchema schema = (JdbcSchema) db;
    Connection connection = schema.getConnection();
    String tableName = "patch_sets";
    String oldColumnName = "push_certficate";
    String newColumnName = "push_certificate";
    Set<String> columns = schema.getDialect().listColumns(connection, tableName);
    if (columns.contains(oldColumnName)) {
        renameColumn(db, tableName, oldColumnName, newColumnName);
    }
    try (Statement stmt = schema.getConnection().createStatement()) {
        stmt.execute("ALTER TABLE " + tableName + " MODIFY " + newColumnName + " clob");
    } catch (SQLException e) {
    // Ignore.  Type may have already been modified manually.
    }
}
Also used : JdbcSchema(com.google.gwtorm.jdbc.JdbcSchema) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection)

Example 3 with JdbcSchema

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

the class Schema_119 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
    JdbcSchema schema = (JdbcSchema) db;
    Connection connection = schema.getConnection();
    String tableName = "accounts";
    String emailStrategy = "email_strategy";
    Set<String> columns = schema.getDialect().listColumns(connection, tableName);
    Map<Account.Id, GeneralPreferencesInfo> imports = new HashMap<>();
    try (Statement stmt = ((JdbcSchema) db).getConnection().createStatement();
        ResultSet rs = stmt.executeQuery("select " + "account_id, " + "maximum_page_size, " + "show_site_header, " + "use_flash_clipboard, " + "download_url, " + "download_command, " + (columns.contains(emailStrategy) ? emailStrategy + ", " : "copy_self_on_email, ") + "date_format, " + "time_format, " + "relative_date_in_change_table, " + "diff_view, " + "size_bar_in_change_table, " + "legacycid_in_change_table, " + "review_category_strategy, " + "mute_common_path_prefixes " + "from " + tableName)) {
        while (rs.next()) {
            GeneralPreferencesInfo p = new GeneralPreferencesInfo();
            Account.Id accountId = new Account.Id(rs.getInt(1));
            p.changesPerPage = (int) rs.getShort(2);
            p.showSiteHeader = toBoolean(rs.getString(3));
            p.useFlashClipboard = toBoolean(rs.getString(4));
            p.downloadScheme = convertToModernNames(rs.getString(5));
            p.downloadCommand = toDownloadCommand(rs.getString(6));
            p.emailStrategy = toEmailStrategy(rs.getString(7), columns.contains(emailStrategy));
            p.dateFormat = toDateFormat(rs.getString(8));
            p.timeFormat = toTimeFormat(rs.getString(9));
            p.relativeDateInChangeTable = toBoolean(rs.getString(10));
            p.diffView = toDiffView(rs.getString(11));
            p.sizeBarInChangeTable = toBoolean(rs.getString(12));
            p.legacycidInChangeTable = toBoolean(rs.getString(13));
            p.reviewCategoryStrategy = toReviewCategoryStrategy(rs.getString(14));
            p.muteCommonPathPrefixes = toBoolean(rs.getString(15));
            p.defaultBaseForMerges = GeneralPreferencesInfo.defaults().defaultBaseForMerges;
            imports.put(accountId, p);
        }
    }
    if (imports.isEmpty()) {
        return;
    }
    try (Repository git = mgr.openRepository(allUsersName);
        RevWalk rw = new RevWalk(git)) {
        BatchRefUpdate bru = git.getRefDatabase().newBatchUpdate();
        for (Map.Entry<Account.Id, GeneralPreferencesInfo> e : imports.entrySet()) {
            try (MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsersName, git, bru)) {
                md.getCommitBuilder().setAuthor(serverUser);
                md.getCommitBuilder().setCommitter(serverUser);
                VersionedAccountPreferences p = VersionedAccountPreferences.forUser(e.getKey());
                p.load(md);
                storeSection(p.getConfig(), UserConfigSections.GENERAL, null, e.getValue(), GeneralPreferencesInfo.defaults());
                p.commit(md);
            }
        }
        bru.execute(rw, NullProgressMonitor.INSTANCE);
    } catch (ConfigInvalidException | IOException ex) {
        throw new OrmException(ex);
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) JdbcSchema(com.google.gwtorm.jdbc.JdbcSchema) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) HashMap(java.util.HashMap) Statement(java.sql.Statement) Connection(java.sql.Connection) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) ResultSet(java.sql.ResultSet) GeneralPreferencesInfo(com.google.gerrit.extensions.client.GeneralPreferencesInfo) VersionedAccountPreferences(com.google.gerrit.server.account.VersionedAccountPreferences) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Example 4 with JdbcSchema

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

the class Schema_89 method migrateData.

@Override
protected void migrateData(ReviewDb db, UpdateUI ui) throws OrmException, SQLException {
    SqlDialect dialect = ((JdbcSchema) db).getDialect();
    try (StatementExecutor e = newExecutor(db)) {
        dialect.dropIndex(e, "patch_set_approvals", "patch_set_approvals_openByUser");
        dialect.dropIndex(e, "patch_set_approvals", "patch_set_approvals_closedByU");
    }
}
Also used : JdbcSchema(com.google.gwtorm.jdbc.JdbcSchema) SqlDialect(com.google.gwtorm.schema.sql.SqlDialect) StatementExecutor(com.google.gwtorm.server.StatementExecutor)

Example 5 with JdbcSchema

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

the class ScriptRunner method run.

void run(final ReviewDb db) throws OrmException {
    try {
        final JdbcSchema schema = (JdbcSchema) db;
        final Connection c = schema.getConnection();
        final SqlDialect dialect = schema.getDialect();
        try (Statement stmt = c.createStatement()) {
            for (String sql : commands) {
                try {
                    if (!dialect.isStatementDelimiterSupported()) {
                        sql = CharMatcher.is(';').trimTrailingFrom(sql);
                    }
                    stmt.execute(sql);
                } catch (SQLException e) {
                    throw new OrmException("Error in " + name + ":\n" + sql, e);
                }
            }
        }
    } catch (SQLException e) {
        throw new OrmException("Cannot run statements for " + name, e);
    }
}
Also used : JdbcSchema(com.google.gwtorm.jdbc.JdbcSchema) SQLException(java.sql.SQLException) OrmException(com.google.gwtorm.server.OrmException) Statement(java.sql.Statement) Connection(java.sql.Connection) SqlDialect(com.google.gwtorm.schema.sql.SqlDialect)

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