Search in sources :

Example 1 with ForeignKey

use of liquibase.database.structure.ForeignKey in project collect by openforis.

the class LiquidbaseDatabaseSnapshotBuilder method createForeignKeys.

protected void createForeignKeys() {
    for (org.openforis.collect.relational.model.Table<?> itable : schema.getTables()) {
        Table ltable = snapshot.getTable(itable.getName());
        List<ReferentialConstraint> ifks = itable.getReferentialContraints();
        for (ReferentialConstraint ifk : ifks) {
            ForeignKey lfk = new ForeignKey();
            lfk.setName(ifk.getName());
            // set base table columns
            lfk.setForeignKeyTable(ltable);
            for (org.openforis.collect.relational.model.Column<?> ifcCol : ifk.getColumns()) {
                lfk.addForeignKeyColumn(ifcCol.getName());
            }
            // set referenced key columns
            UniquenessConstraint iReferencedKey = ifk.getReferencedKey();
            org.openforis.collect.relational.model.Table<?> iReferencedTable = iReferencedKey.getTable();
            Table lReferencedTable = snapshot.getTable(iReferencedTable.getName());
            lfk.setPrimaryKeyTable(lReferencedTable);
            for (org.openforis.collect.relational.model.Column<?> refCol : iReferencedKey.getColumns()) {
                lfk.addPrimaryKeyColumn(refCol.getName());
            }
            // Add fk
            snapshot.getForeignKeys().add(lfk);
        }
    }
}
Also used : Table(liquibase.database.structure.Table) ReferentialConstraint(org.openforis.collect.relational.model.ReferentialConstraint) UniquenessConstraint(org.openforis.collect.relational.model.UniquenessConstraint) ForeignKey(liquibase.database.structure.ForeignKey)

Aggregations

ForeignKey (liquibase.database.structure.ForeignKey)1 Table (liquibase.database.structure.Table)1 ReferentialConstraint (org.openforis.collect.relational.model.ReferentialConstraint)1 UniquenessConstraint (org.openforis.collect.relational.model.UniquenessConstraint)1