Search in sources :

Example 11 with SchemaMetadata

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

the class TestGetSetAllTypesOnRows method addContents.

private void addContents(SchemaMetadata m, List<ColumnType> columnTypes) {
    TableMetadata t = m.create(table("TypeTest"));
    for (ColumnType columnType : columnTypes) {
        t.add(column("test" + columnType).setType(columnType).setRequired(true));
        t.add(column("test" + columnType + "_nullable").setType(columnType));
        t.add(column("test" + columnType + "_readonly").setType(columnType).setReadonly(true));
    }
}
Also used : ColumnType(org.molgenis.emx2.ColumnType)

Example 12 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 13 with SchemaMetadata

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

the class RefAndRefArrayTestExample method createRefAndRefArrayTestExample.

public static void createRefAndRefArrayTestExample(SchemaMetadata schema) {
    ColumnType[] columnTypes = new ColumnType[] { UUID, STRING, BOOL, INT, DECIMAL, TEXT, DATE, DATETIME };
    for (ColumnType columnType : columnTypes) {
        String aTableName = columnType.toString() + "_A";
        String fieldName = "AKeyOf" + columnType;
        schema.create(table(aTableName).add(column(fieldName).setType(columnType).setPkey()));
        String bTableName = columnType.toString() + "_B";
        String refFromBToA = "RefToAKeyOf" + columnType;
        String refArrayFromBToA = "RefArrayToAKeyOf" + columnType;
        schema.create(table(bTableName).add(column(refFromBToA).setType(REF).setRefTable(aTableName)).add(column(refArrayFromBToA).setType(REF_ARRAY).setRefTable(aTableName)));
    }
}
Also used : ColumnType(org.molgenis.emx2.ColumnType)

Example 14 with SchemaMetadata

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

the class SimpleTypeTestExample method createSimpleTypeTest.

public static void createSimpleTypeTest(SchemaMetadata schema) {
    TableMetadata typeTestTable = table(TYPE_TEST).add(column("id").setPkey());
    ColumnType[] columnTypes = new ColumnType[] { UUID, STRING, BOOL, INT, LONG, DECIMAL, TEXT, DATE, DATETIME };
    for (ColumnType columnType : columnTypes) {
        typeTestTable.add(column("Test " + columnType.toString().toLowerCase()).setType(columnType).setRequired(true));
        typeTestTable.add(column("Test " + columnType.toString().toLowerCase() + " nillable").setType(columnType));
    }
    schema.create(typeTestTable);
}
Also used : TableMetadata(org.molgenis.emx2.TableMetadata) ColumnType(org.molgenis.emx2.ColumnType)

Example 15 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)9 TableMetadata (org.molgenis.emx2.TableMetadata)7 SchemaMetadata (org.molgenis.emx2.SchemaMetadata)6 Test (org.junit.Test)4 File (java.io.File)2 OpenAPI (io.swagger.v3.oas.models.OpenAPI)1 InputStreamReader (java.io.InputStreamReader)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