diff --git a/README.md b/README.md index d52e98b..1f0115b 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,13 @@ foreign key relationship is which would also be dropped and recreated. Therefore this option is disabled by default, and FOREIGN KEY differences will cause the tool to fail. +## Note on Proto Bundles + +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. +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. + +**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. + ## Unsupported Spanner DDL features This tool by neccessity will lag behind the implementation of new DDL features @@ -172,6 +179,7 @@ mvn generate-resources compile exec:java \ -Dexec.args="\ --allowRecreateIndexes --allowRecreateForeignKeys + --ignoreProtoBundles --originalDdlFile original.ddl --newDdlFile new.ddl --outputDdlFile alter.ddl @@ -185,6 +193,7 @@ mvn clean generate-resources compile package java -jar target/spanner-ddl-diff-*-jar-with-dependencies.jar \ --allowRecreateIndexes \ + --ignoreProtoBundles \ --originalDdlFile original.ddl \ --newDdlFile new.ddl \ --outputDdlFile alter.ddl @@ -334,6 +343,7 @@ generating statements to drop and recreate the constraint. --allowRecreateIndexes Allows dropping and recreating secondary Indexes to apply changes. --help Show help. + --ignoreProtoBundles Ignores proto bundle definitions. --newDdlFile File path to the new DDL definition. --originalDdlFile File path to the original DDL definition. --outputDdlFile File path to the output DDL to write. diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/diff/DatabaseDefinition.java b/src/main/java/com/google/cloud/solutions/spannerddl/diff/DatabaseDefinition.java index c80fdcd..cd97e55 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/diff/DatabaseDefinition.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/diff/DatabaseDefinition.java @@ -35,6 +35,7 @@ import com.google.common.collect.ImmutableMap; import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import java.util.Optional; /** @@ -54,7 +55,8 @@ public abstract class DatabaseDefinition { * @param statements List of parsed DDL statements * @return DatabaseDefinition instance */ - public static DatabaseDefinition create(List statements) { + public static DatabaseDefinition create( + List statements, Map options) { // Use LinkedHashMap to preserve creation order in original DDL. LinkedHashMap tablesInCreationOrder = new LinkedHashMap<>(); LinkedHashMap indexes = new LinkedHashMap<>(); @@ -128,6 +130,13 @@ public static DatabaseDefinition create(List statements) { (ASTcreate_change_stream_statement) statement); break; + case DdlParserTreeConstants.JJTCREATE_PROTO_BUNDLE_STATEMENT: + case DdlParserTreeConstants.JJTALTER_PROTO_BUNDLE_STATEMENT: + if (!options.get(DdlDiff.IGNORE_PROTO_BUNDLES_OPT)) { + throw new UnsupportedOperationException("Not Implemented"); + } + break; + case DdlParserTreeConstants.JJTCREATE_OR_REPLACE_STATEMENT: // can be one of several types. switch (((ASTcreate_or_replace_statement) statement).getSchemaObject().getId()) { diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiff.java b/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiff.java index ab29370..e1e25c2 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiff.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiff.java @@ -71,14 +71,14 @@ * *

Example usage: * - *

Pass the original and new DDL text to the {@link #build(String, String)} function, and call - * {@link #generateDifferenceStatements(Map)} to generate the list of {@code ALTER} statements. + *

Pass the original and new DDL text to the {@link #build(String, String, Map)} function, and + * call {@link #generateDifferenceStatements(Map)} to generate the list of {@code ALTER} statements. * *

eg: * *

- * List<String> statements = DdlDiff.build(originalDDL, newDDL)
- *    .generateDifferenceStatements(true, true);
+ * List<String> statements = DdlDiff.build(originalDDL, newDDL,
+ * java.util.Collections.emptyMap()).generateDifferenceStatements(java.util.Collections.emptyMap());
  * 
* *

or execute the {@link #main(String[]) main()} function with the {@link @@ -93,6 +93,7 @@ public class DdlDiff { public static final String ALLOW_RECREATE_INDEXES_OPT = "allowRecreateIndexes"; public static final String ALLOW_RECREATE_CONSTRAINTS_OPT = "allowRecreateConstraints"; public static final String ALLOW_DROP_STATEMENTS_OPT = "allowDropStatements"; + public static final String IGNORE_PROTO_BUNDLES_OPT = "ignoreProtoBundles"; public static final String HELP_OPT = "help"; private final DatabaseDefinition originalDb; @@ -670,7 +671,8 @@ private static String generateOptionsUpdates(MapDifference optio * @return DdlDiff instance * @throws DdlDiffException if there is an error in paring the DDL */ - public static DdlDiff build(String originalDdl, String newDdl) throws DdlDiffException { + public static DdlDiff build(String originalDdl, String newDdl, Map options) + throws DdlDiffException { List originalStatements; List newStatements; try { @@ -684,8 +686,8 @@ public static DdlDiff build(String originalDdl, String newDdl) throws DdlDiffExc throw new DdlDiffException("Failed parsing NEW DDL: " + e.getMessage(), e); } - DatabaseDefinition originalDb = DatabaseDefinition.create(originalStatements); - DatabaseDefinition newDb = DatabaseDefinition.create(newStatements); + DatabaseDefinition originalDb = DatabaseDefinition.create(originalStatements, options); + DatabaseDefinition newDb = DatabaseDefinition.create(newStatements, options); return new DdlDiff( originalDb, newDb, getDatabaseNameFromAlterDatabase(originalStatements, newStatements)); @@ -818,6 +820,8 @@ public static List parseDdl(String original, boolean parseAnno case DdlParserTreeConstants.JJTALTER_DATABASE_STATEMENT: case DdlParserTreeConstants.JJTCREATE_CHANGE_STREAM_STATEMENT: case DdlParserTreeConstants.JJTCREATE_SEARCH_INDEX_STATEMENT: + case DdlParserTreeConstants.JJTCREATE_PROTO_BUNDLE_STATEMENT: + case DdlParserTreeConstants.JJTALTER_PROTO_BUNDLE_STATEMENT: // no-op - allowed break; case DdlParserTreeConstants.JJTCREATE_OR_REPLACE_STATEMENT: @@ -860,9 +864,9 @@ public static void main(String[] args) { String originalDdl = new String(Files.readAllBytes(options.originalDdlPath()), UTF_8); String newDdl = new String(Files.readAllBytes(options.newDdlPath()), UTF_8); - validateDdl(newDdl); + validateDdl(newDdl, options.args()); - DdlDiff ddlDiff = DdlDiff.build(originalDdl, newDdl); + DdlDiff ddlDiff = DdlDiff.build(originalDdl, newDdl, options.args()); List alterStatements = ddlDiff.generateDifferenceStatements(options.args()); @@ -890,14 +894,14 @@ public static void main(String[] args) { * @param ddl new DDL to parse * @throws DdlDiffException if there is an error in parsing the DDL */ - public static void validateDdl(String ddl) throws DdlDiffException { + public static void validateDdl(String ddl, Map options) throws DdlDiffException { List statements; try { statements = parseDdl(Strings.nullToEmpty(ddl)); } catch (DdlDiffException e) { throw new DdlDiffException("Failed parsing DDL: " + e.getMessage(), e); } - DatabaseDefinition db = DatabaseDefinition.create(statements); + DatabaseDefinition db = DatabaseDefinition.create(statements, options); validateReferences(db); } diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffOptions.java b/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffOptions.java index b6aa8a4..8eb570a 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffOptions.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffOptions.java @@ -92,6 +92,11 @@ static Options buildOptions() { .longOpt(DdlDiff.ALLOW_DROP_STATEMENTS_OPT) .desc("Enables output of DROP commands to delete objects not used in the new DDL file.") .build()); + options.addOption( + Option.builder() + .longOpt(DdlDiff.IGNORE_PROTO_BUNDLES_OPT) + .desc("Ignores proto bundle definitions when parsing DDLs and generating diffs.") + .build()); options.addOption(Option.builder().longOpt(DdlDiff.HELP_OPT).desc("Show help").build()); return options; } @@ -155,7 +160,9 @@ public static DdlDiffOptions parseCommandLine(String[] args) { DdlDiff.ALLOW_DROP_STATEMENTS_OPT, commandLine.hasOption(DdlDiff.ALLOW_DROP_STATEMENTS_OPT), DdlDiff.ALLOW_RECREATE_CONSTRAINTS_OPT, - commandLine.hasOption(DdlDiff.ALLOW_RECREATE_CONSTRAINTS_OPT)); + commandLine.hasOption(DdlDiff.ALLOW_RECREATE_CONSTRAINTS_OPT), + DdlDiff.IGNORE_PROTO_BUNDLES_OPT, + commandLine.hasOption(DdlDiff.IGNORE_PROTO_BUNDLES_OPT)); return new AutoValue_DdlDiffOptions(originalDdlPath, newDdlPath, outputDdlPath, argsMap); } catch (InvalidPathException e) { diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTalter_proto_bundle_statement.java b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTalter_proto_bundle_statement.java index c03bb34..177fd03 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTalter_proto_bundle_statement.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTalter_proto_bundle_statement.java @@ -18,11 +18,9 @@ public class ASTalter_proto_bundle_statement extends SimpleNode { public ASTalter_proto_bundle_statement(int id) { super(id); - throw new UnsupportedOperationException("Not Implemented"); } public ASTalter_proto_bundle_statement(DdlParser p, int id) { super(p, id); - throw new UnsupportedOperationException("Not Implemented"); } } diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_proto_bundle_statement.java b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_proto_bundle_statement.java index 91da270..6a8cffd 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_proto_bundle_statement.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/parser/ASTcreate_proto_bundle_statement.java @@ -18,11 +18,9 @@ public class ASTcreate_proto_bundle_statement extends SimpleNode { public ASTcreate_proto_bundle_statement(int id) { super(id); - throw new UnsupportedOperationException("Not Implemented"); } public ASTcreate_proto_bundle_statement(DdlParser p, int id) { super(p, id); - throw new UnsupportedOperationException("Not Implemented"); } } diff --git a/src/test/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffFromFilesTest.java b/src/test/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffFromFilesTest.java index b172c90..a016eb4 100644 --- a/src/test/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffFromFilesTest.java +++ b/src/test/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffFromFilesTest.java @@ -62,7 +62,11 @@ public void compareDddTextFiles() throws IOException { : Collections.emptyList(); LOG.info("Processing segment (with drops): " + segmentName); - DdlDiff ddlDiff = DdlDiff.build(originalSegment.getValue(), newSegment.getValue()); + DdlDiff ddlDiff = + DdlDiff.build( + originalSegment.getValue(), + newSegment.getValue(), + ImmutableMap.of(DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false)); // Run diff with allowRecreateIndexes and allowDropStatements List diff = ddlDiff.generateDifferenceStatements( diff --git a/src/test/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffTest.java b/src/test/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffTest.java index 9f255fc..4274973 100644 --- a/src/test/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffTest.java +++ b/src/test/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffTest.java @@ -23,7 +23,6 @@ import static org.junit.Assert.fail; import com.google.cloud.solutions.spannerddl.parser.ASTddl_statement; -import com.google.cloud.solutions.spannerddl.parser.ParseException; import com.google.common.collect.ImmutableMap; import java.util.Arrays; import java.util.List; @@ -32,6 +31,17 @@ public class DdlDiffTest { + private static final Map DEFAULT_OPTIONS = + ImmutableMap.of( + ALLOW_RECREATE_CONSTRAINTS_OPT, + true, + ALLOW_DROP_STATEMENTS_OPT, + true, + ALLOW_RECREATE_INDEXES_OPT, + true, + DdlDiff.IGNORE_PROTO_BUNDLES_OPT, + false); + @Test public void parseMultiDdlStatements() throws DdlDiffException { String DDL = @@ -61,7 +71,7 @@ public void parseMultiDdlStatements() throws DdlDiffException { } @Test - public void parseCreateTable_anonForeignKey() throws DdlDiffException { + public void parseCreateTable_anonForeignKey() { try { DdlDiff.parseDdl( "create table test (" @@ -70,14 +80,14 @@ public void parseCreateTable_anonForeignKey() throws DdlDiffException { + ")" + "primary key (intcol ASC)"); fail("Expected exception not thrown"); - } catch (IllegalArgumentException e) { + } catch (DdlDiffException | IllegalArgumentException e) { assertThat(e.getMessage()) .containsMatch("Can not create diffs when anonymous constraints are used."); } } @Test - public void parseCreateTable_anonCheckConstraint() throws DdlDiffException { + public void parseCreateTable_anonCheckConstraint() { try { DdlDiff.parseDdl( "create table test (" @@ -86,7 +96,7 @@ public void parseCreateTable_anonCheckConstraint() throws DdlDiffException { + ")" + "primary key (intcol ASC)"); fail("Expected exception not thrown"); - } catch (IllegalArgumentException e) { + } catch (DdlDiffException | IllegalArgumentException e) { assertThat(e.getMessage()) .containsMatch("Can not create diffs when anonymous constraints are used."); } @@ -398,7 +408,7 @@ public void generateAlterTable_alterStatementOrdering() throws DdlDiffException } @Test - public void parseAlterDatabaseDifferentDbNames() throws ParseException { + public void parseAlterDatabaseDifferentDbNames() { getDiffCheckDdlDiffException( "ALTER DATABASE dbname SET OPTIONS(hello='world');", "ALTER DATABASE otherdbname SET OPTIONS(hello='world');", @@ -445,7 +455,8 @@ public void diffCreateIndexOnlyStoring() throws DdlDiffException { assertThat( DdlDiff.build( "CREATE INDEX myindex ON mytable ( col1 ) STORING (col2, col3);", - "CREATE INDEX myindex ON mytable ( col1 ) STORING (col3, col4);") + "CREATE INDEX myindex ON mytable ( col1 ) STORING (col3, col4);", + DEFAULT_OPTIONS) .generateDifferenceStatements( ImmutableMap.of( ALLOW_RECREATE_INDEXES_OPT, @@ -453,6 +464,8 @@ public void diffCreateIndexOnlyStoring() throws DdlDiffException { ALLOW_DROP_STATEMENTS_OPT, false, ALLOW_RECREATE_CONSTRAINTS_OPT, + false, + DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false))) .containsExactly( "ALTER INDEX myindex DROP STORED COLUMN col2", @@ -464,7 +477,8 @@ public void diffCreateIndexNotOnlyStoringRecreates() throws DdlDiffException { assertThat( DdlDiff.build( "CREATE UNIQUE INDEX myindex ON mytable ( col1 ) STORING (col2, col3);", - "CREATE INDEX myindex ON mytable ( col1 ) STORING (col3, col4);") + "CREATE INDEX myindex ON mytable ( col1 ) STORING (col3, col4);", + DEFAULT_OPTIONS) .generateDifferenceStatements( ImmutableMap.of( ALLOW_RECREATE_INDEXES_OPT, @@ -472,6 +486,8 @@ public void diffCreateIndexNotOnlyStoringRecreates() throws DdlDiffException { ALLOW_DROP_STATEMENTS_OPT, false, ALLOW_RECREATE_CONSTRAINTS_OPT, + false, + DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false))) .containsExactly( "DROP INDEX myindex", @@ -479,7 +495,7 @@ public void diffCreateIndexNotOnlyStoringRecreates() throws DdlDiffException { } @Test - public void diffCreateIndexNotOnlyStoringThrows() throws DdlDiffException { + public void diffCreateIndexNotOnlyStoringThrows() { getDiffCheckDdlDiffException( "CREATE UNIQUE INDEX myindex ON mytable ( col1 ) STORING (col2, col3);", "CREATE INDEX myindex ON mytable ( col1 ) STORING (col3, col4);", @@ -499,15 +515,23 @@ private static void getDiffCheckDdlDiffException( private static List getDiff( String originalDdl, String newDdl, boolean allowDropStatements) throws DdlDiffException { - return DdlDiff.build(originalDdl, newDdl) - .generateDifferenceStatements( - ImmutableMap.of( - ALLOW_RECREATE_CONSTRAINTS_OPT, - true, - ALLOW_DROP_STATEMENTS_OPT, - allowDropStatements, - ALLOW_RECREATE_INDEXES_OPT, - false)); + return getDiff(originalDdl, newDdl, allowDropStatements, false); + } + + private static List getDiff( + String originalDdl, String newDdl, boolean allowDropStatements, boolean ignoreProtoBundles) + throws DdlDiffException { + Map options = + ImmutableMap.of( + ALLOW_RECREATE_CONSTRAINTS_OPT, + true, + ALLOW_DROP_STATEMENTS_OPT, + allowDropStatements, + ALLOW_RECREATE_INDEXES_OPT, + false, + DdlDiff.IGNORE_PROTO_BUNDLES_OPT, + ignoreProtoBundles); + return DdlDiff.build(originalDdl, newDdl, options).generateDifferenceStatements(options); } @Test @@ -516,7 +540,8 @@ public void generateDifferences_dropTables() throws DdlDiffException { DdlDiff.build( "Create table table1 (col1 int64) primary key (col1);" + "Create table table2 (col2 int64) primary key (col2);", - "Create table table1 (col1 int64) primary key (col1);"); + "Create table table1 (col1 int64) primary key (col1);", + DEFAULT_OPTIONS); assertThat(diff.generateDifferenceStatements(ImmutableMap.of(ALLOW_DROP_STATEMENTS_OPT, true))) .containsExactly("DROP TABLE table2"); @@ -530,7 +555,8 @@ public void generateDifferences_dropIndexes() throws DdlDiffException { DdlDiff.build( "Create index index1 on table1 (col1 desc);" + "Create index index2 on table2 (col2 desc);", - "Create index index1 on table1 (col1 desc);"); + "Create index index1 on table1 (col1 desc);", + DEFAULT_OPTIONS); assertThat(diff.generateDifferenceStatements(ImmutableMap.of(ALLOW_DROP_STATEMENTS_OPT, true))) .containsExactly("DROP INDEX index2"); @@ -541,11 +567,18 @@ public void generateDifferences_dropIndexes() throws DdlDiffException { @Test public void differentIndexesWithNoRecreate() { Map options = - ImmutableMap.of(ALLOW_DROP_STATEMENTS_OPT, false, ALLOW_RECREATE_INDEXES_OPT, false); + ImmutableMap.of( + ALLOW_DROP_STATEMENTS_OPT, + false, + ALLOW_RECREATE_INDEXES_OPT, + false, + DdlDiff.IGNORE_PROTO_BUNDLES_OPT, + false); try { DdlDiff.build( "Create unique null_filtered index index1 on table1 (col1 desc)", - "Create unique null_filtered index index1 on table1 (col1 asc)") + "Create unique null_filtered index index1 on table1 (col1 asc)", + options) .generateDifferenceStatements(options); fail("Expected exception not thrown"); } catch (DdlDiffException e) { @@ -554,7 +587,8 @@ public void differentIndexesWithNoRecreate() { try { DdlDiff.build( "Create unique null_filtered index index1 on table1 (col1 desc)", - "Create unique null_filtered index index1 on table1 (col2 desc)") + "Create unique null_filtered index index1 on table1 (col2 desc)", + options) .generateDifferenceStatements(options); fail("Expected exception not thrown"); } catch (DdlDiffException e) { @@ -563,7 +597,8 @@ public void differentIndexesWithNoRecreate() { try { DdlDiff.build( "Create unique null_filtered index index1 on table1 (col1 desc)", - "Create index index1 on table1 (col1 desc)") + "Create index index1 on table1 (col1 desc)", + options) .generateDifferenceStatements(options); fail("Expected exception not thrown"); } catch (DdlDiffException e) { @@ -574,11 +609,18 @@ public void differentIndexesWithNoRecreate() { @Test public void differentIndexesWithRecreate() throws DdlDiffException { Map options = - ImmutableMap.of(ALLOW_DROP_STATEMENTS_OPT, true, ALLOW_RECREATE_INDEXES_OPT, true); + ImmutableMap.of( + ALLOW_DROP_STATEMENTS_OPT, + true, + ALLOW_RECREATE_INDEXES_OPT, + true, + DdlDiff.IGNORE_PROTO_BUNDLES_OPT, + false); assertThat( DdlDiff.build( "Create unique null_filtered index index1 on table1 (col1 desc)", - "Create unique null_filtered index index1 on table1 (col1 asc)") + "Create unique null_filtered index index1 on table1 (col1 asc)", + options) .generateDifferenceStatements(options)) .isEqualTo( Arrays.asList( @@ -587,7 +629,8 @@ public void differentIndexesWithRecreate() throws DdlDiffException { assertThat( DdlDiff.build( "Create unique null_filtered index index1 on table1 ( col1 desc )", - "Create unique null_filtered index index1 on table1 ( col2 desc )") + "Create unique null_filtered index index1 on table1 ( col2 desc )", + options) .generateDifferenceStatements(options)) .isEqualTo( Arrays.asList( @@ -596,9 +639,27 @@ public void differentIndexesWithRecreate() throws DdlDiffException { assertThat( DdlDiff.build( "Create unique null_filtered index index1 on table1 ( col1 desc )", - "Create index index1 on table1 ( col1 desc )") + "Create index index1 on table1 ( col1 desc )", + options) .generateDifferenceStatements(options)) .isEqualTo( Arrays.asList("DROP INDEX index1", "CREATE INDEX index1 ON table1 ( col1 DESC )")); } + + @Test + public void ignoreProtoBundles_throwsExceptionWhenNotIgnoring() { + try { + getDiff("CREATE PROTO BUNDLE (a,b,c)", "", false, false); + fail("Expected exception not thrown"); + } catch (UnsupportedOperationException e) { + assertThat(e.getMessage()).isEqualTo("Not Implemented"); + } catch (DdlDiffException e) { + fail("Unexpected DdlDiffException: " + e); + } + } + + @Test + public void ignoreProtoBundles_ignoresWhenFlagIsSet() throws DdlDiffException { + assertThat(getDiff("", "CREATE PROTO BUNDLE (a,b,c)", false, true)).isEmpty(); + } } diff --git a/src/test/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffValidationTest.java b/src/test/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffValidationTest.java index be58a0e..8d9e737 100644 --- a/src/test/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffValidationTest.java +++ b/src/test/java/com/google/cloud/solutions/spannerddl/diff/DdlDiffValidationTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.fail; +import com.google.common.collect.ImmutableMap; import org.junit.Test; public class DdlDiffValidationTest { @@ -28,7 +29,7 @@ public void validateForeignKey_missingReferencedTable() { String ddl = "CREATE TABLE test1 ( col1 INT64, col2 STRING(100), CONSTRAINT fk_in_table FOREIGN KEY (col2) REFERENCES othertable (othercol) ) PRIMARY KEY (col1);"; try { - DdlDiff.validateDdl(ddl); + DdlDiff.validateDdl(ddl, ImmutableMap.of(DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false)); fail("Expected DdlDiffException"); } catch (DdlDiffException e) { assertThat(e.getMessage()) @@ -43,7 +44,7 @@ public void validateForeignKey_existingTable() { "CREATE TABLE test1 ( col1 INT64, col2 STRING(100), CONSTRAINT fk_in_table FOREIGN KEY (col2) REFERENCES othertable (othercol) ) PRIMARY KEY (col1);" + "CREATE table othertable ( othercol INT64, col4 STRING(100) ) PRIMARY KEY (othercol);"; try { - DdlDiff.validateDdl(ddl); + DdlDiff.validateDdl(ddl, ImmutableMap.of(DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false)); } catch (DdlDiffException e) { throw new RuntimeException(e); } @@ -55,7 +56,7 @@ public void validateForeignKey_missingReferencedColumn() { "CREATE TABLE othertable (othercol STRING(100)) PRIMARY KEY (othercol); " + "CREATE TABLE test1 ( col1 INT64, col2 STRING(100), CONSTRAINT fk_in_table FOREIGN KEY (col2) REFERENCES othertable (missing_col) ) PRIMARY KEY (col1);"; try { - DdlDiff.validateDdl(ddl); + DdlDiff.validateDdl(ddl, ImmutableMap.of(DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false)); fail("Expected DdlDiffException"); } catch (DdlDiffException e) { assertThat(e.getMessage()) @@ -70,7 +71,7 @@ public void validateForeignKey_existingReferencedColumn() { "CREATE TABLE othertable ( othercol STRING(100), existing_col INT64 ) PRIMARY KEY (othercol); " + "CREATE TABLE test1 ( col1 INT64, col2 STRING(100), CONSTRAINT fk_in_table FOREIGN KEY (col2) REFERENCES othertable (existing_col) ) PRIMARY KEY (col1);"; try { - DdlDiff.validateDdl(ddl); + DdlDiff.validateDdl(ddl, ImmutableMap.of(DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false)); } catch (DdlDiffException e) { throw new RuntimeException(e); } @@ -80,7 +81,7 @@ public void validateForeignKey_existingReferencedColumn() { public void validateIndex_missingTable() { String ddl = "CREATE INDEX myindex ON mytable (col1);"; try { - DdlDiff.validateDdl(ddl); + DdlDiff.validateDdl(ddl, ImmutableMap.of(DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false)); fail("Expected DdlDiffException"); } catch (DdlDiffException e) { assertThat(e.getMessage()) @@ -94,7 +95,7 @@ public void validateIndex_existingTable() { "CREATE TABLE mytable ( col1 INT64, col2 STRING(100) ) PRIMARY KEY (col1); " + "CREATE INDEX myindex ON mytable (col1);"; try { - DdlDiff.validateDdl(ddl); + DdlDiff.validateDdl(ddl, ImmutableMap.of(DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false)); } catch (DdlDiffException e) { throw new RuntimeException(e); } @@ -105,7 +106,7 @@ public void validateIndex_missingColumn() { String ddl = "CREATE TABLE mytable (col1 INT64) PRIMARY KEY (col1); CREATE INDEX myindex ON mytable (missing_col);"; try { - DdlDiff.validateDdl(ddl); + DdlDiff.validateDdl(ddl, ImmutableMap.of(DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false)); fail("Expected DdlDiffException"); } catch (DdlDiffException e) { assertThat(e.getMessage()) @@ -119,7 +120,7 @@ public void validateIndex_existingColumn() { String ddl = "CREATE TABLE mytable (col1 INT64, existing_col INT64 ) PRIMARY KEY (col1); CREATE INDEX myindex ON mytable (existing_col);"; try { - DdlDiff.validateDdl(ddl); + DdlDiff.validateDdl(ddl, ImmutableMap.of(DdlDiff.IGNORE_PROTO_BUNDLES_OPT, false)); } catch (DdlDiffException e) { throw new RuntimeException(e); } diff --git a/src/test/resources/ddlParserUnsupported.txt b/src/test/resources/ddlParserUnsupported.txt index d1bf7af..265378c 100644 --- a/src/test/resources/ddlParserUnsupported.txt +++ b/src/test/resources/ddlParserUnsupported.txt @@ -139,21 +139,21 @@ ALTER TABLE table_name DROP SYNONYM table_name ALTER TABLE table_name ADD SYNONYM table_name -== Test 17a // TODO Create proto bundle - -CREATE PROTO BUNDLE (proto.path.name, other.proto.path) - -== Test 17b Alter proto bundle INSERT - -ALTER PROTO BUNDLE INSERT (proto.path.name, other.proto.path) - -== Test 17c Alter proto bundle UPDATE - -ALTER PROTO BUNDLE UPDATE (proto.path.name, other.proto.path) - -== Test 17d Alter proto bundle DELETE - -ALTER PROTO BUNDLE DELETE (proto.path.name, other.proto.path) +# == Test 17a // TODO Create proto bundle. Commented out to allow user to ignore proto bundles when parsing +# +# CREATE PROTO BUNDLE (proto.path.name, other.proto.path) +# +# == Test 17b Alter proto bundle INSERT +# +# ALTER PROTO BUNDLE INSERT (proto.path.name, other.proto.path) +# +# == Test 17c Alter proto bundle UPDATE +# +# ALTER PROTO BUNDLE UPDATE (proto.path.name, other.proto.path) +# +# == Test 17d Alter proto bundle DELETE +# +# ALTER PROTO BUNDLE DELETE (proto.path.name, other.proto.path) == Test 18a alter search index