/
DbCompareCommand.java
145 lines (119 loc) · 5.22 KB
/
DbCompareCommand.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
/**
* Licensed to JumpMind Inc under one or more contributor
* license agreements. See the NOTICE file distributed
* with this work for additional information regarding
* copyright ownership. JumpMind Inc licenses this file
* to you under the GNU General Public License, version 3.0 (GPLv3)
* (the "License"); you may not use this file except in compliance
* with the License.
*
* You should have received a copy of the GNU General Public License,
* version 3.0 (GPLv3) along with this library; if not, see
* <http://www.gnu.org/licenses/>.
*
* 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.jumpmind.symmetric;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Arrays;
import java.util.Map;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.jumpmind.db.model.Table;
import org.jumpmind.symmetric.common.ParameterConstants;
import org.jumpmind.symmetric.io.DbCompare;
import org.jumpmind.symmetric.io.DbCompareReport;
import org.jumpmind.symmetric.io.DbCompareReport.TableReport;
import org.jumpmind.symmetric.io.data.DbFill;
import org.jumpmind.symmetric.service.IParameterService;
public class DbCompareCommand extends AbstractCommandLauncher {
public DbCompareCommand() {
super("dbcompare", "[tablename...]", "DbCompare.Option.");
}
@Override
protected boolean printHelpIfNoOptionsAreProvided() {
return false;
}
@Override
protected boolean requiresPropertiesFile() {
return false;
}
@Override
protected boolean executeWithOptions(CommandLine line) throws Exception {
String source = line.getOptionValue('s');
if (source == null) {
source = line.getOptionValue(OPTION_SOURCE);
}
if (StringUtils.isEmpty(source)) {
throw new ParseException(String.format("-source properties file is required."));
}
File sourceProperies = new File(source);
if (!sourceProperies.exists()) {
throw new SymmetricException("The source properties file '{}' does not exist.", sourceProperies);
}
String target = line.getOptionValue('t');
if (target == null) {
target = line.getOptionValue(OPTION_TARGET);
}
if (StringUtils.isEmpty(target)) {
throw new ParseException(String.format("-target properties file is required."));
}
File targetProperties = new File(target);
if (!targetProperties.exists()) {
throw new SymmetricException("File {} does not exist.", targetProperties);
}
ISymmetricEngine sourceEngine = new ClientSymmetricEngine(sourceProperies);
ISymmetricEngine targetEngine = new ClientSymmetricEngine(targetProperties);
DbCompare dbCompare = new DbCompare(sourceEngine, targetEngine);
if (line.hasOption(OPTION_OUTPUT_SQL)) {
FileOutputStream fos = new FileOutputStream(line.getOptionValue(OPTION_OUTPUT_SQL));
dbCompare.setSqlDiffStream(fos);
}
if (line.hasOption(OPTION_EXCLUDE)) {
dbCompare.setExcludedTableNames(Arrays.asList(line.getOptionValue(OPTION_EXCLUDE).split(",")));
}
if (!CollectionUtils.isEmpty(line.getArgList())) {
dbCompare.setIncludedTableNames(Arrays.asList(line.getArgList().get(0).toString().split(",")));
}
DbCompareReport report = dbCompare.compare();
for (TableReport tableReport : report.getTableReports()) {
System.out.println(tableReport);
}
return false;
}
public static void main(String[] args) throws Exception {
new DbCompareCommand().execute(args);
}
protected static void initFromServerProperties() {
}
private static final String OPTION_SOURCE = "source";
private static final String OPTION_TARGET = "target";
private static final String OPTION_EXCLUDE = "exclude";
private static final String OPTION_USE_SYM_CONFIG = "use-sym-config";
private static final String OPTION_OUTPUT_SQL = "output-sql";
@Override
protected void printHelp(CommandLine cmd, Options options) {
System.out.println(app + " version " + Version.version());
System.out.println("Compare tables from 2 databases.\n");
super.printHelp(cmd, options);
}
@Override
protected void buildOptions(Options options) {
super.buildOptions(options);
addOption(options, "s", OPTION_SOURCE, true);
addOption(options, "t", OPTION_TARGET, true);
addOption(options, null, OPTION_EXCLUDE, true);
addOption(options, null, OPTION_USE_SYM_CONFIG, false);
addOption(options, null, OPTION_OUTPUT_SQL, true);
}
}