Search in sources :

Example 1 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class Liquibase method generateChangeLog.

public void generateChangeLog(CatalogAndSchema catalogAndSchema, DiffToChangeLog changeLogWriter, PrintStream outputStream, ChangeLogSerializer changeLogSerializer, Class<? extends DatabaseObject>... snapshotTypes) throws DatabaseException, IOException, ParserConfigurationException {
    Set<Class<? extends DatabaseObject>> finalCompareTypes = null;
    if (snapshotTypes != null && snapshotTypes.length > 0) {
        finalCompareTypes = new HashSet<Class<? extends DatabaseObject>>(Arrays.asList(snapshotTypes));
    }
    SnapshotControl snapshotControl = new SnapshotControl(this.getDatabase(), snapshotTypes);
    CompareControl compareControl = new CompareControl(new CompareControl.SchemaComparison[] { new CompareControl.SchemaComparison(catalogAndSchema, catalogAndSchema) }, finalCompareTypes);
    //        compareControl.addStatusListener(new OutDiffStatusListener());
    DatabaseSnapshot originalDatabaseSnapshot = null;
    try {
        originalDatabaseSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(compareControl.getSchemas(CompareControl.DatabaseRole.REFERENCE), getDatabase(), snapshotControl);
        DiffResult diffResult = DiffGeneratorFactory.getInstance().compare(originalDatabaseSnapshot, SnapshotGeneratorFactory.getInstance().createSnapshot(compareControl.getSchemas(CompareControl.DatabaseRole.REFERENCE), null, snapshotControl), compareControl);
        changeLogWriter.setDiffResult(diffResult);
        if (changeLogSerializer != null) {
            changeLogWriter.print(outputStream, changeLogSerializer);
        } else {
            changeLogWriter.print(outputStream);
        }
    } catch (InvalidExampleException e) {
        throw new UnexpectedLiquibaseException(e);
    }
}
Also used : InvalidExampleException(liquibase.snapshot.InvalidExampleException) DatabaseObject(liquibase.structure.DatabaseObject) CompareControl(liquibase.diff.compare.CompareControl) DiffResult(liquibase.diff.DiffResult) SnapshotControl(liquibase.snapshot.SnapshotControl) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 2 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class DiffToChangeLog method getOrderedOutputTypes.

protected List<Class<? extends DatabaseObject>> getOrderedOutputTypes(Class<? extends ChangeGenerator> generatorType) {
    Database comparisonDatabase = diffResult.getComparisonSnapshot().getDatabase();
    DependencyGraph graph = new DependencyGraph();
    for (Class<? extends DatabaseObject> type : diffResult.getReferenceSnapshot().getSnapshotControl().getTypesToInclude()) {
        graph.addType(type);
    }
    List<Class<? extends DatabaseObject>> types = graph.sort(comparisonDatabase, generatorType);
    if (!loggedOrderFor.contains(generatorType)) {
        String log = generatorType.getSimpleName() + " type order: ";
        for (Class<? extends DatabaseObject> type : types) {
            log += "    " + type.getName();
        }
        LogFactory.getLogger().debug(log);
        loggedOrderFor.add(generatorType);
    }
    return types;
}
Also used : DB2Database(liquibase.database.core.DB2Database) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) Database(liquibase.database.Database) DatabaseObject(liquibase.structure.DatabaseObject)

Example 3 with DatabaseObject

use of liquibase.structure.DatabaseObject in project dropwizard by dropwizard.

the class DbDumpCommand method run.

@Override
@SuppressWarnings("UseOfSystemOutOrSystemErr")
public void run(Namespace namespace, Liquibase liquibase) throws Exception {
    final Set<Class<? extends DatabaseObject>> compareTypes = new HashSet<>();
    if (isTrue(namespace.getBoolean("columns"))) {
        compareTypes.add(Column.class);
    }
    if (isTrue(namespace.getBoolean("data"))) {
        compareTypes.add(Data.class);
    }
    if (isTrue(namespace.getBoolean("foreign-keys"))) {
        compareTypes.add(ForeignKey.class);
    }
    if (isTrue(namespace.getBoolean("indexes"))) {
        compareTypes.add(Index.class);
    }
    if (isTrue(namespace.getBoolean("primary-keys"))) {
        compareTypes.add(PrimaryKey.class);
    }
    if (isTrue(namespace.getBoolean("sequences"))) {
        compareTypes.add(Sequence.class);
    }
    if (isTrue(namespace.getBoolean("tables"))) {
        compareTypes.add(Table.class);
    }
    if (isTrue(namespace.getBoolean("unique-constraints"))) {
        compareTypes.add(UniqueConstraint.class);
    }
    if (isTrue(namespace.getBoolean("views"))) {
        compareTypes.add(View.class);
    }
    final DiffToChangeLog diffToChangeLog = new DiffToChangeLog(new DiffOutputControl());
    final Database database = liquibase.getDatabase();
    final String filename = namespace.getString("output");
    if (filename != null) {
        try (PrintStream file = new PrintStream(filename, StandardCharsets.UTF_8.name())) {
            generateChangeLog(database, database.getDefaultSchema(), diffToChangeLog, file, compareTypes);
        }
    } else {
        generateChangeLog(database, database.getDefaultSchema(), diffToChangeLog, outputStream, compareTypes);
    }
}
Also used : PrintStream(java.io.PrintStream) Database(liquibase.database.Database) DatabaseObject(liquibase.structure.DatabaseObject) DiffOutputControl(liquibase.diff.output.DiffOutputControl) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) HashSet(java.util.HashSet)

Example 4 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class MissingDataExternalFileChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl outputControl, Database referenceDatabase, Database comparisionDatabase, ChangeGeneratorChain chain) {
    ResultSet rs = null;
    try (Statement stmt = ((JdbcConnection) referenceDatabase.getConnection()).createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)) {
        Data data = (Data) missingObject;
        Table table = data.getTable();
        if (referenceDatabase.isLiquibaseObject(table)) {
            return null;
        }
        String sql = "SELECT * FROM " + referenceDatabase.escapeTableName(table.getSchema().getCatalogName(), table.getSchema().getName(), table.getName());
        stmt.setFetchSize(100);
        rs = stmt.executeQuery(sql);
        if (rs.isBeforeFirst()) {
            List<String> columnNames = new ArrayList<>();
            for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
                columnNames.add(rs.getMetaData().getColumnName(i + 1));
            }
            String fileName = table.getName().toLowerCase() + ".csv";
            if (dataDir != null) {
                fileName = dataDir + "/" + fileName;
                File parentDir = new File(dataDir);
                if (!parentDir.exists()) {
                    parentDir.mkdirs();
                }
                if (!parentDir.isDirectory()) {
                    throw new IOException(parentDir.getAbsolutePath() + " is not a valid directory");
                }
            }
            String[] dataTypes = new String[0];
            try (FileOutputStream fileOutputStream = new FileOutputStream(fileName);
                OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue());
                CSVWriter outputFile = new CSVWriter(new BufferedWriter(outputStreamWriter))) {
                dataTypes = new String[columnNames.size()];
                String[] line = new String[columnNames.size()];
                for (int i = 0; i < columnNames.size(); i++) {
                    line[i] = columnNames.get(i);
                }
                outputFile.writeNext(line);
                int rowNum = 0;
                while (rs.next()) {
                    line = new String[columnNames.size()];
                    for (int i = 0; i < columnNames.size(); i++) {
                        Object value = JdbcUtil.getResultSetValue(rs, i + 1);
                        if ((dataTypes[i] == null) && (value != null)) {
                            if (value instanceof Number) {
                                dataTypes[i] = "NUMERIC";
                            } else if (value instanceof Boolean) {
                                dataTypes[i] = "BOOLEAN";
                            } else if (value instanceof Date) {
                                dataTypes[i] = "DATE";
                            } else if (value instanceof byte[]) {
                                dataTypes[i] = "BLOB";
                            } else {
                                dataTypes[i] = "STRING";
                            }
                        }
                        if (value == null) {
                            line[i] = "NULL";
                        } else {
                            if (value instanceof Date) {
                                line[i] = new ISODateFormat().format(((Date) value));
                            } else if (value instanceof byte[]) {
                                // extract the value as a Base64 string, to safely store the
                                // binary data
                                line[i] = Base64.getEncoder().encodeToString((byte[]) value);
                            } else {
                                line[i] = value.toString();
                            }
                        }
                    }
                    outputFile.writeNext(line);
                    rowNum++;
                    if ((rowNum % 5000) == 0) {
                        outputFile.flush();
                    }
                }
            }
            LoadDataChange change = new LoadDataChange();
            change.setFile(fileName);
            change.setEncoding(GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue());
            if (outputControl.getIncludeCatalog()) {
                change.setCatalogName(table.getSchema().getCatalogName());
            }
            if (outputControl.getIncludeSchema()) {
                change.setSchemaName(table.getSchema().getName());
            }
            change.setTableName(table.getName());
            for (int i = 0; i < columnNames.size(); i++) {
                String colName = columnNames.get(i);
                LoadDataColumnConfig columnConfig = new LoadDataColumnConfig();
                columnConfig.setHeader(colName);
                columnConfig.setName(colName);
                columnConfig.setType(dataTypes[i] != null ? dataTypes[i] : "skip");
                change.addColumn(columnConfig);
            }
            return new Change[] { change };
        }
        return new Change[] {};
    } catch (Exception e) {
        throw new UnexpectedLiquibaseException(e);
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException ignore) {
            // nothing can be done
            }
        // try...
        }
    // rs == null?
    }
// try... finally
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) CSVWriter(liquibase.util.csv.CSVWriter) JdbcConnection(liquibase.database.jvm.JdbcConnection) ISODateFormat(liquibase.util.ISODateFormat) ResultSet(java.sql.ResultSet) LoadDataColumnConfig(liquibase.change.core.LoadDataColumnConfig) LoadDataChange(liquibase.change.core.LoadDataChange) Table(liquibase.structure.core.Table) Statement(java.sql.Statement) Data(liquibase.structure.core.Data) Change(liquibase.change.Change) LoadDataChange(liquibase.change.core.LoadDataChange) Date(java.util.Date) SQLException(java.sql.SQLException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseObject(liquibase.structure.DatabaseObject) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 5 with DatabaseObject

use of liquibase.structure.DatabaseObject in project liquibase by liquibase.

the class DatabaseObjectFactory method getStandardTypes.

public Set<Class<? extends DatabaseObject>> getStandardTypes() {
    if (standardTypes == null) {
        Set<Class<? extends DatabaseObject>> set = new HashSet<>();
        for (DatabaseObject databaseObject : Scope.getCurrentScope().getServiceLocator().findInstances(DatabaseObject.class)) {
            if (!databaseObject.getClass().equals(LiquibaseColumn.class) && databaseObject.snapshotByDefault()) {
                set.add(databaseObject.getClass());
            }
        }
        standardTypes = set;
    }
    return standardTypes;
}
Also used : DatabaseObject(liquibase.structure.DatabaseObject) HashSet(java.util.HashSet)

Aggregations

DatabaseObject (liquibase.structure.DatabaseObject)47 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)14 CompareControl (liquibase.diff.compare.CompareControl)10 Change (liquibase.change.Change)9 Database (liquibase.database.Database)9 CatalogAndSchema (liquibase.CatalogAndSchema)7 DiffResult (liquibase.diff.DiffResult)6 ObjectDifferences (liquibase.diff.ObjectDifferences)6 ArrayList (java.util.ArrayList)5 EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)5 Column (liquibase.structure.core.Column)5 Table (liquibase.structure.core.Table)5 HashSet (java.util.HashSet)4 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)4 DiffOutputControl (liquibase.diff.output.DiffOutputControl)4 DatabaseException (liquibase.exception.DatabaseException)4 InvalidExampleException (liquibase.snapshot.InvalidExampleException)4 SnapshotControl (liquibase.snapshot.SnapshotControl)4 DatabaseObjectCollection (liquibase.structure.DatabaseObjectCollection)4 List (java.util.List)3