Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vzakharchenko committed Mar 25, 2020
1 parent 7ae238f commit 5287729
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 59 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@
package com.github.vzakharchenko.dynamic.orm.core.dynamic.structure.liquibase;

import com.github.vzakharchenko.dynamic.orm.core.dynamic.IndexData;
import com.github.vzakharchenko.dynamic.orm.core.dynamic.structure.LiquibaseHolder;
import com.github.vzakharchenko.dynamic.orm.core.helper.ModelHelper;
import com.querydsl.core.types.Path;
import liquibase.database.Database;
import liquibase.exception.DatabaseException;
import liquibase.snapshot.DatabaseSnapshot;
import liquibase.snapshot.InvalidExampleException;
import liquibase.structure.DatabaseObject;
import liquibase.structure.DatabaseObjectCollection;
import liquibase.structure.core.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.github.vzakharchenko.dynamic.orm.core.dynamic.structure.liquibase.TableFactory.*;

/**
*
*/
Expand Down Expand Up @@ -125,60 +121,10 @@ private void mergeDatabase(DatabaseObject databaseObject) {
});
}


private boolean isDeletedForeignKeys(ForeignKey foreignKey,
List<com.querydsl.sql.ForeignKey<?>> foreignKeys) {
Set<String> columns = foreignKey.getForeignKeyColumns().stream().map(Column::getName)
.collect(Collectors.toSet());
return CollectionUtils.isNotEmpty(foreignKeys) &&
foreignKeys.stream().anyMatch(foreignKey0 ->
CollectionUtils.isEqualCollection(columns,
foreignKey0.getLocalColumns().stream()
.map((Function<Path<?>, String>)
ModelHelper::getColumnRealName)
.collect(Collectors.toSet())));
}

private boolean isDeleted(DatabaseObject databaseObject, List<String> removedColumns) {
if (CollectionUtils.isNotEmpty(removedColumns)) {
return removedColumns.stream().anyMatch(s ->
removedColumns.contains(StringUtils.upperCase(
databaseObject.getAttribute(
"name", String.class))));
}
return false;
}

private boolean isDeletedTableOrigin(DatabaseObject databaseObject, Relation tableOrigin) {
if (databaseObject instanceof ForeignKey) {
List<com.querydsl.sql.ForeignKey<?>> foreignKeyList =
tableOrigin.getAttribute(DELETED_FOREIGN_KEYS, List.class);
return isDeletedForeignKeys((ForeignKey) databaseObject, foreignKeyList);
}
if (databaseObject instanceof Index) {
List<IndexData> indexList =
tableOrigin.getAttribute(DELETED_INDICES, List.class);
return isDeletedIndex((Index) databaseObject, indexList);
}
return isDeleted(databaseObject, tableOrigin
.getAttribute(DELETED_STRING_OBJECTS, List.class));
}

private boolean isDeletedIndex(Index index, List<IndexData> indexList) {
Set<String> columns = index.getColumns().stream().map(Column::getName)
.collect(Collectors.toSet());
return CollectionUtils.isNotEmpty(indexList) &&
indexList.stream().anyMatch(index0 ->
CollectionUtils.isEqualCollection(columns,
index0.getColumns().stream()
.map(ModelHelper::getColumnRealName)
.collect(Collectors.toSet())));
}

private boolean isDeleted(DatabaseObject databaseObject, Relation table) {
Relation tableOrigin = getDatabaseObjectCollection().get(table, null);
if (tableOrigin != null) {
return isDeletedTableOrigin(databaseObject, tableOrigin);
return LiquibaseHelper.isDeletedTableOrigin(databaseObject, tableOrigin);
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,23 @@
package com.github.vzakharchenko.dynamic.orm.core.dynamic.structure.liquibase;

import com.github.vzakharchenko.dynamic.orm.core.dynamic.IndexData;
import com.github.vzakharchenko.dynamic.orm.core.helper.ModelHelper;
import com.querydsl.core.types.Path;
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.Column;
import liquibase.structure.core.ForeignKey;
import liquibase.structure.core.Index;
import liquibase.structure.core.Relation;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.github.vzakharchenko.dynamic.orm.core.dynamic.structure.liquibase.TableFactory.*;

public final class LiquibaseHelper {
private LiquibaseHelper() {
Expand All @@ -27,4 +41,55 @@ private static Relation getIndexRelation(DatabaseObject databaseObject) {
private static Relation getSimpleRelation(DatabaseObject databaseObject) {
return databaseObject.getAttribute("relation", Relation.class);
}


public static boolean isDeletedTableOrigin(DatabaseObject databaseObject, Relation tableOrigin) {
if (databaseObject instanceof ForeignKey) {
List<com.querydsl.sql.ForeignKey<?>> foreignKeyList =
tableOrigin.getAttribute(DELETED_FOREIGN_KEYS, List.class);
return isDeletedForeignKeys((ForeignKey) databaseObject, foreignKeyList);
}
if (databaseObject instanceof Index) {
List<IndexData> indexList =
tableOrigin.getAttribute(DELETED_INDICES, List.class);
return isDeletedIndex((Index) databaseObject, indexList);
}
return isDeleted(databaseObject, tableOrigin
.getAttribute(DELETED_STRING_OBJECTS, List.class));
}

private static boolean isDeletedForeignKeys(ForeignKey foreignKey,
List<com.querydsl.sql.ForeignKey<?>> foreignKeys) {
Set<String> columns = foreignKey.getForeignKeyColumns().stream().map(Column::getName)
.collect(Collectors.toSet());
return CollectionUtils.isNotEmpty(foreignKeys) &&
foreignKeys.stream().anyMatch(foreignKey0 ->
CollectionUtils.isEqualCollection(columns,
foreignKey0.getLocalColumns().stream()
.map((Function<Path<?>, String>)
ModelHelper::getColumnRealName)
.collect(Collectors.toSet())));
}

private static boolean isDeleted(DatabaseObject databaseObject, List<String> removedColumns) {
if (CollectionUtils.isNotEmpty(removedColumns)) {
return removedColumns.stream().anyMatch(s ->
removedColumns.contains(StringUtils.upperCase(
databaseObject.getAttribute(
"name", String.class))));
}
return false;
}


private static boolean isDeletedIndex(Index index, List<IndexData> indexList) {
Set<String> columns = index.getColumns().stream().map(Column::getName)
.collect(Collectors.toSet());
return CollectionUtils.isNotEmpty(indexList) &&
indexList.stream().anyMatch(index0 ->
CollectionUtils.isEqualCollection(columns,
index0.getColumns().stream()
.map(ModelHelper::getColumnRealName)
.collect(Collectors.toSet())));
}
}

0 comments on commit 5287729

Please sign in to comment.