Search in sources :

Example 21 with MergerToken

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);
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) ObjAttribute(org.apache.cayenne.map.ObjAttribute) DbAttribute(org.apache.cayenne.map.DbAttribute) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) Test(org.junit.Test)

Example 22 with MergerToken

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);
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) DbAttribute(org.apache.cayenne.map.DbAttribute) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) Test(org.junit.Test)

Example 23 with MergerToken

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;
}
Also used : MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken)

Example 24 with MergerToken

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;
}
Also used : ObjectNameGenerator(org.apache.cayenne.dbsync.naming.ObjectNameGenerator) ModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.ModelMergeDelegate) ProxyModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.ProxyModelMergeDelegate) ValidationResult(org.apache.cayenne.validation.ValidationResult) MergerContext(org.apache.cayenne.dbsync.merge.context.MergerContext) LinkedList(java.util.LinkedList) SimpleValidationFailure(org.apache.cayenne.validation.SimpleValidationFailure) ValidationFailure(org.apache.cayenne.validation.ValidationFailure) ObjEntity(org.apache.cayenne.map.ObjEntity) SimpleValidationFailure(org.apache.cayenne.validation.SimpleValidationFailure) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) ProxyModelMergeDelegate(org.apache.cayenne.dbsync.reverse.dbload.ProxyModelMergeDelegate)

Example 25 with MergerToken

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());
}
Also used : DbEntityBuilder(org.apache.cayenne.dbsync.merge.builders.DbEntityBuilder) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Aggregations

MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)41 Test (org.junit.Test)25 DbEntity (org.apache.cayenne.map.DbEntity)20 DataMap (org.apache.cayenne.map.DataMap)17 DbAttribute (org.apache.cayenne.map.DbAttribute)10 ObjEntity (org.apache.cayenne.map.ObjEntity)8 LinkedList (java.util.LinkedList)5 ObjAttribute (org.apache.cayenne.map.ObjAttribute)5 SetColumnTypeToDb (org.apache.cayenne.dbsync.merge.token.db.SetColumnTypeToDb)4 DbJoin (org.apache.cayenne.map.DbJoin)3 DbRelationship (org.apache.cayenne.map.DbRelationship)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 DataSource (javax.sql.DataSource)2 MergerContext (org.apache.cayenne.dbsync.merge.context.MergerContext)2 DbLoaderConfiguration (org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration)2 ObjRelationship (org.apache.cayenne.map.ObjRelationship)2 ArrayList (java.util.ArrayList)1 AbstractTableModel (javax.swing.table.AbstractTableModel)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1