Skip to content

Commit 718984a

Browse files
authored
feat: Add flag to allow ignoring proto statements (#232)
1 parent ae999ff commit 718984a

10 files changed

Lines changed: 161 additions & 69 deletions

File tree

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ foreign key relationship is
135135
which would also be dropped and recreated. Therefore this option is disabled by
136136
default, and FOREIGN KEY differences will cause the tool to fail.
137137

138+
## Note on Proto Bundles
139+
140+
This tool does not support diffing of `PROTO BUNDLE` statements. By default, the tool will fail if it encounters a `PROTO BUNDLE` statement in the DDL.
141+
If you are confident that you do not need to diff `PROTO BUNDLE` statements, you can use the `--ignoreProtoBundles` flag. When this flag is set, the tool will ignore any `PROTO BUNDLE` statements in the DDL and will not generate any `ALTER` statements for them.
142+
143+
**Warning:** This is a feature for a specific use case. Only use this flag if you are confident in your knowledge of how it will work.
144+
138145
## Unsupported Spanner DDL features
139146

140147
This tool by neccessity will lag behind the implementation of new DDL features
@@ -172,6 +179,7 @@ mvn generate-resources compile exec:java \
172179
-Dexec.args="\
173180
--allowRecreateIndexes
174181
--allowRecreateForeignKeys
182+
--ignoreProtoBundles
175183
--originalDdlFile original.ddl
176184
--newDdlFile new.ddl
177185
--outputDdlFile alter.ddl
@@ -185,6 +193,7 @@ mvn clean generate-resources compile package
185193

186194
java -jar target/spanner-ddl-diff-*-jar-with-dependencies.jar \
187195
--allowRecreateIndexes \
196+
--ignoreProtoBundles \
188197
--originalDdlFile original.ddl \
189198
--newDdlFile new.ddl \
190199
--outputDdlFile alter.ddl
@@ -334,6 +343,7 @@ generating statements to drop and recreate the constraint.
334343
--allowRecreateIndexes Allows dropping and recreating secondary
335344
Indexes to apply changes.
336345
--help Show help.
346+
--ignoreProtoBundles Ignores proto bundle definitions.
337347
--newDdlFile <FILE> File path to the new DDL definition.
338348
--originalDdlFile <FILE> File path to the original DDL definition.
339349
--outputDdlFile <FILE> File path to the output DDL to write.

src/main/java/com/google/cloud/solutions/spannerddl/diff/DatabaseDefinition.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.google.common.collect.ImmutableMap;
3636
import java.util.LinkedHashMap;
3737
import java.util.List;
38+
import java.util.Map;
3839
import java.util.Optional;
3940

4041
/**
@@ -54,7 +55,8 @@ public abstract class DatabaseDefinition {
5455
* @param statements List of parsed DDL statements
5556
* @return DatabaseDefinition instance
5657
*/
57-
public static DatabaseDefinition create(List<ASTddl_statement> statements) {
58+
public static DatabaseDefinition create(
59+
List<ASTddl_statement> statements, Map<String, Boolean> options) {
5860
// Use LinkedHashMap to preserve creation order in original DDL.
5961
LinkedHashMap<String, ASTcreate_table_statement> tablesInCreationOrder = new LinkedHashMap<>();
6062
LinkedHashMap<String, ASTcreate_index_statement> indexes = new LinkedHashMap<>();
@@ -128,6 +130,13 @@ public static DatabaseDefinition create(List<ASTddl_statement> statements) {
128130
(ASTcreate_change_stream_statement) statement);
129131
break;
130132

133+
case DdlParserTreeConstants.JJTCREATE_PROTO_BUNDLE_STATEMENT:
134+
case DdlParserTreeConstants.JJTALTER_PROTO_BUNDLE_STATEMENT:
135+
if (!options.get(DdlDiff.IGNORE_PROTO_BUNDLES_OPT)) {
136+
throw new UnsupportedOperationException("Not Implemented");
137+
}
138+
break;
139+
131140
case DdlParserTreeConstants.JJTCREATE_OR_REPLACE_STATEMENT:
132141
// can be one of several types.
133142
switch (((ASTcreate_or_replace_statement) statement).getSchemaObject().getId()) {

src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiff.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,14 @@
7171
*
7272
* <p>Example usage:
7373
*
74-
* <p>Pass the original and new DDL text to the {@link #build(String, String)} function, and call
75-
* {@link #generateDifferenceStatements(Map)} to generate the list of {@code ALTER} statements.
74+
* <p>Pass the original and new DDL text to the {@link #build(String, String, Map)} function, and
75+
* call {@link #generateDifferenceStatements(Map)} to generate the list of {@code ALTER} statements.
7676
*
7777
* <p>eg:
7878
*
7979
* <pre>
80-
* List&lt;String&gt; statements = DdlDiff.build(originalDDL, newDDL)
81-
* .generateDifferenceStatements(true, true);
80+
* List&lt;String&gt; statements = DdlDiff.build(originalDDL, newDDL,
81+
* java.util.Collections.emptyMap()).generateDifferenceStatements(java.util.Collections.emptyMap());
8282
* </pre>
8383
*
8484
* <p>or execute the {@link #main(String[]) main()} function with the {@link
@@ -93,6 +93,7 @@ public class DdlDiff {
9393
public static final String ALLOW_RECREATE_INDEXES_OPT = "allowRecreateIndexes";
9494
public static final String ALLOW_RECREATE_CONSTRAINTS_OPT = "allowRecreateConstraints";
9595
public static final String ALLOW_DROP_STATEMENTS_OPT = "allowDropStatements";
96+
public static final String IGNORE_PROTO_BUNDLES_OPT = "ignoreProtoBundles";
9697
public static final String HELP_OPT = "help";
9798

9899
private final DatabaseDefinition originalDb;
@@ -670,7 +671,8 @@ private static String generateOptionsUpdates(MapDifference<String, String> optio
670671
* @return DdlDiff instance
671672
* @throws DdlDiffException if there is an error in paring the DDL
672673
*/
673-
public static DdlDiff build(String originalDdl, String newDdl) throws DdlDiffException {
674+
public static DdlDiff build(String originalDdl, String newDdl, Map<String, Boolean> options)
675+
throws DdlDiffException {
674676
List<ASTddl_statement> originalStatements;
675677
List<ASTddl_statement> newStatements;
676678
try {
@@ -684,8 +686,8 @@ public static DdlDiff build(String originalDdl, String newDdl) throws DdlDiffExc
684686
throw new DdlDiffException("Failed parsing NEW DDL: " + e.getMessage(), e);
685687
}
686688

687-
DatabaseDefinition originalDb = DatabaseDefinition.create(originalStatements);
688-
DatabaseDefinition newDb = DatabaseDefinition.create(newStatements);
689+
DatabaseDefinition originalDb = DatabaseDefinition.create(originalStatements, options);
690+
DatabaseDefinition newDb = DatabaseDefinition.create(newStatements, options);
689691

690692
return new DdlDiff(
691693
originalDb, newDb, getDatabaseNameFromAlterDatabase(originalStatements, newStatements));
@@ -818,6 +820,8 @@ public static List<ASTddl_statement> parseDdl(String original, boolean parseAnno
818820
case DdlParserTreeConstants.JJTALTER_DATABASE_STATEMENT:
819821
case DdlParserTreeConstants.JJTCREATE_CHANGE_STREAM_STATEMENT:
820822
case DdlParserTreeConstants.JJTCREATE_SEARCH_INDEX_STATEMENT:
823+
case DdlParserTreeConstants.JJTCREATE_PROTO_BUNDLE_STATEMENT:
824+
case DdlParserTreeConstants.JJTALTER_PROTO_BUNDLE_STATEMENT:
821825
// no-op - allowed
822826
break;
823827
case DdlParserTreeConstants.JJTCREATE_OR_REPLACE_STATEMENT:
@@ -860,9 +864,9 @@ public static void main(String[] args) {
860864
String originalDdl = new String(Files.readAllBytes(options.originalDdlPath()), UTF_8);
861865
String newDdl = new String(Files.readAllBytes(options.newDdlPath()), UTF_8);
862866

863-
validateDdl(newDdl);
867+
validateDdl(newDdl, options.args());
864868

865-
DdlDiff ddlDiff = DdlDiff.build(originalDdl, newDdl);
869+
DdlDiff ddlDiff = DdlDiff.build(originalDdl, newDdl, options.args());
866870

867871
List<String> alterStatements = ddlDiff.generateDifferenceStatements(options.args());
868872

@@ -890,14 +894,14 @@ public static void main(String[] args) {
890894
* @param ddl new DDL to parse
891895
* @throws DdlDiffException if there is an error in parsing the DDL
892896
*/
893-
public static void validateDdl(String ddl) throws DdlDiffException {
897+
public static void validateDdl(String ddl, Map<String, Boolean> options) throws DdlDiffException {
894898
List<ASTddl_statement> statements;
895899
try {
896900
statements = parseDdl(Strings.nullToEmpty(ddl));
897901
} catch (DdlDiffException e) {
898902
throw new DdlDiffException("Failed parsing DDL: " + e.getMessage(), e);
899903
}
900-
DatabaseDefinition db = DatabaseDefinition.create(statements);
904+
DatabaseDefinition db = DatabaseDefinition.create(statements, options);
901905
validateReferences(db);
902906
}
903907

src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffOptions.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ static Options buildOptions() {
9292
.longOpt(DdlDiff.ALLOW_DROP_STATEMENTS_OPT)
9393
.desc("Enables output of DROP commands to delete objects not used in the new DDL file.")
9494
.build());
95+
options.addOption(
96+
Option.builder()
97+
.longOpt(DdlDiff.IGNORE_PROTO_BUNDLES_OPT)
98+
.desc("Ignores proto bundle definitions when parsing DDLs and generating diffs.")
99+
.build());
95100
options.addOption(Option.builder().longOpt(DdlDiff.HELP_OPT).desc("Show help").build());
96101
return options;
97102
}
@@ -155,7 +160,9 @@ public static DdlDiffOptions parseCommandLine(String[] args) {
155160
DdlDiff.ALLOW_DROP_STATEMENTS_OPT,
156161
commandLine.hasOption(DdlDiff.ALLOW_DROP_STATEMENTS_OPT),
157162
DdlDiff.ALLOW_RECREATE_CONSTRAINTS_OPT,
158-
commandLine.hasOption(DdlDiff.ALLOW_RECREATE_CONSTRAINTS_OPT));
163+
commandLine.hasOption(DdlDiff.ALLOW_RECREATE_CONSTRAINTS_OPT),
164+
DdlDiff.IGNORE_PROTO_BUNDLES_OPT,
165+
commandLine.hasOption(DdlDiff.IGNORE_PROTO_BUNDLES_OPT));
159166

160167
return new AutoValue_DdlDiffOptions(originalDdlPath, newDdlPath, outputDdlPath, argsMap);
161168
} catch (InvalidPathException e) {

src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTalter_proto_bundle_statement.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
public class ASTalter_proto_bundle_statement extends SimpleNode {
1919
public ASTalter_proto_bundle_statement(int id) {
2020
super(id);
21-
throw new UnsupportedOperationException("Not Implemented");
2221
}
2322

2423
public ASTalter_proto_bundle_statement(DdlParser p, int id) {
2524
super(p, id);
26-
throw new UnsupportedOperationException("Not Implemented");
2725
}
2826
}

src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_proto_bundle_statement.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
public class ASTcreate_proto_bundle_statement extends SimpleNode {
1919
public ASTcreate_proto_bundle_statement(int id) {
2020
super(id);
21-
throw new UnsupportedOperationException("Not Implemented");
2221
}
2322

2423
public ASTcreate_proto_bundle_statement(DdlParser p, int id) {
2524
super(p, id);
26-
throw new UnsupportedOperationException("Not Implemented");
2725
}
2826
}

src/test/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffFromFilesTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,11 @@ public void compareDddTextFiles() throws IOException {
6262
: Collections.emptyList();
6363

6464
LOG.info("Processing segment (with drops): " + segmentName);
65-
DdlDiff ddlDiff = DdlDiff.build(originalSegment.getValue(), newSegment.getValue());
65+
DdlDiff ddlDiff =
66+
DdlDiff.build(
67+
originalSegment.getValue(),
68+
newSegment.getValue(),
69+
ImmutableMap.of(DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false));
6670
// Run diff with allowRecreateIndexes and allowDropStatements
6771
List<String> diff =
6872
ddlDiff.generateDifferenceStatements(

0 commit comments

Comments
 (0)