use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class DropTableToModelIT method testDropTable.
@Test
public void testDropTable() throws Exception {
dropTableIfPresent("NEW_TABLE");
assertTokensAndExecute(0, 0);
DbEntity dbEntity = new DbEntity("NEW_TABLE");
DbAttribute column1 = new DbAttribute("ID", Types.INTEGER, dbEntity);
column1.setMandatory(true);
column1.setPrimaryKey(true);
dbEntity.addAttribute(column1);
DbAttribute column2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity);
column2.setMaxLength(10);
column2.setMandatory(false);
dbEntity.addAttribute(column2);
map.addDbEntity(dbEntity);
assertTokensAndExecute(1, 0);
assertTokensAndExecute(0, 0);
ObjEntity objEntity = new ObjEntity("NewTable");
objEntity.setDbEntity(dbEntity);
ObjAttribute oatr1 = new ObjAttribute("name");
oatr1.setDbAttributePath(column2.getName());
oatr1.setType("java.lang.String");
objEntity.addAttribute(oatr1);
map.addObjEntity(objEntity);
// force drop table in db
MergerToken token = mergerFactory().createDropTableToDb(dbEntity);
execute(token);
List<MergerToken> tokens = createMergeTokens();
assertEquals(1, tokens.size());
token = tokens.get(0);
if (token.getDirection().isToDb()) {
token = token.createReverse(mergerFactory());
}
assertTrue(token instanceof DropTableToModel);
execute(token);
resolver.refreshMappingCache();
assertNull(map.getDbEntity(dbEntity.getName()));
assertNull(map.getObjEntity(objEntity.getName()));
// clear up
map.removeObjEntity(objEntity.getName(), true);
map.removeDbEntity(dbEntity.getName(), true);
resolver.refreshMappingCache();
assertNull(map.getObjEntity(objEntity.getName()));
assertNull(map.getDbEntity(dbEntity.getName()));
assertFalse(map.getDbEntities().contains(dbEntity));
assertTokensAndExecute(0, 0);
}
use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class SetGeneratedFlagToModelIT method test.
@Test
public void test() throws Exception {
if (!accessStackAdapter.supportsGeneratedKeysAdd()) {
// nothing to do here
return;
}
DbEntity dbEntity = map.getDbEntity("PAINTING");
assertNotNull(dbEntity);
DbAttribute attribute = dbEntity.getAttribute("PAINTING_ID");
assertNotNull(attribute);
assertFalse(attribute.isGenerated());
attribute.setGenerated(true);
List<MergerToken> tokens = createMergeTokens();
assertEquals(1, tokens.size());
MergerToken token = tokens.get(0);
if (token.getDirection().isToDb()) {
token = token.createReverse(mergerFactory());
}
assertTrue(token instanceof SetGeneratedFlagToModel);
execute(token);
assertFalse(attribute.isGenerated());
assertTokensAndExecute(0, 0);
}
use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class DefaultDbImportAction method log.
private Collection<MergerToken> log(List<MergerToken> tokens) {
logger.info("");
if (tokens.isEmpty()) {
logger.info("Detected changes: No changes to import.");
return tokens;
}
logger.info("Detected changes: ");
for (MergerToken token : tokens) {
logger.info(String.format(" %-20s %s", token.getTokenName(), token.getTokenValue()));
}
logger.info("");
return tokens;
}
use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class DefaultDbImportAction method applyTokens.
private boolean applyTokens(DataMap targetDataMap, Collection<MergerToken> tokens, DbImportConfiguration config) {
if (tokens.isEmpty()) {
logger.info("");
logger.info("Detected changes: No changes to import.");
return false;
}
final Collection<ObjEntity> loadedObjEntities = new LinkedList<>();
ModelMergeDelegate mergeDelegate = new ProxyModelMergeDelegate(config.createMergeDelegate()) {
@Override
public void objEntityAdded(ObjEntity ent) {
loadedObjEntities.add(ent);
super.objEntityAdded(ent);
}
};
ObjectNameGenerator nameGenerator = config.createNameGenerator();
MergerContext mergerContext = MergerContext.builder(targetDataMap).delegate(mergeDelegate).nameGenerator(nameGenerator).usingPrimitives(config.isUsePrimitives()).usingJava7Types(config.isUseJava7Types()).meaningfulPKFilter(config.createMeaningfulPKFilter()).build();
for (MergerToken token : tokens) {
try {
token.execute(mergerContext);
} catch (Throwable th) {
String message = "Migration Error. Can't apply changes from token: " + token.getTokenName() + " (" + token.getTokenValue() + ")";
logger.error(message, th);
mergerContext.getValidationResult().addFailure(new SimpleValidationFailure(th, message));
}
}
ValidationResult failures = mergerContext.getValidationResult();
if (failures.hasFailures()) {
logger.info("Migration Complete.");
logger.warn("Migration finished. The following problem(s) were encountered and ignored.");
for (ValidationFailure failure : failures.getFailures()) {
logger.warn(failure.toString());
}
} else {
logger.info("Migration Complete Successfully.");
}
flattenManyToManyRelationships(targetDataMap, loadedObjEntities, nameGenerator);
relationshipsSanity(targetDataMap);
return true;
}
use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class DataMapMergerTest method testAddTable.
@Test
public void testAddTable() throws Exception {
DbEntityBuilder dbEntity = dbEntity("table1").attributes(dbAttr("attr01").typeInt());
DataMap existing = dataMap().with(dbEntity).build();
DataMap db = dataMap().build();
List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
assertEquals(1, tokens.size());
assertEquals(factory().createCreateTableToDb(dbEntity.build()).getTokenValue(), tokens.get(0).getTokenValue());
}
Aggregations