use of com.axway.ats.environment.database.model.DbTable in project ats-framework by Axway.
the class Test_DatabaseEnvironmentUnit method getDescription.
@Test
public void getDescription() throws EnvironmentCleanupException, IOException {
//temp file is created and it exists, so backup will not be triggered
DbTable table1 = new DbTable("table1");
DbTable table2 = new DbTable("table2");
List<DbTable> tables = new ArrayList<DbTable>();
tables.add(table1);
tables.add(table2);
expect(mockDbConnection.getDbType()).andReturn(DbConnMySQL.DATABASE_TYPE);
replay(mockDbConnection);
DatabaseEnvironmentUnit dbEnvironmentUnit = new DatabaseEnvironmentUnit(tempFile.getParentFile().getCanonicalPath(), tempFile.getName(), mockDbConnection, tables, mockFactory);
assertEquals("MYSQL database in file " + tempFile.getCanonicalPath(), dbEnvironmentUnit.getDescription());
verify(mockDbConnection);
}
use of com.axway.ats.environment.database.model.DbTable in project ats-framework by Axway.
the class DatabaseEnvironmentUnit method backup.
@Override
@PublicAtsApi
public void backup() throws EnvironmentCleanupException {
BackupHandler dbBackup = null;
try {
log.info("Creating backup of environment unit " + getDescription() + "...");
//create db backup handler instance
dbBackup = environmentHandlerFactory.createDbBackupHandler(dbConnection);
dbBackup.setLockTables(addLocks);
dbBackup.setForeignKeyCheck(disableForeignKeys);
dbBackup.setIncludeDeleteStatements(includeDeleteStatements);
for (DbTable dbTable : dbTables) {
dbBackup.addTable(dbTable);
}
String backupFile = getBackupFile();
createDirIfNotExist(backupFile);
dbBackup.createBackup(backupFile);
log.info("Successfully created backup of environment unit " + getDescription());
} finally {
setTempBackupDir(null);
if (dbBackup != null) {
dbBackup.disconnect();
}
}
}
use of com.axway.ats.environment.database.model.DbTable in project ats-framework by Axway.
the class DatabaseEnvironmentUnit method getNewCopy.
public EnvironmentUnit getNewCopy() {
DatabaseEnvironmentUnit newDatabaseEnvironmentUnit = new DatabaseEnvironmentUnit(this.backupDirPath, this.backupFileName, // we do not copy this object, seems to be constant
this.dbConnection, null);
List<DbTable> newDbTables = new ArrayList<DbTable>();
for (DbTable dbTable : dbTables) {
newDbTables.add(dbTable.getNewCopy());
}
newDatabaseEnvironmentUnit.dbTables = newDbTables;
return newDatabaseEnvironmentUnit;
}
use of com.axway.ats.environment.database.model.DbTable in project ats-framework by Axway.
the class AbstractEnvironmentHandler method writeBackupToFile.
/**
* Write the backup to a file
*
* @param fileWriter the file writer
* @throws IOException on io error
* @throws DatabaseEnvironmentCleanupException on error
* @throws DbException on error reading from the database
* @throws ParseException
*/
protected void writeBackupToFile(FileWriter fileWriter) throws IOException, DatabaseEnvironmentCleanupException, DbException, ParseException {
if (disableForeignKeys) {
fileWriter.write(disableForeignKeyChecksStart());
}
for (DbTable dbTable : dbTables) {
if (log.isDebugEnabled()) {
log.debug("Preparing data for backup of table " + (dbTable != null ? dbTable.getTableName() : null));
}
List<ColumnDescription> columnsToSelect = null;
columnsToSelect = getColumnsToSelect(dbTable, dbConnection.getUser());
if (columnsToSelect == null || columnsToSelect.size() == 0) {
// it contains some meaningful data and so it has columns
throw new DatabaseEnvironmentCleanupException("No columns to backup for table " + (dbTable != null ? dbTable.getTableName() : ""));
}
StringBuilder selectQuery = new StringBuilder();
selectQuery.append("SELECT ");
selectQuery.append(getColumnsString(columnsToSelect));
selectQuery.append(" FROM ");
selectQuery.append(dbTable.getTableName());
DbQuery query = new DbQuery(selectQuery.toString());
DbRecordValuesList[] records = dbProvider.select(query, DbReturnModes.ESCAPED_STRING);
writeTableToFile(columnsToSelect, dbTable, records, fileWriter);
}
if (disableForeignKeys) {
fileWriter.write(disableForeignKeyChecksEnd());
}
}
use of com.axway.ats.environment.database.model.DbTable in project ats-framework by Axway.
the class ConfigurationParser method parseDbEnvironment.
/**
* Parse the database environment node in the Agent descriptor.
*
* @param dbEnvironmentNode the DB environment node.
* @throws AgentException on error.
*/
private EnvironmentUnit parseDbEnvironment(Node dbEnvironmentNode, String backupFolder) throws AgentException {
//create the connection descriptor
DbConnection dbConnection = createConnection(dbEnvironmentNode.getAttributes());
//read the tables
List<DbTable> dbTables = new ArrayList<DbTable>();
NodeList dbChildNodes = dbEnvironmentNode.getChildNodes();
for (int k = 0; k < dbChildNodes.getLength(); k++) {
Node dbChildNode = dbChildNodes.item(k);
if (dbChildNode.getNodeName().equals(TABLE)) {
String tableName = dbChildNode.getAttributes().getNamedItem("name").getNodeValue();
String[] columnsToExclude = {};
if (dbChildNode.getAttributes().getNamedItem("columnsToExclude") != null) {
columnsToExclude = dbChildNode.getAttributes().getNamedItem("columnsToExclude").getNodeValue().split(",");
}
DbTable dbTable = null;
// parse db table 'lock' attribute
if (dbChildNode.getAttributes().getNamedItem("lock") != null) {
if (dbConnection.getDbType().equals(DbConnOracle.DATABASE_TYPE)) {
log.warn("Db table 'lock' attribute is NOT implemented for Oracle yet. " + "Table locking is skipped for the moment.");
}
String nodeValue = dbChildNode.getAttributes().getNamedItem("lock").getNodeValue().trim();
if ("false".equalsIgnoreCase(nodeValue) || "true".equalsIgnoreCase(nodeValue)) {
dbTable = new DbTable(tableName, Arrays.asList(columnsToExclude), Boolean.parseBoolean(nodeValue));
} else {
log.warn("Invalid db table 'lock' attribute value '" + nodeValue + "'. Valid values are 'true' and 'false'. The default value 'true' will be used.");
}
}
if (dbTable == null) {
dbTable = new DbTable(tableName, Arrays.asList(columnsToExclude));
}
// parse db table 'autoIncrementResetValue' attribute
if (dbChildNode.getAttributes().getNamedItem(TABLE_ATTR_AUTOINCR_RESET_VALUE) != null) {
if (dbConnection.getDbType().equals(DbConnOracle.DATABASE_TYPE)) {
throw new AgentException("Db table 'autoIncrementResetValue' attribute is NOT implemented for Oracle yet.");
}
String autoInrcResetValue = dbChildNode.getAttributes().getNamedItem(TABLE_ATTR_AUTOINCR_RESET_VALUE).getNodeValue().trim();
try {
Integer.parseInt(autoInrcResetValue);
} catch (NumberFormatException nfe) {
throw new AgentException("Ivalid db table 'autoIncrementResetValue' attribute value '" + autoInrcResetValue + "'. It must be valid number.");
}
dbTable.setAutoIncrementResetValue(autoInrcResetValue);
}
dbTables.add(dbTable);
}
}
String backupFileName = componentName + "-" + dbConnection.getDb() + ".sql";
//create the environment unit
DatabaseEnvironmentUnit dbEnvironment = new DatabaseEnvironmentUnit(backupFolder, backupFileName, dbConnection, dbTables);
return dbEnvironment;
}
Aggregations