Search in sources :

Example 1 with MergerTokenFactory

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);
    }
}
Also used : DbAdapter(org.apache.cayenne.dba.DbAdapter) Connection(java.sql.Connection) MergerToken(org.apache.cayenne.dbsync.merge.token.MergerToken) DataNodeDescriptor(org.apache.cayenne.configuration.DataNodeDescriptor) MergerTokenFactory(org.apache.cayenne.dbsync.merge.factory.MergerTokenFactory) DataSource(javax.sql.DataSource) DataMap(org.apache.cayenne.map.DataMap) DbLoaderConfiguration(org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration)

Example 2 with MergerTokenFactory

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);
    }
}
Also used : LoggingDbLoaderDelegate(org.apache.cayenne.dbsync.reverse.dbload.LoggingDbLoaderDelegate) SQLException(java.sql.SQLException) Connection(java.sql.Connection) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) MergerTokenFactory(org.apache.cayenne.dbsync.merge.factory.MergerTokenFactory) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) SQLException(java.sql.SQLException) IOException(java.io.IOException) DbLoaderConfiguration(org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration) DataSource(javax.sql.DataSource) DataMap(org.apache.cayenne.map.DataMap) DefaultObjectNameGenerator(org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator) DataMapMerger(org.apache.cayenne.dbsync.merge.DataMapMerger) FiltersConfig(org.apache.cayenne.dbsync.reverse.filters.FiltersConfig) DbLoader(org.apache.cayenne.dbsync.reverse.dbload.DbLoader)

Aggregations

Connection (java.sql.Connection)2 DataSource (javax.sql.DataSource)2 MergerTokenFactory (org.apache.cayenne.dbsync.merge.factory.MergerTokenFactory)2 DbLoaderConfiguration (org.apache.cayenne.dbsync.reverse.dbload.DbLoaderConfiguration)2 DataMap (org.apache.cayenne.map.DataMap)2 IOException (java.io.IOException)1 SQLException (java.sql.SQLException)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 DataNodeDescriptor (org.apache.cayenne.configuration.DataNodeDescriptor)1 DbAdapter (org.apache.cayenne.dba.DbAdapter)1 DataMapMerger (org.apache.cayenne.dbsync.merge.DataMapMerger)1 MergerToken (org.apache.cayenne.dbsync.merge.token.MergerToken)1 DefaultObjectNameGenerator (org.apache.cayenne.dbsync.naming.DefaultObjectNameGenerator)1 DbLoader (org.apache.cayenne.dbsync.reverse.dbload.DbLoader)1 LoggingDbLoaderDelegate (org.apache.cayenne.dbsync.reverse.dbload.LoggingDbLoaderDelegate)1 FiltersConfig (org.apache.cayenne.dbsync.reverse.filters.FiltersConfig)1