Search in sources :

Example 6 with SchemaMetadata

use of org.molgenis.emx2.SchemaMetadata in project molgenis-emx2 by molgenis.

the class JsonYamlApi method postSchemaJSON.

static String postSchemaJSON(Request request, Response response) throws IOException {
    Schema schema = getSchema(request);
    SchemaMetadata otherSchema = jsonToSchema(request.body());
    schema.migrate(otherSchema);
    response.status(200);
    return "patch metadata success";
}
Also used : MolgenisWebservice.getSchema(org.molgenis.emx2.web.MolgenisWebservice.getSchema)

Example 7 with SchemaMetadata

use of org.molgenis.emx2.SchemaMetadata in project molgenis-emx2 by molgenis.

the class TestCohortCatalogue method importTest.

@Test
public void importTest() throws IOException {
    StopWatch.print("begin");
    // load data model
    SchemaMetadata schema = Emx2.fromRowList(CsvTableReader.read(new File("../../data/datacatalogue/molgenis.csv")));
    cohortsSchema.migrate(schema);
    // load ontologies
    MolgenisIO.fromDirectory(new File("../../data/datacatalogue/CatalogueOntologies").toPath(), ontologySchema, false);
    MolgenisIO.fromDirectory(new File("../../data/datacatalogue/Cohorts").toPath(), cohortsSchema, false);
    MolgenisIO.importFromExcelFile(new File("../../data/datacatalogue/Cohorts_CoreVariables.xlsx").toPath(), cohortsSchema, // todo fix data so we can put strict=true
    false);
    assertEquals(36, TestCohortCatalogue.cohortsSchema.getTableNames().size());
// export import schema to compare
}
Also used : SchemaMetadata(org.molgenis.emx2.SchemaMetadata) File(java.io.File) Test(org.junit.Test)

Example 8 with SchemaMetadata

use of org.molgenis.emx2.SchemaMetadata in project molgenis-emx2 by molgenis.

the class TestCohortCatalogue method importTestRWE.

@Test
public void importTestRWE() throws IOException {
    StopWatch.print("begin");
    // load data model
    SchemaMetadata schema = Emx2.fromRowList(CsvTableReader.read(new File("../../data/datacatalogue/molgenis.csv")));
    rweSchema.migrate(schema);
    // should be possible to update same schema
    database.clearCache();
    schema = Emx2.fromRowList(CsvTableReader.read(new File("../../data/datacatalogue/molgenis.csv")));
    rweSchema.migrate(schema);
    // load ontologies
    MolgenisIO.fromDirectory(new File("../../data/datacatalogue/CatalogueOntologies").toPath(), ontologySchema, false);
    MolgenisIO.fromDirectory(new File("../../data/datacatalogue/RWEcatalogue").toPath(), rweSchema, false);
    assertEquals(36, TestCohortCatalogue.rweSchema.getTableNames().size());
// export import schema to compare
}
Also used : SchemaMetadata(org.molgenis.emx2.SchemaMetadata) File(java.io.File) Test(org.junit.Test)

Example 9 with SchemaMetadata

use of org.molgenis.emx2.SchemaMetadata in project molgenis-emx2 by molgenis.

the class TestMergeDrop method testDrop.

@Test
public void testDrop() {
    SchemaMetadata newSchema = new SchemaMetadata();
    newSchema.create(table("Person", column("name")));
    schema.migrate(newSchema);
    // idempotent
    schema.migrate(newSchema);
    assertNotNull(schema.getTable("Person"));
    newSchema = new SchemaMetadata();
    newSchema.create(table("Person"));
    newSchema.getTableMetadata("Person").drop();
    schema.migrate(newSchema);
    // should be idempotent
    schema.migrate(newSchema);
    assertNull(schema.getTable("Person"));
    // now more complex, with dependency
    newSchema = new SchemaMetadata();
    newSchema.create(table("Person", column("name").setPkey()));
    newSchema.create(table("Pet", column("name").setPkey(), column("owner").setType(ColumnType.REF).setRefTable("Person")));
    schema.migrate(newSchema);
    // should be idempotent so repeat
    schema.migrate(newSchema);
    assertNotNull(schema.getTable("Person"));
    assertNotNull(schema.getTable("Pet"));
    schema.getTable("Person").insert(row("name", "Donald"));
    schema.getTable("Pet").insert(row("name", "Pluto", "owner", "Donald"));
    // should fail
    newSchema = new SchemaMetadata();
    try {
        newSchema.create(table("Person"));
        newSchema.getTableMetadata("Person").drop();
        schema.migrate(newSchema);
        fail("should fail because of foreign key");
    } catch (Exception e) {
    // fine
    }
    // should succeed
    newSchema.create(table("Pet"));
    newSchema.getTableMetadata("Pet").drop();
    schema.migrate(newSchema);
    // should be idempotent so repeat
    schema.migrate(newSchema);
    assertNull(schema.getTable("Person"));
    assertNull(schema.getTable("Pet"));
}
Also used : SchemaMetadata(org.molgenis.emx2.SchemaMetadata) Test(org.junit.Test)

Example 10 with SchemaMetadata

use of org.molgenis.emx2.SchemaMetadata in project molgenis-emx2 by molgenis.

the class GraphqlSchemaFieldFactory method changeTables.

private void changeTables(Schema schema, DataFetchingEnvironment dataFetchingEnvironment) throws IOException {
    Object tables = dataFetchingEnvironment.getArgument(GraphqlConstants.TABLES);
    // tables
    if (tables != null) {
        Map tableMap = Map.of("tables", tables);
        String json = JsonUtil.getWriter().writeValueAsString(tableMap);
        SchemaMetadata otherSchema = jsonToSchema(json);
        schema.migrate(otherSchema);
    }
}
Also used : SqlSchemaMetadata(org.molgenis.emx2.sql.SqlSchemaMetadata) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

ColumnType (org.molgenis.emx2.ColumnType)6 SchemaMetadata (org.molgenis.emx2.SchemaMetadata)5 TableMetadata (org.molgenis.emx2.TableMetadata)5 Test (org.junit.Test)4 File (java.io.File)2 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1 SqlSchemaMetadata (org.molgenis.emx2.sql.SqlSchemaMetadata)1 MolgenisWebservice.getSchema (org.molgenis.emx2.web.MolgenisWebservice.getSchema)1