Skip to content

Commit 3ced0d8

Browse files
committed
updated core to 11.1.0, code clean up
1 parent 1b22f78 commit 3ced0d8

File tree

4 files changed

+44
-72
lines changed

4 files changed

+44
-72
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
<dependency>
4848
<groupId>org.pgcodekeeper</groupId>
4949
<artifactId>pgcodekeeper-core</artifactId>
50-
<version>11.0.0</version>
50+
<version>11.1.0</version>
5151
</dependency>
5252
<dependency>
5353
<groupId>ch.qos.logback</groupId>

src/main/java/org/pgcodekeeper/cli/CliArgs.java

Lines changed: 12 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,7 @@
1515
*******************************************************************************/
1616
package org.pgcodekeeper.cli;
1717

18-
import java.io.ByteArrayOutputStream;
19-
import java.io.OutputStreamWriter;
20-
import java.io.PrintWriter;
21-
import java.nio.charset.Charset;
22-
import java.nio.charset.StandardCharsets;
23-
import java.util.ArrayList;
24-
import java.util.Collection;
25-
import java.util.Collections;
26-
import java.util.List;
27-
28-
import org.kohsuke.args4j.Argument;
29-
import org.kohsuke.args4j.CmdLineException;
30-
import org.kohsuke.args4j.CmdLineParser;
31-
import org.kohsuke.args4j.Option;
32-
import org.kohsuke.args4j.OptionHandlerRegistry;
33-
import org.kohsuke.args4j.ParserProperties;
34-
18+
import org.kohsuke.args4j.*;
3519
import org.pgcodekeeper.cli.localizations.CliArgsLocalizationsBundle;
3620
import org.pgcodekeeper.cli.localizations.Messages;
3721
import org.pgcodekeeper.cli.opthandlers.BooleanNoDefOptionHandler;
@@ -45,6 +29,16 @@
4529
import org.pgcodekeeper.core.model.difftree.DbObjType;
4630
import org.pgcodekeeper.core.settings.ISettings;
4731

32+
import java.io.ByteArrayOutputStream;
33+
import java.io.OutputStreamWriter;
34+
import java.io.PrintWriter;
35+
import java.nio.charset.Charset;
36+
import java.nio.charset.StandardCharsets;
37+
import java.util.ArrayList;
38+
import java.util.Collection;
39+
import java.util.Collections;
40+
import java.util.List;
41+
4842
/**
4943
* Extension of {@link ISettings} with annotated CLI fields.
5044
* Override getters so that clients will access either CLI or parent fields
@@ -57,7 +51,7 @@ enum CliMode {
5751
PARSE,
5852
GRAPH,
5953
INSERT,
60-
VERIFY;
54+
VERIFY
6155
}
6256

6357
private static final String URL_START_JDBC = "jdbc:"; //$NON-NLS-1$
@@ -295,9 +289,6 @@ enum CliMode {
295289
@Option(name="--verify-rule-set", metaVar=CliArgsLocalizationsBundle.PATH, usage="verify-rule-set")
296290
private String verifyRuleSetPath;
297291

298-
private SourceFormat oldSrcFormat;
299-
private SourceFormat newSrcFormat;
300-
301292
CliMode getMode() {
302293
return mode;
303294
}
@@ -552,14 +543,6 @@ public FormatConfiguration getFormatConfiguration() {
552543
return null;
553544
}
554545

555-
public SourceFormat getNewSrcFormat() {
556-
return newSrcFormat;
557-
}
558-
559-
public SourceFormat getOldSrcFormat() {
560-
return oldSrcFormat;
561-
}
562-
563546
@Override
564547
public CliArgs copy() {
565548
var args = new CliArgs();
@@ -595,9 +578,7 @@ public CliArgs copy() {
595578
args.libSafeMode = libSafeMode;
596579
args.mode = mode;
597580
args.newSrc = newSrc;
598-
args.newSrcFormat = newSrcFormat;
599581
args.oldSrc = oldSrc;
600-
args.oldSrcFormat = oldSrcFormat;
601582
args.outCharsetName = outCharsetName;
602583
args.outputTarget = outputTarget;
603584
args.postFilePath = postFilePath;
@@ -671,14 +652,8 @@ public boolean parse(PrintWriter writer, String[] args) throws CmdLineException
671652
if (oldSrc == null || newSrc == null) {
672653
badArgs(Messages.CliArgs_error_source_dest);
673654
}
674-
oldSrcFormat = SourceFormat.parsePath(oldSrc);
675655
} else if (CliMode.PARSE == mode && projUpdate) {
676656
oldSrc = outputTarget;
677-
oldSrcFormat = SourceFormat.parsePath(oldSrc);
678-
}
679-
680-
if (CliMode.VERIFY != mode) {
681-
newSrcFormat = SourceFormat.parsePath(newSrc);
682657
}
683658

684659
return true;

src/main/java/org/pgcodekeeper/cli/PgDiffCli.java

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.pgcodekeeper.cli.exception.LibraryObjectDuplicationException;
1919
import org.pgcodekeeper.cli.localizations.Messages;
2020
import org.pgcodekeeper.core.PgCodekeeperException;
21-
import org.pgcodekeeper.core.PgDiff;
2221
import org.pgcodekeeper.core.api.DatabaseFactory;
2322
import org.pgcodekeeper.core.api.PgCodeKeeperApi;
2423
import org.pgcodekeeper.core.loader.FullAnalyze;
@@ -27,18 +26,21 @@
2726
import org.pgcodekeeper.core.schema.AbstractDatabase;
2827
import org.pgcodekeeper.core.schema.PgOverride;
2928
import org.pgcodekeeper.core.xmlstore.DependenciesXmlStore;
29+
import org.slf4j.Logger;
30+
import org.slf4j.LoggerFactory;
3031

3132
import java.io.IOException;
3233
import java.nio.file.Paths;
34+
import java.util.ArrayList;
3335
import java.util.Collection;
3436
import java.util.List;
3537

36-
public final class PgDiffCli extends PgDiff {
37-
38+
public final class PgDiffCli {
39+
private static final Logger LOG = LoggerFactory.getLogger(PgDiffCli.class);
40+
private final List<Object> errors = new ArrayList<>();
3841
private final CliArgs arguments;
3942

4043
public PgDiffCli(CliArgs arguments) {
41-
super(arguments);
4244
this.arguments = arguments;
4345
}
4446

@@ -47,25 +49,25 @@ public void updateProject()
4749
AbstractDatabase oldDatabase = loadOldDatabaseWithLibraries();
4850
AbstractDatabase newDatabase = loadNewDatabaseWithLibraries();
4951

50-
PgCodeKeeperApi.update(settings, oldDatabase, newDatabase,
52+
PgCodeKeeperApi.update(arguments, oldDatabase, newDatabase,
5153
arguments.getOutputTarget(), arguments.getIgnoreLists(), null);
5254
}
5355

5456
public void exportProject() throws IOException, InterruptedException, PgCodekeeperException {
55-
AbstractDatabase newDb = loadNewDatabase();
56-
PgCodeKeeperApi.export(settings, newDb, arguments.getOutputTarget(), arguments.getIgnoreLists(), null);
57+
AbstractDatabase newDb = loadDatabaseSchema(arguments.getNewSrc());
58+
PgCodeKeeperApi.export(arguments, newDb, arguments.getOutputTarget(), arguments.getIgnoreLists(), null);
5759
}
5860

5961
public String createDiff() throws InterruptedException, IOException, PgCodekeeperException {
6062
AbstractDatabase oldDatabase = loadOldDatabaseWithLibraries();
6163
AbstractDatabase newDatabase = loadNewDatabaseWithLibraries();
62-
return PgCodeKeeperApi.diff(settings, oldDatabase, newDatabase, arguments.getIgnoreLists());
64+
return PgCodeKeeperApi.diff(arguments, oldDatabase, newDatabase, arguments.getIgnoreLists());
6365
}
6466

6567
public AbstractDatabase loadNewDatabaseWithLibraries()
6668
throws IOException, InterruptedException, PgCodekeeperException {
6769
LOG.info(Messages.PgDiffCli_log_load_new_db);
68-
AbstractDatabase newDatabase = loadNewDatabase();
70+
AbstractDatabase newDatabase = loadDatabaseSchema(arguments.getNewSrc());
6971
LOG.info(Messages.PgDiffCli_log_load_new_db_lib);
7072
loadLibraries(newDatabase, arguments.getTargetLibXmls(), arguments.getTargetLibs(),
7173
arguments.getTargetLibsWithoutPriv());
@@ -78,7 +80,7 @@ public AbstractDatabase loadNewDatabaseWithLibraries()
7880

7981
// read additional privileges from special folder
8082
LOG.info(Messages.PgDiffCli_log_load_new_db_overrides);
81-
loadOverrides(newDatabase, arguments.getNewSrcFormat(), arguments.getNewSrc());
83+
loadOverrides(newDatabase, arguments.getNewSrc());
8284

8385
LOG.info(Messages.PgDiffCli_log_start_db_analyze);
8486
analyzeDatabase(newDatabase);
@@ -89,7 +91,7 @@ public AbstractDatabase loadNewDatabaseWithLibraries()
8991
public AbstractDatabase loadOldDatabaseWithLibraries()
9092
throws IOException, InterruptedException, PgCodekeeperException {
9193
LOG.info(Messages.PgDiffCli_log_load_old_db);
92-
AbstractDatabase oldDatabase = loadOldDatabase();
94+
AbstractDatabase oldDatabase = loadDatabaseSchema(arguments.getOldSrc());
9395

9496
LOG.info(Messages.PgDiffCli_log_load_old_db_lib);
9597
loadLibraries(oldDatabase, arguments.getSourceLibXmls(), arguments.getSourceLibs(),
@@ -102,7 +104,7 @@ public AbstractDatabase loadOldDatabaseWithLibraries()
102104
}
103105
LOG.info(Messages.PgDiffCli_log_load_old_db_overrides);
104106
// read additional privileges from special folder
105-
loadOverrides(oldDatabase, arguments.getOldSrcFormat(), arguments.getOldSrc());
107+
loadOverrides(oldDatabase, arguments.getOldSrc());
106108

107109
LOG.info(Messages.PgDiffCli_log_start_db_analyze);
108110
analyzeDatabase(oldDatabase);
@@ -116,53 +118,41 @@ private void analyzeDatabase(AbstractDatabase db)
116118
assertErrors();
117119
}
118120

119-
private void loadOverrides(AbstractDatabase db, SourceFormat format, String source)
121+
private void loadOverrides(AbstractDatabase db, String source)
120122
throws InterruptedException, IOException, PgCodekeeperException {
121-
if (SourceFormat.PARSED != format) {
123+
if (SourceFormat.PARSED != SourceFormat.parsePath(source)) {
122124
return;
123125
}
124126

125-
new ProjectLoader(source, settings, errors).loadOverrides(db);
127+
new ProjectLoader(source, arguments, errors).loadOverrides(db);
126128
assertErrors();
127129
}
128130

129131
private void loadLibraries(AbstractDatabase db, Collection<String> libXmls, Collection<String> libs,
130-
Collection<String> libsWithoutPriv)
132+
Collection<String> libsWithoutPrivileges)
131133
throws InterruptedException, IOException, PgCodekeeperException {
132134
LibraryLoader ll = new LibraryLoader(db, Utils.getMetaPath(), errors);
133135

134136
for (String xml : libXmls) {
135-
ll.loadXml(new DependenciesXmlStore(Paths.get(xml)), settings);
137+
ll.loadXml(new DependenciesXmlStore(Paths.get(xml)), arguments);
136138
}
137139

138-
ll.loadLibraries(settings, false, libs);
139-
ll.loadLibraries(settings, true, libsWithoutPriv);
140+
ll.loadLibraries(arguments, false, libs);
141+
ll.loadLibraries(arguments, true, libsWithoutPrivileges);
140142
assertErrors();
141143
}
142144

143-
private AbstractDatabase loadNewDatabase()
144-
throws IOException, InterruptedException, PgCodekeeperException {
145-
return loadDatabaseSchema(arguments.getNewSrcFormat(), arguments.getNewSrc());
146-
}
147-
148-
private AbstractDatabase loadOldDatabase()
149-
throws IOException, InterruptedException, PgCodekeeperException {
150-
return loadDatabaseSchema(arguments.getOldSrcFormat(), arguments.getOldSrc());
151-
}
152-
153145
/**
154146
* Loads database schema choosing the provided method.
155147
*
156-
* @param format format of the database source, must be "dump", "parsed" or
157-
* "db" otherwise exception is thrown
158148
* @param srcPath path to the database source to load
159149
*
160150
* @return the loaded database
161151
*/
162-
private AbstractDatabase loadDatabaseSchema(SourceFormat format, String srcPath)
152+
private AbstractDatabase loadDatabaseSchema(String srcPath)
163153
throws InterruptedException, IOException, PgCodekeeperException {
164-
var factory = new DatabaseFactory(settings, arguments.isIgnoreErrors(), false);
165-
return switch (format) {
154+
var factory = new DatabaseFactory(arguments, arguments.isIgnoreErrors(), false);
155+
return switch (SourceFormat.parsePath(srcPath)) {
166156
case DB -> factory.loadFromJdbc(srcPath, arguments.getIgnoreSchemaList());
167157
case DUMP -> factory.loadFromDump(srcPath);
168158
case PARSED -> factory.loadFromProject(srcPath, arguments.getIgnoreSchemaList());
@@ -174,4 +164,8 @@ private void assertErrors() throws PgCodekeeperException {
174164
throw new PgCodekeeperException(Messages.PgDiffCli_error_while_load_database);
175165
}
176166
}
167+
168+
public List<Object> getErrors() {
169+
return errors;
170+
}
177171
}

src/test/java/org/pgcodekeeper/cli/TestUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public static String readResource(Class<?> clazz, String fileName) throws IOExce
4242

4343
public static Path getPathToResource(Class<?> clazz, String fileName) throws URISyntaxException {
4444
URL url = clazz.getResource(fileName);
45+
if (url == null) {
46+
throw new IllegalArgumentException("File not found %s".formatted(fileName));
47+
}
4548
return Paths.get(url.toURI());
4649
}
4750

0 commit comments

Comments
 (0)