Skip to content

Commit cd0ee4e

Browse files
committed
CAY-2966 "comment" field is lost when upgrading from v10 to v12
1 parent 5713180 commit cd0ee4e

6 files changed

Lines changed: 61 additions & 32 deletions

File tree

RELEASE-NOTES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ CAY-2960 Undoing renamed relationship change throws
2727
CAY-2961 PostgreSQL "text" column is reverse-engineered as CLOB
2828
CAY-2964 ClassCastException for non-generated meaningful PKs
2929
CAY-2965 MCP Cgen should not fail on an absent "<cgen>" tag
30+
CAY-2966 "comment" field is lost when upgrading from v10 to v12
3031

3132
----------------------------------
3233
Release: 5.0-M2

cayenne-project/src/main/java/org/apache/cayenne/project/upgrade/handlers/UpgradeHandler.java

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,24 +39,28 @@ public interface UpgradeHandler {
3939

4040
/**
4141
* root tag for the cgen extension
42+
*
4243
* @since 5.0-M1
4344
*/
4445
String CGEN = "cgen";
4546

4647
/**
4748
* root tag for the dbImport extension
49+
*
4850
* @since 5.0-M1
4951
*/
5052
String DB_IMPORT = "dbImport";
5153

5254
/**
5355
* root tag for the graph extension
56+
*
5457
* @since 5.0-M1
5558
*/
5659
String GRAPH = "graph";
5760

5861
/**
5962
* root tag for the validation extension
63+
*
6064
* @since 5.0-M2
6165
*/
6266
String VALIDATION = "validation";
@@ -86,71 +90,99 @@ default void processModel(DataChannelDescriptor dataChannelDescriptor) {
8690

8791
/**
8892
* Upgrade Domain schema and version info
93+
*
8994
* @param upgradeUnit for the datamap
9095
*/
9196
default void updateDomainSchemaAndVersion(UpgradeUnit upgradeUnit) {
9297
Element domain = upgradeUnit.getDocument().getDocumentElement();
9398
// update schema
94-
domain.setAttribute("xmlns","http://cayenne.apache.org/schema/"+getVersion()+"/domain");
95-
domain.setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
96-
domain.setAttribute("xsi:schemaLocation", "http://cayenne.apache.org/schema/"+getVersion()+"/domain " +
97-
"https://cayenne.apache.org/schema/"+getVersion()+"/domain.xsd");
99+
domain.setAttribute("xmlns", "http://cayenne.apache.org/schema/" + getVersion() + "/domain");
100+
domain.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
101+
domain.setAttribute("xsi:schemaLocation", "http://cayenne.apache.org/schema/" + getVersion() + "/domain " +
102+
"https://cayenne.apache.org/schema/" + getVersion() + "/domain.xsd");
98103
// update version
99104
domain.setAttribute("project-version", getVersion());
100105
}
101106

102107
/**
103108
* Upgrade DataMap schema and version info
109+
*
104110
* @param upgradeUnit for the datamap
105111
*/
106112
default void updateDataMapSchemaAndVersion(UpgradeUnit upgradeUnit) {
107113
Element dataMap = upgradeUnit.getDocument().getDocumentElement();
108114
// update schema
109-
dataMap.setAttribute("xmlns","http://cayenne.apache.org/schema/"+getVersion()+"/modelMap");
110-
dataMap.setAttribute("xsi:schemaLocation", "http://cayenne.apache.org/schema/"+getVersion()+"/modelMap " +
111-
"https://cayenne.apache.org/schema/"+getVersion()+"/modelMap.xsd");
115+
dataMap.setAttribute("xmlns", "http://cayenne.apache.org/schema/" + getVersion() + "/modelMap");
116+
dataMap.setAttribute("xsi:schemaLocation", "http://cayenne.apache.org/schema/" + getVersion() + "/modelMap " +
117+
"https://cayenne.apache.org/schema/" + getVersion() + "/modelMap.xsd");
112118
// update version
113119
dataMap.setAttribute("project-version", getVersion());
114120
}
115121

116122
/**
117123
* Update schema for the given extension in a datamap file (root element: data-map)
124+
*
118125
* @param upgradeUnit a unit to work with
119-
* @param extension name of the extension (cgen, dbImport, etc.)
126+
* @param extension name of the extension (cgen, dbImport, etc.)
120127
*/
121128
default void updateExtensionSchema(UpgradeUnit upgradeUnit, String extension) {
122129
XPath xpath = XPathFactory.newInstance().newXPath();
123130
NodeList nodes;
124131
try {
125-
nodes = (NodeList) xpath.evaluate("/data-map/*[local-name()='"+extension+"']",
132+
nodes = (NodeList) xpath.evaluate("/data-map/*[local-name()='" + extension + "']",
126133
upgradeUnit.getDocument(), XPathConstants.NODESET);
127134
} catch (XPathExpressionException e) {
128135
return;
129136
}
130137
for (int j = 0; j < nodes.getLength(); j++) {
131138
Element element = (Element) nodes.item(j);
132-
element.setAttribute("xmlns", "http://cayenne.apache.org/schema/"+getVersion()+"/"+extension.toLowerCase());
139+
element.setAttribute("xmlns", "http://cayenne.apache.org/schema/" + getVersion() + "/" + extension.toLowerCase());
133140
}
134141
}
135142

136143
/**
137144
* Update schema for the given extension in a project file (root element: domain)
145+
*
138146
* @param upgradeUnit a unit to work with
139-
* @param extension name of the extension (e.g. validation)
147+
* @param extension name of the extension (e.g. validation)
140148
* @since 5.0-M2
141149
*/
142150
default void updateDomainExtensionSchema(UpgradeUnit upgradeUnit, String extension) {
143151
XPath xpath = XPathFactory.newInstance().newXPath();
144152
NodeList nodes;
145153
try {
146-
nodes = (NodeList) xpath.evaluate("/domain/*[local-name()='"+extension+"']",
154+
nodes = (NodeList) xpath.evaluate("/domain/*[local-name()='" + extension + "']",
147155
upgradeUnit.getDocument(), XPathConstants.NODESET);
148156
} catch (XPathExpressionException e) {
149157
return;
150158
}
151159
for (int j = 0; j < nodes.getLength(); j++) {
152160
Element element = (Element) nodes.item(j);
153-
element.setAttribute("xmlns", "http://cayenne.apache.org/schema/"+getVersion()+"/"+extension.toLowerCase());
161+
element.setAttribute("xmlns", "http://cayenne.apache.org/schema/" + getVersion() + "/" + extension.toLowerCase());
162+
}
163+
}
164+
165+
/**
166+
* Update the version-stamped namespace on {@code info:property} elements
167+
* (entity/attribute comments) in a datamap file.
168+
*
169+
* @param upgradeUnit a unit to work with
170+
* @since 5.0
171+
*/
172+
default void updateInfoSchema(UpgradeUnit upgradeUnit) {
173+
XPath xpath = XPathFactory.newInstance().newXPath();
174+
NodeList nodes;
175+
try {
176+
nodes = (NodeList) xpath.evaluate("//*[local-name()='property']",
177+
upgradeUnit.getDocument(), XPathConstants.NODESET);
178+
} catch (XPathExpressionException e) {
179+
return;
180+
}
181+
for (int j = 0; j < nodes.getLength(); j++) {
182+
Element element = (Element) nodes.item(j);
183+
if (element.hasAttribute("xmlns:info")) {
184+
element.setAttribute("xmlns:info", "http://cayenne.apache.org/schema/" + getVersion() + "/info");
185+
}
154186
}
155187
}
156188
}

cayenne-project/src/main/java/org/apache/cayenne/project/upgrade/handlers/UpgradeHandler_V11.java

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public void processDataMapDom(UpgradeUnit upgradeUnit) {
7979
updateExtensionSchema(upgradeUnit, CGEN);
8080
updateExtensionSchema(upgradeUnit, DB_IMPORT);
8181
updateExtensionSchema(upgradeUnit, GRAPH);
82-
upgradeComments(upgradeUnit);
82+
updateInfoSchema(upgradeUnit);
8383

8484
dropROPProperties(upgradeUnit);
8585
dropObjEntityClientInfo(upgradeUnit);
@@ -88,24 +88,6 @@ public void processDataMapDom(UpgradeUnit upgradeUnit) {
8888
updateDbImportConfig(upgradeUnit);
8989
}
9090

91-
private void upgradeComments(UpgradeUnit upgradeUnit) {
92-
XPath xpath = XPathFactory.newInstance().newXPath();
93-
NodeList infoNodes;
94-
try {
95-
infoNodes = (NodeList) xpath.evaluate("//*[local-name()='property']",
96-
upgradeUnit.getDocument(), XPathConstants.NODESET);
97-
} catch (Exception e) {
98-
throw new RuntimeException(e);
99-
}
100-
101-
for (int j = 0; j < infoNodes.getLength(); j++) {
102-
Element infoElement = (Element) infoNodes.item(j);
103-
if (infoElement.hasAttribute("xmlns:info")) {
104-
infoElement.setAttribute("xmlns:info", "http://cayenne.apache.org/schema/11/info");
105-
}
106-
}
107-
}
108-
10991
private void dropROPProperties(UpgradeUnit upgradeUnit) {
11092
Element dataMap = upgradeUnit.getDocument().getDocumentElement();
11193
NodeList propertyNodes;

cayenne-project/src/main/java/org/apache/cayenne/project/upgrade/handlers/UpgradeHandler_V12.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public void processDataMapDom(UpgradeUnit upgradeUnit) {
6262
updateDataMapSchemaAndVersion(upgradeUnit);
6363
updateExtensionSchema(upgradeUnit, CGEN);
6464
updateExtensionSchema(upgradeUnit, DB_IMPORT);
65+
updateInfoSchema(upgradeUnit);
6566
}
6667

6768
private void removeGraphIncludes(UpgradeUnit upgradeUnit) {

cayenne-project/src/test/java/org/apache/cayenne/project/upgrade/handlers/UpgradeHandler_V12Test.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,18 @@ public void dataMapDomUpgrade() throws Exception {
113113
assertEquals(1, dbImport.getLength());
114114
assertEquals("http://cayenne.apache.org/schema/12/dbimport",
115115
((Element) dbImport.item(0)).getAttribute("xmlns"));
116+
117+
NodeList properties = (NodeList) xpath.evaluate("//*[local-name()='property']",
118+
document, XPathConstants.NODESET);
119+
int infoComments = 0;
120+
for (int i = 0; i < properties.getLength(); i++) {
121+
Element property = (Element) properties.item(i);
122+
if (property.hasAttribute("xmlns:info")) {
123+
infoComments++;
124+
assertEquals("http://cayenne.apache.org/schema/12/info", property.getAttribute("xmlns:info"));
125+
}
126+
}
127+
assertEquals(1, infoComments, "info:property comment must be preserved and namespace-bumped");
116128
}
117129

118130
@Test

cayenne-project/src/test/resources/org/apache/cayenne/project/upgrade/v12/map1.map.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<property name="defaultPackage" value="org.apache.cayenne.x"/>
77
<db-entity name="db_entity">
88
<db-attribute name="untitledAttr" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>
9+
<info:property xmlns:info="http://cayenne.apache.org/schema/11/info" name="comment" value="mixed case column names"/>
910
</db-entity>
1011
<db-entity name="db_entity1">
1112
<db-attribute name="untitledAttr" type="INTEGER" isPrimaryKey="true" isMandatory="true"/>

0 commit comments

Comments
 (0)