use of com.google.gwtorm.schema.java.JavaSchemaModel in project gerrit by GerritCodeReview.
the class ProtoGen method run.
@Override
public int run() throws Exception {
LockFile lock = new LockFile(file.getAbsoluteFile());
if (!lock.lock()) {
throw die("Cannot lock " + file);
}
try {
JavaSchemaModel jsm = new JavaSchemaModel(ReviewDb.class);
try (OutputStream o = lock.getOutputStream();
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(o, UTF_8)))) {
String header;
try (InputStream in = getClass().getResourceAsStream("ProtoGenHeader.txt")) {
ByteBuffer buf = IO.readWholeStream(in, 1024);
int ptr = buf.arrayOffset() + buf.position();
int len = buf.remaining();
header = new String(buf.array(), ptr, len, UTF_8);
}
String version = com.google.gerrit.common.Version.getVersion();
out.write(header.replace("@@VERSION@@", version));
jsm.generateProto(out);
out.flush();
}
if (!lock.commit()) {
throw die("Could not write to " + file);
}
} finally {
lock.unlock();
}
System.out.println("Created " + file.getPath());
return 0;
}
use of com.google.gwtorm.schema.java.JavaSchemaModel 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;
}
use of com.google.gwtorm.schema.java.JavaSchemaModel 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;
}
Aggregations