Skip to content
This repository was archived by the owner on May 14, 2025. It is now read-only.

Commit c40910e

Browse files
author
Corneil du Plessis
authored
Drop JOB_CONFIGURATION_LOCATION from batch tables. (#5592)
* DROP JOB_CONFIGURATION_LOCATION from BOOT3_BATCH_JOB_EXECUTION. * Update DropColumnSqlCommands.java Add comment to describe reason for finding actual names.
1 parent dd7e8f1 commit c40910e

File tree

7 files changed

+299
-0
lines changed

7 files changed

+299
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
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+
* https://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.springframework.cloud.dataflow.server.db.migration;
17+
18+
import org.slf4j.Logger;
19+
import org.slf4j.LoggerFactory;
20+
import org.springframework.cloud.dataflow.common.flyway.SqlCommand;
21+
import org.springframework.jdbc.core.JdbcTemplate;
22+
import org.springframework.util.Assert;
23+
import org.springframework.util.StringUtils;
24+
25+
import java.sql.Connection;
26+
import java.sql.ResultSet;
27+
import java.sql.SQLException;
28+
import java.util.ArrayList;
29+
import java.util.Arrays;
30+
import java.util.List;
31+
32+
/**
33+
* Utility class that can be used in future to drop columns.
34+
* This checks for the existence of the column before dropping.
35+
* @author Corneil du Plessis
36+
*/
37+
public class DropColumnSqlCommands extends SqlCommand {
38+
private final static Logger logger = LoggerFactory.getLogger(DropColumnSqlCommands.class);
39+
40+
private final List<String> columnNames = new ArrayList<>();
41+
42+
public DropColumnSqlCommands(String... columnName) {
43+
columnNames.addAll(Arrays.asList(columnName));
44+
}
45+
46+
@Override
47+
public void handle(JdbcTemplate jdbcTemplate, Connection connection) {
48+
for(String name : columnNames) {
49+
try {
50+
dropColumn(jdbcTemplate, connection, name);
51+
} catch (SQLException e) {
52+
throw new RuntimeException(e);
53+
}
54+
}
55+
}
56+
57+
@Override
58+
public boolean canHandleInJdbcTemplate() {
59+
return true;
60+
}
61+
62+
protected void dropColumn(JdbcTemplate jdbcTemplate, Connection connection, String name) throws SQLException {
63+
logger.debug("dropping:{}", name);
64+
String [] parts = StringUtils.split(name, ".");
65+
Assert.notNull(parts, "Expected 2 or more parts from " + name);
66+
Assert.isTrue(parts.length > 1, "Expected 2 or more parts from " + name);
67+
String columnName = parts[parts.length - 1];
68+
String tableName = parts[parts.length - 2];
69+
String schemaName = parts.length > 2 ? parts[parts.length - 3] : null;
70+
logger.debug("Searching for {}.{}", tableName, columnName);
71+
if(hasColumn(connection, schemaName, tableName, columnName)) {
72+
String sql = String.format("alter table %s drop column %s", tableName, columnName);
73+
logger.debug("Executing: {}", sql);
74+
jdbcTemplate.execute(sql);
75+
}
76+
}
77+
protected boolean hasColumn(Connection connection, String schemaName, String tableName, String columnName) throws SQLException {
78+
String actualSchemaName = null;
79+
if(StringUtils.hasText(schemaName)) {
80+
try(ResultSet resultSet = connection.getMetaData().getSchemas()) {
81+
while (resultSet.next()) {
82+
String name = resultSet.getString("SCHEMA_NAME");
83+
// determine the actual name used in specific database metadata.
84+
if(name.equalsIgnoreCase(schemaName)) {
85+
actualSchemaName = name;
86+
break;
87+
}
88+
}
89+
}
90+
}
91+
String actualTableName = tableName;
92+
try(ResultSet resultSet = connection.getMetaData().getTables(null, actualSchemaName, null, new String[] {"TABLE"})) {
93+
while (resultSet.next()) {
94+
String name = resultSet.getString("TABLE_NAME");
95+
// determine the actual name used in specific database metadata.
96+
if(name.equalsIgnoreCase(tableName)) {
97+
actualTableName = name;
98+
break;
99+
}
100+
}
101+
}
102+
// actual names need to be same case as reported by meta data query for some databases.
103+
try (ResultSet resultSet = connection.getMetaData().getColumns(null, actualSchemaName, actualTableName, null)) {
104+
while (resultSet.next()) {
105+
String foundColumnName = resultSet.getString("COLUMN_NAME");
106+
if (foundColumnName.equalsIgnoreCase(columnName)) {
107+
return true;
108+
}
109+
}
110+
}
111+
return false;
112+
}
113+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
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+
* https://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.springframework.cloud.dataflow.server.db.migration.db2;
17+
18+
import org.springframework.cloud.dataflow.common.flyway.AbstractMigration;
19+
import org.springframework.cloud.dataflow.server.db.migration.DropColumnSqlCommands;
20+
21+
import java.util.Collections;
22+
23+
/**
24+
* Removes extra JOB_CONFIGURATION_LOCATION columns.
25+
* @author Corneil du Plessis
26+
*/
27+
public class V9__DropJobConfigurationLocation extends AbstractMigration {
28+
public V9__DropJobConfigurationLocation() {
29+
super(Collections.singletonList(new DropColumnSqlCommands("BOOT3_BATCH_JOB_EXECUTION.JOB_CONFIGURATION_LOCATION")));
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
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+
* https://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.springframework.cloud.dataflow.server.db.migration.mariadb;
17+
18+
import java.util.Collections;
19+
20+
import org.springframework.cloud.dataflow.common.flyway.AbstractMigration;
21+
import org.springframework.cloud.dataflow.server.db.migration.DropColumnSqlCommands;
22+
23+
/**
24+
* Removes extra JOB_CONFIGURATION_LOCATION columns.
25+
* @author Corneil du Plessis
26+
*/
27+
public class V10__DropJobConfigurationLocation extends AbstractMigration {
28+
public V10__DropJobConfigurationLocation() {
29+
super(Collections.singletonList(new DropColumnSqlCommands("BOOT3_BATCH_JOB_EXECUTION.JOB_CONFIGURATION_LOCATION")));
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
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+
* https://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.springframework.cloud.dataflow.server.db.migration.mysql;
17+
18+
import java.util.Collections;
19+
20+
import org.springframework.cloud.dataflow.common.flyway.AbstractMigration;
21+
import org.springframework.cloud.dataflow.server.db.migration.DropColumnSqlCommands;
22+
23+
/**
24+
* Removes extra JOB_CONFIGURATION_LOCATION columns.
25+
* @author Corneil du Plessis
26+
*/
27+
public class V10__DropJobConfigurationLocation extends AbstractMigration {
28+
public V10__DropJobConfigurationLocation() {
29+
super(Collections.singletonList(new DropColumnSqlCommands("BOOT3_BATCH_JOB_EXECUTION.JOB_CONFIGURATION_LOCATION")));
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
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+
* https://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.springframework.cloud.dataflow.server.db.migration.oracle;
17+
18+
import java.util.Collections;
19+
20+
import org.springframework.cloud.dataflow.common.flyway.AbstractMigration;
21+
import org.springframework.cloud.dataflow.server.db.migration.DropColumnSqlCommands;
22+
23+
/**
24+
* Removes extra JOB_CONFIGURATION_LOCATION columns.
25+
* @author Corneil du Plessis
26+
*/
27+
public class V10__DropJobConfigurationLocation extends AbstractMigration {
28+
public V10__DropJobConfigurationLocation() {
29+
super(Collections.singletonList(new DropColumnSqlCommands("BOOT3_BATCH_JOB_EXECUTION.JOB_CONFIGURATION_LOCATION")));
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
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+
* https://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.springframework.cloud.dataflow.server.db.migration.postgresql;
17+
18+
import java.util.Collections;
19+
20+
import org.springframework.cloud.dataflow.common.flyway.AbstractMigration;
21+
import org.springframework.cloud.dataflow.server.db.migration.DropColumnSqlCommands;
22+
23+
/**
24+
* Removes extra JOB_CONFIGURATION_LOCATION columns.
25+
* @author Corneil du Plessis
26+
*/
27+
public class V11__DropJobConfigurationLocation extends AbstractMigration {
28+
public V11__DropJobConfigurationLocation() {
29+
super(Collections.singletonList(new DropColumnSqlCommands("BOOT3_BATCH_JOB_EXECUTION.JOB_CONFIGURATION_LOCATION")));
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
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+
* https://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.springframework.cloud.dataflow.server.db.migration.sqlserver;
17+
18+
import java.util.Collections;
19+
20+
import org.springframework.cloud.dataflow.common.flyway.AbstractMigration;
21+
import org.springframework.cloud.dataflow.server.db.migration.DropColumnSqlCommands;
22+
23+
/**
24+
* Removes extra JOB_CONFIGURATION_LOCATION columns.
25+
* @author Corneil du Plessis
26+
*/
27+
public class V9__DropJobConfigurationLocation extends AbstractMigration {
28+
public V9__DropJobConfigurationLocation() {
29+
super(Collections.singletonList(new DropColumnSqlCommands("BOOT3_BATCH_JOB_EXECUTION.JOB_CONFIGURATION_LOCATION")));
30+
}
31+
}

0 commit comments

Comments
 (0)