Search in sources :

Example 1 with ForeignKeys

use of org.folio.dbschema.ForeignKeys in project raml-module-builder by folio-org.

the class SchemaMaker method tables.

/**
 * @return the tables of schema plus tables to delete (tables that exist in previousSchema but not in schema);
 *   add foreign keys to delete (those that exist in previousSchema but not in schema).
 *   Nothing to do for indexes because rmb_internal_index handles them.
 */
List<Table> tables() {
    if (previousSchema == null || previousSchema.getTables() == null) {
        return schema.getTables();
    }
    Map<String, Table> tableForName = new HashMap<>();
    schema.getTables().forEach(table -> tableForName.put(table.getTableName(), table));
    List<Table> list = new ArrayList<>(schema.getTables());
    previousSchema.getTables().forEach(oldTable -> {
        Table newTable = tableForName.get(oldTable.getTableName());
        if (newTable == null) {
            oldTable.setMode("delete");
            list.add(oldTable);
            return;
        }
        List<ForeignKeys> oldForeignKeys = oldTable.getForeignKeys();
        if (oldForeignKeys == null || oldForeignKeys.isEmpty()) {
            return;
        }
        List<ForeignKeys> newForeignKeys = newTable.getForeignKeys() == null ? Collections.emptyList() : newTable.getForeignKeys();
        List<ForeignKeys> allForeignKeys = new ArrayList<>(newForeignKeys);
        oldForeignKeys.forEach(oldForeignKey -> {
            if (newForeignKeys.stream().anyMatch(newForeignKey -> sameForeignKey(oldForeignKey, newForeignKey))) {
                // an entry for oldForeignKey exists in newForeignKeys, nothing to do
                return;
            }
            oldForeignKey.settOps(TableOperation.DELETE);
            allForeignKeys.add(oldForeignKey);
        });
        newTable.setForeignKeys(allForeignKeys);
    });
    return list;
}
Also used : Table(org.folio.dbschema.Table) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ForeignKeys(org.folio.dbschema.ForeignKeys)

Example 2 with ForeignKeys

use of org.folio.dbschema.ForeignKeys in project raml-module-builder by folio-org.

the class ForeignKeysTest method targetTableQuote.

@Test
void targetTableQuote() {
    assertThrows(IllegalArgumentException.class, () -> new ForeignKeys("otherId", "bee's"));
    assertThrows(IllegalArgumentException.class, () -> new ForeignKeys("otherId", "rock'n'roll", TableOperation.ADD));
}
Also used : ForeignKeys(org.folio.dbschema.ForeignKeys) Test(org.junit.jupiter.api.Test)

Example 3 with ForeignKeys

use of org.folio.dbschema.ForeignKeys in project raml-module-builder by folio-org.

the class SchemaMakerTest method sameForeignKeyNullsString.

@ParameterizedTest
@CsvSource({ "a,  , a,  , true", " , b,  , b, true", "a, b, a, b, true", "a, b, c, d, false", "a, a, b, b, false", "a, b, b, a, false", " ,  ,  ,  , false" })
public void sameForeignKeyNullsString(String fieldNameA, String fieldPathA, String fieldNameB, String fieldPathB, boolean expected) {
    ForeignKeys a = new ForeignKeys();
    if (fieldNameA != null) {
        a.setFieldName(fieldNameA);
    }
    if (fieldPathA != null) {
        a.setFieldPath(fieldPathA);
    }
    ForeignKeys b = new ForeignKeys();
    if (fieldNameB != null) {
        b.setFieldName(fieldNameB);
    }
    if (fieldPathB != null) {
        b.setFieldPath(fieldPathB);
    }
    assertThat(SchemaMaker.sameForeignKey(a, b), is(expected));
}
Also used : ForeignKeys(org.folio.dbschema.ForeignKeys) CsvSource(org.junit.jupiter.params.provider.CsvSource) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with ForeignKeys

use of org.folio.dbschema.ForeignKeys in project raml-module-builder by folio-org.

the class ForeignKeysTest method testAlias.

@Test
void testAlias() {
    ForeignKeys fk = new ForeignKeys();
    assertNull(fk.getTableAlias());
    assertNull(fk.getTargetTableAlias());
    fk.setTableAlias("a");
    assertEquals("a", fk.getTableAlias());
    fk.setTargetTable("b");
    assertEquals("b", fk.getTargetTable());
    assertEquals(null, fk.getTargetTableAlias());
    fk.setTargetTableAlias("c");
    assertEquals("c", fk.getTargetTableAlias());
    assertNull(fk.getTargetPath());
    fk.setTargetPath(Collections.singletonList(null));
    assertEquals(1, fk.getTargetPath().size());
    String result = fk.toString();
    assertTrue(result.contains("tableAlias=a"));
    assertTrue(result.contains("targetTable=b"));
    assertTrue(result.contains("targetTableAlias=c"));
}
Also used : ForeignKeys(org.folio.dbschema.ForeignKeys) Test(org.junit.jupiter.api.Test)

Example 5 with ForeignKeys

use of org.folio.dbschema.ForeignKeys in project raml-module-builder by folio-org.

the class ForeignKeysTest method constructors.

@Test
void constructors() {
    ForeignKeys foreignKeys2 = new ForeignKeys("other", "bee");
    ForeignKeys foreignKeys3 = new ForeignKeys("ref", "honey", TableOperation.DELETE);
    assertEquals("other", foreignKeys2.getFieldPath());
    assertEquals("ref", foreignKeys3.getFieldPath());
    assertEquals("bee", foreignKeys2.getTargetTable());
    assertEquals("honey", foreignKeys3.getTargetTable());
    assertEquals(TableOperation.ADD, foreignKeys2.gettOps());
    assertEquals(TableOperation.DELETE, foreignKeys3.gettOps());
}
Also used : ForeignKeys(org.folio.dbschema.ForeignKeys) Test(org.junit.jupiter.api.Test)

Aggregations

ForeignKeys (org.folio.dbschema.ForeignKeys)5 Test (org.junit.jupiter.api.Test)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Table (org.folio.dbschema.Table)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 CsvSource (org.junit.jupiter.params.provider.CsvSource)1