Search in sources :

Example 11 with MergerToken

use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.

the class DataMapMergerTest method testAddColumn.

@Test
public void testAddColumn() throws Exception {
    DataMap existing = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt())).build();
    DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt())).build();
    List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
    assertEquals(1, tokens.size());
    DbEntity entity = existing.getDbEntity("table1");
    assertEquals(factory().createAddColumnToDb(entity, entity.getAttribute("attr02")).getTokenValue(), tokens.get(0).getTokenValue());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 12 with MergerToken

use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.

the class DataMapMergerTest method testAddPrimaryKey.

@Test
public void testAddPrimaryKey() throws Exception {
    DataMap existing = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt().primaryKey())).build();
    DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt())).build();
    List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
    assertEquals(1, tokens.size());
}
Also used : MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 13 with MergerToken

use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.

the class DataMapMergerTest method testChangeColumnLength.

@Test
public void testChangeColumnLength() throws Exception {
    DataMap existing = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeVarchar(60))).build();
    DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeVarchar(30))).build();
    List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
    assertEquals(1, tokens.size());
    DbEntity entity = existing.getDbEntity("table1");
    DbEntity entityDb = db.getDbEntity("table1");
    assertTrue(tokens.get(0) instanceof SetColumnTypeToDb);
    assertEquals(factory().createSetColumnTypeToDb(entity, entityDb.getAttribute("attr01"), entity.getAttribute("attr01")).getTokenValue(), tokens.get(0).getTokenValue());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) SetColumnTypeToDb(org.apache.cayenne.dbsync.merge.token.db.SetColumnTypeToDb) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 14 with MergerToken

use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.

the class DataMapMergerTest method testRemoveRelationship.

@Test
public void testRemoveRelationship() throws Exception {
    DataMap existing = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt()), dbEntity("table2").attributes(dbAttr("attr01").typeInt().primaryKey(), dbAttr("attr02").typeInt())).build();
    DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt()), dbEntity("table2").attributes(dbAttr("attr01").typeInt().primaryKey(), dbAttr("attr02").typeInt())).join("rel", "table1.attr01", "table2.attr01").build();
    List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
    assertEquals(1, tokens.size());
    DbEntity entity = db.getDbEntity("table1");
    assertEquals(factory().createDropRelationshipToDb(entity, entity.getRelationship("rel")).getTokenValue(), tokens.get(0).getTokenValue());
}
Also used : DbEntity(org.apache.cayenne.map.DbEntity) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DataMap(org.apache.cayenne.map.DataMap) Test(org.junit.Test)

Example 15 with MergerToken

use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.

the class MergerOptions method applyTokens.

private boolean applyTokens(List<MergerToken> tokensToMigrate, MergerContext mergerContext) {
    boolean modelChanged = false;
    try {
        for (MergerToken tok : tokensToMigrate) {
            int numOfFailuresBefore = getFailuresCount(mergerContext);
            tok.execute(mergerContext);
            if (!modelChanged && tok.getDirection().equals(MergeDirection.TO_MODEL)) {
                modelChanged = true;
            }
            if (numOfFailuresBefore == getFailuresCount(mergerContext)) {
                // looks like the token executed without failures
                tokens.removeToken(tok);
            }
        }
    } catch (Throwable th) {
        reportError("Migration Error", th);
    }
    return modelChanged;
}
Also used : MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken)

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