use of org.apache.cayenne.dbsync.merge.factory.MergerTokenFactory 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.factory.MergerTokenFactory 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);
}
}
Aggregations