Search in sources :

Example 11 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class DiffToChangeLog method sortObjects.

private List<DatabaseObject> sortObjects(final String type, Collection<DatabaseObject> objects, Database database) {
    if (!objects.isEmpty() && supportsSortingObjects(database) && (database.getConnection() != null) && !(database.getConnection() instanceof OfflineConnection)) {
        List<String> schemas = new ArrayList<>();
        CompareControl.SchemaComparison[] schemaComparisons = this.diffOutputControl.getSchemaComparisons();
        if (schemaComparisons != null) {
            for (CompareControl.SchemaComparison comparison : schemaComparisons) {
                String schemaName = comparison.getReferenceSchema().getSchemaName();
                if (schemaName == null) {
                    schemaName = database.getDefaultSchemaName();
                }
                schemas.add(schemaName);
            }
        }
        if (schemas.isEmpty()) {
            schemas.add(database.getDefaultSchemaName());
        }
        try {
            final List<String> dependencyOrder = new ArrayList<>();
            DependencyUtil.NodeValueListener<String> nameListener = new DependencyUtil.NodeValueListener<String>() {

                @Override
                public void evaluating(String nodeValue) {
                    dependencyOrder.add(nodeValue);
                }
            };
            DependencyUtil.DependencyGraph<String> graph = new DependencyUtil.DependencyGraph<String>(nameListener);
            addDependencies(graph, schemas, database);
            graph.computeDependencies();
            if (!dependencyOrder.isEmpty()) {
                final List<DatabaseObject> toSort = new ArrayList<>();
                final List<DatabaseObject> toNotSort = new ArrayList<>();
                for (DatabaseObject obj : objects) {
                    if (!(obj instanceof Column)) {
                        String schemaName = null;
                        if (obj.getSchema() != null) {
                            schemaName = obj.getSchema().getName();
                        }
                        String name = schemaName + "." + obj.getName();
                        if (dependencyOrder.contains(name)) {
                            toSort.add(obj);
                        } else {
                            toNotSort.add(obj);
                        }
                    } else {
                        toNotSort.add(obj);
                    }
                }
                Collections.sort(toSort, new Comparator<DatabaseObject>() {

                    @Override
                    public int compare(DatabaseObject o1, DatabaseObject o2) {
                        String o1Schema = null;
                        if (o1.getSchema() != null) {
                            o1Schema = o1.getSchema().getName();
                        }
                        String o2Schema = null;
                        if (o2.getSchema() != null) {
                            o2Schema = o2.getSchema().getName();
                        }
                        Integer o1Order = dependencyOrder.indexOf(o1Schema + "." + o1.getName());
                        int o2Order = dependencyOrder.indexOf(o2Schema + "." + o2.getName());
                        int order = o1Order.compareTo(o2Order);
                        if ("unexpected".equals(type)) {
                            order = order * -1;
                        }
                        return order;
                    }
                });
                toSort.addAll(toNotSort);
                return toSort;
            }
        } catch (DatabaseException e) {
            Scope.getCurrentScope().getLog(getClass()).fine("Cannot get object dependencies: " + e.getMessage());
        }
    }
    return new ArrayList<>(objects);
}
Also used : DependencyUtil(liquibase.util.DependencyUtil) Column(liquibase.structure.core.Column) CompareControl(liquibase.diff.compare.CompareControl) DatabaseObject(liquibase.structure.DatabaseObject) DatabaseException(liquibase.exception.DatabaseException)

Example 12 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class ChangedForeignKeyChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    ForeignKey fk = (ForeignKey) changedObject;
    StringUtil.StringUtilFormatter formatter = new StringUtil.StringUtilFormatter<Column>() {

        @Override
        public String toString(Column obj) {
            return obj.toString(false);
        }
    };
    DropForeignKeyConstraintChange dropFkChange = new DropForeignKeyConstraintChange();
    dropFkChange.setConstraintName(fk.getName());
    dropFkChange.setBaseTableName(fk.getForeignKeyTable().getName());
    AddForeignKeyConstraintChange addFkChange = new AddForeignKeyConstraintChange();
    addFkChange.setConstraintName(fk.getName());
    addFkChange.setBaseTableName(fk.getForeignKeyTable().getName());
    addFkChange.setBaseColumnNames(StringUtil.join(fk.getForeignKeyColumns(), ",", formatter));
    addFkChange.setReferencedTableName(fk.getPrimaryKeyTable().getName());
    addFkChange.setReferencedColumnNames(StringUtil.join(fk.getPrimaryKeyColumns(), ",", formatter));
    addFkChange.setOnDelete(fk.getDeleteRule());
    addFkChange.setOnUpdate(fk.getUpdateRule());
    if (control.getIncludeCatalog()) {
        dropFkChange.setBaseTableCatalogName(fk.getForeignKeyTable().getSchema().getCatalogName());
        addFkChange.setBaseTableCatalogName(fk.getForeignKeyTable().getSchema().getCatalogName());
        addFkChange.setReferencedTableCatalogName(fk.getPrimaryKeyTable().getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        dropFkChange.setBaseTableSchemaName(fk.getForeignKeyTable().getSchema().getName());
        addFkChange.setBaseTableSchemaName(fk.getForeignKeyTable().getSchema().getName());
        addFkChange.setReferencedTableSchemaName(fk.getPrimaryKeyTable().getSchema().getName());
    }
    if (fk.getBackingIndex() != null) {
        control.setAlreadyHandledChanged(fk.getBackingIndex());
    }
    return new Change[] { dropFkChange, addFkChange };
}
Also used : Column(liquibase.structure.core.Column) DropForeignKeyConstraintChange(liquibase.change.core.DropForeignKeyConstraintChange) AddForeignKeyConstraintChange(liquibase.change.core.AddForeignKeyConstraintChange) AddForeignKeyConstraintChange(liquibase.change.core.AddForeignKeyConstraintChange) Change(liquibase.change.Change) DropForeignKeyConstraintChange(liquibase.change.core.DropForeignKeyConstraintChange) ForeignKey(liquibase.structure.core.ForeignKey) StringUtil(liquibase.util.StringUtil)

Example 13 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class ChangedViewChangeGenerator method fixChanged.

@Override
public Change[] fixChanged(DatabaseObject changedObject, ObjectDifferences differences, DiffOutputControl control, Database referenceDatabase, final Database comparisonDatabase, ChangeGeneratorChain chain) {
    View view = (View) changedObject;
    CreateViewChange change = createViewChange();
    change.setViewName(view.getName());
    change.setReplaceIfExists(true);
    if (control.getIncludeCatalog()) {
        change.setCatalogName(view.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(view.getSchema().getName());
    }
    String selectQuery = view.getDefinition();
    boolean fullDefinitionOverridden = false;
    if (selectQuery == null) {
        selectQuery = "COULD NOT DETERMINE VIEW QUERY";
    } else if ((comparisonDatabase instanceof OracleDatabase) && (view.getColumns() != null) && !view.getColumns().isEmpty()) {
        String viewName;
        if ((change.getCatalogName() == null) && (change.getSchemaName() == null)) {
            viewName = comparisonDatabase.escapeObjectName(change.getViewName(), View.class);
        } else {
            viewName = comparisonDatabase.escapeViewName(change.getCatalogName(), change.getSchemaName(), change.getViewName());
        }
        selectQuery = "CREATE OR REPLACE FORCE VIEW " + viewName + " (" + StringUtil.join(view.getColumns(), ", ", new StringUtil.StringUtilFormatter() {

            @Override
            public String toString(Object obj) {
                if ((((Column) obj).getComputed() != null) && ((Column) obj).getComputed()) {
                    return ((Column) obj).getName();
                } else {
                    return comparisonDatabase.escapeColumnName(null, null, null, ((Column) obj).getName(), false);
                }
            }
        }) + ") AS " + selectQuery;
        change.setFullDefinition(true);
        fullDefinitionOverridden = true;
    }
    change.setSelectQuery(selectQuery);
    if (!fullDefinitionOverridden) {
        change.setFullDefinition(view.getContainsFullDefinition());
    }
    List<Change> changes = new ArrayList<>();
    changes.add(change);
    Difference changedRemarks = differences.getDifference("remarks");
    if (changedRemarks != null) {
        SetTableRemarksChange setRemarksChange = new SetTableRemarksChange();
        if (control.getIncludeCatalog()) {
            setRemarksChange.setCatalogName(view.getSchema().getCatalogName());
        }
        if (control.getIncludeSchema()) {
            setRemarksChange.setSchemaName(view.getSchema().getName());
        }
        setRemarksChange.setTableName(view.getName());
        setRemarksChange.setRemarks(view.getRemarks());
        changes.add(setRemarksChange);
    }
    return changes.toArray(new Change[changes.size()]);
}
Also used : CreateViewChange(liquibase.change.core.CreateViewChange) ArrayList(java.util.ArrayList) Change(liquibase.change.Change) CreateViewChange(liquibase.change.core.CreateViewChange) SetTableRemarksChange(liquibase.change.core.SetTableRemarksChange) Difference(liquibase.diff.Difference) View(liquibase.structure.core.View) OracleDatabase(liquibase.database.core.OracleDatabase) Column(liquibase.structure.core.Column) DatabaseObject(liquibase.structure.DatabaseObject) SetTableRemarksChange(liquibase.change.core.SetTableRemarksChange)

Example 14 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class MissingTableChangeGenerator method getSingleColumnUniqueConstraints.

private Map<Column, UniqueConstraint> getSingleColumnUniqueConstraints(Table missingTable) {
    Map<Column, UniqueConstraint> map = new HashMap<>();
    List<UniqueConstraint> constraints = missingTable.getUniqueConstraints() == null ? null : missingTable.getUniqueConstraints();
    for (UniqueConstraint constraint : constraints) {
        if (constraint.getColumns().size() == 1) {
            map.put(constraint.getColumns().get(0), constraint);
        }
    }
    return map;
}
Also used : Column(liquibase.structure.core.Column) UniqueConstraint(liquibase.structure.core.UniqueConstraint)

Example 15 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class MissingViewChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, final Database comparisonDatabase, ChangeGeneratorChain chain) {
    View view = (View) missingObject;
    CreateViewChange change = createViewChange();
    change.setViewName(view.getName());
    if (control.getIncludeCatalog()) {
        change.setCatalogName(view.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(view.getSchema().getName());
    }
    if (view.getRemarks() != null) {
        change.setRemarks(view.getRemarks());
    }
    String selectQuery = view.getDefinition();
    boolean fullDefinitionOverridden = false;
    if (selectQuery == null) {
        selectQuery = "COULD NOT DETERMINE VIEW QUERY";
    } else if ((comparisonDatabase instanceof OracleDatabase) && (view.getColumns() != null) && !view.getColumns().isEmpty()) {
        String viewName;
        if ((change.getCatalogName() == null) && (change.getSchemaName() == null)) {
            viewName = comparisonDatabase.escapeObjectName(change.getViewName(), View.class);
        } else {
            viewName = comparisonDatabase.escapeViewName(change.getCatalogName(), change.getSchemaName(), change.getViewName());
        }
        selectQuery = "CREATE OR REPLACE FORCE VIEW " + viewName + " (" + StringUtil.join(view.getColumns(), ", ", new StringUtil.StringUtilFormatter() {

            @Override
            public String toString(Object obj) {
                if ((((Column) obj).getComputed() != null) && ((Column) obj).getComputed()) {
                    return ((Column) obj).getName();
                } else {
                    return comparisonDatabase.escapeColumnName(null, null, null, ((Column) obj).getName(), false);
                }
            }
        }) + ") AS " + selectQuery;
        change.setFullDefinition(true);
        fullDefinitionOverridden = true;
    }
    change.setSelectQuery(selectQuery);
    if (!fullDefinitionOverridden) {
        change.setFullDefinition(view.getContainsFullDefinition());
    }
    return new Change[] { change };
}
Also used : OracleDatabase(liquibase.database.core.OracleDatabase) Column(liquibase.structure.core.Column) CreateViewChange(liquibase.change.core.CreateViewChange) DatabaseObject(liquibase.structure.DatabaseObject) Change(liquibase.change.Change) CreateViewChange(liquibase.change.core.CreateViewChange) View(liquibase.structure.core.View)

Aggregations

Column (liquibase.structure.core.Column)43 Table (liquibase.structure.core.Table)22 Change (liquibase.change.Change)8 SnapshotControl (liquibase.snapshot.SnapshotControl)8 Index (liquibase.structure.core.Index)8 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)7 DatabaseObject (liquibase.structure.DatabaseObject)6 Test (org.junit.Test)6 DatabaseException (liquibase.exception.DatabaseException)5 SqlStatement (liquibase.statement.SqlStatement)5 Schema (liquibase.structure.core.Schema)5 UniqueConstraint (liquibase.structure.core.UniqueConstraint)5 ArrayList (java.util.ArrayList)4 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)4 CatalogAndSchema (liquibase.CatalogAndSchema)3 Liquibase (liquibase.Liquibase)3 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)3 RawSqlStatement (liquibase.statement.core.RawSqlStatement)3 PrimaryKey (liquibase.structure.core.PrimaryKey)3