Skip to content
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

Generated indexes are not including "desc" #956

Merged
merged 4 commits into from Jan 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -15,10 +15,7 @@
import liquibase.structure.core.PrimaryKey;
import liquibase.structure.core.Table;
import liquibase.structure.core.UniqueConstraint;
import liquibase.util.ISODateFormat;
import liquibase.util.ObjectUtil;
import liquibase.util.StringUtils;
import liquibase.util.NowAndTodayUtil;
import liquibase.util.*;

import java.math.BigInteger;
import java.text.NumberFormat;
Expand Down Expand Up @@ -71,8 +68,8 @@ public class ColumnConfig extends AbstractLiquibaseSerializable {
*/
public ColumnConfig(Column columnSnapshot) {
setName(columnSnapshot.getName());
setComputed(((columnSnapshot.getComputed() != null) && columnSnapshot.getComputed()) ? Boolean.TRUE : null);
setDescending(((columnSnapshot.getDescending() != null) && columnSnapshot.getDescending()) ? Boolean.TRUE : null);
setComputed(BooleanUtils.isTrue(columnSnapshot.getComputed()) ? Boolean.TRUE : null);
setDescending(BooleanUtils.isTrue(columnSnapshot.getDescending()) ? Boolean.TRUE : null);
if (columnSnapshot.getType() != null) {
setType(columnSnapshot.getType().toString());
}
Expand Down
5 changes: 3 additions & 2 deletions liquibase-core/src/main/java/liquibase/diff/DiffResult.java
Expand Up @@ -9,6 +9,7 @@
import liquibase.structure.core.Catalog;
import liquibase.structure.core.Column;
import liquibase.structure.core.Schema;
import liquibase.util.BooleanUtils;

import java.io.IOException;
import java.util.*;
Expand Down Expand Up @@ -95,7 +96,7 @@ public <T extends DatabaseObject> T getMissingObject(T example, CompareControl.S
}

public void addMissingObject(DatabaseObject obj) {
if ((obj instanceof Column) && (((Column) obj).getComputed() != null) && ((Column) obj).getComputed()) {
if ((obj instanceof Column) && (BooleanUtils.isTrue(((Column) obj).getComputed()) || BooleanUtils.isTrue(((Column) obj).getDescending()))) {
return; //not really missing, it's a virtual column
}
missingObjects.add(obj);
Expand Down Expand Up @@ -197,4 +198,4 @@ public boolean areEqual() throws DatabaseException, IOException {
public Set<Class<? extends DatabaseObject>> getComparedTypes() {
return compareControl.getComparedTypes();
}
}
}
Expand Up @@ -35,6 +35,9 @@ public String[] hash(DatabaseObject databaseObject, Database accordingTo, Databa
if (BooleanUtils.isTrue(column.getComputed())) {
hash += ":computed";
}
if (BooleanUtils.isTrue(column.getDescending())) {
hash += ":descending";
}
return new String[] {hash.toLowerCase(Locale.US)};
}

Expand All @@ -60,6 +63,10 @@ public boolean isSameObject(DatabaseObject databaseObject1, DatabaseObject datab
return false;
}

if (BooleanUtils.isTrue(thisColumn.getDescending()) != BooleanUtils.isTrue(otherColumn.getDescending())) {
return false;
}

return true;
}

Expand Down
Expand Up @@ -11,6 +11,7 @@
import liquibase.diff.output.changelog.UnexpectedObjectChangeGenerator;
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.*;
import liquibase.util.BooleanUtils;

public class UnexpectedColumnChangeGenerator extends AbstractChangeGenerator implements UnexpectedObjectChangeGenerator {
@Override
Expand Down Expand Up @@ -42,7 +43,7 @@ public Change[] fixUnexpected(DatabaseObject unexpectedObject, DiffOutputControl
// continue;
// }

if ((column.getComputed() != null) && column.getComputed()) { //not really a column to drop, probably part of an index or something
if (BooleanUtils.isTrue(column.getComputed()) || BooleanUtils.isTrue(column.getDescending()) ) { //not really a column to drop, probably part of an index or something
return null;
}
if (column.getRelation() instanceof View) {
Expand Down
Expand Up @@ -12,6 +12,8 @@
import liquibase.structure.DatabaseObject;
import liquibase.structure.DatabaseObjectCollection;
import liquibase.structure.DatabaseObjectComparator;
import liquibase.structure.core.Column;
import liquibase.util.BooleanUtils;
import liquibase.util.ISODateFormat;
import liquibase.util.StringUtils;
import org.yaml.snakeyaml.nodes.Node;
Expand Down Expand Up @@ -53,7 +55,10 @@ public void write(DatabaseSnapshot snapshot, OutputStream out) throws IOExceptio
@Override
protected Object toMap(final LiquibaseSerializable object) {
if (object instanceof DatabaseObject) {
if (alreadySerializingObject) {
if (object instanceof Column && (BooleanUtils.isTrue(((Column) object).getDescending()) || BooleanUtils.isTrue(((Column) object).getComputed()))) {
//not really a "real" column that has a snapshot to reference, just serialize it
return super.toMap(object);
} else if (alreadySerializingObject) {
String snapshotId = ((DatabaseObject) object).getSnapshotId();
if (snapshotId == null) {
String name = ((DatabaseObject) object).getName();
Expand Down
Expand Up @@ -20,6 +20,7 @@
import liquibase.structure.DatabaseObject;
import liquibase.structure.DatabaseObjectCollection;
import liquibase.structure.core.*;
import liquibase.util.BooleanUtils;
import liquibase.util.ISODateFormat;
import liquibase.util.ObjectUtil;
import liquibase.util.StringUtils;
Expand Down Expand Up @@ -354,8 +355,9 @@ private void includeNestedObjects(DatabaseObject object) throws DatabaseExceptio
if ("columns".equals(field) && ((object.getClass() == PrimaryKey.class) || (object.getClass() == Index
.class) || (object.getClass() == UniqueConstraint.class))) {
if ((fieldValue != null) && !((Collection) fieldValue).isEmpty()) {
String columnName = ((Column) ((Collection) fieldValue).iterator().next()).getName();
if (columnName.endsWith(" ASC") || columnName.endsWith(" DESC") || columnName.endsWith(" RANDOM")) {
final Column column = (Column) ((Collection) fieldValue).iterator().next();
String columnName = column.getName();
if (BooleanUtils.isTrue(column.getDescending()) || columnName.endsWith(" ASC") || columnName.endsWith(" DESC") || columnName.endsWith(" RANDOM")) {
continue;
}
}
Expand Down
Expand Up @@ -21,6 +21,7 @@
import liquibase.statement.core.RawSqlStatement;
import liquibase.structure.DatabaseObject;
import liquibase.structure.core.*;
import liquibase.util.BooleanUtils;
import liquibase.util.SqlUtil;
import liquibase.util.StringUtils;

Expand Down Expand Up @@ -51,15 +52,12 @@ public ColumnSnapshotGenerator() {

@Override
protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot snapshot) throws DatabaseException {
if ((((Column) example).getComputed() != null) && ((Column) example).getComputed()) {
if (BooleanUtils.isTrue(((Column) example).getComputed()) || BooleanUtils.isTrue(((Column) example).getDescending())) {
return example;
}
Database database = snapshot.getDatabase();

Relation relation = ((Column) example).getRelation();
if (((Column) example).getComputed() != null && ((Column) example).getComputed()) {
return example;
}

Schema schema = relation.getSchema();
try {
Expand Down
Expand Up @@ -11,11 +11,15 @@
public class ColumnSnapshotGeneratorH2 extends ColumnSnapshotGenerator {
@Override
public int getPriority(Class<? extends DatabaseObject> objectType, Database database) {
if (Column.class.isAssignableFrom(objectType) && (database instanceof H2Database)) {
return PRIORITY_DATABASE;
} else {
if (!(database instanceof H2Database)) {
return PRIORITY_NONE;
}

int priority = super.getPriority(objectType, database);
if (priority == 0) {
return priority;
}
return priority + 5;
}

@Override
Expand Down
12 changes: 12 additions & 0 deletions liquibase-core/src/main/java/liquibase/structure/core/Column.java
Expand Up @@ -8,11 +8,13 @@
import liquibase.serializer.AbstractLiquibaseSerializable;
import liquibase.structure.AbstractDatabaseObject;
import liquibase.structure.DatabaseObject;
import liquibase.util.BooleanUtils;
import liquibase.util.StringUtils;

import java.math.BigInteger;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

public class Column extends AbstractDatabaseObject {

Expand Down Expand Up @@ -456,5 +458,15 @@ public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throw
this.generationType = parsedNode.getChildValue(null, "generationType", String.class);
}
}

@Override
public Set<String> getSerializableFields() {
final Set<String> fields = super.getSerializableFields();
//if this is a computed or indexed column, don't have the serializer try to traverse down to the relation since it may not be a "real" object with an objectId
if (BooleanUtils.isTrue(getDescending()) || BooleanUtils.isTrue(getComputed())) {
fields.remove("relation");
}
return fields;
}
}