-
-
Notifications
You must be signed in to change notification settings - Fork 265
Expand file tree
/
Copy pathDefaultDbMigration.java
More file actions
912 lines (825 loc) · 29.9 KB
/
DefaultDbMigration.java
File metadata and controls
912 lines (825 loc) · 29.9 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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
package io.ebeaninternal.dbmigration;
import io.avaje.applog.AppLog;
import io.avaje.classpath.scanner.core.Location;
import io.ebean.DB;
import io.ebean.Database;
import io.ebean.DatabaseBuilder;
import io.ebean.annotation.Platform;
import io.ebean.config.*;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.config.dbplatform.DatabasePlatformProvider;
import io.ebean.dbmigration.DbMigration;
import io.ebean.util.IOUtils;
import io.ebean.util.StringHelper;
import io.ebeaninternal.api.DbOffline;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlOptions;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
import io.ebeaninternal.dbmigration.migration.Migration;
import io.ebeaninternal.dbmigration.migrationreader.MigrationXmlWriter;
import io.ebeaninternal.dbmigration.model.*;
import io.ebeaninternal.extraddl.model.DdlScript;
import io.ebeaninternal.extraddl.model.ExtraDdl;
import io.ebeaninternal.extraddl.model.ExtraDdlXmlReader;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.*;
import static io.ebeaninternal.api.PlatformMatch.matchPlatform;
import static java.lang.System.Logger.Level.*;
/**
* Generates DB Migration xml and sql scripts.
* <p>
* Reads the prior migrations and compares with the current model of the EbeanServer
* and generates a migration 'diff' in the form of xml document with the logical schema
* changes and a series of sql scripts to apply, rollback the applied changes if necessary
* and drop objects (drop tables, drop columns).
* </p>
* <p>
* This does not run the migration or ddl scripts but just generates them.
* </p>
* <pre>{@code
*
* DbMigration migration = DbMigration.create();
* migration.setPathToResources("src/main/resources");
* migration.setPlatform(Platform.POSTGRES);
*
* migration.generateMigration();
*
* }</pre>
*/
public class DefaultDbMigration implements DbMigration {
protected static final System.Logger logger = AppLog.getLogger("io.ebean.GenerateMigration");
private static final String initialVersion = "1.0";
private static final String GENERATED_COMMENT = "THIS IS A GENERATED FILE - DO NOT MODIFY";
private List<DatabasePlatformProvider> platformProviders = new ArrayList<>();
protected boolean online;
private boolean logToSystemOut = true;
protected SpiEbeanServer server;
protected String pathToResources = "src/main/resources";
protected String migrationPath = "dbmigration";
protected String migrationInitPath = "dbinit";
protected String modelPath = "model";
protected String modelSuffix = ".model.xml";
protected DatabasePlatform databasePlatform;
private boolean vanillaPlatform;
protected List<Pair> platforms = new ArrayList<>();
protected DatabaseBuilder.Settings databaseBuilder;
protected DbConstraintNaming constraintNaming;
@Deprecated
protected Boolean strictMode;
protected boolean includeGeneratedFileComment;
@Deprecated
protected String header;
protected String applyPrefix = "";
protected String version;
protected String name;
protected String generatePendingDrop;
private boolean addForeignKeySkipCheck;
private int lockTimeoutSeconds;
protected boolean includeBuiltInPartitioning = true;
protected boolean includeIndex;
protected boolean generate = false;
protected boolean generateInit = false;
private boolean keepLastInit = true;
/**
* Create for offline migration generation.
*/
public DefaultDbMigration() {
this.online = false;
for (DatabasePlatformProvider platformProvider : ServiceLoader.load(DatabasePlatformProvider.class)) {
platformProviders.add(platformProvider);
}
}
@Override
public void setPathToResources(String pathToResources) {
this.pathToResources = pathToResources;
}
@Override
public void setMigrationPath(String migrationPath) {
this.migrationPath = migrationPath;
}
@Override
public void setServer(Database database) {
this.server = (SpiEbeanServer) database;
setServerConfig(server.config());
}
@Override
public void setServerConfig(DatabaseBuilder builder) {
var config = builder.settings();
if (this.databaseBuilder == null) {
this.databaseBuilder = config;
}
if (constraintNaming == null) {
this.constraintNaming = databaseBuilder.getConstraintNaming();
}
if (databasePlatform == null) {
this.databasePlatform = databaseBuilder.getDatabasePlatform();
}
Properties properties = config.getProperties();
if (properties != null) {
PropertiesWrapper props = new PropertiesWrapper("ebean", config.getName(), properties, config.getClassLoadConfig());
migrationPath = props.get("migration.migrationPath", migrationPath);
migrationInitPath = props.get("migration.migrationInitPath", migrationInitPath);
pathToResources = props.get("migration.pathToResources", pathToResources);
addForeignKeySkipCheck = props.getBoolean("migration.addForeignKeySkipCheck", addForeignKeySkipCheck);
applyPrefix = props.get("migration.applyPrefix", applyPrefix);
databasePlatform = props.createInstance(DatabasePlatform.class, "migration.databasePlatform", databasePlatform);
generatePendingDrop = props.get("migration.generatePendingDrop", generatePendingDrop);
includeBuiltInPartitioning = props.getBoolean("migration.includeBuiltInPartitioning", includeBuiltInPartitioning);
includeGeneratedFileComment = props.getBoolean("migration.includeGeneratedFileComment", includeGeneratedFileComment);
includeIndex = props.getBoolean("migration.includeIndex", includeIndex);
lockTimeoutSeconds = props.getInt("migration.lockTimeoutSeconds", lockTimeoutSeconds);
logToSystemOut = props.getBoolean("migration.logToSystemOut", logToSystemOut);
modelPath = props.get("migration.modelPath", modelPath);
modelSuffix = props.get("migration.modelSuffix", modelSuffix);
name = props.get("migration.name", name);
online = props.getBoolean("migration.online", online);
vanillaPlatform = props.getBoolean("migration.vanillaPlatform", vanillaPlatform);
version = props.get("migration.version", version);
generate = props.getBoolean("migration.generate", generate);
generateInit = props.getBoolean("migration.generateInit", generateInit);
// header & strictMode must be configured at DatabaseConfig level
parsePlatforms(props, config);
}
}
protected void parsePlatforms(PropertiesWrapper props, DatabaseBuilder.Settings config) {
String platforms = props.get("migration.platforms");
if (platforms == null || platforms.isEmpty()) {
return;
}
String[] tmp = StringHelper.splitNames(platforms);
for (String plat : tmp) {
DatabasePlatform dbPlatform;
String platformName = plat;
String platformPrefix = null;
int pos = plat.indexOf('=');
if (pos != -1) {
platformName = plat.substring(0, pos);
platformPrefix = plat.substring(pos + 1);
}
if (platformName.indexOf('.') == -1) {
// parse platform as enum value
Platform platform = Enum.valueOf(Platform.class, platformName.toUpperCase());
dbPlatform = platform(platform);
} else {
// parse platform as class
dbPlatform = (DatabasePlatform) config.getClassLoadConfig().newInstance(platformName);
}
if (platformPrefix == null) {
platformPrefix = dbPlatform.platform().name().toLowerCase();
}
addDatabasePlatform(dbPlatform, platformPrefix);
}
}
@Override
public void setStrictMode(boolean strictMode) {
this.strictMode = strictMode;
}
@Override
public void setApplyPrefix(String applyPrefix) {
this.applyPrefix = applyPrefix;
}
@Override
public void setVersion(String version) {
this.version = version;
}
@Override
public void setName(String name) {
this.name = name;
}
@Override
public void setAddForeignKeySkipCheck(boolean addForeignKeySkipCheck) {
this.addForeignKeySkipCheck = addForeignKeySkipCheck;
}
@Override
public void setLockTimeout(int seconds) {
this.lockTimeoutSeconds = seconds;
}
@Override
public void setGeneratePendingDrop(String generatePendingDrop) {
this.generatePendingDrop = generatePendingDrop;
}
@Override
public void setIncludeIndex(boolean includeIndex) {
this.includeIndex = includeIndex;
}
@Override
public void setIncludeGeneratedFileComment(boolean includeGeneratedFileComment) {
this.includeGeneratedFileComment = includeGeneratedFileComment;
}
@Override
public void setIncludeBuiltInPartitioning(boolean includeBuiltInPartitioning) {
this.includeBuiltInPartitioning = includeBuiltInPartitioning;
}
@Override
public void setHeader(String header) {
this.header = header;
}
/**
* Set the specific platform to generate DDL for.
* <p>
* If not set this defaults to the platform of the default server.
* </p>
*/
@Override
public void setPlatform(Platform platform) {
vanillaPlatform = true;
setPlatform(platform(platform));
}
/**
* Set the specific platform to generate DDL for.
* <p>
* If not set this defaults to the platform of the default server.
* </p>
*/
@Override
public void setPlatform(DatabasePlatform databasePlatform) {
this.databasePlatform = databasePlatform;
if (!online) {
DbOffline.setPlatform(databasePlatform.platform());
}
}
@Override
public void addPlatform(Platform platform) {
String prefix = platform.base().name().toLowerCase();
addPlatform(platform, prefix);
}
@Override
public void addPlatform(Platform platform, String prefix) {
platforms.add(new Pair(platform(platform), prefix));
}
@Override
public void addDatabasePlatform(DatabasePlatform databasePlatform, String prefix) {
platforms.add(new Pair(databasePlatform, prefix));
}
/**
* Generate the next migration xml file and associated apply and rollback sql scripts.
* <p>
* This does not run the migration or ddl scripts but just generates them.
* </p>
* <h3>Example: Run for a single specific platform</h3>
* <pre>{@code
*
* DbMigration migration = DbMigration.create();
* migration.setPathToResources("src/main/resources");
* migration.setPlatform(Platform.ORACLE);
*
* migration.generateMigration();
*
* }</pre>
* <p>
* <h3>Example: Run migration generating DDL for multiple platforms</h3>
* <pre>{@code
*
* DbMigration migration = DbMigration.create();
* migration.setPathToResources("src/main/resources");
*
* migration.addPlatform(Platform.POSTGRES);
* migration.addPlatform(Platform.MYSQL);
* migration.addPlatform(Platform.ORACLE);
*
* migration.generateMigration();
*
* }</pre>
*
* @return the generated migration or null
*/
@Override
public String generateMigration() throws IOException {
final String version = generateMigrationFor(false);
if (includeIndex) {
generateIndex();
}
return version;
}
/**
* Generate the {@code idx_platform.migrations} file.
*/
private void generateIndex() throws IOException {
final File topDir = migrationDirectory(false);
if (!platforms.isEmpty()) {
for (Pair pair : platforms) {
new IndexMigration(topDir, pair).generate();
}
} else {
new IndexMigration(topDir, databasePlatform).generate();
}
}
@Override
public String generateInitMigration() throws IOException {
return generateMigrationFor(true);
}
private String generateMigrationFor(boolean initMigration) throws IOException {
if (!online) {
// use this flag to stop other plugins like full DDL generation
DbOffline.setGenerateMigration();
if (databasePlatform == null && !platforms.isEmpty()) {
// for multiple platform generation the first platform
// is used to generate the "logical" model diff
setPlatform(platforms.get(0).platform);
}
}
setDefaults();
if (!platforms.isEmpty()) {
configurePlatforms();
}
try {
Request request = createRequest(initMigration);
if (!initMigration) {
// repeatable migrations
if (platforms.isEmpty()) {
generateExtraDdl(request.migrationDir, databasePlatform, request.isTablePartitioning());
} else {
for (Pair pair : platforms) {
PlatformDdlWriter platformWriter = createDdlWriter(pair.platform);
File subPath = platformWriter.subPath(request.migrationDir, pair.prefix);
generateExtraDdl(subPath, pair.platform, request.isTablePartitioning());
}
}
}
String pendingVersion = generatePendingDrop();
if ("auto".equals(pendingVersion)) {
StringJoiner sj = new StringJoiner(",");
String diff = generateDiff(request);
if (diff != null) {
sj.add(diff);
request = createRequest(initMigration);
}
for (String pendingDrop : request.getPendingDrops()) {
sj.add(generatePendingDrop(request, pendingDrop));
}
return sj.length() == 0 ? null : sj.toString();
} else if (pendingVersion != null) {
return generatePendingDrop(request, pendingVersion);
} else {
return generateDiff(request);
}
} catch (UnknownResourcePathException e) {
logError("ERROR - " + e.getMessage());
logError("Check the working directory or change dbMigration.setPathToResources() value?");
return null;
} finally {
if (!online) {
DbOffline.reset();
}
}
}
/**
* Return the versions containing pending drops.
*/
@Override
public List<String> getPendingDrops() {
if (!online) {
DbOffline.setGenerateMigration();
}
setDefaults();
try {
return createRequest(false).getPendingDrops();
} finally {
if (!online) {
DbOffline.reset();
}
}
}
/**
* Load the configuration for each of the target platforms.
*/
private void configurePlatforms() {
for (Pair pair : platforms) {
PlatformConfig config = databaseBuilder.newPlatformConfig("dbmigration.platform", pair.prefix);
pair.platform.configure(config);
}
}
/**
* Generate "repeatable" migration scripts.
* <p>
* These take scrips from extra-ddl.xml (typically views) and outputs "repeatable"
* migration scripts (starting with "R__") to be run by FlywayDb or Ebean's own
* migration runner.
* </p>
*/
private void generateExtraDdl(File migrationDir, DatabasePlatform dbPlatform, boolean tablePartitioning) throws IOException {
if (dbPlatform != null) {
if (tablePartitioning && includeBuiltInPartitioning) {
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.readBuiltinTablePartitioning(), false);
}
// skip built-in migration stored procedures based on isUseMigrationStoredProcedures
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.readBuiltin(), true);
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.read(), false);
}
}
private void generateExtraDdlFor(File migrationDir, DatabasePlatform dbPlatform, ExtraDdl extraDdl, boolean checkSkip) throws IOException {
if (extraDdl != null) {
List<DdlScript> ddlScript = extraDdl.getDdlScript();
for (DdlScript script : ddlScript) {
if (!script.isDrop() && matchPlatform(dbPlatform.platform(), script.getPlatforms())) {
if (!checkSkip || dbPlatform.useMigrationStoredProcedures()) {
writeExtraDdl(migrationDir, script);
}
}
}
}
}
/**
* Write (or override) the "repeatable" migration script.
*/
private void writeExtraDdl(File migrationDir, DdlScript script) throws IOException {
String fullName = repeatableMigrationName(script.isInit(), script.getName());
logger.log(DEBUG, "writing repeatable script {0}", fullName);
File file = new File(migrationDir, fullName);
try (Writer writer = IOUtils.newWriter(file)) {
writer.write(script.getValue());
writer.flush();
}
}
@Override
public void setLogToSystemOut(boolean logToSystemOut) {
this.logToSystemOut = logToSystemOut;
}
private void logError(String message) {
if (logToSystemOut) {
System.out.println("DbMigration> " + message);
} else {
logger.log(ERROR, message);
}
}
private void logInfo(String message, Object value) {
if (value != null) {
message = String.format(message, value);
}
if (logToSystemOut) {
System.out.println("DbMigration> " + message);
} else {
logger.log(INFO, message);
}
}
private String repeatableMigrationName(boolean init, String scriptName) {
StringBuilder sb = new StringBuilder();
if (init) {
sb.append("I__");
} else {
sb.append("R__");
}
sb.append(scriptName.replace(' ', '_'));
sb.append(".sql");
return sb.toString();
}
/**
* Generate the diff migration.
*/
private String generateDiff(Request request) throws IOException {
List<String> pendingDrops = request.getPendingDrops();
if (!pendingDrops.isEmpty()) {
logInfo("Pending un-applied drops in versions %s", pendingDrops);
}
Migration migration = request.createDiffMigration();
if (migration == null) {
logInfo("no changes detected - no migration written", null);
return null;
} else {
// there were actually changes to write
return generateMigration(request, migration, null);
}
}
/**
* Generate the migration based on the pendingDrops from a prior version.
*/
private String generatePendingDrop(Request request, String pendingVersion) throws IOException {
Migration migration = request.migrationForPendingDrop(pendingVersion);
String version = generateMigration(request, migration, pendingVersion);
List<String> pendingDrops = request.getPendingDrops();
if (!pendingDrops.isEmpty()) {
logInfo("... remaining pending un-applied drops in versions %s", pendingDrops);
}
return version;
}
private Request createRequest(boolean initMigration) {
return new Request(initMigration);
}
private class Request {
final boolean initMigration;
final File migrationDir;
final File modelDir;
final CurrentModel currentModel;
final ModelContainer migrated;
final ModelContainer current;
private Request(boolean initMigration) {
this.initMigration = initMigration;
this.currentModel = new CurrentModel(server, constraintNaming);
this.current = currentModel.read();
this.migrationDir = migrationDirectory(initMigration);
if (initMigration) {
this.modelDir = null;
this.migrated = new ModelContainer();
} else {
this.modelDir = modelDirectory(migrationDir);
MigrationModel migrationModel = new MigrationModel(modelDir, modelSuffix);
this.migrated = migrationModel.read(false);
}
}
boolean isTablePartitioning() {
return current.isTablePartitioning();
}
/**
* Return the next migration version (based on existing migration versions).
*/
String nextVersion() {
// always read the next version using the main migration directory (not dbinit)
File migDirectory = migrationDirectory(false);
File modelDir = modelDirectory(migDirectory);
return LastMigration.nextVersion(migDirectory, modelDir, initMigration);
}
/**
* Return the migration for the pending drops for a given version.
*/
Migration migrationForPendingDrop(String pendingVersion) {
Migration migration = migrated.migrationForPendingDrop(pendingVersion);
// register any remaining pending drops
migrated.registerPendingHistoryDropColumns(current);
return migration;
}
/**
* Return the list of versions that have pending un-applied drops.
*/
public List<String> getPendingDrops() {
return migrated.getPendingDrops();
}
/**
* Create and return the diff of the current model to the migration model.
*/
Migration createDiffMigration() {
ModelDiff diff = new ModelDiff(migrated);
diff.compareTo(current);
return diff.isEmpty() ? null : diff.getMigration();
}
}
private String generateMigration(Request request, Migration dbMigration, String dropsFor) throws IOException {
String fullVersion = fullVersion(request.nextVersion(), dropsFor);
logInfo("generating migration:%s", fullVersion);
if (!request.initMigration && !writeMigrationXml(dbMigration, request.modelDir, fullVersion)) {
logError("migration already exists, not generating DDL");
return null;
} else {
if (!platforms.isEmpty()) {
writeExtraPlatformDdl(fullVersion, request.currentModel, dbMigration, request.migrationDir, request.initMigration && keepLastInit);
} else if (databasePlatform != null) {
// writer needs the current model to provide table/column details for
// history ddl generation (triggers, history tables etc)
DdlOptions options = new DdlOptions(addForeignKeySkipCheck);
DdlWrite writer = new DdlWrite(new MConfiguration(), request.current, options);
PlatformDdlWriter platformWriter = createDdlWriter(databasePlatform);
platformWriter.processMigration(dbMigration, writer, request.migrationDir, fullVersion);
}
return fullVersion;
}
}
/**
* Return true if the next pending drop changeSet should be generated as the next migration.
*/
private String generatePendingDrop() {
String nextDrop = System.getProperty("ddl.migration.pendingDropsFor");
if (nextDrop != null) {
return nextDrop;
}
return generatePendingDrop;
}
/**
* Return the full version for the migration being generated.
* <p>
* The full version can contain a comment suffix after a "__" double underscore.
*/
private String fullVersion(String nextVersion, String dropsFor) {
String version = version();
if (version == null) {
version = (nextVersion != null) ? nextVersion : initialVersion;
}
checkDropVersion(version, dropsFor);
String fullVersion = applyPrefix + version;
String name = name();
if (name != null) {
fullVersion += "__" + toUnderScore(name);
} else if (dropsFor != null) {
fullVersion += "__" + toUnderScore("dropsFor_" + trimDropsFor(dropsFor));
} else if (version.equals(initialVersion)) {
fullVersion += "__initial";
}
return fullVersion;
}
void checkDropVersion(String version, String dropsFor) {
if (dropsFor != null && dropsFor.equals(version)) {
throw new IllegalArgumentException("The next migration version must not be the same as the pending drops version of " +
dropsFor + ". Please make the next migration version higher than " + dropsFor + ".");
}
}
String trimDropsFor(String dropsFor) {
if (dropsFor.startsWith("V") || dropsFor.startsWith("v")) {
dropsFor = dropsFor.substring(1);
}
int commentStart = dropsFor.indexOf("__");
if (commentStart > -1) {
// trim off the trailing comment
dropsFor = dropsFor.substring(0, commentStart);
}
return dropsFor;
}
/**
* Replace spaces with underscores.
*/
private String toUnderScore(String name) {
return name.replace(' ', '_');
}
/**
* Write any extra platform ddl.
*/
private void writeExtraPlatformDdl(String fullVersion, CurrentModel currentModel, Migration dbMigration, File writePath, boolean clear) throws IOException {
DdlOptions options = new DdlOptions(addForeignKeySkipCheck);
for (Pair pair : platforms) {
DdlWrite writer = new DdlWrite(new MConfiguration(), currentModel.read(), options);
PlatformDdlWriter platformWriter = createDdlWriter(pair.platform);
File subPath = platformWriter.subPath(writePath, pair.prefix);
if (clear) {
for (File existing : subPath.listFiles()) {
existing.delete();
}
}
platformWriter.processMigration(dbMigration, writer, subPath, fullVersion);
}
}
private PlatformDdlWriter createDdlWriter(DatabasePlatform platform) {
return new PlatformDdlWriter(platform, databaseBuilder, lockTimeoutSeconds);
}
/**
* Write the migration xml.
*/
private boolean writeMigrationXml(Migration dbMigration, File resourcePath, String fullVersion) {
String modelFile = fullVersion + modelSuffix;
File file = new File(resourcePath, modelFile);
if (file.exists()) {
return false;
}
String comment = includeGeneratedFileComment ? GENERATED_COMMENT : null;
MigrationXmlWriter xmlWriter = new MigrationXmlWriter(comment);
xmlWriter.write(dbMigration, file);
return true;
}
/**
* Set default server and platform if necessary.
*/
private void setDefaults() {
if (server == null) {
setServer(DB.getDefault());
}
if (vanillaPlatform || databasePlatform == null) {
// not explicitly set so use the platform of the server
databasePlatform = server.databasePlatform();
}
if (databaseBuilder != null) {
// FIXME: StrictMode and header may be defined HERE and in DatabaseConfig.
// We shoild change either DefaultDbMigration or databaseConfig, so that it is only
// defined on one place
if (strictMode != null) {
databaseBuilder.ddlStrictMode(strictMode);
}
if (header != null) {
databaseBuilder.ddlHeader(header);
}
}
}
/**
* Return the migration version (typically FlywayDb compatible).
* <p>
* Example: 1.1.1_2
* <p>
* The version is expected to be the combination of the current pom version plus
* a 'feature' id. The combined version must be unique and ordered to work with
* FlywayDb so each developer sets a unique version so that the migration script
* generated is unique (typically just prior to being submitted as a merge request).
*/
private String version() {
String envVersion = readEnvironment("ddl.migration.version");
if (!isEmpty(envVersion)) {
return envVersion.trim();
}
return version;
}
/**
* Return the migration name which is short description text that can be appended to
* the migration version to become the ddl script file name.
* <p>
* So if the name is "a foo table" then the ddl script file could be:
* "1.1.1_2__a-foo-table.sql"
* </p>
* <p>
* When the DB migration relates to a git feature (merge request) then this description text
* is a short description of the feature.
* </p>
*/
private String name() {
String envName = readEnvironment("ddl.migration.name");
if (!isEmpty(envName)) {
return envName.trim();
}
return name;
}
/**
* Return true if the string is null or empty.
*/
private boolean isEmpty(String val) {
return val == null || val.trim().isEmpty();
}
/**
* Return the system or environment property.
*/
private String readEnvironment(String key) {
String val = System.getProperty(key);
if (val == null) {
val = System.getenv(key);
}
return val;
}
@Override
public File migrationDirectory() {
return migrationDirectory(false);
}
/**
* Return the file path to write the xml and sql to.
*/
File migrationDirectory(boolean initMigration) {
Location resourcePath = migrationPath(initMigration);
// expect to be a path to something like - src/main/resources/dbmigration
File path;
if (resourcePath.isClassPath()) {
// path to src/main/resources in typical maven project
File resourceRootDir = new File(pathToResources);
if (!resourceRootDir.exists()) {
String msg = String.format("Error - path to resources %s does not exist. Absolute path is %s", pathToResources, resourceRootDir.getAbsolutePath());
throw new UnknownResourcePathException(msg);
}
path = new File(resourceRootDir, resourcePath.path());
} else {
path = new File(resourcePath.path());
}
if (!path.exists()) {
if (!path.mkdirs()) {
logInfo("Warning - Unable to ensure migration directory exists at %s", path.getAbsolutePath());
}
}
return path;
}
private Location migrationPath(boolean initMigration) {
// remove classpath: or filesystem: prefix
return new Location(initMigration ? migrationInitPath : migrationPath);
}
/**
* Return the model directory (relative to the migration directory).
*/
private File modelDirectory(File migrationDirectory) {
if (modelPath == null || modelPath.isEmpty()) {
return migrationDirectory;
}
File modelDir = new File(migrationDirectory, modelPath);
if (!modelDir.exists() && !modelDir.mkdirs()) {
logInfo("Warning - Unable to ensure migration model directory exists at %s", modelDir.getAbsolutePath());
}
return modelDir;
}
/**
* Return the DatabasePlatform given the platform key.
*/
protected DatabasePlatform platform(Platform platform) {
switch (platform) {
case SQLSERVER:
throw new IllegalArgumentException("Please choose the more specific SQLSERVER16 or SQLSERVER17 platform. Refer to issue #1340 for details");
case DB2:
logger.log(WARNING, "Using DB2LegacyPlatform. It is recommended to migrate to db2luw/db2zos/db2fori. Refer to issue #2514 for details");
case GENERIC:
return new DatabasePlatform();
default:
for (DatabasePlatformProvider platformProvider : platformProviders) {
if (platformProvider.matchPlatform(platform)) {
return platformProvider.create(platform);
}
}
throw new IllegalArgumentException("Platform missing? " + platform);
}
}
/**
* Holds a platform and prefix. Used to generate multiple platform specific DDL
* for a single migration.
*/
static class Pair {
/**
* The platform to generate the DDL for.
*/
final DatabasePlatform platform;
/**
* A prefix included into the file/resource names indicating the platform.
*/
final String prefix;
Pair(DatabasePlatform platform, String prefix) {
this.platform = platform;
this.prefix = prefix;
}
}
}