Search in sources :

Example 1 with CommandResult

use of liquibase.command.CommandResult in project liquibase by liquibase.

the class ConvertCommand method run.

@Override
protected CommandResult run() throws Exception {
    List<ResourceAccessor> openers = new ArrayList<ResourceAccessor>();
    openers.add(new FileSystemResourceAccessor());
    openers.add(new ClassLoaderResourceAccessor());
    if (classpath != null) {
        openers.add(new FileSystemResourceAccessor(classpath));
    }
    ResourceAccessor resourceAccessor = new CompositeResourceAccessor(openers);
    ChangeLogParser sourceParser = ChangeLogParserFactory.getInstance().getParser(src, resourceAccessor);
    ChangeLogSerializer outSerializer = ChangeLogSerializerFactory.getInstance().getSerializer(out);
    DatabaseChangeLog changeLog = sourceParser.parse(src, new ChangeLogParameters(), resourceAccessor);
    File outFile = new File(out);
    if (!outFile.exists()) {
        outFile.getParentFile().mkdirs();
    }
    FileOutputStream outputStream = new FileOutputStream(outFile);
    try {
        outSerializer.write(changeLog.getChangeSets(), outputStream);
    } finally {
        outputStream.flush();
        outputStream.close();
    }
    return new CommandResult("Converted successfully");
}
Also used : CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ResourceAccessor(liquibase.resource.ResourceAccessor) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ArrayList(java.util.ArrayList) ChangeLogSerializer(liquibase.serializer.ChangeLogSerializer) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) CommandResult(liquibase.command.CommandResult) CompositeResourceAccessor(liquibase.resource.CompositeResourceAccessor) ChangeLogParameters(liquibase.changelog.ChangeLogParameters) ChangeLogParser(liquibase.parser.ChangeLogParser) FileOutputStream(java.io.FileOutputStream) FileSystemResourceAccessor(liquibase.resource.FileSystemResourceAccessor) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) File(java.io.File)

Example 2 with CommandResult

use of liquibase.command.CommandResult in project liquibase by liquibase.

the class DiffCommand method run.

@Override
protected CommandResult run() throws Exception {
    DiffResult diffResult = createDiffResult();
    new DiffToReport(diffResult, outputStream).print();
    return new CommandResult("OK");
}
Also used : DiffToReport(liquibase.diff.output.report.DiffToReport) DiffResult(liquibase.diff.DiffResult) CommandResult(liquibase.command.CommandResult)

Example 3 with CommandResult

use of liquibase.command.CommandResult in project liquibase by liquibase.

the class DiffToChangeLogCommand method run.

@Override
protected CommandResult run() throws Exception {
    DiffResult diffResult = createDiffResult();
    PrintStream outputStream = this.getOutputStream();
    if (outputStream == null) {
        outputStream = System.out;
    }
    if (StringUtils.trimToNull(changeLogFile) == null) {
        createDiffToChangeLogObject(diffResult).print(outputStream);
    } else {
        createDiffToChangeLogObject(diffResult).print(changeLogFile);
    }
    return new CommandResult("OK");
}
Also used : PrintStream(java.io.PrintStream) DiffResult(liquibase.diff.DiffResult) CommandResult(liquibase.command.CommandResult)

Example 4 with CommandResult

use of liquibase.command.CommandResult in project liquibase by liquibase.

the class DropAllCommand method run.

@Override
protected CommandResult run() throws Exception {
    try {
        LockServiceFactory.getInstance().getLockService(database).waitForLock();
        for (CatalogAndSchema schema : schemas) {
            log.info("Dropping Database Objects in schema: " + schema);
            checkLiquibaseTables(false, null, new Contexts(), new LabelExpression());
            database.dropDatabaseObjects(schema);
        }
    } catch (DatabaseException e) {
        throw e;
    } catch (Exception e) {
        throw new DatabaseException(e);
    } finally {
        LockServiceFactory.getInstance().getLockService(database).destroy();
        resetServices();
    }
    return new CommandResult("All objects dropped from " + database.getConnection().getConnectionUserName() + "@" + database.getConnection().getURL());
}
Also used : LabelExpression(liquibase.LabelExpression) CatalogAndSchema(liquibase.CatalogAndSchema) Contexts(liquibase.Contexts) DatabaseException(liquibase.exception.DatabaseException) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException) CommandResult(liquibase.command.CommandResult)

Example 5 with CommandResult

use of liquibase.command.CommandResult in project liquibase by liquibase.

the class ExecuteSqlCommand method run.

@Override
protected CommandResult run() throws Exception {
    Executor executor = ExecutorService.getInstance().getExecutor(database);
    String sqlText;
    if (sqlFile == null) {
        sqlText = sql;
    } else {
        File file = new File(sqlFile);
        if (!file.exists()) {
            throw new LiquibaseException(String.format("The file '%s' does not exist", file.getCanonicalPath()));
        }
        sqlText = FileUtil.getContents(file);
    }
    String out = "";
    String[] sqlStrings = StringUtils.processMutliLineSQL(sqlText, true, true, delimiter);
    for (String sql : sqlStrings) {
        if (sql.toLowerCase().matches("\\s*select .*")) {
            List<Map<String, ?>> rows = executor.queryForList(new RawSqlStatement(sql));
            out += "Output of " + sql + ":\n";
            if (rows.size() == 0) {
                out += "-- Empty Resultset --\n";
            } else {
                SortedSet<String> keys = new TreeSet<String>();
                for (Map<String, ?> row : rows) {
                    keys.addAll(row.keySet());
                }
                out += StringUtils.join(keys, " | ") + " |\n";
                for (Map<String, ?> row : rows) {
                    for (String key : keys) {
                        out += row.get(key) + " | ";
                    }
                    out += "\n";
                }
            }
        } else {
            executor.execute(new RawSqlStatement(sql));
            out += "Successfully Executed: " + sql + "\n";
        }
        out += "\n";
    }
    database.commit();
    return new CommandResult(out.trim());
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) Executor(liquibase.executor.Executor) TreeSet(java.util.TreeSet) LiquibaseException(liquibase.exception.LiquibaseException) File(java.io.File) Map(java.util.Map) CommandResult(liquibase.command.CommandResult)

Aggregations

CommandResult (liquibase.command.CommandResult)6 DiffResult (liquibase.diff.DiffResult)3 File (java.io.File)2 PrintStream (java.io.PrintStream)2 LiquibaseException (liquibase.exception.LiquibaseException)2 FileOutputStream (java.io.FileOutputStream)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 TreeSet (java.util.TreeSet)1 CatalogAndSchema (liquibase.CatalogAndSchema)1 Contexts (liquibase.Contexts)1 LabelExpression (liquibase.LabelExpression)1 ChangeLogParameters (liquibase.changelog.ChangeLogParameters)1 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)1 DiffToChangeLog (liquibase.diff.output.changelog.DiffToChangeLog)1 DiffToReport (liquibase.diff.output.report.DiffToReport)1 DatabaseException (liquibase.exception.DatabaseException)1 Executor (liquibase.executor.Executor)1 ChangeLogParser (liquibase.parser.ChangeLogParser)1 ClassLoaderResourceAccessor (liquibase.resource.ClassLoaderResourceAccessor)1