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;
}
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));
}
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));
}
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"));
}
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());
}
Aggregations