Skip to content

Commit 430c4d8

Browse files
committed
CAY-2915 CayenneModeler DB Import forces meaningful PK
- treat empty string as an `exclude all`, not `include all`
1 parent c3f2ed3 commit 430c4d8

3 files changed

Lines changed: 86 additions & 8 deletions

File tree

cayenne-dbsync/src/main/java/org/apache/cayenne/dbsync/reverse/dbimport/DbImportConfiguration.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
package org.apache.cayenne.dbsync.reverse.dbimport;
2020

2121
import java.io.File;
22+
import java.util.ArrayList;
23+
import java.util.List;
2224
import java.util.regex.Pattern;
2325

2426
import org.apache.cayenne.CayenneRuntimeException;
@@ -166,18 +168,24 @@ public void setCayenneProject(File cayenneProject) {
166168

167169
public NameFilter createMeaningfulPKFilter() {
168170

169-
if (meaningfulPkTables == null) {
171+
if (meaningfulPkTables == null || meaningfulPkTables.trim().isEmpty()) {
170172
return NamePatternMatcher.EXCLUDE_ALL;
171173
}
172174

173175
// TODO: this filter can't handle table names with comma in them
174176
String[] patternStrings = meaningfulPkTables.split(",");
175-
Pattern[] patterns = new Pattern[patternStrings.length];
176-
for (int i = 0; i < patterns.length; i++) {
177-
patterns[i] = Pattern.compile(patternStrings[i]);
177+
List<Pattern> patterns = new ArrayList<>(patternStrings.length);
178+
for (String patternString : patternStrings) {
179+
if (!patternString.trim().isEmpty()) {
180+
patterns.add(Pattern.compile(patternString));
181+
}
182+
}
183+
184+
if (patterns.isEmpty()) {
185+
return NamePatternMatcher.EXCLUDE_ALL;
178186
}
179187

180-
return new NamePatternMatcher(patterns, new Pattern[0]);
188+
return new NamePatternMatcher(patterns.toArray(new Pattern[0]), new Pattern[0]);
181189
}
182190

183191
public ObjectNameGenerator createNameGenerator() {
@@ -201,7 +209,7 @@ public ObjectNameGenerator createNameGenerator() {
201209
}
202210

203211
protected DbEntityNameStemmer createStemmer() {
204-
return (stripFromTableNames == null || stripFromTableNames.length() == 0)
212+
return (stripFromTableNames == null || stripFromTableNames.isEmpty())
205213
? NoStemStemmer.getInstance()
206214
: new PatternStemmer(stripFromTableNames, false);
207215
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*****************************************************************
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
****************************************************************/
19+
20+
package org.apache.cayenne.dbsync.reverse.dbimport;
21+
22+
import org.apache.cayenne.dbsync.filter.NameFilter;
23+
import org.junit.Test;
24+
25+
import static org.junit.Assert.*;
26+
27+
public class DbImportConfigurationTest {
28+
29+
@Test
30+
public void testCreateMeaningfulPKFilter_NullExcludesAll() {
31+
DbImportConfiguration config = new DbImportConfiguration();
32+
config.setMeaningfulPkTables(null);
33+
NameFilter filter = config.createMeaningfulPKFilter();
34+
35+
assertFalse(filter.isIncluded("ARTIST"));
36+
assertFalse(filter.isIncluded("PAINTING"));
37+
assertFalse(filter.isIncluded("ANY_TABLE"));
38+
}
39+
40+
@Test
41+
public void testCreateMeaningfulPKFilter_EmptyStringExcludesAll() {
42+
DbImportConfiguration config = new DbImportConfiguration();
43+
config.setMeaningfulPkTables("");
44+
NameFilter filter = config.createMeaningfulPKFilter();
45+
46+
assertFalse(filter.isIncluded("ARTIST"));
47+
assertFalse(filter.isIncluded("ANY_TABLE"));
48+
}
49+
50+
@Test
51+
public void testCreateMeaningfulPKFilter_WhitespaceStringExcludesAll() {
52+
DbImportConfiguration config = new DbImportConfiguration();
53+
config.setMeaningfulPkTables(" ");
54+
NameFilter filter = config.createMeaningfulPKFilter();
55+
56+
assertFalse(filter.isIncluded("ARTIST"));
57+
assertFalse(filter.isIncluded("ANY_TABLE"));
58+
}
59+
60+
@Test
61+
public void testCreateMeaningfulPKFilter_CommaSeparatedPatterns() {
62+
DbImportConfiguration config = new DbImportConfiguration();
63+
config.setMeaningfulPkTables("^ART.*$,,^T1$"); // Note double comma
64+
NameFilter filter = config.createMeaningfulPKFilter();
65+
66+
assertTrue(filter.isIncluded("ARTIST"));
67+
assertTrue(filter.isIncluded("T1"));
68+
69+
assertFalse("Empty tokens should not match everything", filter.isIncluded("PAINTING"));
70+
}
71+
}

modeler/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/editor/dbimport/DbImportView.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,7 @@ public boolean isUseJava7Typed() {
250250
}
251251

252252
public String getMeaningfulPk() {
253-
return "".equals(configPanel.getMeaningfulPk().getComponent().getText())
254-
? null : configPanel.getMeaningfulPk().getComponent().getText();
253+
return configPanel.getMeaningfulPk().getComponent().getText();
255254
}
256255

257256
public String getNamingStrategy() {

0 commit comments

Comments
 (0)