Skip to content

Commit f1df122

Browse files
authored
Merge pull request #44 from flyway/db2zos
Add DB2Z support to Flyway Community DB Support
2 parents 72608eb + 56db772 commit f1df122

File tree

24 files changed

+1345
-9
lines changed

24 files changed

+1345
-9
lines changed

flyway-community-db-support-archetype/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>org.flywaydb</groupId>
77
<artifactId>flyway-community-db-support-archetype</artifactId>
8-
<version>10.13.0</version>
8+
<version>10.14.0</version>
99
<packaging>maven-archetype</packaging>
1010

1111
<name>Archetype - flyway-community-db-support-archetype</name>

flyway-database-clickhouse/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<parent>
2424
<groupId>org.flywaydb</groupId>
2525
<artifactId>flyway-community-db-support</artifactId>
26-
<version>10.13.0</version>
26+
<version>10.14.0</version>
2727
</parent>
2828

2929
<artifactId>flyway-database-clickhouse</artifactId>

flyway-database-databricks/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
<parent>
2424
<groupId>org.flywaydb</groupId>
2525
<artifactId>flyway-community-db-support</artifactId>
26-
<version>10.13.0</version>
26+
<version>10.14.0</version>
2727
</parent>
2828

2929
<artifactId>flyway-database-databricks</artifactId>

flyway-database-db2zOS/pom.xml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright (C) Red Gate Software Ltd 2010-2024
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
<parent>
24+
<groupId>org.flywaydb</groupId>
25+
<artifactId>flyway-community-db-support</artifactId>
26+
<version>10.14.0</version>
27+
</parent>
28+
29+
<artifactId>flyway-database-db2zOS</artifactId>
30+
<name>${project.artifactId}</name>
31+
32+
<dependencies>
33+
<dependency>
34+
<groupId>${project.groupId}</groupId>
35+
<artifactId>flyway-core</artifactId>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.projectlombok</groupId>
39+
<artifactId>lombok</artifactId>
40+
<scope>provided</scope>
41+
</dependency>
42+
</dependencies>
43+
44+
<build>
45+
<resources>
46+
<resource>
47+
<directory>src/main/resources</directory>
48+
<filtering>true</filtering>
49+
</resource>
50+
</resources>
51+
<plugins>
52+
<plugin>
53+
<artifactId>maven-resources-plugin</artifactId>
54+
</plugin>
55+
<plugin>
56+
<artifactId>maven-jar-plugin</artifactId>
57+
</plugin>
58+
</plugins>
59+
</build>
60+
</project>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright (C) Red Gate Software Ltd 2010-2022
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.flywaydb.community.database;
17+
18+
import java.io.IOException;
19+
import java.nio.charset.StandardCharsets;
20+
import org.flywaydb.community.database.db2z.DB2ZConfigurationExtension;
21+
import org.flywaydb.core.api.FlywayException;
22+
import org.flywaydb.core.extensibility.PluginMetadata;
23+
import org.flywaydb.core.internal.util.FileUtils;
24+
25+
public class DB2ZDatabaseExtension implements PluginMetadata {
26+
27+
public String getDescription() {
28+
return "Community-contributed DB2/zOS database support extension " + readVersion() + " by Redgate";
29+
}
30+
31+
public static String readVersion() {
32+
try {
33+
return FileUtils.copyToString(
34+
DB2ZConfigurationExtension.class.getClassLoader().getResourceAsStream("org/flywaydb/community/database/db2z/version.txt"),
35+
StandardCharsets.UTF_8);
36+
} catch (IOException e) {
37+
throw new FlywayException("Unable to read extension version: " + e.getMessage(), e);
38+
}
39+
}
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright (C) Red Gate Software Ltd 2010-2022
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.flywaydb.community.database.db2z;
17+
18+
import java.sql.SQLException;
19+
import java.util.List;
20+
import java.util.regex.Pattern;
21+
import org.flywaydb.core.api.configuration.Configuration;
22+
import org.flywaydb.core.internal.jdbc.JdbcTemplate;
23+
import org.flywaydb.core.internal.jdbc.Result;
24+
import org.flywaydb.core.internal.jdbc.Results;
25+
import org.flywaydb.core.internal.sqlscript.Delimiter;
26+
import org.flywaydb.core.internal.sqlscript.ParsedSqlStatement;
27+
import org.flywaydb.core.internal.sqlscript.SqlScriptExecutor;
28+
29+
30+
/**
31+
* A DB2Z CALL PROCEDURE statement.
32+
*/
33+
public class DB2ZCallProcedureParsedStatement extends ParsedSqlStatement {
34+
35+
private final String procedureName;
36+
private final Object[] parms;
37+
38+
private static final Pattern DB2Z_DSNUTILU_PROCNAME = Pattern.compile(
39+
"\"?SYSPROC\"?\\.\"?DSNUTILU\"?", Pattern.CASE_INSENSITIVE);
40+
/**
41+
* Creates a new DB2Z CALL PROCEDURE statement.
42+
*/
43+
public DB2ZCallProcedureParsedStatement(int pos, int line, int col, String sql, Delimiter delimiter,
44+
boolean canExecuteInTransaction, boolean batchable,
45+
String procedureName, Object[] parms) {
46+
super(pos, line, col, sql, delimiter, canExecuteInTransaction, batchable);
47+
this.procedureName = procedureName;
48+
this.parms = parms;
49+
}
50+
51+
@Override
52+
public Results execute(JdbcTemplate jdbcTemplate, SqlScriptExecutor sqlScriptExecutor, Configuration config) {
53+
Results results;
54+
String callStmt = "CALL " + procedureName + "(";
55+
for(int i=0; i < parms.length; i++) {
56+
callStmt += (i > 0 ? ", ?" : "?");
57+
}
58+
callStmt += ")";
59+
60+
results = ((DB2ZJdbcTemplate)jdbcTemplate).executeCallableStatement(callStmt, parms);
61+
62+
//For SYSPROC.DSNUTILU invocations, check last result row to detect any error
63+
if(DB2Z_DSNUTILU_PROCNAME.matcher(procedureName).matches()) {
64+
List<Result> resultList = results.getResults();
65+
if(resultList.size() > 0) {
66+
Result result = resultList.get(0);
67+
if(result != null) {
68+
List<List<String>> resultData = result.getData();
69+
if(resultData != null && resultData.size() > 0) {
70+
List<String> lastResultRow = resultData.get(resultData.size()-1);
71+
if(lastResultRow != null && lastResultRow.size() > 0 ) {
72+
String lastMessage = lastResultRow.get(lastResultRow.size()-1);
73+
if(lastMessage != null && (
74+
lastMessage.contains("DSNUGBAC - UTILITY EXECUTION TERMINATED, HIGHEST RETURN CODE=") ||
75+
lastMessage.contains("DSNUGBAC - UTILITY BATCH MEMORY EXECUTION ABENDED"))) {
76+
String message = "DSNUTILU TERMINATED WITH OUTPUT:\n";
77+
for(List<String> row : resultData) {
78+
message += row.get(row.size()-1) + "\n";
79+
}
80+
results.setException(new SQLException(message));
81+
}
82+
}
83+
}
84+
}
85+
}
86+
}
87+
88+
return results;
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (C) Red Gate Software Ltd 2010-2022
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.flywaydb.community.database.db2z;
17+
18+
import java.util.Map;
19+
import lombok.Data;
20+
import org.flywaydb.core.extensibility.ConfigurationExtension;
21+
22+
@Data
23+
public class DB2ZConfigurationExtension implements ConfigurationExtension {
24+
private static final String DATABASE_NAME = "flyway.db2z.databaseName";
25+
private static final String SQL_ID = "flyway.db2z.sqlId";
26+
27+
/**
28+
* The database name for DB2 on z/OS (required for DB2 on z/OS)
29+
*/
30+
private String databaseName = "";
31+
/**
32+
* The SQLID for DB2 on z/OS (does not necessarily match with schema)
33+
*/
34+
private String sqlId = "";
35+
36+
37+
@Override
38+
public String getNamespace() {
39+
return "db2z";
40+
}
41+
42+
@Override
43+
public void extractParametersFromConfiguration(Map<String, String> configuration) {
44+
databaseName = configuration.getOrDefault(DATABASE_NAME, databaseName);
45+
sqlId = configuration.getOrDefault(SQL_ID, sqlId);
46+
configuration.remove(DATABASE_NAME);
47+
configuration.remove(SQL_ID);
48+
}
49+
50+
@Override
51+
public String getConfigurationParameterFromEnvironmentVariable(String environmentVariable) {
52+
if ("FLYWAY_DB2Z_DATABASE_NAME".equals(environmentVariable)) {
53+
return DATABASE_NAME;
54+
}
55+
if ("FLYWAY_DB2Z_SQL_ID".equals(environmentVariable)) {
56+
return SQL_ID;
57+
}
58+
return null;
59+
}
60+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright (C) Red Gate Software Ltd 2010-2022
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.flywaydb.community.database.db2z;
17+
18+
import java.sql.SQLException;
19+
import lombok.CustomLog;
20+
import org.flywaydb.core.internal.database.base.Connection;
21+
import org.flywaydb.core.internal.database.base.Schema;
22+
import org.flywaydb.core.internal.exception.FlywaySqlException;
23+
24+
/**
25+
* DB2 connection.
26+
*/
27+
@CustomLog
28+
public class DB2ZConnection extends Connection<DB2ZDatabase> {
29+
DB2ZConnection(DB2ZDatabase database, java.sql.Connection connection) {
30+
super(database, connection);
31+
this.jdbcTemplate = new DB2ZJdbcTemplate(connection, database.getDatabaseType());
32+
}
33+
34+
@Override
35+
protected String getCurrentSchemaNameOrSearchPath() throws SQLException {
36+
return jdbcTemplate.queryForString("select current_schema from sysibm.sysdummy1");
37+
}
38+
39+
@Override
40+
public void changeCurrentSchemaTo(Schema schema) {
41+
try {
42+
if (!schema.exists()) {
43+
return;
44+
}
45+
doChangeCurrentSchemaOrSearchPathTo(schema.getName());
46+
} catch (SQLException e) {
47+
String sqlId = (database.getSqlId() == "") ? schema.getName() : database.getSqlId();
48+
LOG.info("SET CURRENT SQLID = '" + sqlId + "'");
49+
LOG.info("SET SCHEMA " + database.quote(schema.getName()));
50+
throw new FlywaySqlException("Error setting current sqlid and/or schema", e);
51+
}
52+
}
53+
54+
@Override
55+
public void doChangeCurrentSchemaOrSearchPathTo(String schema) throws SQLException {
56+
// Maybe sqlid not same as schema name and entered as config property
57+
String sqlId = (database.getSqlId() == "") ? schema : database.getSqlId();
58+
jdbcTemplate.execute("SET CURRENT SQLID = '" + sqlId + "'");
59+
jdbcTemplate.execute("SET SCHEMA " + database.quote(schema));
60+
}
61+
62+
@Override
63+
public Schema getSchema(String name) {
64+
return new DB2ZSchema(jdbcTemplate, database, name);
65+
}
66+
}

0 commit comments

Comments
 (0)