Search in sources :

Example 11 with DbTable

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);
}
Also used : ArrayList(java.util.ArrayList) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) DbTable(com.axway.ats.environment.database.model.DbTable) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Example 12 with DbTable

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();
        }
    }
}
Also used : BackupHandler(com.axway.ats.environment.database.model.BackupHandler) DbTable(com.axway.ats.environment.database.model.DbTable) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 13 with DbTable

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;
}
Also used : ArrayList(java.util.ArrayList) DbTable(com.axway.ats.environment.database.model.DbTable)

Example 14 with DbTable

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());
    }
}
Also used : DatabaseEnvironmentCleanupException(com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException) DbQuery(com.axway.ats.common.dbaccess.DbQuery) DbRecordValuesList(com.axway.ats.core.dbaccess.DbRecordValuesList) ColumnDescription(com.axway.ats.core.dbaccess.ColumnDescription) DbTable(com.axway.ats.environment.database.model.DbTable)

Example 15 with DbTable

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;
}
Also used : AgentException(com.axway.ats.agent.core.exceptions.AgentException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) DbConnection(com.axway.ats.core.dbaccess.DbConnection) DbTable(com.axway.ats.environment.database.model.DbTable)

Aggregations

DbTable (com.axway.ats.environment.database.model.DbTable)18 BaseTest (com.axway.ats.environment.BaseTest)14 Test (org.junit.Test)14 ArrayList (java.util.ArrayList)10 DbRecordValuesList (com.axway.ats.core.dbaccess.DbRecordValuesList)9 DbQuery (com.axway.ats.common.dbaccess.DbQuery)8 DbRecordValue (com.axway.ats.core.dbaccess.DbRecordValue)8 MysqlEnvironmentHandler (com.axway.ats.environment.database.MysqlEnvironmentHandler)5 OracleEnvironmentHandler (com.axway.ats.environment.database.OracleEnvironmentHandler)5 DatabaseEnvironmentUnit (com.axway.ats.environment.database.DatabaseEnvironmentUnit)4 AgentException (com.axway.ats.agent.core.exceptions.AgentException)1 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 ColumnDescription (com.axway.ats.core.dbaccess.ColumnDescription)1 DbConnection (com.axway.ats.core.dbaccess.DbConnection)1 DbException (com.axway.ats.core.dbaccess.exceptions.DbException)1 DatabaseEnvironmentCleanupException (com.axway.ats.environment.database.exceptions.DatabaseEnvironmentCleanupException)1 BackupHandler (com.axway.ats.environment.database.model.BackupHandler)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1