forked from ebean-orm/ebean
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDdlPlugin.java
More file actions
371 lines (326 loc) · 11.3 KB
/
DdlPlugin.java
File metadata and controls
371 lines (326 loc) · 11.3 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package io.ebeaninternal.dbmigration;
import io.ebean.annotation.Platform;
import io.ebean.config.DatabaseConfig;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.ddlrunner.DdlRunner;
import io.ebean.ddlrunner.ScriptTransform;
import io.ebean.plugin.Plugin;
import io.ebean.plugin.SpiServer;
import io.ebean.util.IOUtils;
import io.ebean.util.JdbcClose;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.dbmigration.model.CurrentModel;
import io.ebeaninternal.dbmigration.model.MTable;
import io.ebeaninternal.extraddl.model.ExtraDdlXmlReader;
import io.ebeaninternal.server.deploy.PartitionMeta;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.persistence.PersistenceException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.SQLException;
/**
* Controls the generation and execution of "Create All" and "Drop All" DDL scripts.
* <p>
* Typically the "Create All" DDL is executed for running tests etc and has nothing to do
* with DB Migration (diff based) DDL.
*/
public class DdlPlugin implements Plugin {
private static final Logger log = LoggerFactory.getLogger(DdlPlugin.class);
private static final String[] BUILD_DIRS = {"target", "build"};
private SpiEbeanServer server;
private boolean generateDdl;
private boolean runDdl;
private boolean extraDdl;
private boolean createOnly;
private boolean jaxbPresent;
private boolean ddlAutoCommit;
private String dbSchema;
private ScriptTransform scriptTransform;
private Platform platform;
private String platformName;
private boolean useMigrationStoredProcedures;
private CurrentModel currentModel;
private String dropAllContent;
private String createAllContent;
private File baseDir;
@Override
public void configure(SpiServer server) {
this.server = (SpiEbeanServer) server;
final DatabaseConfig config = server.config();
this.generateDdl = config.isDdlGenerate();
this.runDdl = config.isDdlRun() && !config.isDocStoreOnly();
if (generateDdl || runDdl) {
this.jaxbPresent = Detect.isJAXBPresent(config);
this.extraDdl = config.isDdlExtra();
this.createOnly = config.isDdlCreateOnly();
this.dbSchema = config.getDbSchema();
final DatabasePlatform databasePlatform = server.databasePlatform();
this.platform = databasePlatform.getPlatform();
this.platformName = platform.base().name();
this.ddlAutoCommit = databasePlatform.isDdlAutoCommit();
this.useMigrationStoredProcedures = config.getDatabasePlatform().isUseMigrationStoredProcedures();
this.scriptTransform = createScriptTransform(config);
this.baseDir = initBaseDir();
}
}
/**
* Generate the DDL drop and create scripts if the properties have been set.
*/
@Override
public void online(boolean online) {
if (generateDdl) {
if (!createOnly) {
writeDrop(getDropFileName());
}
writeCreate(getCreateFileName());
}
}
/**
* Run the DDL drop and DDL create scripts if properties have been set.
*/
@Override
public void start() {
if (runDdl) {
Connection connection = null;
try {
connection = obtainConnection();
runDdlWith(connection);
} finally {
JdbcClose.rollback(connection);
JdbcClose.close(connection);
}
}
}
@Override
public void shutdown() {}
private File initBaseDir() {
for (String buildDir : BUILD_DIRS) {
File dir = new File(buildDir);
if (dir.exists() && dir.isDirectory()) {
return dir;
}
}
return new File(".");
}
private void runDdlWith(Connection connection) {
try {
if (dbSchema != null) {
createSchemaIfRequired(connection);
}
runInitSql(connection);
runDropSql(connection);
runCreateSql(connection);
runSeedSql(connection);
} catch (IOException e) {
throw new RuntimeException("Error reading drop/create script from file system", e);
}
}
private Connection obtainConnection() {
try {
return server.dataSource().getConnection();
} catch (SQLException e) {
throw new PersistenceException("Failed to obtain connection to run DDL", e);
}
}
private void createSchemaIfRequired(Connection connection) {
try {
for (String schema : dbSchema.split(",")) {
server.databasePlatform().createSchemaIfNotExists(schema, connection);
}
} catch (SQLException e) {
throw new PersistenceException("Failed to create DB Schema", e);
}
}
/**
* Execute all the DDL statements in the script.
*/
void runScript(Connection connection, boolean expectErrors, String content, String scriptName) {
DdlRunner runner = createDdlRunner(expectErrors, scriptName);
try {
if (expectErrors || ddlAutoCommit) {
connection.setAutoCommit(true);
}
runner.runAll(scriptTransform.transform(content), connection);
if (expectErrors || ddlAutoCommit) {
connection.setAutoCommit(false);
}
connection.commit();
runner.runNonTransactional(connection);
} catch (SQLException e) {
throw new PersistenceException("Failed to run script", e);
} finally {
JdbcClose.rollback(connection);
}
}
private DdlRunner createDdlRunner(boolean expectErrors, String scriptName) {
return new DdlRunner(expectErrors, scriptName, platformName);
}
protected void runDropSql(Connection connection) throws IOException {
if (!createOnly) {
if (extraDdl && jaxbPresent && useMigrationStoredProcedures) {
String extraApply = ExtraDdlXmlReader.buildExtra(platform, true);
if (extraApply != null) {
runScript(connection, false, extraApply, "extra-ddl");
}
}
if (dropAllContent == null) {
dropAllContent = readFile(getDropFileName());
}
runScript(connection, true, dropAllContent, getDropFileName());
}
}
protected void runCreateSql(Connection connection) throws IOException {
if (createAllContent == null) {
createAllContent = readFile(getCreateFileName());
}
runScript(connection, false, createAllContent, getCreateFileName());
if (extraDdl && jaxbPresent) {
if (currentModel().isTablePartitioning()) {
String extraPartitioning = ExtraDdlXmlReader.buildPartitioning(platform);
if (extraPartitioning != null && !extraPartitioning.isEmpty() && useMigrationStoredProcedures) {
runScript(connection, false, extraPartitioning, "builtin-partitioning-ddl");
}
}
String extraApply = ExtraDdlXmlReader.buildExtra(platform, false);
if (extraApply != null) {
runScript(connection, false, extraApply, "extra-ddl");
}
if (currentModel().isTablePartitioning()) {
checkInitialTablePartitions(connection);
}
}
}
/**
* Check if table partitions exist and if not create some. The expectation is that
* extra-ddl.xml should have some partition initialisation but this helps people get going.
*/
private void checkInitialTablePartitions(Connection connection) {
DatabasePlatform databasePlatform = server.databasePlatform();
try {
StringBuilder sb = new StringBuilder();
for (MTable table : currentModel.getPartitionedTables()) {
String tableName = table.getName();
if (!databasePlatform.tablePartitionsExist(connection, tableName)) {
log.info("No table partitions for table {}", tableName);
PartitionMeta meta = table.getPartitionMeta();
String initPart = databasePlatform.tablePartitionInit(tableName, meta.getMode(), meta.getProperty(), table.singlePrimaryKey());
sb.append(initPart).append("\n");
}
}
String initialPartitionSql = sb.toString();
if (!initialPartitionSql.isEmpty()) {
runScript(connection, false, initialPartitionSql, "initial table partitions");
}
} catch (SQLException e) {
log.error("Error checking initial table partitions", e);
}
}
protected void runInitSql(Connection connection) throws IOException {
runResourceScript(connection, server.config().getDdlInitSql());
}
protected void runSeedSql(Connection connection) throws IOException {
runResourceScript(connection, server.config().getDdlSeedSql());
}
protected void runResourceScript(Connection connection, String sqlScript) throws IOException {
if (sqlScript != null) {
try (InputStream is = getClassLoader().getResourceAsStream(sqlScript)) {
if (is == null) {
log.warn("sql script {} was not found as a resource", sqlScript);
} else {
String content = readContent(IOUtils.newReader(is)); // 'is' is closed
runScript(connection, false, content, sqlScript);
}
}
}
}
/**
* Return the classLoader to use to read sql scripts as resources.
*/
protected ClassLoader getClassLoader() {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
if (cl == null) {
cl = this.getClassLoader();
}
return cl;
}
protected void writeDrop(String dropFile) {
try {
writeFile(dropFile, generateDropAllDdl());
} catch (IOException e) {
throw new PersistenceException("Error generating Drop DDL", e);
}
}
protected void writeCreate(String createFile) {
try {
writeFile(createFile, generateCreateAllDdl());
} catch (IOException e) {
throw new PersistenceException("Error generating Create DDL", e);
}
}
protected String generateDropAllDdl() {
try {
dropAllContent = currentModel().getDropAllDdl();
return dropAllContent;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
protected String generateCreateAllDdl() {
try {
createAllContent = currentModel().getCreateDdl();
return createAllContent;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
protected String getDropFileName() {
return server.name() + "-drop-all.sql";
}
protected String getCreateFileName() {
return server.name() + "-create-all.sql";
}
protected CurrentModel currentModel() {
if (currentModel == null) {
currentModel = new CurrentModel(server);
}
return currentModel;
}
protected void writeFile(String fileName, String fileContent) throws IOException {
File f = new File(baseDir, fileName);
try (Writer fw = IOUtils.newWriter(f)) {
fw.write(fileContent);
fw.flush();
}
}
protected String readFile(String fileName) throws IOException {
File f = new File(baseDir, fileName);
if (!f.exists()) {
return null;
}
try (Reader reader = IOUtils.newReader(f)) {
return readContent(reader);
}
}
protected String readContent(Reader reader) throws IOException {
StringBuilder buf = new StringBuilder();
try (LineNumberReader lineReader = new LineNumberReader(reader)) {
String s;
while ((s = lineReader.readLine()) != null) {
buf.append(s).append("\n");
}
return buf.toString();
}
}
/**
* Create the ScriptTransform for placeholder key/value replacement.
*/
private ScriptTransform createScriptTransform(DatabaseConfig config) {
return ScriptTransform.build(config.getDdlPlaceholders(), config.getDdlPlaceholderMap());
}
}