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");
}
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");
}
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");
}
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;
}
Aggregations