use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class DbAttributeMerger method createTokensForMissingImported.
/**
* Add column to db
* @param original attribute found in model but missing in db
*/
@Override
Collection<MergerToken> createTokensForMissingImported(DbAttribute original) {
DbEntity originalDbEntity = original.getEntity();
List<MergerToken> tokens = new LinkedList<>();
tokens.add(getTokenFactory().createAddColumnToDb(originalDbEntity, original));
// Create not null check
if (original.isMandatory()) {
if (valueForNull.hasValueFor(originalDbEntity, original)) {
tokens.add(getTokenFactory().createSetValueForNullToDb(originalDbEntity, original, valueForNull));
}
tokens.add(getTokenFactory().createSetNotNullToDb(originalDbEntity, original));
}
if (original.isPrimaryKey() && originalDbEntity instanceof DetectedDbEntity && "VIEW".equals(((DetectedDbEntity) originalDbEntity).getType())) {
// Views doesn't has PKs in a database, but if the user selects some PKs in a model, we put these keys.
return null;
}
return tokens;
}
use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class DbRelationshipMerger method createTokensForMissingImported.
/**
* @param original DbRelationship that is in model but not in db
* @return generated tokens
*/
@Override
Collection<MergerToken> createTokensForMissingImported(DbRelationship original) {
if (skipRelationshipsTokens) {
return null;
}
DbEntity originalDbEntity = getOriginalSourceDbEntity(original);
MergerToken token = getTokenFactory().createAddRelationshipToDb(originalDbEntity, original);
return Collections.singleton(token);
}
use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class DbRelationshipMerger method createTokensForMissingOriginal.
/**
* @param imported DbRelationship that is in db but not in model
* @return generated tokens
*/
@Override
Collection<MergerToken> createTokensForMissingOriginal(DbRelationship imported) {
DbEntity originalDbEntity = getOriginalSourceDbEntity(imported);
DbEntity targetEntity = getOriginalTargetDbEntity(imported);
if (targetEntity != null) {
imported.setTargetEntityName(targetEntity);
}
imported.setSourceEntity(originalDbEntity);
// manipulate the joins to match the DbAttributes in the model
for (DbJoin join : imported.getJoins()) {
DbAttribute sourceAttr = findDbAttribute(originalDbEntity, join.getSourceName());
if (sourceAttr != null) {
join.setSourceName(sourceAttr.getName());
}
DbAttribute targetAttr = findDbAttribute(targetEntity, join.getTargetName());
if (targetAttr != null) {
join.setTargetName(targetAttr.getName());
}
}
// Add all relationships. Tokens will decide whether or not to execute
MergerToken token = getTokenFactory().createDropRelationshipToDb(originalDbEntity, imported);
return Collections.singleton(token);
}
use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class DefaultDbImportAction method execute.
@Override
public void execute(DbImportConfiguration config) throws Exception {
if (logger.isDebugEnabled()) {
logger.debug("DB connection: " + config.getDataSourceInfo());
logger.debug(String.valueOf(config));
}
boolean hasChanges = false;
DataNodeDescriptor dataNodeDescriptor = config.createDataNodeDescriptor();
DataSource dataSource = dataSourceFactory.getDataSource(dataNodeDescriptor);
DbAdapter adapter = adapterFactory.createAdapter(dataNodeDescriptor, dataSource);
DataMap sourceDataMap;
try (Connection connection = dataSource.getConnection()) {
sourceDataMap = load(config, adapter, connection);
}
DataMap targetDataMap = existingTargetMap(config);
if (targetDataMap == null) {
String path = config.getTargetDataMap() == null ? "null" : config.getTargetDataMap().getAbsolutePath() + "'";
logger.info("");
logger.info("Map file does not exist. Loaded db model will be saved into '" + path);
hasChanges = true;
targetDataMap = newTargetDataMap(config);
}
// transform source DataMap before merging
transformSourceBeforeMerge(sourceDataMap, targetDataMap, config);
MergerTokenFactory mergerTokenFactory = mergerTokenFactoryProvider.get(adapter);
DbLoaderConfiguration loaderConfig = config.getDbLoaderConfig();
Collection<MergerToken> tokens = DataMapMerger.builder(mergerTokenFactory).filters(loaderConfig.getFiltersConfig()).skipPKTokens(loaderConfig.isSkipPrimaryKeyLoading()).skipRelationshipsTokens(loaderConfig.isSkipRelationshipsLoading()).build().createMergeTokens(targetDataMap, sourceDataMap);
tokens = log(sort(reverse(mergerTokenFactory, tokens)));
hasChanges |= syncDataMapProperties(targetDataMap, config);
hasChanges |= applyTokens(targetDataMap, tokens, config);
hasChanges |= syncProcedures(targetDataMap, sourceDataMap, loaderConfig.getFiltersConfig());
if (hasChanges) {
saveLoaded(targetDataMap, config);
}
}
use of org.apache.cayenne.dbsync.merge.token.MergerToken in project cayenne by apache.
the class DataMapMergerTest method testAddRelationship1.
@Test
public void testAddRelationship1() 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().primaryKey(), dbAttr("attr03").typeInt().primaryKey())).join("rel", "table1.attr01", "table2.attr01").join("rel1", "table1.attr01", "table2.attr03").build();
DataMap db = dataMap().with(dbEntity("table1").attributes(dbAttr("attr01").typeInt(), dbAttr("attr02").typeInt()), dbEntity("table2").attributes(dbAttr("attr01").typeInt().primaryKey(), dbAttr("attr02").typeInt().primaryKey(), dbAttr("attr03").typeInt().primaryKey())).join("rel", "table1.attr01", "table2.attr02").join("rel1", "table1.attr01", "table2.attr03").build();
List<MergerToken> tokens = dbMerger().createMergeTokens(existing, db);
assertEquals(2, tokens.size());
DbEntity entity = existing.getDbEntity("table1");
assertEquals(factory().createDropRelationshipToDb(entity, entity.getRelationship("rel")).getTokenValue(), tokens.get(0).getTokenValue());
entity = db.getDbEntity("table1");
assertEquals(factory().createAddRelationshipToDb(entity, entity.getRelationship("rel")).getTokenValue(), tokens.get(0).getTokenValue());
}
Aggregations