-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathliquibase-helper
executable file
·94 lines (78 loc) · 3.89 KB
/
liquibase-helper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/java --source 21
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Scanner;
public class LiquibaseHelper {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Liquibase Timestamp Helper - Type 'help' for commands or 'exit' to quit");
while (true) {
System.out.print("Command> ");
String input = scanner.nextLine().trim().toLowerCase();
switch (input) {
case "timestamp" -> {
String[] timestamps = generateTimestamps();
System.out.println("Filename timestamp: " + timestamps[0]);
System.out.println("Changeset ID timestamp: " + timestamps[1]);
}
case "generate" -> {
System.out.print("Enter change description: ");
String description = scanner.nextLine().trim();
System.out.print("Enter author name: ");
String author = scanner.nextLine().trim();
var template = createChangeFileTemplate(description, author);
System.out.println("\nGenerated filename: " + template.filename());
System.out.println("Generated changeset ID: " + template.changesetId());
System.out.println("\nXML Content:");
System.out.println(template.xmlContent());
}
case "help" -> System.out.println("""
Available commands:
- timestamp: Generate UTC timestamps for filename and changeset ID
- generate: Create a complete Liquibase change file template
- help: Show this help message
- exit: Exit the program
""");
case "exit" -> {
System.out.println("Goodbye!");
return;
}
default -> System.out.println("Unknown command. Type 'help' for available commands.");
}
}
}
private static String[] generateTimestamps() {
Instant now = Instant.now();
DateTimeFormatter filenameFormatter = DateTimeFormatter
.ofPattern("yyyyMMdd'T'HHmmss")
.withZone(ZoneOffset.UTC);
DateTimeFormatter changesetFormatter = DateTimeFormatter
.ofPattern("yyyyMMddHHmmss")
.withZone(ZoneOffset.UTC);
return new String[]{
filenameFormatter.format(now),
changesetFormatter.format(now)
};
}
private static ChangeTemplate createChangeFileTemplate(String description, String author) {
String[] timestamps = generateTimestamps();
String normalizedDesc = description.toLowerCase().replace(' ', '_');
String filename = String.format("%s_%s.xml", timestamps[0], normalizedDesc);
String changesetId = String.format("%s", timestamps[1]);
String xmlTemplate = String.format("""
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
https://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd">
<changeSet id="%s" author="%s">
<!-- Your changes here -->
</changeSet>
</databaseChangeLog>""",
changesetId, author);
return new ChangeTemplate(filename, changesetId, xmlTemplate);
}
private record ChangeTemplate(String filename, String changesetId, String xmlContent) {}
}