Skip to content

Instantly share code, notes, and snippets.

@t3rmian
Created July 26, 2020 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save t3rmian/500096ab817d653dcd6e4ff8255257f9 to your computer and use it in GitHub Desktop.
Save t3rmian/500096ab817d653dcd6e4ff8255257f9 to your computer and use it in GitHub Desktop.
Sample Java program to extract schema.object_name entries from migration scripts (Oracle)
// #!/usr/bin/java --source 11
// CC0 1.0 (public domain)
import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class ExtractDDL {
private static final String START_TAG = "CREATE ";
private static final String END_TAG = ";";
private static final List<String> DB_OBJECTS = Arrays.asList("CLUSTER", "CONTEXT", "CONTROLFILE",
"LINK", "DATABASE", "DIMENSION", "DIRECTORY", "FUNCTION", "INDEXTYPE", "INDEX", "JAVA",
"LIBRARY", "LOG", "OPERATOR", "OUTLINE", "BODY", "PACKAGE", "PFILE", "PROCEDURE",
"PROFILE", "SEGMENT", "SCHEMA", "SEQUENCE", "SNAPSHOT", "SPFILE", "SYNONYM",
"TABLESPACE", "TABLE", "TRIGGER", "TYPE", "USER", "VIEW");
public static void main(String... args) {
if (args.length != 1) {
System.out.println("Usage: ./script /path/to/migrations/directory");
return;
}
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:**/*.sql");
try (Stream<Path> walk = Files.walk(Paths.get(args[0]))) {
walk.filter(pathMatcher::matches)
.flatMap(path -> streamTagContents(START_TAG, END_TAG, path))
.map(statement -> statement.replaceAll("\\s+", " "))
.map(statement -> truncateToObjectName(DB_OBJECTS, statement))
.filter(statement -> !statement.isEmpty())
.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
private static String truncateToObjectName(List<String> objectNames, String statement) {
return Stream.of(statement.split(" "))
.limit(objectNames.stream()
.map(d -> Arrays.asList(statement.split(" ")).indexOf(d))
.filter(i -> i > 0).findFirst().orElse(-2) + 2)
.collect(Collectors.joining(" "));
}
private static Stream<? extends String> streamTagContents(String startTag, String endTag, Path path) {
List<String> results = new ArrayList<>();
try {
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
String fullString = String.join(" ", lines).toUpperCase();
for (int createIndex = fullString.indexOf(startTag, 0),
endIndex = fullString.indexOf(endTag, createIndex);
createIndex != -1 && endIndex != -1;
createIndex = endIndex,
createIndex = fullString.indexOf(startTag, createIndex),
endIndex = fullString.indexOf(endTag, createIndex)) {
results.add(fullString.substring(createIndex, endIndex));
}
} catch (IOException e) {
e.printStackTrace();
}
return results.stream();
}
}
CREATE
TABLE
schema_name.table_name
(
column_1 data_type column_constraint,
column_2 data_type column_constraint,
...
table_constraint
);
CREATE
TABLE
schema_name2.table_name2
(
column_1 data_type column_constraint,
column_2 data_type column_constraint,
...
table_constraint
);
-- CREATE TABLE SCHEMA_NAME.TABLE_NAME
-- CREATE TABLE SCHEMA_NAME2.TABLE_NAME2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment