Skip to content

Commit

Permalink
app: Always rename studies from storage when changing organization id…
Browse files Browse the repository at this point in the history
…. #TASK-7207
  • Loading branch information
j-coll committed Nov 15, 2024
1 parent be61b15 commit d30dac9
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.opencb.commons.datastore.core.ObjectMap;
import org.opencb.commons.datastore.mongodb.MongoDataStore;
import org.opencb.commons.utils.CryptoUtils;
import org.opencb.commons.utils.FileUtils;
Expand All @@ -27,6 +28,7 @@
import org.opencb.opencga.catalog.utils.FqnUtils;
import org.opencb.opencga.catalog.utils.ParamUtils;
import org.opencb.opencga.core.api.ParamConstants;
import org.opencb.opencga.core.common.JacksonUtils;
import org.opencb.opencga.core.common.TimeUtils;
import org.opencb.opencga.core.config.AuthenticationOrigin;
import org.opencb.opencga.core.config.Configuration;
Expand Down Expand Up @@ -510,38 +512,46 @@ private void changeFqns() throws CatalogDBException, MigrationException {
Document datastores = internal.get("datastores", Document.class);
if (datastores != null) {
Document variant = datastores.get("variant", Document.class);
DataStore dataStore;
boolean updateVariant = false;
if (variant == null) {
DataStore dataStore = VariantStorageManager.defaultDataStore(configuration.getDatabasePrefix(), oldProjectFqn);
logger.info("Undefined variant \"internal.datastores.variant\" at project '{}'.", oldProjectFqn);

// Update only if the project exists in the variant storage
try (VariantStorageEngine variantStorageEngine = storageEngineFactory
.getVariantStorageEngine(dataStore.getStorageEngine(), dataStore.getDbName())) {
if (variantStorageEngine.getMetadataManager().exists()) {
logger.info("Project exists in the variant storage. Setting variant data store: {}", dataStore);
dataStore = VariantStorageManager.defaultDataStore(configuration.getDatabasePrefix(), oldProjectFqn);
updateVariant = true;
} else {
dataStore = JacksonUtils.getDefaultObjectMapper().convertValue(variant, DataStore.class);
}
// Update only if the project exists in the variant storage
try (VariantStorageEngine variantStorageEngine = storageEngineFactory
.getVariantStorageEngine(dataStore.getStorageEngine(), dataStore.getDbName())) {
logger.info("Project '{}' exists in the variant storage.", oldProjectFqn);
if (variantStorageEngine.getMetadataManager().exists()) {
if (updateVariant) {
logger.info("Undefined variant \"internal.datastores.variant\" at project '{}'.", oldProjectFqn);
set.append("internal.datastores.variant", new Document()
.append("storageEngine", dataStore.getStorageEngine())
.append("dbName", dataStore.getDbName())
.append("options", new Document()));

for (String oldStudyFqn : variantStorageEngine.getMetadataManager().getStudies().keySet()) {
String newStudyFqn = FqnUtils.buildFqn(this.organizationId, projectId, FqnUtils.parse(oldStudyFqn).getStudy());
logger.info("Changing study fqn from '{}' to '{}'", oldStudyFqn, newStudyFqn);
variantStorageEngine.getMetadataManager().updateStudyMetadata(oldStudyFqn, studyMetadata -> {
studyMetadata.setName(newStudyFqn);
studyMetadata.getAttributes().put("OPENCGA.3_0_0", new Document()
.append("date", date)
.append("oldFqn", oldStudyFqn)
);
});
}
} else {
logger.info("Project does not exist in the variant storage. Skipping");
logger.info("Datastore variant at project '{}': {}", oldProjectFqn, datastores);
}
} catch (StorageEngineException | IOException e) {
throw new RuntimeException(e);
}

for (String oldStudyFqn : variantStorageEngine.getMetadataManager().getStudies().keySet()) {
String study = FqnUtils.parse(oldStudyFqn).getStudy();
String newStudyFqn = FqnUtils.buildFqn(this.organizationId, projectId, study);
logger.info("Changing study fqn from '{}' to '{}'", oldStudyFqn, newStudyFqn);
variantStorageEngine.getMetadataManager().updateStudyMetadata(oldStudyFqn, studyMetadata -> {
studyMetadata.setName(newStudyFqn);
studyMetadata.getAttributes().put("OPENCGA_3_0_0", new ObjectMap()
.append("date", date)
.append("oldFqn", oldStudyFqn)
);
});
}
} else {
logger.info("Project '{}' does not exist in the variant storage. Skipping", oldProjectFqn);
}
} catch (StorageEngineException | IOException e) {
throw new RuntimeException(e);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public String toString() {
final StringBuilder sb = new StringBuilder("DataStore{");
sb.append("storageEngine='").append(storageEngine).append('\'');
sb.append(", dbName='").append(dbName).append('\'');
sb.append(", options=").append(options);
sb.append('}');
return sb.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.opencb.biodata.models.variant.VariantFileMetadata;
import org.opencb.commons.datastore.core.ObjectMap;
import org.opencb.commons.datastore.core.QueryOptions;
import org.opencb.opencga.core.common.TimeUtils;
import org.opencb.opencga.core.tools.ToolParams;
import org.opencb.opencga.storage.core.exceptions.StorageEngineException;
import org.opencb.opencga.storage.core.metadata.VariantStorageMetadataManager;
Expand Down Expand Up @@ -151,6 +152,10 @@ private void rename(String currentStudyName, String newStudyName) throws Storage
int studyId = mm.getStudyId(currentStudyName);
mm.updateStudyMetadata(studyId, studyMetadata -> {
studyMetadata.setName(newStudyName);
studyMetadata.getAttributes().put("rename_" + TimeUtils.getTime(), new ObjectMap()
.append("newName", newStudyName)
.append("oldName", currentStudyName)
);
});
}
}
Expand Down

0 comments on commit d30dac9

Please sign in to comment.