Search in sources :

Example 6 with EmptyDatabaseSnapshot

use of liquibase.snapshot.EmptyDatabaseSnapshot in project liquibase by liquibase.

the class DiffToChangeLog method print.

public void print(String changeLogFile, ChangeLogSerializer changeLogSerializer) throws ParserConfigurationException, IOException, DatabaseException {
    this.changeSetPath = changeLogFile;
    File file = new File(changeLogFile);
    final Map<String, Object> newScopeObjects = new HashMap<>();
    File objectsDir = null;
    if (changeLogFile.toLowerCase().endsWith("sql")) {
        DeprecatedConfigurationValueProvider.setData("liquibase.pro.sql.inline", "true");
    } else if (this.diffResult.getComparisonSnapshot() instanceof EmptyDatabaseSnapshot) {
        objectsDir = new File(file.getParentFile(), "objects");
    } else {
        objectsDir = new File(file.getParentFile(), "objects-" + new Date().getTime());
    }
    if (objectsDir != null) {
        if (objectsDir.exists()) {
            throw new UnexpectedLiquibaseException("The generatechangelog command would overwrite your existing stored logic files. To run this command please remove or rename the '" + objectsDir.getCanonicalPath() + "' dir in your local project directory");
        }
        newScopeObjects.put(EXTERNAL_FILE_DIR_SCOPE_KEY, objectsDir);
    }
    newScopeObjects.put(DIFF_OUTPUT_CONTROL_SCOPE_KEY, diffOutputControl);
    try {
        // 
        // Get a Database instance and save it in the scope for later use
        // 
        DatabaseSnapshot snapshot = diffResult.getReferenceSnapshot();
        Database database = determineDatabase(diffResult.getReferenceSnapshot());
        if (database == null) {
            database = determineDatabase(diffResult.getComparisonSnapshot());
        }
        newScopeObjects.put(DIFF_SNAPSHOT_DATABASE, database);
        Scope.child(newScopeObjects, new Scope.ScopedRunner() {

            @Override
            public void run() {
                try {
                    if (!file.exists()) {
                        // print changeLog only if there are available changeSets to print instead of printing it always
                        printNew(changeLogSerializer, file);
                    } else {
                        Scope.getCurrentScope().getLog(getClass()).info(file + " exists, appending");
                        ByteArrayOutputStream out = new ByteArrayOutputStream();
                        print(new PrintStream(out, true, GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue()), changeLogSerializer);
                        String xml = new String(out.toByteArray(), GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue());
                        String innerXml = xml.replaceFirst("(?ms).*<databaseChangeLog[^>]*>", "");
                        innerXml = innerXml.replaceFirst(DATABASE_CHANGE_LOG_CLOSING_XML_TAG, "");
                        innerXml = innerXml.trim();
                        if ("".equals(innerXml)) {
                            Scope.getCurrentScope().getLog(getClass()).info("No changes found, nothing to do");
                            return;
                        }
                        try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw")) {
                            String line;
                            long offset = 0;
                            boolean foundEndTag = false;
                            while ((line = randomAccessFile.readLine()) != null) {
                                int index = line.indexOf(DATABASE_CHANGE_LOG_CLOSING_XML_TAG);
                                if (index >= 0) {
                                    foundEndTag = true;
                                    break;
                                } else {
                                    offset = randomAccessFile.getFilePointer();
                                }
                            }
                            String lineSeparator = GlobalConfiguration.OUTPUT_LINE_SEPARATOR.getCurrentValue();
                            if (foundEndTag) {
                                randomAccessFile.seek(offset);
                                randomAccessFile.writeBytes("    ");
                                randomAccessFile.write(innerXml.getBytes(GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue()));
                                randomAccessFile.writeBytes(lineSeparator);
                                randomAccessFile.writeBytes(DATABASE_CHANGE_LOG_CLOSING_XML_TAG + lineSeparator);
                            } else {
                                randomAccessFile.seek(0);
                                long length = randomAccessFile.length();
                                randomAccessFile.seek(length);
                                randomAccessFile.write(xml.getBytes(GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue()));
                            }
                        }
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });
    } catch (Exception e) {
        // rethrow known exceptions. TODO: Fix this up with final Scope API
        final Throwable cause = e.getCause();
        if (cause instanceof ParserConfigurationException) {
            throw (ParserConfigurationException) cause;
        }
        if (cause instanceof IOException) {
            throw (IOException) cause;
        }
        if (cause instanceof DatabaseException) {
            throw (DatabaseException) cause;
        }
        throw new RuntimeException(e);
    }
}
Also used : UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Scope(liquibase.Scope) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) DatabaseObject(liquibase.structure.DatabaseObject) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) EmptyDatabaseSnapshot(liquibase.snapshot.EmptyDatabaseSnapshot) DatabaseException(liquibase.exception.DatabaseException)

Aggregations

EmptyDatabaseSnapshot (liquibase.snapshot.EmptyDatabaseSnapshot)6 DatabaseObject (liquibase.structure.DatabaseObject)6 DiffResult (liquibase.diff.DiffResult)5 CompareControl (liquibase.diff.compare.CompareControl)4 MySQLDatabase (liquibase.database.core.MySQLDatabase)3 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)3 Test (org.junit.Test)3 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)2 SnapshotControl (liquibase.snapshot.SnapshotControl)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Scope (liquibase.Scope)1 Change (liquibase.change.Change)1 DropTableChange (liquibase.change.core.DropTableChange)1 ChangeSet (liquibase.changelog.ChangeSet)1 RanChangeSet (liquibase.changelog.RanChangeSet)1 DiffOutputControl (liquibase.diff.output.DiffOutputControl)1 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)1 DatabaseException (liquibase.exception.DatabaseException)1 LiquibaseException (liquibase.exception.LiquibaseException)1 InvalidExampleException (liquibase.snapshot.InvalidExampleException)1