use of org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration in project cayenne by apache.
the class DefaultDbImportActionTest method testImportWithFieldChanged.
@Test
public void testImportWithFieldChanged() throws Exception {
DbImportConfiguration config = mock(DbImportConfiguration.class);
when(config.getTargetDataMap()).thenReturn(FILE_STUB);
when(config.createMergeDelegate()).thenReturn(new DefaultModelMergeDelegate());
when(config.getDbLoaderConfig()).thenReturn(new DbLoaderConfiguration());
when(config.createNameGenerator()).thenReturn(new DefaultObjectNameGenerator(NoStemStemmer.getInstance()));
when(config.createMeaningfulPKFilter()).thenReturn(NamePatternMatcher.EXCLUDE_ALL);
DbLoader dbLoader = new DbLoader(mockAdapter, mockConnection, config.getDbLoaderConfig(), mockDelegate, mockNameGenerator) {
@Override
public DataMap load() throws SQLException {
DataMap dataMap = new DataMap();
new DataMapBuilder(dataMap).with(dbEntity("ARTGROUP").attributes(dbAttr("GROUP_ID").typeInt().primaryKey(), dbAttr("NAME").typeVarchar(100).mandatory(), dbAttr("NAME_01").typeVarchar(100).mandatory(), dbAttr("PARENT_GROUP_ID").typeInt())).with(objEntity("org.apache.cayenne.testdo.testmap", "ArtGroup", "ARTGROUP").attributes(objAttr("name").type(String.class).dbPath("NAME")));
return dataMap;
}
};
final boolean[] haveWeTriedToSave = { false };
DefaultDbImportAction action = buildDbImportAction(new FileProjectSaver(Collections.<ProjectExtension>emptyList()) {
@Override
public void save(Project project) {
haveWeTriedToSave[0] = true;
// Validation phase
DataMap rootNode = (DataMap) project.getRootNode();
assertEquals(1, rootNode.getObjEntities().size());
assertEquals(1, rootNode.getDbEntityMap().size());
DbEntity entity = rootNode.getDbEntity("ARTGROUP");
assertNotNull(entity);
assertEquals(4, entity.getAttributes().size());
assertNotNull(entity.getAttribute("NAME_01"));
}
}, new DataMapLoader() {
@Override
public DataMap load(Resource configurationResource) throws CayenneRuntimeException {
return new DataMapBuilder().with(dbEntity("ARTGROUP").attributes(dbAttr("GROUP_ID").typeInt().primaryKey(), dbAttr("NAME").typeVarchar(100).mandatory(), dbAttr("PARENT_GROUP_ID").typeInt())).with(objEntity("org.apache.cayenne.testdo.testmap", "ArtGroup", "ARTGROUP").attributes(objAttr("name").type(String.class).dbPath("NAME"))).build();
}
}, dbLoader);
action.execute(config);
assertTrue("We should try to save.", haveWeTriedToSave[0]);
}
use of org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration in project cayenne by apache.
the class DefaultDbImportActionTest method testImportWithoutChanges.
@Test
public void testImportWithoutChanges() throws Exception {
DbImportConfiguration config = mock(DbImportConfiguration.class);
when(config.getTargetDataMap()).thenReturn(FILE_STUB);
when(config.createMergeDelegate()).thenReturn(new DefaultModelMergeDelegate());
when(config.getDbLoaderConfig()).thenReturn(new DbLoaderConfiguration());
DbLoader dbLoader = new DbLoader(mockAdapter, mockConnection, config.getDbLoaderConfig(), mockDelegate, mockNameGenerator) {
@Override
public DataMap load() throws SQLException {
DataMap dataMap = new DataMap();
new DataMapBuilder(dataMap).with(dbEntity("ARTGROUP").attributes(dbAttr("NAME").typeVarchar(100).mandatory()));
return dataMap;
}
};
FileProjectSaver projectSaver = mock(FileProjectSaver.class);
doNothing().when(projectSaver).save(any(Project.class));
DataMapLoader mapLoader = mock(DataMapLoader.class);
when(mapLoader.load(any(Resource.class))).thenReturn(new DataMapBuilder().with(dbEntity("ARTGROUP").attributes(dbAttr("NAME").typeVarchar(100).mandatory())).build());
DefaultDbImportAction action = buildDbImportAction(projectSaver, mapLoader, dbLoader);
action.execute(config);
// no changes - we still
verify(projectSaver, never()).save(any(Project.class));
verify(mapLoader, times(1)).load(any(Resource.class));
}
use of org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration 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.reverse.dbload.DbLoaderConfiguration in project cayenne by apache.
the class MergerOptions method prepareMigrator.
/**
* check database and create the {@link List} of {@link MergerToken}s
*/
protected void prepareMigrator() {
try {
adapter = connectionInfo.makeAdapter(getApplication().getClassLoadingService());
MergerTokenFactory mergerTokenFactory = mergerTokenFactoryProvider.get(adapter);
tokens.setMergerTokenFactory(mergerTokenFactory);
FiltersConfig filters = FiltersConfig.create(defaultCatalog, defaultSchema, TableFilter.everything(), PatternFilter.INCLUDE_NOTHING);
DataMapMerger merger = DataMapMerger.builder(mergerTokenFactory).filters(filters).build();
DbLoaderConfiguration config = new DbLoaderConfiguration();
config.setFiltersConfig(filters);
DataSource dataSource = connectionInfo.makeDataSource(getApplication().getClassLoadingService());
DataMap dbImport;
try (Connection conn = dataSource.getConnection()) {
dbImport = new DbLoader(adapter, conn, config, new LoggingDbLoaderDelegate(LoggerFactory.getLogger(DbLoader.class)), new DefaultObjectNameGenerator(NoStemStemmer.getInstance())).load();
} catch (SQLException e) {
throw new CayenneRuntimeException("Can't doLoad dataMap from db.", e);
}
tokens.setTokens(merger.createMergeTokens(dataMap, dbImport));
} catch (Exception ex) {
reportError("Error loading adapter", ex);
}
}
use of org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration in project cayenne by apache.
the class DefaultDbImportActionTest method testNewDataMapImport.
@Test
public void testNewDataMapImport() throws Exception {
DbImportConfiguration config = mock(DbImportConfiguration.class);
when(config.createMergeDelegate()).thenReturn(new DefaultModelMergeDelegate());
when(config.getDbLoaderConfig()).thenReturn(new DbLoaderConfiguration());
when(config.getTargetDataMap()).thenReturn(new File("xyz.map.xml"));
when(config.createNameGenerator()).thenReturn(new DefaultObjectNameGenerator(NoStemStemmer.getInstance()));
when(config.createMeaningfulPKFilter()).thenReturn(NamePatternMatcher.EXCLUDE_ALL);
DbLoader dbLoader = new DbLoader(mockAdapter, mockConnection, config.getDbLoaderConfig(), mockDelegate, mockNameGenerator) {
@Override
public DataMap load() throws SQLException {
DataMap map = new DataMap();
new DataMapBuilder(map).withDbEntities(2).build();
return map;
}
};
final boolean[] haveWeTriedToSave = { false };
DefaultDbImportAction action = buildDbImportAction(new FileProjectSaver(Collections.emptyList()) {
@Override
public void save(Project project) {
haveWeTriedToSave[0] = true;
// Validation phase
assertTrue(project.getRootNode() instanceof DataMap);
}
}, null, dbLoader);
action.execute(config);
assertTrue("We should try to save.", haveWeTriedToSave[0]);
}
Aggregations