/
SchemaMaker.java
219 lines (179 loc) · 6.16 KB
/
SchemaMaker.java
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
package org.folio.rest.persist.ddlgen;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.folio.rest.tools.PomReader;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import freemarker.template.Version;
/**
* @author shale
*
*/
public class SchemaMaker {
private static Configuration cfg;
private Map<String, Object> templateInput = new HashMap<>();
private String tenant;
private String module;
private TenantOperation mode;
private String previousVersion;
private String newVersion;
private String rmbVersion;
private Schema schema;
private Schema previousSchema;
private String schemaJson = "{}";
/**
* @param onTable
*/
public SchemaMaker(String tenant, String module, TenantOperation mode, String previousVersion, String newVersion){
if(SchemaMaker.cfg == null){
//do this ONLY ONCE
SchemaMaker.cfg = new Configuration(new Version(2, 3, 26));
// Where do we load the templates from:
cfg.setClassForTemplateLoading(SchemaMaker.class, "/templates/db_scripts");
cfg.setDefaultEncoding("UTF-8");
cfg.setLocale(Locale.US);
cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
}
this.tenant = tenant;
this.module = module;
this.mode = mode;
this.previousVersion = previousVersion;
this.newVersion = newVersion;
this.rmbVersion = PomReader.INSTANCE.getRmbVersion();
}
public String generateDDL() throws IOException, TemplateException {
return generateDDL(false);
}
public String generateDDL(boolean recreateIndexMode) throws IOException, TemplateException {
templateInput.put("myuniversity", this.tenant);
templateInput.put("mymodule", this.module);
templateInput.put("mode", this.mode);
if("delete".equalsIgnoreCase(this.mode.name())){
return handleDelete();
}
if(this.schema == null){
//log this
System.out.print("Must call setSchema() first...");
return null;
}
String pVersion = this.previousVersion;
if(pVersion == null){
//will be null on deletes unless its read from db by rmb
pVersion = "0.0";
}
if(newVersion == null){
newVersion = "0.0";
}
templateInput.put("version", pVersion);
templateInput.put("newVersion", this.newVersion);
//TODO - check the rmbVersion in the internal_rmb table and compare to this passed in
//version, to check if core rmb scripts need updating due to an update
templateInput.put("rmbVersion", this.rmbVersion);
templateInput.put("schemaJson", this.getSchemaJson());
this.schema.setup();
templateInput.put("tables", tables());
templateInput.put("views", this.schema.getViews());
templateInput.put("scripts", this.schema.getScripts());
templateInput.put("exactCount", this.schema.getExactCount()+"");
String template = "main.ftl";
if(recreateIndexMode){
template = "indexes_only.ftl";
}
Template tableTemplate = cfg.getTemplate(template);
Writer writer = new StringWriter();
tableTemplate.process(templateInput, writer);
return writer.toString();
}
private String handleDelete() throws IOException, TemplateException {
Writer writer = new StringWriter();
Template tableTemplate = cfg.getTemplate("delete.ftl");
tableTemplate.process(templateInput, writer);
return writer.toString();
}
/**
* @return the tables of schema plus tables to delete (tables that exist in previousSchema but not in schema);
* add foreign keys to delete (those that exist in previousSchema but not in schema).
* Nothing to do for indexes because rmb_internal_index handles them.
*/
List<Table> tables() {
if (previousSchema == null || previousSchema.getTables() == null) {
return schema.getTables();
}
Map<String,Table> tableForName = new HashMap<>();
schema.getTables().forEach(table -> tableForName.put(table.getTableName(), table));
List<Table> list = new ArrayList<>(schema.getTables());
previousSchema.getTables().forEach(oldTable -> {
Table newTable = tableForName.get(oldTable.getTableName());
if (newTable == null) {
oldTable.setMode("delete");
oldTable.setup();
list.add(oldTable);
return;
}
List<ForeignKeys> oldForeignKeys = oldTable.getForeignKeys();
if (oldForeignKeys == null || oldForeignKeys.isEmpty()) {
return;
}
List<ForeignKeys> newForeignKeys =
newTable.getForeignKeys() == null ? Collections.emptyList() : newTable.getForeignKeys();
List<ForeignKeys> allForeignKeys = new ArrayList<>(newForeignKeys);
oldForeignKeys.forEach(oldForeignKey -> {
if (newForeignKeys.stream().anyMatch(newForeignKey ->
oldForeignKey.getFieldName().equals(newForeignKey.getFieldName()))) {
// an entry for oldForeignKey exists in newForeignKeys, nothing to do
return;
}
oldForeignKey.settOps(TableOperation.DELETE);
oldForeignKey.setup();
allForeignKeys.add(oldForeignKey);
});
newTable.setForeignKeys(allForeignKeys);
});
return list;
}
public String getTenant() {
return tenant;
}
public void setTenant(String tenant) {
this.tenant = tenant;
}
public String getModule() {
return module;
}
public void setModule(String module) {
this.module = module;
}
public TenantOperation getMode() {
return mode;
}
public void setMode(TenantOperation mode) {
this.mode = mode;
}
public Schema getSchema() {
return schema;
}
public void setSchema(Schema schema) {
this.schema = schema;
}
public Schema getPreviousSchema() {
return previousSchema;
}
public void setPreviousSchema(Schema previousSchema) {
this.previousSchema = previousSchema;
}
public String getSchemaJson() {
return schemaJson;
}
public void setSchemaJson(String schemaJson) {
this.schemaJson = schemaJson;
}
}