Search in sources :

Example 1 with RelationModel

use of com.google.gwtorm.schema.RelationModel in project gerrit by GerritCodeReview.

the class ProtobufImport method run.

@Override
public int run() throws Exception {
    mustHaveValidSite();
    Injector dbInjector = createDbInjector(SINGLE_USER);
    manager.add(dbInjector);
    manager.start();
    RuntimeShutdown.add(manager::stop);
    dbInjector.injectMembers(this);
    ProgressMonitor progress = new TextProgressMonitor();
    progress.beginTask("Importing entities", ProgressMonitor.UNKNOWN);
    try (ReviewDb db = schemaFactory.open()) {
        for (RelationModel model : new JavaSchemaModel(ReviewDb.class).getRelations()) {
            relations.put(model.getRelationID(), Relation.create(model, db));
        }
        Parser<UnknownFieldSet> parser = UnknownFieldSet.getDefaultInstance().getParserForType();
        try (InputStream in = new BufferedInputStream(Files.newInputStream(file.toPath()))) {
            UnknownFieldSet msg;
            while ((msg = parser.parseDelimitedFrom(in)) != null) {
                Map.Entry<Integer, UnknownFieldSet.Field> e = Iterables.getOnlyElement(msg.asMap().entrySet());
                Relation rel = checkNotNull(relations.get(e.getKey()), "unknown relation ID %s in message: %s", e.getKey(), msg);
                List<ByteString> values = e.getValue().getLengthDelimitedList();
                checkState(values.size() == 1, "expected one string field in message: %s", msg);
                upsert(rel, values.get(0));
                progress.update(1);
            }
        }
        progress.endTask();
    }
    return 0;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) ByteString(com.google.protobuf.ByteString) TextProgressMonitor(org.eclipse.jgit.lib.TextProgressMonitor) ProgressMonitor(org.eclipse.jgit.lib.ProgressMonitor) TextProgressMonitor(org.eclipse.jgit.lib.TextProgressMonitor) BufferedInputStream(java.io.BufferedInputStream) Injector(com.google.inject.Injector) RelationModel(com.google.gwtorm.schema.RelationModel) UnknownFieldSet(com.google.protobuf.UnknownFieldSet) HashMap(java.util.HashMap) Map(java.util.Map) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) JavaSchemaModel(com.google.gwtorm.schema.java.JavaSchemaModel)

Example 2 with RelationModel

use of com.google.gwtorm.schema.RelationModel in project gerrit by GerritCodeReview.

the class Schema_101 method findPKUpdates.

private Map<String, PrimaryKey> findPKUpdates() throws OrmException, SQLException {
    Map<String, PrimaryKey> corrections = new TreeMap<>();
    DatabaseMetaData meta = conn.getMetaData();
    JavaSchemaModel jsm = new JavaSchemaModel(ReviewDb.class);
    for (RelationModel rm : jsm.getRelations()) {
        String tableName = rm.getRelationName();
        List<String> expectedPKCols = relationPK(rm);
        PrimaryKey actualPK = dbTablePK(meta, tableName);
        if (!expectedPKCols.equals(actualPK.cols)) {
            actualPK.cols = expectedPKCols;
            corrections.put(tableName, actualPK);
        }
    }
    return corrections;
}
Also used : RelationModel(com.google.gwtorm.schema.RelationModel) TreeMap(java.util.TreeMap) DatabaseMetaData(java.sql.DatabaseMetaData) JavaSchemaModel(com.google.gwtorm.schema.java.JavaSchemaModel)

Aggregations

RelationModel (com.google.gwtorm.schema.RelationModel)2 JavaSchemaModel (com.google.gwtorm.schema.java.JavaSchemaModel)2 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)1 Injector (com.google.inject.Injector)1 ByteString (com.google.protobuf.ByteString)1 UnknownFieldSet (com.google.protobuf.UnknownFieldSet)1 BufferedInputStream (java.io.BufferedInputStream)1 InputStream (java.io.InputStream)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ProgressMonitor (org.eclipse.jgit.lib.ProgressMonitor)1 TextProgressMonitor (org.eclipse.jgit.lib.TextProgressMonitor)1