[feat](Nereids): drop foreign key after dropping primary key that is referenced by the foreign key (#30417)
This commit is contained in:
@ -45,9 +45,11 @@ import java.io.IOException;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public interface TableIf {
|
||||
Logger LOG = LogManager.getLogger(TableIf.class);
|
||||
@ -341,8 +343,12 @@ public interface TableIf {
|
||||
writeLock();
|
||||
try {
|
||||
Map<String, Constraint> constraintMap = getConstraintsMapUnsafe();
|
||||
constraintMap.entrySet().removeIf(e -> e.getValue() instanceof ForeignKeyConstraint
|
||||
&& ((ForeignKeyConstraint) e.getValue()).isReferringPK(table, constraint));
|
||||
Set<String> fkName = constraintMap.entrySet().stream()
|
||||
.filter(e -> e.getValue() instanceof ForeignKeyConstraint
|
||||
&& ((ForeignKeyConstraint) e.getValue()).isReferringPK(table, constraint))
|
||||
.map(Entry::getKey)
|
||||
.collect(Collectors.toSet());
|
||||
fkName.forEach(constraintMap::remove);
|
||||
} finally {
|
||||
writeUnlock();
|
||||
}
|
||||
|
||||
@ -58,6 +58,10 @@ public class ForeignKeyConstraint extends Constraint {
|
||||
return foreignToReference.keySet();
|
||||
}
|
||||
|
||||
public Set<String> getPrimaryKeyNames() {
|
||||
return ImmutableSet.copyOf(foreignToReference.values());
|
||||
}
|
||||
|
||||
public Set<String> getReferencedColumnNames() {
|
||||
return ImmutableSet.copyOf(foreignToReference.values());
|
||||
}
|
||||
@ -87,7 +91,7 @@ public class ForeignKeyConstraint extends Constraint {
|
||||
}
|
||||
|
||||
public Boolean isReferringPK(TableIf table, PrimaryKeyConstraint constraint) {
|
||||
return constraint.getPrimaryKeyNames().equals(getForeignKeyNames())
|
||||
return constraint.getPrimaryKeyNames().equals(getPrimaryKeyNames())
|
||||
&& getReferencedTable().equals(table);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user