Search in sources :

Example 1 with ISODateFormat

use of liquibase.util.ISODateFormat in project liquibase by liquibase.

the class OfflineChangeLogHistoryService method appendChangeSet.

protected void appendChangeSet(ChangeSet changeSet, ChangeSet.ExecType execType) throws DatabaseException {
    File oldFile = this.changeLogFile;
    File newFile = new File(oldFile.getParentFile(), oldFile.getName() + ".new");
    try (Reader reader = new InputStreamReader(new FileInputStream(oldFile), GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue());
        Writer writer = new OutputStreamWriter(new FileOutputStream(newFile), GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue());
        CSVReader csvReader = new CSVReader(reader);
        CSVWriter csvWriter = new CSVWriter(writer)) {
        String[] line;
        while ((line = csvReader.readNext()) != null) {
            csvWriter.writeNext(line);
        }
        String tag = "";
        for (Change change : changeSet.getChanges()) {
            if (change instanceof TagDatabaseChange) {
                TagDatabaseChange tagChange = (TagDatabaseChange) change;
                tag = tagChange.getTag();
            }
        }
        String[] newLine = new String[Columns.values().length];
        newLine[Columns.ID.ordinal()] = changeSet.getId();
        newLine[Columns.AUTHOR.ordinal()] = changeSet.getAuthor();
        newLine[Columns.FILENAME.ordinal()] = changeSet.getFilePath();
        newLine[Columns.DATEEXECUTED.ordinal()] = new ISODateFormat().format(new java.sql.Timestamp(new Date().getTime()));
        newLine[Columns.ORDEREXECUTED.ordinal()] = String.valueOf(getNextSequenceValue());
        newLine[Columns.EXECTYPE.ordinal()] = execType.value;
        newLine[Columns.MD5SUM.ordinal()] = changeSet.generateCheckSum().toString();
        newLine[Columns.DESCRIPTION.ordinal()] = changeSet.getDescription();
        newLine[Columns.COMMENTS.ordinal()] = changeSet.getComments();
        newLine[Columns.TAG.ordinal()] = tag;
        newLine[Columns.LIQUIBASE.ordinal()] = LiquibaseUtil.getBuildVersion();
        newLine[Columns.CONTEXTS.ordinal()] = (changeSet.getContexts() == null) ? null : changeSet.getContexts().toString();
        newLine[Columns.LABELS.ordinal()] = (changeSet.getLabels() == null) ? null : changeSet.getLabels().toString();
        newLine[Columns.DEPLOYMENT_ID.ordinal()] = getDeploymentId();
        csvWriter.writeNext(newLine);
    } catch (Exception e) {
        throw new DatabaseException(e);
    }
    oldFile.delete();
    newFile.renameTo(oldFile);
}
Also used : CSVReader(liquibase.util.csv.CSVReader) TagDatabaseChange(liquibase.change.core.TagDatabaseChange) CSVReader(liquibase.util.csv.CSVReader) CSVWriter(liquibase.util.csv.CSVWriter) Change(liquibase.change.Change) TagDatabaseChange(liquibase.change.core.TagDatabaseChange) Date(java.util.Date) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException) ISODateFormat(liquibase.util.ISODateFormat) DatabaseException(liquibase.exception.DatabaseException) CSVWriter(liquibase.util.csv.CSVWriter)

Example 2 with ISODateFormat

use of liquibase.util.ISODateFormat in project liquibase by liquibase.

the class OfflineChangeLogHistoryService method getRanChangeSets.

@Override
public List<RanChangeSet> getRanChangeSets() throws DatabaseException {
    try (Reader reader = new InputStreamReader(new FileInputStream(this.changeLogFile), GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue())) {
        CSVReader csvReader = new CSVReader(reader);
        String[] line = csvReader.readNext();
        if (line == null) {
            // empty file
            writeHeader(this.changeLogFile);
            return new ArrayList<>();
        }
        if (!"ID".equals(line[Columns.ID.ordinal()])) {
            throw new DatabaseException("Missing header in file " + this.changeLogFile.getAbsolutePath());
        }
        List<RanChangeSet> returnList = new ArrayList<>();
        while ((line = csvReader.readNext()) != null) {
            ContextExpression contexts = new ContextExpression();
            if (line.length > Columns.CONTEXTS.ordinal()) {
                contexts = new ContextExpression(line[Columns.CONTEXTS.ordinal()]);
            }
            Labels labels = new Labels();
            if (line.length > Columns.LABELS.ordinal()) {
                labels = new Labels(line[Columns.LABELS.ordinal()]);
            }
            String deploymentId = null;
            if (line.length > Columns.DEPLOYMENT_ID.ordinal()) {
                deploymentId = line[Columns.DEPLOYMENT_ID.ordinal()];
            }
            returnList.add(new RanChangeSet(line[Columns.FILENAME.ordinal()], line[Columns.ID.ordinal()], line[Columns.AUTHOR.ordinal()], CheckSum.parse(line[Columns.MD5SUM.ordinal()]), new ISODateFormat().parse(line[Columns.DATEEXECUTED.ordinal()]), line[Columns.TAG.ordinal()], ChangeSet.ExecType.valueOf(line[Columns.EXECTYPE.ordinal()]), line[Columns.DESCRIPTION.ordinal()], line[Columns.COMMENTS.ordinal()], contexts, labels, deploymentId));
        }
        return returnList;
    } catch (Exception e) {
        throw new DatabaseException(e);
    }
}
Also used : CSVReader(liquibase.util.csv.CSVReader) ContextExpression(liquibase.ContextExpression) ArrayList(java.util.ArrayList) CSVReader(liquibase.util.csv.CSVReader) Labels(liquibase.Labels) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException) ISODateFormat(liquibase.util.ISODateFormat) DatabaseException(liquibase.exception.DatabaseException)

Example 3 with ISODateFormat

use of liquibase.util.ISODateFormat 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 4 with ISODateFormat

use of liquibase.util.ISODateFormat in project liquibase by liquibase.

the class ChangedColumnChangeGenerator method handleDefaultValueDifferences.

protected void handleDefaultValueDifferences(Column column, ObjectDifferences differences, DiffOutputControl control, List<Change> changes, Database referenceDatabase, Database comparisonDatabase) {
    Difference difference = differences.getDifference("defaultValue");
    if (difference != null) {
        Object value = difference.getReferenceValue();
        LiquibaseDataType columnDataType = DataTypeFactory.getInstance().from(column.getType(), comparisonDatabase);
        if (value == null) {
            DropDefaultValueChange change = new DropDefaultValueChange();
            if (control.getIncludeCatalog()) {
                change.setCatalogName(column.getRelation().getSchema().getCatalog().getName());
            }
            if (control.getIncludeSchema()) {
                change.setSchemaName(column.getRelation().getSchema().getName());
            }
            change.setTableName(column.getRelation().getName());
            change.setColumnName(column.getName());
            change.setColumnDataType(columnDataType.toString());
            changes.add(change);
        } else if (shouldTriggerAddDefaultChange(column, difference, comparisonDatabase)) {
            AddDefaultValueChange change = new AddDefaultValueChange();
            if (control.getIncludeCatalog()) {
                change.setCatalogName(column.getRelation().getSchema().getCatalog().getName());
            }
            if (control.getIncludeSchema()) {
                change.setSchemaName(column.getRelation().getSchema().getName());
            }
            change.setTableName(column.getRelation().getName());
            change.setColumnName(column.getName());
            change.setColumnDataType(columnDataType.toString());
            // 
            if (value instanceof Boolean || columnDataType instanceof BooleanType) {
                if (value instanceof Boolean) {
                    change.setDefaultValueBoolean((Boolean) value);
                } else if (columnDataType instanceof BooleanType) {
                    if (value instanceof DatabaseFunction) {
                        if (value.equals(new DatabaseFunction("'false'"))) {
                            change.setDefaultValueBoolean(false);
                        } else if (value.equals(new DatabaseFunction("'true'"))) {
                            change.setDefaultValueBoolean(true);
                        } else {
                            change.setDefaultValueComputed(((DatabaseFunction) value));
                        }
                    }
                }
            } else if (value instanceof Date) {
                change.setDefaultValueDate(new ISODateFormat().format(((Date) value)));
            } else if (value instanceof Number) {
                change.setDefaultValueNumeric(value.toString());
            } else if (value instanceof DatabaseFunction) {
                change.setDefaultValueComputed(((DatabaseFunction) value));
            } else {
                change.setDefaultValue(value.toString());
            }
            change.setDefaultValueConstraintName(column.getDefaultValueConstraintName());
            changes.add(change);
        }
    }
}
Also used : ISODateFormat(liquibase.util.ISODateFormat) DatabaseFunction(liquibase.statement.DatabaseFunction) LiquibaseDataType(liquibase.datatype.LiquibaseDataType) BooleanType(liquibase.datatype.core.BooleanType) DatabaseObject(liquibase.structure.DatabaseObject) Difference(liquibase.diff.Difference) Date(java.util.Date)

Example 5 with ISODateFormat

use of liquibase.util.ISODateFormat in project liquibase by liquibase.

the class Main method doMigration.

/**
 * Do the actual database migration, i.e. apply the ChangeSets.
 *
 * @throws Exception
 */
protected void doMigration() throws Exception {
    if (COMMANDS.HELP.equalsIgnoreCase(command)) {
        printHelp(System.err);
        return;
    }
    // 
    if (StringUtil.isNotEmpty(HubConfiguration.LIQUIBASE_HUB_API_KEY.getCurrentValue())) {
        LOG.fine("Liquibase Hub API Key:  " + HubConfiguration.LIQUIBASE_HUB_API_KEY.getCurrentValueObfuscated());
    }
    if (StringUtil.isNotEmpty(HubConfiguration.LIQUIBASE_HUB_URL.getCurrentValue())) {
        LOG.fine("Liquibase Hub URL:      " + HubConfiguration.LIQUIBASE_HUB_URL.getCurrentValue());
    }
    LOG.fine("Liquibase Hub Mode:     " + HubConfiguration.LIQUIBASE_HUB_MODE.getCurrentValue());
    // 
    // Check for a valid license to run PRO commands
    // 
    String formatValue = getCommandParam(OPTIONS.FORMAT, null);
    if (isLicenseableCommand(formatValue)) {
        if (isFormattedDiff()) {
            if (formatValue != null && !formatValue.equalsIgnoreCase("json")) {
                String messageString = "\nWARNING: The diff command optional Pro parameter '--format' " + "currently supports only 'TXT' or 'JSON' as values.  (Blank defaults to 'TXT')";
                throw new LiquibaseException(String.format(messageString));
            }
        }
        if (!commandParams.contains("--help") && !liquibaseProLicenseValid) {
            String warningAboutCommand = command;
            if (command.equalsIgnoreCase(COMMANDS.DIFF) && formatValue != null && !formatValue.isEmpty()) {
                warningAboutCommand = "diff --format=" + formatValue;
            }
            String messageString = String.format(coreBundle.getString("no.pro.license.found"), warningAboutCommand);
            throw new LiquibaseException(messageString);
        }
    }
    try {
    // if (null != logFile) {
    // Scope.getCurrentScope().getLog(getClass()).setLogLevel(logLevel, logFile);
    // } else {
    // Scope.getCurrentScope().getLog(getClass()).setLogLevel(logLevel);
    // }
    } catch (IllegalArgumentException e) {
        throw new CommandLineParsingException(e.getMessage(), e);
    }
    final ResourceAccessor fileOpener;
    if (Main.runningFromNewCli) {
        fileOpener = Scope.getCurrentScope().getResourceAccessor();
    } else {
        fileOpener = new CompositeResourceAccessor(new FileSystemResourceAccessor(Paths.get(".").toAbsolutePath().toFile()), new CommandLineResourceAccessor(classLoader));
    }
    Database database = null;
    if (dbConnectionNeeded(command) && this.url != null) {
        database = CommandLineUtils.createDatabaseObject(fileOpener, this.url, this.username, this.password, this.driver, this.defaultCatalogName, this.defaultSchemaName, Boolean.parseBoolean(outputDefaultCatalog), Boolean.parseBoolean(outputDefaultSchema), this.databaseClass, this.driverPropertiesFile, this.propertyProviderClass, this.liquibaseCatalogName, this.liquibaseSchemaName, this.databaseChangeLogTableName, this.databaseChangeLogLockTableName);
        if (this.databaseChangeLogTablespaceName != null) {
            database.setLiquibaseTablespaceName(this.databaseChangeLogTablespaceName);
        } else {
            database.setLiquibaseTablespaceName(GlobalConfiguration.LIQUIBASE_TABLESPACE_NAME.getCurrentConfiguredValue().getValue());
        }
    }
    try {
        if ((excludeObjects != null) && (includeObjects != null)) {
            throw new UnexpectedLiquibaseException(String.format(coreBundle.getString("cannot.specify.both"), OPTIONS.EXCLUDE_OBJECTS, OPTIONS.INCLUDE_OBJECTS));
        }
        if (GlobalConfiguration.SHOULD_SNAPSHOT_DATA.getCurrentValue().equals(false) && dataOutputDirectory != null) {
            // If we are not otherwise going to snapshot data, still snapshot data if dataOutputDirectory is set
            DeprecatedConfigurationValueProvider.setData(GlobalConfiguration.SHOULD_SNAPSHOT_DATA, true);
        }
        ObjectChangeFilter objectChangeFilter = null;
        CompareControl.ComputedSchemas computedSchemas = CompareControl.computeSchemas(schemas, referenceSchemas, outputSchemasAs, defaultCatalogName, defaultSchemaName, referenceDefaultCatalogName, referenceDefaultSchemaName, database);
        CompareControl.SchemaComparison[] finalSchemaComparisons = computedSchemas.finalSchemaComparisons;
        DiffOutputControl diffOutputControl = new DiffOutputControl(includeCatalog, includeSchema, includeTablespace, finalSchemaComparisons);
        if (excludeObjects != null) {
            objectChangeFilter = new StandardObjectChangeFilter(StandardObjectChangeFilter.FilterType.EXCLUDE, excludeObjects);
            diffOutputControl.setObjectChangeFilter(objectChangeFilter);
        }
        if (includeObjects != null) {
            objectChangeFilter = new StandardObjectChangeFilter(StandardObjectChangeFilter.FilterType.INCLUDE, includeObjects);
            diffOutputControl.setObjectChangeFilter(objectChangeFilter);
        }
        for (CompareControl.SchemaComparison schema : finalSchemaComparisons) {
            diffOutputControl.addIncludedSchema(schema.getReferenceSchema());
            diffOutputControl.addIncludedSchema(schema.getComparisonSchema());
        }
        if (COMMANDS.DIFF.equalsIgnoreCase(command)) {
            if (commandParams.contains("--help")) {
                outputStream.println("liquibase diff" + "\n" + "          Outputs a description of differences.  If you have a Liquibase Pro key, you can output the differences as JSON using the --format=JSON option\n");
                System.exit(0);
            }
            if (isFormattedDiff()) {
                CommandScope liquibaseCommand = new CommandScope("internalFormattedDiff");
                CommandScope diffCommand = CommandLineUtils.createDiffCommand(createReferenceDatabaseFromCommandParams(commandParams, fileOpener), database, StringUtil.trimToNull(diffTypes), finalSchemaComparisons, objectChangeFilter, new PrintStream(getOutputStream()));
                liquibaseCommand.addArgumentValue("format", getCommandParam(OPTIONS.FORMAT, "JSON").toUpperCase());
                liquibaseCommand.addArgumentValue("diffCommand", diffCommand);
                liquibaseCommand.setOutput(getOutputStream());
                liquibaseCommand.execute();
            } else {
                CommandLineUtils.doDiff(createReferenceDatabaseFromCommandParams(commandParams, fileOpener), database, StringUtil.trimToNull(diffTypes), finalSchemaComparisons, objectChangeFilter, new PrintStream(getOutputStream()));
            }
            return;
        } else if (COMMANDS.DIFF_CHANGELOG.equalsIgnoreCase(command)) {
            CommandLineUtils.doDiffToChangeLog(changeLogFile, createReferenceDatabaseFromCommandParams(commandParams, fileOpener), database, diffOutputControl, objectChangeFilter, StringUtil.trimToNull(diffTypes), finalSchemaComparisons);
            return;
        } else if (COMMANDS.GENERATE_CHANGELOG.equalsIgnoreCase(command)) {
            String currentChangeLogFile = this.changeLogFile;
            if (currentChangeLogFile == null) {
                // will output to stdout:
                currentChangeLogFile = "";
            }
            File file = new File(currentChangeLogFile);
            if (file.exists() && (!Boolean.parseBoolean(overwriteOutputFile))) {
                throw new LiquibaseException(String.format(coreBundle.getString("changelogfile.already.exists"), currentChangeLogFile));
            } else {
                try {
                    if (!file.delete()) {
                    // Nothing needs to be done
                    }
                } catch (SecurityException e) {
                    throw new LiquibaseException(String.format(coreBundle.getString("attempt.to.delete.the.file.failed.cannot.continue"), currentChangeLogFile), e);
                }
            }
            CatalogAndSchema[] finalTargetSchemas = computedSchemas.finalTargetSchemas;
            CommandLineUtils.doGenerateChangeLog(currentChangeLogFile, database, finalTargetSchemas, StringUtil.trimToNull(diffTypes), StringUtil.trimToNull(changeSetAuthor), StringUtil.trimToNull(changeSetContext), StringUtil.trimToNull(dataOutputDirectory), diffOutputControl);
            return;
        } else if (COMMANDS.SNAPSHOT.equalsIgnoreCase(command)) {
            CommandScope snapshotCommand = new CommandScope("internalSnapshot");
            snapshotCommand.addArgumentValue(InternalSnapshotCommandStep.DATABASE_ARG, database).addArgumentValue(InternalSnapshotCommandStep.SCHEMAS_ARG, InternalSnapshotCommandStep.parseSchemas(database, getSchemaParams(database))).addArgumentValue(InternalSnapshotCommandStep.SERIALIZER_FORMAT_ARG, getCommandParam(OPTIONS.SNAPSHOT_FORMAT, null));
            Writer outputWriter = getOutputWriter();
            String result = InternalSnapshotCommandStep.printSnapshot(snapshotCommand, snapshotCommand.execute());
            outputWriter.write(result);
            outputWriter.flush();
            return;
        } else if (COMMANDS.EXECUTE_SQL.equalsIgnoreCase(command)) {
            CommandScope executeSqlCommand = new CommandScope("internalExecuteSql").addArgumentValue(InternalExecuteSqlCommandStep.DATABASE_ARG, database).addArgumentValue(InternalExecuteSqlCommandStep.SQL_ARG, getCommandParam("sql", null)).addArgumentValue(InternalExecuteSqlCommandStep.SQLFILE_ARG, getCommandParam("sqlFile", null)).addArgumentValue(InternalExecuteSqlCommandStep.DELIMITER_ARG, getCommandParam("delimiter", ";"));
            CommandResults results = executeSqlCommand.execute();
            Writer outputWriter = getOutputWriter();
            String output = (String) results.getResult("output");
            outputWriter.write(output);
            outputWriter.flush();
            return;
        } else if (COMMANDS.SNAPSHOT_REFERENCE.equalsIgnoreCase(command)) {
            CommandScope snapshotCommand = new CommandScope("internalSnapshot");
            Database referenceDatabase = createReferenceDatabaseFromCommandParams(commandParams, fileOpener);
            snapshotCommand.addArgumentValue(InternalSnapshotCommandStep.DATABASE_ARG, referenceDatabase).addArgumentValue(InternalSnapshotCommandStep.SCHEMAS_ARG, InternalSnapshotCommandStep.parseSchemas(referenceDatabase, getSchemaParams(referenceDatabase))).addArgumentValue(InternalSnapshotCommandStep.SERIALIZER_FORMAT_ARG, getCommandParam(OPTIONS.SNAPSHOT_FORMAT, null));
            Writer outputWriter = getOutputWriter();
            outputWriter.write(InternalSnapshotCommandStep.printSnapshot(snapshotCommand, snapshotCommand.execute()));
            outputWriter.flush();
            return;
        }
        Liquibase liquibase = new Liquibase(changeLogFile, fileOpener, database);
        if (Main.newCliChangelogParameters != null) {
            for (Map.Entry<String, String> param : Main.newCliChangelogParameters.entrySet()) {
                liquibase.setChangeLogParameter(param.getKey(), param.getValue());
            }
        }
        try {
            if (hubConnectionId != null) {
                try {
                    liquibase.setHubConnectionId(UUID.fromString(hubConnectionId));
                } catch (IllegalArgumentException e) {
                    throw new LiquibaseException("The command '" + command + "' failed because parameter 'hubConnectionId' has invalid value '" + hubConnectionId + "' Learn more at https://hub.liquibase.com");
                }
            }
        } catch (IllegalArgumentException e) {
            throw new LiquibaseException("Unexpected hubConnectionId format: " + hubConnectionId, e);
        }
        ChangeExecListener listener = ChangeExecListenerUtils.getChangeExecListener(liquibase.getDatabase(), liquibase.getResourceAccessor(), changeExecListenerClass, changeExecListenerPropertiesFile);
        liquibase.setChangeExecListener(listener);
        if (database != null) {
            database.setCurrentDateTimeFunction(currentDateTimeFunction);
        }
        for (Map.Entry<String, Object> entry : changeLogParameters.entrySet()) {
            liquibase.setChangeLogParameter(entry.getKey(), entry.getValue());
        }
        if (COMMANDS.LIST_LOCKS.equalsIgnoreCase(command)) {
            liquibase.reportLocks(System.err);
            return;
        } else if (COMMANDS.RELEASE_LOCKS.equalsIgnoreCase(command)) {
            LockService lockService = LockServiceFactory.getInstance().getLockService(database);
            lockService.forceReleaseLock();
            Scope.getCurrentScope().getUI().sendMessage(String.format(coreBundle.getString("successfully.released.database.change.log.locks"), liquibase.getDatabase().getConnection().getConnectionUserName() + "@" + liquibase.getDatabase().getConnection().getURL()));
            return;
        } else if (COMMANDS.TAG.equalsIgnoreCase(command)) {
            liquibase.tag(getCommandArgument());
            Scope.getCurrentScope().getUI().sendMessage(String.format(coreBundle.getString("successfully.tagged"), liquibase.getDatabase().getConnection().getConnectionUserName() + "@" + liquibase.getDatabase().getConnection().getURL()));
            return;
        } else if (COMMANDS.TAG_EXISTS.equalsIgnoreCase(command)) {
            String tag = commandParams.iterator().next();
            boolean exists = liquibase.tagExists(tag);
            if (exists) {
                Scope.getCurrentScope().getUI().sendMessage(String.format(coreBundle.getString("tag.exists"), tag, liquibase.getDatabase().getConnection().getConnectionUserName() + "@" + liquibase.getDatabase().getConnection().getURL()));
            } else {
                Scope.getCurrentScope().getUI().sendMessage(String.format(coreBundle.getString("tag.does.not.exist"), tag, liquibase.getDatabase().getConnection().getConnectionUserName() + "@" + liquibase.getDatabase().getConnection().getURL()));
            }
            return;
        } else if (COMMANDS.ROLLBACK_ONE_CHANGE_SET.equalsIgnoreCase(command)) {
            Map<String, Object> argsMap = new HashMap<>();
            loadChangeSetInfoToMap(argsMap);
            argsMap.put("changeLogFile", changeLogFile);
            CommandScope liquibaseCommand = createLiquibaseCommand(database, liquibase, "internalRollbackOneChangeSet", argsMap);
            liquibaseCommand.execute();
            return;
        } else if (COMMANDS.ROLLBACK_ONE_CHANGE_SET_SQL.equalsIgnoreCase(command)) {
            Writer outputWriter = getOutputWriter();
            Map<String, Object> argsMap = new HashMap<>();
            loadChangeSetInfoToMap(argsMap);
            argsMap.put("changeLogFile", changeLogFile);
            argsMap.put("outputWriter", outputWriter);
            argsMap.put("force", Boolean.TRUE);
            CommandScope liquibaseCommand = createLiquibaseCommand(database, liquibase, "internalRollbackOneChangeSetSQL", argsMap);
            liquibaseCommand.execute();
            return;
        } else if (COMMANDS.ROLLBACK_ONE_UPDATE.equalsIgnoreCase(command)) {
            Map<String, Object> argsMap = new HashMap<>();
            argsMap.put("changeLogFile", changeLogFile);
            argsMap.put("deploymentId", getCommandParam(OPTIONS.DEPLOYMENT_ID, null));
            CommandScope liquibaseCommand = createLiquibaseCommand(database, liquibase, "internalRollbackOneUpdate", argsMap);
            liquibaseCommand.execute();
            return;
        } else if (COMMANDS.ROLLBACK_ONE_UPDATE_SQL.equalsIgnoreCase(command)) {
            Writer outputWriter = getOutputWriter();
            Map<String, Object> argsMap = new HashMap<>();
            argsMap.put("deploymentId", getCommandParam(OPTIONS.DEPLOYMENT_ID, null));
            argsMap.put("force", Boolean.TRUE);
            argsMap.put("outputWriter", outputWriter);
            CommandScope liquibaseCommand = createLiquibaseCommand(database, liquibase, "internalRollbackOneUpdateSQL", argsMap);
            liquibaseCommand.execute();
            return;
        } else if (COMMANDS.DEACTIVATE_CHANGELOG.equalsIgnoreCase(command)) {
            Map<String, Object> argsMap = new HashMap<>();
            CommandScope liquibaseCommand = createLiquibaseCommand(database, liquibase, COMMANDS.DEACTIVATE_CHANGELOG, argsMap);
            liquibaseCommand.addArgumentValue(DeactivateChangelogCommandStep.CHANGELOG_FILE_ARG, changeLogFile);
            liquibaseCommand.execute();
            return;
        } else if (COMMANDS.REGISTER_CHANGELOG.equalsIgnoreCase(command)) {
            Map<String, Object> argsMap = new HashMap<>();
            CommandScope liquibaseCommand = createLiquibaseCommand(database, liquibase, COMMANDS.REGISTER_CHANGELOG, argsMap);
            if (hubProjectId != null && hubProjectName != null) {
                throw new LiquibaseException("\nThe 'registerchangelog' command failed because too many parameters were provided. Command expects a Hub project ID or new Hub project name, but not both.\n");
            }
            try {
                if (hubProjectId != null) {
                    try {
                        liquibaseCommand.addArgumentValue(RegisterChangelogCommandStep.HUB_PROJECT_ID_ARG, UUID.fromString(hubProjectId));
                    } catch (IllegalArgumentException e) {
                        throw new LiquibaseException("The command '" + command + "' failed because parameter 'hubProjectId' has invalid value '" + hubProjectId + "'. Learn more at https://hub.liquibase.com");
                    }
                }
            } catch (IllegalArgumentException e) {
                throw new LiquibaseException("Unexpected hubProjectId format: " + hubProjectId, e);
            }
            if (hubProjectName != null) {
                liquibaseCommand.addArgumentValue(RegisterChangelogCommandStep.HUB_PROJECT_NAME_ARG.getName(), hubProjectName);
            }
            liquibaseCommand.execute();
            return;
        } else if (COMMANDS.SYNC_HUB.equalsIgnoreCase(command)) {
            executeSyncHub(database, liquibase);
            return;
        } else if (COMMANDS.DROP_ALL.equalsIgnoreCase(command)) {
            CommandScope dropAllCommand = new CommandScope("internalDropAll");
            if (hubConnectionId != null) {
                dropAllCommand.addArgumentValue(InternalDropAllCommandStep.HUB_CONNECTION_ID_ARG, UUID.fromString(hubConnectionId));
            }
            if (hubProjectId != null) {
                dropAllCommand.addArgumentValue(InternalDropAllCommandStep.HUB_PROJECT_ID_ARG, UUID.fromString(hubProjectId));
            }
            dropAllCommand.addArgumentValue(InternalDropAllCommandStep.DATABASE_ARG, liquibase.getDatabase()).addArgumentValue(InternalDropAllCommandStep.SCHEMAS_ARG, InternalSnapshotCommandStep.parseSchemas(database, getSchemaParams(database))).addArgumentValue(InternalDropAllCommandStep.CHANGELOG_FILE_ARG, changeLogFile);
            dropAllCommand.execute();
            return;
        } else if (COMMANDS.STATUS.equalsIgnoreCase(command)) {
            boolean runVerbose = false;
            if (commandParams.contains("--" + OPTIONS.VERBOSE)) {
                runVerbose = true;
            }
            liquibase.reportStatus(runVerbose, new Contexts(contexts), new LabelExpression(labels), getOutputWriter());
            return;
        } else if (COMMANDS.UNEXPECTED_CHANGESETS.equalsIgnoreCase(command)) {
            boolean runVerbose = false;
            if (commandParams.contains("--" + OPTIONS.VERBOSE)) {
                runVerbose = true;
            }
            liquibase.reportUnexpectedChangeSets(runVerbose, contexts, getOutputWriter());
            return;
        } else if (COMMANDS.VALIDATE.equalsIgnoreCase(command)) {
            liquibase.validate();
            Scope.getCurrentScope().getUI().sendMessage(coreBundle.getString("no.validation.errors.found"));
            return;
        } else if (COMMANDS.CLEAR_CHECKSUMS.equalsIgnoreCase(command)) {
            liquibase.clearCheckSums();
            return;
        } else if (COMMANDS.CALCULATE_CHECKSUM.equalsIgnoreCase(command)) {
            CheckSum checkSum = null;
            checkSum = liquibase.calculateCheckSum(commandParams.iterator().next());
            Scope.getCurrentScope().getUI().sendMessage(checkSum.toString());
            return;
        } else if (COMMANDS.DB_DOC.equalsIgnoreCase(command)) {
            if (commandParams.isEmpty()) {
                throw new CommandLineParsingException(coreBundle.getString("dbdoc.requires.output.directory"));
            }
            if (changeLogFile == null) {
                throw new CommandLineParsingException(coreBundle.getString("dbdoc.requires.changelog.parameter"));
            }
            liquibase.generateDocumentation(commandParams.iterator().next(), contexts);
            return;
        }
        try {
            if (COMMANDS.UPDATE.equalsIgnoreCase(command)) {
                liquibase.update(new Contexts(contexts), new LabelExpression(labels));
            } else if (COMMANDS.CHANGELOG_SYNC.equalsIgnoreCase(command)) {
                liquibase.changeLogSync(new Contexts(contexts), new LabelExpression(labels));
            } else if (COMMANDS.CHANGELOG_SYNC_SQL.equalsIgnoreCase(command)) {
                liquibase.changeLogSync(new Contexts(contexts), new LabelExpression(labels), getOutputWriter());
            } else if (COMMANDS.CHANGELOG_SYNC_TO_TAG.equalsIgnoreCase(command)) {
                if ((commandParams == null) || commandParams.isEmpty()) {
                    throw new CommandLineParsingException(String.format(coreBundle.getString("command.requires.tag"), COMMANDS.CHANGELOG_SYNC_TO_TAG));
                }
                liquibase.changeLogSync(commandParams.iterator().next(), new Contexts(contexts), new LabelExpression(labels));
            } else if (COMMANDS.CHANGELOG_SYNC_TO_TAG_SQL.equalsIgnoreCase(command)) {
                if ((commandParams == null) || commandParams.isEmpty()) {
                    throw new CommandLineParsingException(String.format(coreBundle.getString("command.requires.tag"), COMMANDS.CHANGELOG_SYNC_TO_TAG_SQL));
                }
                liquibase.changeLogSync(commandParams.iterator().next(), new Contexts(contexts), new LabelExpression(labels), getOutputWriter());
            } else if (COMMANDS.MARK_NEXT_CHANGESET_RAN.equalsIgnoreCase(command)) {
                liquibase.markNextChangeSetRan(new Contexts(contexts), new LabelExpression(labels));
            } else if (COMMANDS.MARK_NEXT_CHANGESET_RAN_SQL.equalsIgnoreCase(command)) {
                liquibase.markNextChangeSetRan(new Contexts(contexts), new LabelExpression(labels), getOutputWriter());
            } else if (COMMANDS.UPDATE_COUNT.equalsIgnoreCase(command)) {
                liquibase.update(Integer.parseInt(commandParams.iterator().next()), new Contexts(contexts), new LabelExpression(labels));
            } else if (COMMANDS.UPDATE_COUNT_SQL.equalsIgnoreCase(command)) {
                liquibase.update(Integer.parseInt(commandParams.iterator().next()), new Contexts(contexts), new LabelExpression(labels), getOutputWriter());
            } else if (COMMANDS.UPDATE_TO_TAG.equalsIgnoreCase(command)) {
                if ((commandParams == null) || commandParams.isEmpty()) {
                    throw new CommandLineParsingException(String.format(coreBundle.getString("command.requires.tag"), COMMANDS.UPDATE_TO_TAG));
                }
                liquibase.update(commandParams.iterator().next(), new Contexts(contexts), new LabelExpression(labels));
            } else if (COMMANDS.UPDATE_TO_TAG_SQL.equalsIgnoreCase(command)) {
                if ((commandParams == null) || commandParams.isEmpty()) {
                    throw new CommandLineParsingException(String.format(coreBundle.getString("command.requires.tag"), COMMANDS.UPDATE_TO_TAG_SQL));
                }
                liquibase.update(commandParams.iterator().next(), new Contexts(contexts), new LabelExpression(labels), getOutputWriter());
            } else if (COMMANDS.UPDATE_SQL.equalsIgnoreCase(command)) {
                liquibase.update(new Contexts(contexts), new LabelExpression(labels), getOutputWriter());
            } else if (COMMANDS.ROLLBACK.equalsIgnoreCase(command)) {
                if (getCommandArgument() == null) {
                    throw new CommandLineParsingException(String.format(coreBundle.getString("command.requires.tag"), COMMANDS.ROLLBACK));
                }
                liquibase.rollback(getCommandArgument(), getCommandParam(COMMANDS.ROLLBACK_SCRIPT, null), new Contexts(contexts), new LabelExpression(labels));
            } else if (COMMANDS.ROLLBACK_TO_DATE.equalsIgnoreCase(command)) {
                if (getCommandArgument() == null) {
                    throw new CommandLineParsingException(String.format(coreBundle.getString("command.requires.timestamp"), COMMANDS.ROLLBACK_TO_DATE));
                }
                liquibase.rollback(new ISODateFormat().parse(getCommandArgument()), getCommandParam(COMMANDS.ROLLBACK_SCRIPT, null), new Contexts(contexts), new LabelExpression(labels));
            } else if (COMMANDS.ROLLBACK_COUNT.equalsIgnoreCase(command)) {
                liquibase.rollback(Integer.parseInt(getCommandArgument()), getCommandParam(COMMANDS.ROLLBACK_SCRIPT, null), new Contexts(contexts), new LabelExpression(labels));
            } else if (COMMANDS.ROLLBACK_SQL.equalsIgnoreCase(command)) {
                if (getCommandArgument() == null) {
                    throw new CommandLineParsingException(String.format(coreBundle.getString("command.requires.tag"), COMMANDS.ROLLBACK_SQL));
                }
                liquibase.rollback(getCommandArgument(), getCommandParam(COMMANDS.ROLLBACK_SCRIPT, null), new Contexts(contexts), new LabelExpression(labels), getOutputWriter());
            } else if (COMMANDS.ROLLBACK_TO_DATE_SQL.equalsIgnoreCase(command)) {
                if (getCommandArgument() == null) {
                    throw new CommandLineParsingException(String.format(coreBundle.getString("command.requires.timestamp"), COMMANDS.ROLLBACK_TO_DATE_SQL));
                }
                liquibase.rollback(new ISODateFormat().parse(getCommandArgument()), getCommandParam(COMMANDS.ROLLBACK_SCRIPT, null), new Contexts(contexts), new LabelExpression(labels), getOutputWriter());
            } else if (COMMANDS.ROLLBACK_COUNT_SQL.equalsIgnoreCase(command)) {
                if (getCommandArgument() == null) {
                    throw new CommandLineParsingException(String.format(coreBundle.getString("command.requires.count"), COMMANDS.ROLLBACK_COUNT_SQL));
                }
                liquibase.rollback(Integer.parseInt(getCommandArgument()), getCommandParam(COMMANDS.ROLLBACK_SCRIPT, null), new Contexts(contexts), new LabelExpression(labels), getOutputWriter());
            } else if (COMMANDS.FUTURE_ROLLBACK_SQL.equalsIgnoreCase(command)) {
                liquibase.futureRollbackSQL(new Contexts(contexts), new LabelExpression(labels), getOutputWriter());
            } else if (COMMANDS.FUTURE_ROLLBACK_COUNT_SQL.equalsIgnoreCase(command)) {
                if (getCommandArgument() == null) {
                    throw new CommandLineParsingException(String.format(coreBundle.getString("command.requires.count"), COMMANDS.FUTURE_ROLLBACK_COUNT_SQL));
                }
                liquibase.futureRollbackSQL(Integer.valueOf(getCommandArgument()), new Contexts(contexts), new LabelExpression(labels), getOutputWriter());
            } else if (COMMANDS.FUTURE_ROLLBACK_FROM_TAG_SQL.equalsIgnoreCase(command)) {
                if (getCommandArgument() == null) {
                    throw new CommandLineParsingException(String.format(coreBundle.getString("command.requires.tag"), COMMANDS.FUTURE_ROLLBACK_FROM_TAG_SQL));
                }
                liquibase.futureRollbackSQL(getCommandArgument(), new Contexts(contexts), new LabelExpression(labels), getOutputWriter());
            } else if (COMMANDS.UPDATE_TESTING_ROLLBACK.equalsIgnoreCase(command)) {
                liquibase.updateTestingRollback(new Contexts(contexts), new LabelExpression(labels));
            } else if (COMMANDS.HISTORY.equalsIgnoreCase(command)) {
                CommandScope historyCommand = new CommandScope("internalHistory");
                historyCommand.addArgumentValue(InternalHistoryCommandStep.DATABASE_ARG, database);
                historyCommand.setOutput(getOutputStream());
                historyCommand.execute();
            } else {
                throw new CommandLineParsingException(String.format(coreBundle.getString("command.unknown"), command));
            }
        } catch (ParseException ignored) {
            throw new CommandLineParsingException(coreBundle.getString("timeformat.invalid"));
        }
    } finally {
        try {
            if (database != null) {
                database.rollback();
                database.close();
            }
        } catch (DatabaseException e) {
            Scope.getCurrentScope().getLog(getClass()).warning(coreBundle.getString("problem.closing.connection"), e);
        }
    }
}
Also used : ResourceAccessor(liquibase.resource.ResourceAccessor) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ISODateFormat(liquibase.util.ISODateFormat) StandardObjectChangeFilter(liquibase.diff.output.StandardObjectChangeFilter) ChangeExecListener(liquibase.changelog.visitor.ChangeExecListener) Database(liquibase.database.Database) CommandScope(liquibase.command.CommandScope) ObjectChangeFilter(liquibase.diff.output.ObjectChangeFilter) StandardObjectChangeFilter(liquibase.diff.output.StandardObjectChangeFilter) LockService(liquibase.lockservice.LockService) DiffOutputControl(liquibase.diff.output.DiffOutputControl) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) CheckSum(liquibase.change.CheckSum) CompareControl(liquibase.diff.compare.CompareControl) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ParseException(java.text.ParseException) JarFile(java.util.jar.JarFile) CommandResults(liquibase.command.CommandResults)

Aggregations

ISODateFormat (liquibase.util.ISODateFormat)14 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)6 Date (java.util.Date)5 ParseException (java.text.ParseException)4 DatabaseException (liquibase.exception.DatabaseException)3 DatabaseObject (liquibase.structure.DatabaseObject)3 ArrayList (java.util.ArrayList)2 Matcher (java.util.regex.Matcher)2 Change (liquibase.change.Change)2 LiquibaseException (liquibase.exception.LiquibaseException)2 ParsedNodeException (liquibase.parser.core.ParsedNodeException)2 CSVReader (liquibase.util.csv.CSVReader)2 CSVWriter (liquibase.util.csv.CSVWriter)2 IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringWriter (java.io.StringWriter)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Timestamp (java.sql.Timestamp)1