Skip to content

Commit

Permalink
Merge pull request #44 from flyway/db2zos
Browse files Browse the repository at this point in the history
Add DB2Z support to Flyway Community DB Support
  • Loading branch information
Barry-RG authored Jun 11, 2024
2 parents 72608eb + 56db772 commit f1df122
Show file tree
Hide file tree
Showing 24 changed files with 1,345 additions and 9 deletions.
2 changes: 1 addition & 1 deletion flyway-community-db-support-archetype/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-community-db-support-archetype</artifactId>
<version>10.13.0</version>
<version>10.14.0</version>
<packaging>maven-archetype</packaging>

<name>Archetype - flyway-community-db-support-archetype</name>
Expand Down
2 changes: 1 addition & 1 deletion flyway-database-clickhouse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-community-db-support</artifactId>
<version>10.13.0</version>
<version>10.14.0</version>
</parent>

<artifactId>flyway-database-clickhouse</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion flyway-database-databricks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<parent>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-community-db-support</artifactId>
<version>10.13.0</version>
<version>10.14.0</version>
</parent>

<artifactId>flyway-database-databricks</artifactId>
Expand Down
60 changes: 60 additions & 0 deletions flyway-database-db2zOS/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (C) Red Gate Software Ltd 2010-2024
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-community-db-support</artifactId>
<version>10.14.0</version>
</parent>

<artifactId>flyway-database-db2zOS</artifactId>
<name>${project.artifactId}</name>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) Red Gate Software Ltd 2010-2022
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flywaydb.community.database;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.flywaydb.community.database.db2z.DB2ZConfigurationExtension;
import org.flywaydb.core.api.FlywayException;
import org.flywaydb.core.extensibility.PluginMetadata;
import org.flywaydb.core.internal.util.FileUtils;

public class DB2ZDatabaseExtension implements PluginMetadata {

public String getDescription() {
return "Community-contributed DB2/zOS database support extension " + readVersion() + " by Redgate";
}

public static String readVersion() {
try {
return FileUtils.copyToString(
DB2ZConfigurationExtension.class.getClassLoader().getResourceAsStream("org/flywaydb/community/database/db2z/version.txt"),
StandardCharsets.UTF_8);
} catch (IOException e) {
throw new FlywayException("Unable to read extension version: " + e.getMessage(), e);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright (C) Red Gate Software Ltd 2010-2022
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flywaydb.community.database.db2z;

import java.sql.SQLException;
import java.util.List;
import java.util.regex.Pattern;
import org.flywaydb.core.api.configuration.Configuration;
import org.flywaydb.core.internal.jdbc.JdbcTemplate;
import org.flywaydb.core.internal.jdbc.Result;
import org.flywaydb.core.internal.jdbc.Results;
import org.flywaydb.core.internal.sqlscript.Delimiter;
import org.flywaydb.core.internal.sqlscript.ParsedSqlStatement;
import org.flywaydb.core.internal.sqlscript.SqlScriptExecutor;


/**
* A DB2Z CALL PROCEDURE statement.
*/
public class DB2ZCallProcedureParsedStatement extends ParsedSqlStatement {

private final String procedureName;
private final Object[] parms;

private static final Pattern DB2Z_DSNUTILU_PROCNAME = Pattern.compile(
"\"?SYSPROC\"?\\.\"?DSNUTILU\"?", Pattern.CASE_INSENSITIVE);
/**
* Creates a new DB2Z CALL PROCEDURE statement.
*/
public DB2ZCallProcedureParsedStatement(int pos, int line, int col, String sql, Delimiter delimiter,
boolean canExecuteInTransaction, boolean batchable,
String procedureName, Object[] parms) {
super(pos, line, col, sql, delimiter, canExecuteInTransaction, batchable);
this.procedureName = procedureName;
this.parms = parms;
}

@Override
public Results execute(JdbcTemplate jdbcTemplate, SqlScriptExecutor sqlScriptExecutor, Configuration config) {
Results results;
String callStmt = "CALL " + procedureName + "(";
for(int i=0; i < parms.length; i++) {
callStmt += (i > 0 ? ", ?" : "?");
}
callStmt += ")";

results = ((DB2ZJdbcTemplate)jdbcTemplate).executeCallableStatement(callStmt, parms);

//For SYSPROC.DSNUTILU invocations, check last result row to detect any error
if(DB2Z_DSNUTILU_PROCNAME.matcher(procedureName).matches()) {
List<Result> resultList = results.getResults();
if(resultList.size() > 0) {
Result result = resultList.get(0);
if(result != null) {
List<List<String>> resultData = result.getData();
if(resultData != null && resultData.size() > 0) {
List<String> lastResultRow = resultData.get(resultData.size()-1);
if(lastResultRow != null && lastResultRow.size() > 0 ) {
String lastMessage = lastResultRow.get(lastResultRow.size()-1);
if(lastMessage != null && (
lastMessage.contains("DSNUGBAC - UTILITY EXECUTION TERMINATED, HIGHEST RETURN CODE=") ||
lastMessage.contains("DSNUGBAC - UTILITY BATCH MEMORY EXECUTION ABENDED"))) {
String message = "DSNUTILU TERMINATED WITH OUTPUT:\n";
for(List<String> row : resultData) {
message += row.get(row.size()-1) + "\n";
}
results.setException(new SQLException(message));
}
}
}
}
}
}

return results;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) Red Gate Software Ltd 2010-2022
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flywaydb.community.database.db2z;

import java.util.Map;
import lombok.Data;
import org.flywaydb.core.extensibility.ConfigurationExtension;

@Data
public class DB2ZConfigurationExtension implements ConfigurationExtension {
private static final String DATABASE_NAME = "flyway.db2z.databaseName";
private static final String SQL_ID = "flyway.db2z.sqlId";

/**
* The database name for DB2 on z/OS (required for DB2 on z/OS)
*/
private String databaseName = "";
/**
* The SQLID for DB2 on z/OS (does not necessarily match with schema)
*/
private String sqlId = "";


@Override
public String getNamespace() {
return "db2z";
}

@Override
public void extractParametersFromConfiguration(Map<String, String> configuration) {
databaseName = configuration.getOrDefault(DATABASE_NAME, databaseName);
sqlId = configuration.getOrDefault(SQL_ID, sqlId);
configuration.remove(DATABASE_NAME);
configuration.remove(SQL_ID);
}

@Override
public String getConfigurationParameterFromEnvironmentVariable(String environmentVariable) {
if ("FLYWAY_DB2Z_DATABASE_NAME".equals(environmentVariable)) {
return DATABASE_NAME;
}
if ("FLYWAY_DB2Z_SQL_ID".equals(environmentVariable)) {
return SQL_ID;
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright (C) Red Gate Software Ltd 2010-2022
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.flywaydb.community.database.db2z;

import java.sql.SQLException;
import lombok.CustomLog;
import org.flywaydb.core.internal.database.base.Connection;
import org.flywaydb.core.internal.database.base.Schema;
import org.flywaydb.core.internal.exception.FlywaySqlException;

/**
* DB2 connection.
*/
@CustomLog
public class DB2ZConnection extends Connection<DB2ZDatabase> {
DB2ZConnection(DB2ZDatabase database, java.sql.Connection connection) {
super(database, connection);
this.jdbcTemplate = new DB2ZJdbcTemplate(connection, database.getDatabaseType());
}

@Override
protected String getCurrentSchemaNameOrSearchPath() throws SQLException {
return jdbcTemplate.queryForString("select current_schema from sysibm.sysdummy1");
}

@Override
public void changeCurrentSchemaTo(Schema schema) {
try {
if (!schema.exists()) {
return;
}
doChangeCurrentSchemaOrSearchPathTo(schema.getName());
} catch (SQLException e) {
String sqlId = (database.getSqlId() == "") ? schema.getName() : database.getSqlId();
LOG.info("SET CURRENT SQLID = '" + sqlId + "'");
LOG.info("SET SCHEMA " + database.quote(schema.getName()));
throw new FlywaySqlException("Error setting current sqlid and/or schema", e);
}
}

@Override
public void doChangeCurrentSchemaOrSearchPathTo(String schema) throws SQLException {
// Maybe sqlid not same as schema name and entered as config property
String sqlId = (database.getSqlId() == "") ? schema : database.getSqlId();
jdbcTemplate.execute("SET CURRENT SQLID = '" + sqlId + "'");
jdbcTemplate.execute("SET SCHEMA " + database.quote(schema));
}

@Override
public Schema getSchema(String name) {
return new DB2ZSchema(jdbcTemplate, database, name);
}
}
Loading

0 comments on commit f1df122

Please sign in to comment.