-
Notifications
You must be signed in to change notification settings - Fork 19
feat: Add more schema features #217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,13 +28,15 @@ | |
import com.google.cloud.solutions.spannerddl.parser.ASTcreate_change_stream_statement; | ||
import com.google.cloud.solutions.spannerddl.parser.ASTcreate_index_statement; | ||
import com.google.cloud.solutions.spannerddl.parser.ASTcreate_or_replace_statement; | ||
import com.google.cloud.solutions.spannerddl.parser.ASTcreate_locality_group_statement; | ||
import com.google.cloud.solutions.spannerddl.parser.ASTcreate_schema_statement; | ||
import com.google.cloud.solutions.spannerddl.parser.ASTcreate_search_index_statement; | ||
import com.google.cloud.solutions.spannerddl.parser.ASTcreate_table_statement; | ||
import com.google.cloud.solutions.spannerddl.parser.ASTddl_statement; | ||
import com.google.cloud.solutions.spannerddl.parser.ASTforeign_key; | ||
import com.google.cloud.solutions.spannerddl.parser.ASToptions_clause; | ||
import com.google.cloud.solutions.spannerddl.parser.ASTrow_deletion_policy_clause; | ||
import com.google.cloud.solutions.spannerddl.parser.ASTtable_interleave_clause; | ||
import com.google.cloud.solutions.spannerddl.parser.DdlParser; | ||
import com.google.cloud.solutions.spannerddl.parser.DdlParserTreeConstants; | ||
import com.google.cloud.solutions.spannerddl.parser.ParseException; | ||
|
@@ -54,6 +56,7 @@ | |
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.TreeMap; | ||
import java.util.function.Function; | ||
|
@@ -103,6 +106,8 @@ public class DdlDiff { | |
private final MapDifference<String, ASTcreate_search_index_statement> searchIndexDifferences; | ||
private final String databaseName; // for alter Database | ||
private final MapDifference<String, ASTcreate_schema_statement> schemaDifferences; | ||
private final MapDifference<String, ASTcreate_locality_group_statement> | ||
localityGroupDifferences; | ||
|
||
private DdlDiff(DatabaseDefinition originalDb, DatabaseDefinition newDb, String databaseName) | ||
throws DdlDiffException { | ||
|
@@ -122,6 +127,8 @@ private DdlDiff(DatabaseDefinition originalDb, DatabaseDefinition newDb, String | |
this.searchIndexDifferences = | ||
Maps.difference(originalDb.searchIndexes(), newDb.searchIndexes()); | ||
this.schemaDifferences = Maps.difference(originalDb.schemas(), newDb.schemas()); | ||
this.localityGroupDifferences = | ||
Maps.difference(originalDb.localityGroups(), newDb.localityGroups()); | ||
|
||
if (!alterDatabaseOptionsDifferences.areEqual() && Strings.isNullOrEmpty(databaseName)) { | ||
// should never happen, but... | ||
|
@@ -197,6 +204,15 @@ public List<String> generateDifferenceStatements(Map<String, Boolean> options) | |
} | ||
} | ||
|
||
// Drop deleted locality groups. | ||
if (options.get(ALLOW_DROP_STATEMENTS_OPT)) { | ||
for (ASTcreate_locality_group_statement lg : | ||
localityGroupDifferences.entriesOnlyOnLeft().values()) { | ||
LOG.info("Dropping deleted locality group: {}", lg.getNameOrDefault()); | ||
output.add("DROP LOCALITY GROUP " + lg.getNameOrDefault()); | ||
} | ||
} | ||
|
||
// drop deleted search indexes. | ||
if (options.get(ALLOW_DROP_STATEMENTS_OPT)) { | ||
for (String searchIndexName : searchIndexDifferences.entriesOnlyOnLeft().keySet()) { | ||
|
@@ -273,6 +289,36 @@ public List<String> generateDifferenceStatements(Map<String, Boolean> options) | |
generateAlterTableStatements(difference.leftValue(), difference.rightValue(), options)); | ||
} | ||
|
||
// update existing locality groups (options only) | ||
for (ValueDifference<ASTcreate_locality_group_statement> lgDiff : | ||
localityGroupDifferences.entriesDiffering().values()) { | ||
ASTcreate_locality_group_statement left = lgDiff.leftValue(); | ||
ASTcreate_locality_group_statement right = lgDiff.rightValue(); | ||
|
||
// Only OPTIONS diffs are supported | ||
ASToptions_clause leftOptionsClause = left.getOptionsClause(); | ||
ASToptions_clause rightOptionsClause = right.getOptionsClause(); | ||
|
||
Map<String, String> leftOptions = | ||
leftOptionsClause == null ? Collections.emptyMap() : leftOptionsClause.getKeyValueMap(); | ||
Map<String, String> rightOptions = | ||
rightOptionsClause == null ? Collections.emptyMap() : rightOptionsClause.getKeyValueMap(); | ||
MapDifference<String, String> optionsDiff = Maps.difference(leftOptions, rightOptions); | ||
|
||
String updateText = generateOptionsUpdates(optionsDiff); | ||
if (!Strings.isNullOrEmpty(updateText)) { | ||
output.add( | ||
"ALTER LOCALITY GROUP " + right.getNameOrDefault() + " SET OPTIONS (" + updateText + ")"); | ||
} | ||
} | ||
|
||
// Create new locality groups | ||
for (ASTcreate_locality_group_statement lg : | ||
localityGroupDifferences.entriesOnlyOnRight().values()) { | ||
LOG.info("Creating new locality group: {}", lg.getNameOrDefault()); | ||
output.add(lg.toStringOptionalExistClause(false)); | ||
} | ||
|
||
// create schemas | ||
for (ASTcreate_schema_statement schema : schemaDifferences.entriesOnlyOnRight().values()) { | ||
LOG.info("creating schema: {}", schema.getName()); | ||
|
@@ -456,36 +502,51 @@ static List<String> generateAlterTableStatements( | |
// - change not null on column. | ||
// note that constraints need to be dropped before columns, and created after columns. | ||
|
||
// Check interleaving has not changed. | ||
if (left.getInterleaveClause().isPresent() != right.getInterleaveClause().isPresent()) { | ||
throw new DdlDiffException("Cannot change interleaving on table " + left.getTableName()); | ||
} | ||
|
||
if (left.getInterleaveClause().isPresent() | ||
&& !left.getInterleaveClause() | ||
.get() | ||
.getParentTableName() | ||
.equals(right.getInterleaveClause().get().getParentTableName())) { | ||
throw new DdlDiffException( | ||
"Cannot change interleaved parent of table " + left.getTableName()); | ||
} | ||
|
||
// Check Key is same | ||
if (!left.getPrimaryKey().toString().equals(right.getPrimaryKey().toString())) { | ||
throw new DdlDiffException("Cannot change primary key of table " + left.getTableName()); | ||
} | ||
|
||
// On delete changed | ||
if (left.getInterleaveClause().isPresent() | ||
&& !left.getInterleaveClause() | ||
.get() | ||
.getOnDelete() | ||
.equals(right.getInterleaveClause().get().getOnDelete())) { | ||
alterStatements.add( | ||
"ALTER TABLE " | ||
+ left.getTableName() | ||
+ " SET " | ||
+ right.getInterleaveClause().get().getOnDelete()); | ||
// Interleaving changed (added, parent changed, or ON DELETE changed) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. From the documentation for ALTER TABLE T SET INTERLEAVE IN
Also:
While the tool requires the DDL to be valid (and hence "INTERLEAVE IN T ON DELETE CASCADE" is not valid DDL), adding a sanity check for this in the ASTtable_interleave_clause class would be useful. Can you add the code checks for both of this please. |
||
final Optional<ASTtable_interleave_clause> leftInterleave = left.getInterleaveClause(); | ||
final Optional<ASTtable_interleave_clause> rightInterleave = right.getInterleaveClause(); | ||
|
||
if (leftInterleave.isPresent() != rightInterleave.isPresent()) { | ||
if (rightInterleave.isPresent()) { | ||
// Added interleave | ||
ASTtable_interleave_clause ri = rightInterleave.get(); | ||
alterStatements.add( | ||
Joiner.on(" ") | ||
.skipNulls() | ||
.join( | ||
"ALTER TABLE", | ||
left.getTableName(), | ||
"SET INTERLEAVE IN", | ||
(ri.hasParentKeyword() ? "PARENT" : null), | ||
ri.getParentTableName(), | ||
ri.getOnDelete())); | ||
} else { | ||
// Removal not supported | ||
throw new DdlDiffException( | ||
"Cannot change interleaving on table " + left.getTableName()); | ||
} | ||
} else if (leftInterleave.isPresent()) { | ||
ASTtable_interleave_clause li = leftInterleave.get(); | ||
ASTtable_interleave_clause ri = rightInterleave.get(); | ||
if (!li.getParentTableName().equals(ri.getParentTableName()) | ||
|| !li.getOnDelete().equals(ri.getOnDelete()) | ||
|| li.hasParentKeyword() != ri.hasParentKeyword()) { | ||
alterStatements.add( | ||
Joiner.on(" ") | ||
.skipNulls() | ||
.join( | ||
"ALTER TABLE", | ||
left.getTableName(), | ||
"SET INTERLEAVE IN", | ||
(ri.hasParentKeyword() ? "PARENT" : null), | ||
ri.getParentTableName(), | ||
ri.getOnDelete())); | ||
} | ||
} | ||
|
||
// compare columns. | ||
|
@@ -815,6 +876,7 @@ public static List<ASTddl_statement> 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_LOCALITY_GROUP_STATEMENT: | ||
// no-op - allowed | ||
break; | ||
case DdlParserTreeConstants.JJTCREATE_OR_REPLACE_STATEMENT: | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it possible to drop the DEFAULT locality group? If not might be worth adding a check here.