-
Notifications
You must be signed in to change notification settings - Fork 20
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
0cc0280
d5ed186
2fffb7a
0923148
dc5a4fd
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 |
|---|---|---|
|
|
@@ -27,14 +27,17 @@ | |
| import com.google.cloud.solutions.spannerddl.parser.ASTcolumn_type; | ||
| 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_locality_group_statement; | ||
| import com.google.cloud.solutions.spannerddl.parser.ASTcreate_or_replace_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.ASTon_delete_clause; | ||
| 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 +57,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 +107,7 @@ 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,37 @@ 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( | ||
| String.format( | ||
| "ALTER LOCALITY GROUP %s SET OPTIONS (%s)", right.getNameOrDefault(), 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 +503,62 @@ 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) | ||
|
Collaborator
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( | ||
| buildSetInterleaveStatement( | ||
| left.getTableName(), | ||
| ri.getParentTableName(), | ||
| ri.hasParentKeyword(), | ||
| 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()) | ||
| || !Objects.equals(li.getOnDelete(), ri.getOnDelete()) | ||
| || li.hasParentKeyword() != ri.hasParentKeyword()) { | ||
| if (!li.hasParentKeyword() | ||
| && ri.hasParentKeyword() | ||
| && Objects.equals(ri.getParentTableName(), li.getParentTableName()) | ||
| && Objects.equals(ri.getOnDelete(), ASTon_delete_clause.ON_DELETE_CASCADE)) { | ||
| // Migration from INTERLEAVE IN -> INTERLEAVE IN PARENT ... ON DELETE CASCADE must | ||
| // be performed in two statements. | ||
| alterStatements.add( | ||
| buildSetInterleaveStatement( | ||
| left.getTableName(), | ||
| ri.getParentTableName(), | ||
| true, | ||
| ASTon_delete_clause.ON_DELETE_NO_ACTION)); | ||
| alterStatements.add( | ||
| buildSetInterleaveStatement( | ||
| left.getTableName(), | ||
| ri.getParentTableName(), | ||
| true, | ||
| ASTon_delete_clause.ON_DELETE_CASCADE)); | ||
| } else { | ||
| alterStatements.add( | ||
| buildSetInterleaveStatement( | ||
| left.getTableName(), | ||
| ri.getParentTableName(), | ||
| ri.hasParentKeyword(), | ||
| ri.getOnDelete())); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // compare columns. | ||
|
|
@@ -511,6 +584,19 @@ static List<String> generateAlterTableStatements( | |
| return alterStatements; | ||
| } | ||
|
|
||
| private static String buildSetInterleaveStatement( | ||
| String tableName, String parentTableName, boolean includeParentKeyword, String onDeleteClause) { | ||
| return Joiner.on(" ") | ||
| .skipNulls() | ||
| .join( | ||
| "ALTER TABLE", | ||
| tableName, | ||
| "SET INTERLEAVE IN", | ||
| (includeParentKeyword ? "PARENT" : null), | ||
| parentTableName, | ||
| onDeleteClause); | ||
| } | ||
|
|
||
| private static void addColumnDiffs( | ||
| String tableName, List<String> alterStatements, ValueDifference<ASTcolumn_def> columnDiff) | ||
| throws DdlDiffException { | ||
|
|
@@ -815,6 +901,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: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Copyright 2024 Google LLC | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * 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 com.google.cloud.solutions.spannerddl.parser; | ||
|
|
||
| import static com.google.cloud.solutions.spannerddl.diff.AstTreeUtils.getChildByType; | ||
| import static com.google.cloud.solutions.spannerddl.diff.AstTreeUtils.getOptionalChildByType; | ||
|
|
||
| import com.google.cloud.solutions.spannerddl.diff.AstTreeUtils; | ||
| import com.google.common.base.Joiner; | ||
| import com.google.common.collect.ImmutableSet; | ||
|
|
||
| public class ASTcreate_locality_group_statement extends SimpleNode { | ||
| public ASTcreate_locality_group_statement(int id) { | ||
| super(id); | ||
| } | ||
|
|
||
| public ASTcreate_locality_group_statement(DdlParser p, int id) { | ||
| super(p, id); | ||
| } | ||
|
|
||
| private void validateChildren() { | ||
| AstTreeUtils.validateChildrenClasses( | ||
| children, | ||
| ImmutableSet.of( | ||
| ASTif_not_exists.class, ASTdefaultt.class, ASTname.class, ASToptions_clause.class)); | ||
| } | ||
|
|
||
| public boolean isDefault() { | ||
| return getOptionalChildByType(children, ASTdefaultt.class) != null; | ||
| } | ||
|
|
||
| public String getNameOrDefault() { | ||
| return isDefault() ? "DEFAULT" : getChildByType(children, ASTname.class).toString(); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return toStringOptionalExistClause(true); | ||
| } | ||
|
|
||
| /** Create string version, optionally including the IF NOT EXISTS clause */ | ||
| public String toStringOptionalExistClause(boolean includeExists) { | ||
| validateChildren(); | ||
| return Joiner.on(" ") | ||
| .skipNulls() | ||
| .join( | ||
| "CREATE", | ||
| "LOCALITY", | ||
| "GROUP", | ||
| (includeExists ? getOptionalChildByType(children, ASTif_not_exists.class) : null), | ||
| getNameOrDefault(), | ||
| AstTreeUtils.getOptionalChildByType(children, ASToptions_clause.class)); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| return (obj instanceof ASTcreate_locality_group_statement) && toString().equals(obj.toString()); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return toString().hashCode(); | ||
| } | ||
|
|
||
| public ASToptions_clause getOptionsClause() { | ||
| return getOptionalChildByType(children, ASToptions_clause.class); | ||
| } | ||
| } |
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.