-
Notifications
You must be signed in to change notification settings - Fork 477
/
CTLEventsMigration.java
174 lines (147 loc) · 7.9 KB
/
CTLEventsMigration.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
/*
* Copyright 2014-2016 CyberVision, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.kaaproject.kaa.server.datamigration;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.kaaproject.kaa.server.datamigration.model.EventClass;
import org.kaaproject.kaa.server.datamigration.model.EventSchemaVersion;
import org.kaaproject.kaa.server.datamigration.model.Schema;
import org.kaaproject.kaa.server.datamigration.utils.BaseSchemaIdCounter;
import org.kaaproject.kaa.server.datamigration.utils.datadefinition.Constraint;
import org.kaaproject.kaa.server.datamigration.utils.datadefinition.ReferenceOptions;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class CTLEventsMigration extends AbstractCTLMigration {
private static final String EVENT_SCHEMA_VERSION_TABLE_NAME = "event_schems_versions";
private static final String EVENT_CLASS_FAMILY_TABLE_NAME = "events_class_family";
private static final String EVENT_CLASS_FAMILY_VERSION_TABLE_NAME = "events_class_family_versions";
private static final String EVENT_CLASS_TABLE_NAME = "events_class";
private static final String BASE_SCHEMA_TABLE_NAME = "base_schems";
private static final String APPLICATION_EVENT_MAP_TABLE_NAME = "application_event_map";
public CTLEventsMigration(Connection connection) {
super(connection);
}
//actually not needed here
@Override
protected String getPrefixTableName() {
return null;
}
@Override
public void beforeTransform() throws SQLException {
dd.dropUnnamedFK(EVENT_CLASS_TABLE_NAME, EVENT_CLASS_FAMILY_TABLE_NAME);
dd.dropUnnamedFK(APPLICATION_EVENT_MAP_TABLE_NAME, EVENT_CLASS_TABLE_NAME);
runner.update(connection, "ALTER TABLE " + BASE_SCHEMA_TABLE_NAME + " CHANGE application_id application_id bigint(20)");
}
@Override
public void afterTransform() throws SQLException {
dd.alterTable(EVENT_CLASS_TABLE_NAME)
.add(Constraint.constraint("FK_events_class_family_versions_id")
.foreignKey("events_class_family_versions_id")
.references("events_class_family_versions", "id")
.onDelete(ReferenceOptions.CASCADE)
.onUpdate(ReferenceOptions.CASCADE))
.execute();
dd.alterTable(APPLICATION_EVENT_MAP_TABLE_NAME)
.add(Constraint.constraint("FK_events_class_id")
.foreignKey("events_class_id")
.references("events_class", "id")
.onDelete(ReferenceOptions.CASCADE))
.execute();
}
@Override
protected List<Schema> transform() throws SQLException {
// fetch schemas of appropriate feature like configuration
List<Schema> schemas = new ArrayList<>();
List<EventSchemaVersion> oldESVs = runner.query(connection,
"SELECT id, schems, created_time, created_username FROM " + EVENT_SCHEMA_VERSION_TABLE_NAME,
new BeanListHandler<EventSchemaVersion>(EventSchemaVersion.class));
List<EventClass> oldECs = runner.query(connection, "SELECT id, schems, version FROM " + EVENT_CLASS_TABLE_NAME +
" WHERE schems not like '{\"type\":\"enum\"%'", new BeanListHandler<EventClass>(EventClass.class));
runner.update(connection, "ALTER TABLE " + EVENT_SCHEMA_VERSION_TABLE_NAME + " DROP COLUMN schems");
runner.update(connection, "ALTER TABLE " + EVENT_SCHEMA_VERSION_TABLE_NAME + " RENAME " + EVENT_CLASS_FAMILY_VERSION_TABLE_NAME);
runner.update(connection, "ALTER TABLE " + EVENT_CLASS_TABLE_NAME + " CHANGE events_class_family_id events_class_family_versions_id bigint(20)");
runner.update(connection, "ALTER TABLE " + EVENT_CLASS_TABLE_NAME + " DROP COLUMN schems");
runner.update(connection, "ALTER TABLE " + EVENT_CLASS_TABLE_NAME + " DROP COLUMN version");
for (EventClass ec : oldECs) {
updateFamilyVersionId(ec, oldESVs, runner);
}
for (EventClass ec : oldECs) {
EventSchemaVersion esv = findParent(ec, oldESVs);
Long id = ec.getId();
Long createdTime = esv.getCreatedTime();
String createUsername = esv.getCreatedUsername();
String description = null;
String name = parseName(ec.getSchems());
String schems = ec.getSchems();
Integer version = ec.getVersion();
Long applicationId = null;
String type = null; //fixme: what is type?
Schema schema = new Schema(id, version, name, description, createUsername, createdTime, applicationId, schems, type);
schemas.add(schema);
}
// shift ids in order to avoid PK constraint violation during adding record to base_schema
Long shift = runner.query(connection, "select max(id) as max_id from "+ EVENT_CLASS_TABLE_NAME, rs -> rs.next() ? rs.getLong("max_id") : null);
Long idShift = BaseSchemaIdCounter.getInstance().getAndShift(shift);
runner.update(connection, "update " + EVENT_CLASS_TABLE_NAME + " set id = id + " + idShift + " order by id desc");
runner.update(connection, "update " + APPLICATION_EVENT_MAP_TABLE_NAME + " set events_class_id = events_class_id + " + idShift + " order by id desc");
schemas.forEach(s -> s.setId(s.getId() + idShift));
return schemas;
}
private EventSchemaVersion findParent(EventClass ec, List<EventSchemaVersion> versions) {
for (EventSchemaVersion esv : versions) {
if (ecBelongToThisFamilyVersion(ec, esv)) return esv;
}
return null;
}
private void updateFamilyVersionId(EventClass ec, List<EventSchemaVersion> versions, QueryRunner runner) throws SQLException {
for (EventSchemaVersion esv : versions) {
if (ecBelongToThisFamilyVersion(ec, esv)) {
int updateCount = runner.update(this.connection,
"UPDATE " + EVENT_CLASS_TABLE_NAME + " SET events_class_family_versions_id=? WHERE id=?",
esv.getId(), ec.getId());
if (updateCount != 1) {
System.err.println("Error: failed to update event class's reference to ECFV: " + ec);
}
break;
}
}
}
private boolean ecBelongToThisFamilyVersion(EventClass ec, EventSchemaVersion esv) {
try {
JsonNode jsonNode = new ObjectMapper().readTree(ec.getSchems());
String namespace = jsonNode.get("namespace").asText();
String name = jsonNode.get("name").asText();
return esv.getSchems().contains(name) && esv.getSchems().contains(namespace);
} catch (IOException e) {
System.err.println("Failed to read EventClass schema: " + ec);
}
return false;
}
private String parseName(String body) {
try {
JsonNode jsonNode = new ObjectMapper().readTree(body);
return jsonNode.get("name").asText();
} catch (IOException e) {
System.err.println("Failed to parse name from schema: " + body);
}
return "";
}
}