Search in sources :

Example 1 with CdmUpdater

use of eu.etaxonomy.cdm.database.update.CdmUpdater in project cdmlib by cybertaxonomy.

the class TestModelUpdate method updateH2.

private void updateH2(String pathToProject) {
    String pathInProject = "src\\test\\resources\\h2";
    String path = pathToProject + pathInProject;
    ICdmDataSource dataSource = CdmDataSource.NewH2EmbeddedInstance("cdmTest", "sa", "", path);
    try {
        CdmUpdater updater = new CdmUpdater();
        SchemaUpdateResult result = updater.updateToCurrentVersion(dataSource, DefaultProgressMonitor.NewInstance());
        System.out.println(result.createReport());
    } catch (Exception e) {
        e.printStackTrace();
    }
    // CdmPersistentDataSource.save(dataSource.getName(), dataSource);
    CdmApplicationController appCtr;
    appCtr = CdmApplicationController.NewInstance(dataSource, DbSchemaValidation.VALIDATE);
    appCtr.close();
    System.out.println("\nEnd Datasource");
}
Also used : ICdmDataSource(eu.etaxonomy.cdm.database.ICdmDataSource) CdmApplicationController(eu.etaxonomy.cdm.api.application.CdmApplicationController) SchemaUpdateResult(eu.etaxonomy.cdm.database.update.SchemaUpdateResult) CdmUpdater(eu.etaxonomy.cdm.database.update.CdmUpdater)

Example 2 with CdmUpdater

use of eu.etaxonomy.cdm.database.update.CdmUpdater in project cdmlib by cybertaxonomy.

the class TestModelUpdate method updateEdaphobasePostgres.

// enable only if needed
@SuppressWarnings("unused")
private void updateEdaphobasePostgres() {
    String serverSql = "130.133.70.26";
    String database = "cdm_edaphobase";
    int port = 5433;
    String username = "edaphobase";
    String password = AccountStore.readOrStorePassword(database, serverSql, username, null);
    ICdmDataSource dataSource = CdmDataSource.NewPostgreSQLInstance(serverSql, database, port, username, password);
    try {
        CdmUpdater updater = new CdmUpdater();
        SchemaUpdateResult result = updater.updateToCurrentVersion(dataSource, DefaultProgressMonitor.NewInstance());
        System.out.println(result.createReport());
    } catch (Exception e) {
        e.printStackTrace();
    }
    // CdmPersistentDataSource.save(dataSource.getName(), dataSource);
    CdmApplicationController appCtr;
    appCtr = CdmApplicationController.NewInstance(dataSource, DbSchemaValidation.VALIDATE);
    appCtr.close();
    System.out.println("\nEnd Datasource");
}
Also used : ICdmDataSource(eu.etaxonomy.cdm.database.ICdmDataSource) CdmApplicationController(eu.etaxonomy.cdm.api.application.CdmApplicationController) SchemaUpdateResult(eu.etaxonomy.cdm.database.update.SchemaUpdateResult) CdmUpdater(eu.etaxonomy.cdm.database.update.CdmUpdater)

Example 3 with CdmUpdater

use of eu.etaxonomy.cdm.database.update.CdmUpdater in project cdmlib by cybertaxonomy.

the class TestModelUpdate method testSelectedDb.

private void testSelectedDb() {
    DbSchemaValidation schema = DbSchemaValidation.VALIDATE;
    DatabaseTypeEnum dbType = DatabaseTypeEnum.MySQL;
    String database = (schema == DbSchemaValidation.VALIDATE ? "cdm527" : "cdm529");
    // database = "cdm_test1";
    CdmDataSource dataSource = getDatasource(dbType, database);
    try {
        // int n = dataSource.executeUpdate("UPDATE CdmMetaData SET value = '3.1.0.0.201607300000' WHERE propertyname = 0 ");
        CdmUpdater updater = new CdmUpdater();
        if (schema == DbSchemaValidation.VALIDATE) {
            SchemaUpdateResult result = updater.updateToCurrentVersion(dataSource, DefaultProgressMonitor.NewInstance());
            String report = result.createReport().toString();
            System.out.println(report);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        CdmApplicationController appCtr = CdmApplicationController.NewInstance(dataSource, schema);
        if (schema == DbSchemaValidation.CREATE) {
            System.out.println("fillData");
            appCtr.getCommonService().createFullSampleData();
            appCtr.getNameService().list(null, null, null, null, null);
            TransactionStatus tx = appCtr.startTransaction(false);
            TemporalData td = (TemporalData) appCtr.getDescriptionElementService().find(UUID.fromString("9a1c91c0-fc58-4310-94cb-8c26115985d3"));
            td.getFeature().setSupportsCategoricalData(true);
            appCtr.getTermService().saveOrUpdate(td.getFeature());
            System.out.println(td.getPeriod());
            appCtr.commitTransaction(tx);
        }
        appCtr.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println("Ready");
}
Also used : DbSchemaValidation(eu.etaxonomy.cdm.database.DbSchemaValidation) CdmApplicationController(eu.etaxonomy.cdm.api.application.CdmApplicationController) DatabaseTypeEnum(eu.etaxonomy.cdm.database.DatabaseTypeEnum) TemporalData(eu.etaxonomy.cdm.model.description.TemporalData) ICdmDataSource(eu.etaxonomy.cdm.database.ICdmDataSource) CdmDataSource(eu.etaxonomy.cdm.database.CdmDataSource) TransactionStatus(org.springframework.transaction.TransactionStatus) SchemaUpdateResult(eu.etaxonomy.cdm.database.update.SchemaUpdateResult) CdmUpdater(eu.etaxonomy.cdm.database.update.CdmUpdater)

Example 4 with CdmUpdater

use of eu.etaxonomy.cdm.database.update.CdmUpdater in project cdmlib by cybertaxonomy.

the class DataSourceConfigurer method dataSource.

@Bean
@Order(Ordered.HIGHEST_PRECEDENCE)
public DataSource dataSource() {
    String beanName = findProperty(ATTRIBUTE_DATASOURCE_NAME, true);
    String jndiName = null;
    if (this.dataSource == null) {
        jndiName = findProperty(ATTRIBUTE_JDBC_JNDI_NAME, false);
        if (jndiName != null) {
            try {
                dataSource = useJndiDataSource(jndiName);
                dataSourceId = FilenameUtils.getName(jndiName);
            } catch (NamingException e) {
                throw new DataSourceException("JNDI data source (" + jndiName + ") not found. Jdbc URI correct? Does the database exist?", e);
            }
        } else {
            dataSource = loadDataSourceBean(beanName);
            dataSourceId = beanName;
        }
    }
    if (dataSource == null) {
        return null;
    }
    MutablePropertySources sources = env.getPropertySources();
    Properties props = new Properties();
    props.setProperty(CdmConfigurationKeys.CDM_DATA_SOURCE_ID, dataSourceId);
    sources.addFirst(new PropertiesPropertySource("cdm-datasource", props));
    if (!isForceSchemaCreate()) {
        // validate correct schema version
        Connection connection = null;
        try {
            connection = dataSource.getConnection();
            String metadataTableName = "CdmMetaData";
            if (inferHibernateDialectName(dataSource).equals(H2CorrectedDialect.class.getName())) {
                metadataTableName = metadataTableName.toUpperCase();
            }
            ResultSet tables = connection.getMetaData().getTables(connection.getCatalog(), null, metadataTableName, null);
            if (tables.first()) {
                ResultSet resultSet;
                try {
                    resultSet = connection.createStatement().executeQuery(CdmMetaDataPropertyName.DB_SCHEMA_VERSION.getSqlQuery());
                } catch (Exception e) {
                    try {
                        resultSet = connection.createStatement().executeQuery(CdmMetaDataPropertyName.DB_SCHEMA_VERSION.getSqlQueryOld());
                    } catch (Exception e1) {
                        throw e1;
                    }
                }
                String version = null;
                if (resultSet.next()) {
                    version = resultSet.getString(1);
                } else {
                    CdmDatabaseException cde = new CdmDatabaseException("Unable to retrieve version info from data source " + dataSource.toString() + " -  the database may have been corrupted or is not a cdm database");
                    addErrorMessageToServletContextAttributes(cde.getMessage());
                    throw cde;
                }
                connection.close();
                if (!CdmMetaData.isDbSchemaVersionCompatible(version)) {
                    /*
                         * any exception thrown here would be nested into a spring
                         * BeanException which can not be caught in the servlet
                         * container, so we post the information into the
                         * ServletContext
                         */
                    String errorMessage = "Incompatible version [" + (beanName != null ? beanName : jndiName) + "] expected version: " + CdmMetaData.getDbSchemaVersion() + ",  data base version  " + version;
                    addErrorMessageToServletContextAttributes(errorMessage);
                }
            } else {
                CdmDatabaseException cde = new CdmDatabaseException("database " + dataSource.toString() + " is empty or not a cdm database");
                logger.error(cde.getMessage());
            // throw cde; // TODO: No exception was thrown here before. Is this correct behavior or
            }
        } catch (SQLException e) {
            CdmDatabaseException re = new CdmDatabaseException("Unable to connect or to retrieve version info from data source " + dataSource.toString(), e);
            addErrorMessageToServletContextAttributes(re.getMessage());
            throw re;
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                // IGNORE //
                }
            }
        }
    // END // validate correct schema version
    }
    // END !isForceSchemaCreate()
    String forceSchemaUpdate = findProperty(ATTRIBUTE_FORCE_SCHEMA_UPDATE, false);
    if (forceSchemaUpdate != null) {
        if (!isForceSchemaCreate()) {
            logger.info("Update of data source requested by property '" + ATTRIBUTE_FORCE_SCHEMA_UPDATE + "'");
            CdmUpdater updater = CdmUpdater.NewInstance();
            WrappedCdmDataSource cdmDataSource = new WrappedCdmDataSource(dataSource);
            updater.updateToCurrentVersion(cdmDataSource, null);
            cdmDataSource.closeOpenConnections();
        } else {
            logger.info("Update of data source requested by property '" + ATTRIBUTE_FORCE_SCHEMA_UPDATE + "' but overwritten by " + ATTRIBUTE_FORCE_SCHEMA_CREATE);
        }
    }
    return dataSource;
}
Also used : H2CorrectedDialect(org.hibernate.dialect.H2CorrectedDialect) SQLException(java.sql.SQLException) Connection(java.sql.Connection) Properties(java.util.Properties) NamingException(javax.naming.NamingException) SQLException(java.sql.SQLException) CdmDatabaseException(eu.etaxonomy.cdm.database.CdmDatabaseException) BeansException(org.springframework.beans.BeansException) InvocationTargetException(java.lang.reflect.InvocationTargetException) CdmUpdater(eu.etaxonomy.cdm.database.update.CdmUpdater) PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) ResultSet(java.sql.ResultSet) CdmDatabaseException(eu.etaxonomy.cdm.database.CdmDatabaseException) WrappedCdmDataSource(eu.etaxonomy.cdm.database.WrappedCdmDataSource) NamingException(javax.naming.NamingException) MutablePropertySources(org.springframework.core.env.MutablePropertySources) Order(org.springframework.core.annotation.Order) JndiObjectFactoryBean(org.springframework.jndi.JndiObjectFactoryBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

CdmUpdater (eu.etaxonomy.cdm.database.update.CdmUpdater)4 CdmApplicationController (eu.etaxonomy.cdm.api.application.CdmApplicationController)3 ICdmDataSource (eu.etaxonomy.cdm.database.ICdmDataSource)3 SchemaUpdateResult (eu.etaxonomy.cdm.database.update.SchemaUpdateResult)3 CdmDataSource (eu.etaxonomy.cdm.database.CdmDataSource)1 CdmDatabaseException (eu.etaxonomy.cdm.database.CdmDatabaseException)1 DatabaseTypeEnum (eu.etaxonomy.cdm.database.DatabaseTypeEnum)1 DbSchemaValidation (eu.etaxonomy.cdm.database.DbSchemaValidation)1 WrappedCdmDataSource (eu.etaxonomy.cdm.database.WrappedCdmDataSource)1 TemporalData (eu.etaxonomy.cdm.model.description.TemporalData)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Properties (java.util.Properties)1 NamingException (javax.naming.NamingException)1 H2CorrectedDialect (org.hibernate.dialect.H2CorrectedDialect)1 BeansException (org.springframework.beans.BeansException)1 Bean (org.springframework.context.annotation.Bean)1 Order (org.springframework.core.annotation.Order)1