use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class MergerOptions method generateSchemaAction.
/**
* Performs configured schema operations via DbGenerator.
*/
public void generateSchemaAction() {
refreshGeneratorAction();
// sanity check...
List<MergerToken> tokensToMigrate = tokens.getSelectedTokens();
if (tokensToMigrate.isEmpty()) {
JOptionPane.showMessageDialog(getView(), "Nothing to migrate.");
return;
}
DataSource dataSource;
try {
dataSource = connectionInfo.makeDataSource(getApplication().getClassLoadingService());
} catch (SQLException ex) {
reportError("Migration Error", ex);
return;
}
final Collection<ObjEntity> loadedObjEntities = new LinkedList<>();
MergerContext mergerContext = MergerContext.builder(dataMap).syntheticDataNode(dataSource, adapter).delegate(createDelegate(loadedObjEntities)).build();
boolean modelChanged = applyTokens(tokensToMigrate, mergerContext);
DefaultDbImportAction.flattenManyToManyRelationships(dataMap, loadedObjEntities, mergerContext.getNameGenerator());
notifyProjectModified(modelChanged);
reportFailures(mergerContext);
if (tokens.isReverse()) {
getApplication().getUndoManager().discardAllEdits();
}
}
use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class MergerTokenSelectorController method reverseAllAction.
public void reverseAllAction() {
isReverse = !isReverse;
for (int i = 0; i < selectableTokensList.size(); i++) {
MergerToken token = selectableTokensList.get(i);
MergerToken reverse = token.createReverse(mergerTokenFactory);
selectableTokensList.set(i, reverse);
if (excludedTokens.remove(token)) {
excludedTokens.add(reverse);
}
}
Collections.sort(selectableTokensList);
AbstractTableModel model = (AbstractTableModel) view.getTokens().getModel();
model.fireTableDataChanged();
}
use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class SetGeneratedFlagToDbIT method setGeneratedFlag.
@Test
public void setGeneratedFlag() throws Exception {
DbEntity dbEntity = createTestTable(false);
assertNotNull(dbEntity);
DbAttribute attribute = dbEntity.getAttribute("ID");
assertNotNull(attribute);
assertFalse(attribute.isGenerated());
attribute.setGenerated(true);
List<MergerToken> tokens = createMergeTokens();
if (!dbAdapter.supportsGeneratedKeys()) {
assertEquals(0, tokens.size());
return;
}
assertEquals(1, tokens.size());
MergerToken token = tokens.get(0);
assertTrue(token instanceof SetGeneratedFlagToDb);
try {
execute(token);
if (!dbAdapter.supportsGeneratedKeysAdd()) {
fail("SetGeneratedFlagToDb should fail on current DB");
}
} catch (UnsupportedOperationException ignored) {
return;
}
assertTokensAndExecute(0, 0);
}
use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class AddColumnToModelIT method testAddColumn.
@Test
public void testAddColumn() 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);
// remove name column
objEntity.removeAttribute(oatr1.getName());
dbEntity.removeAttribute(column2.getName());
assertNull(objEntity.getAttribute(oatr1.getName()));
assertEquals(0, objEntity.getAttributes().size());
assertNull(dbEntity.getAttribute(column2.getName()));
List<MergerToken> tokens = createMergeTokens();
assertEquals(1, tokens.size());
MergerToken token = tokens.get(0);
if (token.getDirection().isToDb()) {
token = token.createReverse(mergerFactory());
}
assertTrue(token instanceof AddColumnToModel);
execute(token);
assertEquals(1, objEntity.getAttributes().size());
assertEquals("java.lang.String", objEntity.getAttributes().iterator().next().getType());
// 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(1, 0);
assertTokensAndExecute(0, 0);
}
use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class DropRelationshipToModelIT method testForeignKey.
@Test
public void testForeignKey() throws Exception {
dropTableIfPresent("NEW_TABLE");
dropTableIfPresent("NEW_TABLE2");
assertTokensAndExecute(0, 0);
DbEntity dbEntity1 = new DbEntity("NEW_TABLE");
DbAttribute e1col1 = new DbAttribute("ID", Types.INTEGER, dbEntity1);
e1col1.setMandatory(true);
e1col1.setPrimaryKey(true);
dbEntity1.addAttribute(e1col1);
DbAttribute e1col2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity1);
e1col2.setMaxLength(10);
e1col2.setMandatory(false);
dbEntity1.addAttribute(e1col2);
map.addDbEntity(dbEntity1);
DbEntity dbEntity2 = new DbEntity("NEW_TABLE2");
DbAttribute e2col1 = new DbAttribute("ID", Types.INTEGER, dbEntity2);
e2col1.setMandatory(true);
e2col1.setPrimaryKey(true);
dbEntity2.addAttribute(e2col1);
DbAttribute e2col2 = new DbAttribute("FK", Types.INTEGER, dbEntity2);
dbEntity2.addAttribute(e2col2);
DbAttribute e2col3 = new DbAttribute("NAME", Types.VARCHAR, dbEntity2);
e2col3.setMaxLength(10);
dbEntity2.addAttribute(e2col3);
map.addDbEntity(dbEntity2);
// create db relationships
DbRelationship rel1To2 = new DbRelationship("rel1To2");
rel1To2.setSourceEntity(dbEntity1);
rel1To2.setTargetEntityName(dbEntity2);
rel1To2.setToMany(true);
rel1To2.addJoin(new DbJoin(rel1To2, e1col1.getName(), e2col2.getName()));
dbEntity1.addRelationship(rel1To2);
DbRelationship rel2To1 = new DbRelationship("rel2To1");
rel2To1.setSourceEntity(dbEntity2);
rel2To1.setTargetEntityName(dbEntity1);
rel2To1.setToMany(false);
rel2To1.addJoin(new DbJoin(rel2To1, e2col2.getName(), e1col1.getName()));
dbEntity2.addRelationship(rel2To1);
assertSame(rel1To2, rel2To1.getReverseRelationship());
assertSame(rel2To1, rel1To2.getReverseRelationship());
assertTokensAndExecute(3, 0);
assertTokensAndExecute(0, 0);
// create ObjEntities
ObjEntity objEntity1 = new ObjEntity("NewTable");
objEntity1.setDbEntity(dbEntity1);
ObjAttribute oatr1 = new ObjAttribute("name");
oatr1.setDbAttributePath(e1col2.getName());
oatr1.setType("java.lang.String");
objEntity1.addAttribute(oatr1);
map.addObjEntity(objEntity1);
ObjEntity objEntity2 = new ObjEntity("NewTable2");
objEntity2.setDbEntity(dbEntity2);
ObjAttribute o2a1 = new ObjAttribute("name");
o2a1.setDbAttributePath(e2col3.getName());
o2a1.setType("java.lang.String");
objEntity2.addAttribute(o2a1);
map.addObjEntity(objEntity2);
// create ObjRelationships
assertEquals(0, objEntity1.getRelationships().size());
assertEquals(0, objEntity2.getRelationships().size());
ObjRelationship objRel1To2 = new ObjRelationship("objRel1To2");
objRel1To2.addDbRelationship(rel1To2);
objRel1To2.setSourceEntity(objEntity1);
objRel1To2.setTargetEntityName(objEntity2);
objEntity1.addRelationship(objRel1To2);
ObjRelationship objRel2To1 = new ObjRelationship("objRel2To1");
objRel2To1.addDbRelationship(rel2To1);
objRel2To1.setSourceEntity(objEntity2);
objRel2To1.setTargetEntityName(objEntity1);
objEntity2.addRelationship(objRel2To1);
assertEquals(1, objEntity1.getRelationships().size());
assertEquals(1, objEntity2.getRelationships().size());
assertSame(objRel1To2, objRel2To1.getReverseRelationship());
assertSame(objRel2To1, objRel1To2.getReverseRelationship());
// remove relationship and fk from model, merge to db and read to model
dbEntity2.removeRelationship(rel2To1.getName());
dbEntity1.removeRelationship(rel1To2.getName());
dbEntity2.removeAttribute(e2col2.getName());
List<MergerToken> tokens = createMergeTokens();
/**
* Drop Relationship NEW_TABLE2->NEW_TABLE To DB
* Drop Column NEW_TABLE2.FK To DB
*/
assertTokens(tokens, 2, 0);
for (MergerToken token : tokens) {
if (token.getDirection().isToDb()) {
execute(token);
}
}
assertTokensAndExecute(0, 0);
dbEntity2.addRelationship(rel2To1);
dbEntity1.addRelationship(rel1To2);
dbEntity2.addAttribute(e2col2);
// try do use the merger to remove the relationship in the model
tokens = createMergeTokens();
assertTokens(tokens, 2, 0);
// TODO: reversing the following two tokens should also reverse the
// order
MergerToken token0 = tokens.get(0).createReverse(mergerFactory());
MergerToken token1 = tokens.get(1).createReverse(mergerFactory());
if (!(token0 instanceof DropRelationshipToModel && token1 instanceof DropColumnToModel || token1 instanceof DropRelationshipToModel && token0 instanceof DropColumnToModel)) {
fail();
}
execute(token0);
execute(token1);
// check after merging
assertNull(dbEntity2.getAttribute(e2col2.getName()));
assertEquals(0, dbEntity1.getRelationships().size());
assertEquals(0, dbEntity2.getRelationships().size());
assertEquals(0, objEntity1.getRelationships().size());
assertEquals(0, objEntity2.getRelationships().size());
// clear up
dbEntity1.removeRelationship(rel1To2.getName());
dbEntity2.removeRelationship(rel2To1.getName());
map.removeObjEntity(objEntity1.getName(), true);
map.removeDbEntity(dbEntity1.getName(), true);
map.removeObjEntity(objEntity2.getName(), true);
map.removeDbEntity(dbEntity2.getName(), true);
resolver.refreshMappingCache();
assertNull(map.getObjEntity(objEntity1.getName()));
assertNull(map.getDbEntity(dbEntity1.getName()));
assertNull(map.getObjEntity(objEntity2.getName()));
assertNull(map.getDbEntity(dbEntity2.getName()));
assertFalse(map.getDbEntities().contains(dbEntity1));
assertFalse(map.getDbEntities().contains(dbEntity2));
assertTokensAndExecute(2, 0);
assertTokensAndExecute(0, 0);
}
Aggregations