-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiquibaseDatabaseInitializer.java
More file actions
149 lines (126 loc) · 5.07 KB
/
LiquibaseDatabaseInitializer.java
File metadata and controls
149 lines (126 loc) · 5.07 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package nl.wouterh.pgpool.liquibase;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.Scope;
import liquibase.ThreadLocalScopeManager;
import liquibase.changelog.ChangeSet;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.LiquibaseException;
import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.resource.ResourceAccessor;
import nl.wouterh.pgpool.DatabaseInitializer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A {@link DatabaseInitializer} which integrates {@link Liquibase}
*/
public class LiquibaseDatabaseInitializer implements DatabaseInitializer {
private static final Logger log = LoggerFactory.getLogger(LiquibaseDatabaseInitializer.class);
protected final String changeLogFile;
protected final ResourceAccessor resourceAccessor;
protected final String liquibaseContexts;
static {
// Makes liquibase thread-safe
Scope.setScopeManager(new ThreadLocalScopeManager());
}
public LiquibaseDatabaseInitializer(String changeLogFile) {
this(changeLogFile, "");
}
public LiquibaseDatabaseInitializer(String changeLogFile, String liquibaseContexts) {
this(changeLogFile, new ClassLoaderResourceAccessor(), liquibaseContexts);
}
public LiquibaseDatabaseInitializer(String changeLogFile, ResourceAccessor resourceAccessor,
String liquibaseContexts) {
this.changeLogFile = changeLogFile;
this.resourceAccessor = resourceAccessor;
this.liquibaseContexts = liquibaseContexts;
}
@Override
public void run(Connection conn) throws SQLException {
try {
boolean isAutoCommit = conn.getAutoCommit();
Contexts contexts = new Contexts(Arrays.stream(liquibaseContexts.split("\\s*,\\s*"))
.map(String::trim)
.filter((e) -> !e.isEmpty())
.toArray(String[]::new));
log.info("Starting database migration with contexts {}",
contexts.getContexts().stream().sorted().collect(Collectors.joining(", ")));
Database database = DatabaseFactory.getInstance()
.findCorrectDatabaseImplementation(new JdbcConnection(conn));
Liquibase liquibase = new Liquibase(changeLogFile, resourceAccessor, database);
liquibase.update(contexts, new LabelExpression());
conn.commit();
log.info("Finished migration");
if (conn.getAutoCommit() != isAutoCommit) {
conn.setAutoCommit(isAutoCommit);
}
} catch (LiquibaseException e) {
throw new SQLException(e);
}
}
@Override
public byte[] calculateChecksum() throws IOException {
try {
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.update(("context=" + liquibaseContexts).getBytes(StandardCharsets.UTF_8));
String extra = getExtraVersion();
if (extra != null) {
crypt.update(("extra=" + extra).getBytes(StandardCharsets.UTF_8));
}
Liquibase liquibase = new Liquibase(changeLogFile, resourceAccessor, (Database) null);
runInScope(liquibase, () -> {
List<ChangeSet> changeSets = liquibase.getDatabaseChangeLog().getChangeSets();
crypt.update(("count=" + changeSets.size()).getBytes(StandardCharsets.UTF_8));
for (ChangeSet changeSet : changeSets) {
crypt.update(("author=" + changeSet.getAuthor()).getBytes(StandardCharsets.UTF_8));
crypt.update(("id=" + changeSet.getId()).getBytes(StandardCharsets.UTF_8));
crypt.update(("file=" + changeSet.getFilePath()).getBytes(StandardCharsets.UTF_8));
crypt.update(
("checksum=" + changeSet.generateCheckSum()).getBytes(StandardCharsets.UTF_8));
}
});
return crypt.digest();
} catch (LiquibaseException e) {
throw new IOException(e);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
}
}
protected String getExtraVersion() {
return null;
}
/**
* Certain public liquibase methods expect to be called from a scope, but they aren't actually
* scoped, and initiating a scope is done in a private method so this method is a copy of the
* implementation.
*/
private static void runInScope(Liquibase liquibase, Scope.ScopedRunner scopedRunner)
throws LiquibaseException {
Map<String, Object> scopeObjects = new HashMap<>();
scopeObjects.put(Scope.Attr.database.name(), liquibase.getDatabase());
scopeObjects.put(Scope.Attr.resourceAccessor.name(), liquibase.getResourceAccessor());
try {
Scope.child(scopeObjects, scopedRunner);
} catch (Exception e) {
if (e instanceof LiquibaseException) {
throw (LiquibaseException) e;
} else {
throw new LiquibaseException(e);
}
}
}
}