Search in sources :

Example 31 with CatalogAndSchema

use of liquibase.CatalogAndSchema in project liquibase by liquibase.

the class DropAllTask method executeWithLiquibaseClassloader.

@Override
public void executeWithLiquibaseClassloader() throws BuildException {
    Liquibase liquibase = getLiquibase();
    try {
        if (StringUtils.trimToNull(schemas) != null) {
            List<String> schemaNames = StringUtils.splitAndTrim(this.schemas, ",");
            List<CatalogAndSchema> schemas = new ArrayList<CatalogAndSchema>();
            for (String name : schemaNames) {
                schemas.add(new CatalogAndSchema(catalog, name));
            }
            liquibase.dropAll(schemas.toArray(new CatalogAndSchema[schemas.size()]));
        } else {
            liquibase.dropAll();
        }
    } catch (LiquibaseException e) {
        throw new BuildException("Unable to drop all objects from database. " + e.toString(), e);
    }
}
Also used : Liquibase(liquibase.Liquibase) ArrayList(java.util.ArrayList) LiquibaseException(liquibase.exception.LiquibaseException) BuildException(org.apache.tools.ant.BuildException) CatalogAndSchema(liquibase.CatalogAndSchema)

Example 32 with CatalogAndSchema

use of liquibase.CatalogAndSchema in project liquibase by liquibase.

the class GenerateChangeLogTask method executeWithLiquibaseClassloader.

@Override
public void executeWithLiquibaseClassloader() throws BuildException {
    Liquibase liquibase = getLiquibase();
    Database database = liquibase.getDatabase();
    CatalogAndSchema catalogAndSchema = buildCatalogAndSchema(database);
    DiffOutputControl diffOutputControl = getDiffOutputControl();
    DiffToChangeLog diffToChangeLog = new DiffToChangeLog(diffOutputControl);
    for (ChangeLogOutputFile changeLogOutputFile : changeLogOutputFiles) {
        String encoding = getOutputEncoding(changeLogOutputFile);
        PrintStream printStream = null;
        try {
            FileResource outputFile = changeLogOutputFile.getOutputFile();
            ChangeLogSerializer changeLogSerializer = changeLogOutputFile.getChangeLogSerializer();
            log("Writing change log file " + outputFile.toString(), Project.MSG_INFO);
            printStream = new PrintStream(outputFile.getOutputStream(), true, encoding);
            liquibase.generateChangeLog(catalogAndSchema, diffToChangeLog, printStream, changeLogSerializer);
        } catch (UnsupportedEncodingException e) {
            throw new BuildException("Unable to generate a change log. Encoding [" + encoding + "] is not supported.", e);
        } catch (IOException e) {
            throw new BuildException("Unable to generate a change log. Error creating output stream.", e);
        } catch (ParserConfigurationException e) {
            throw new BuildException("Unable to generate a change log. Error configuring parser.", e);
        } catch (DatabaseException e) {
            throw new BuildException("Unable to generate a change log. " + e.toString(), e);
        } finally {
            FileUtils.close(printStream);
        }
    }
}
Also used : PrintStream(java.io.PrintStream) FileResource(org.apache.tools.ant.types.resources.FileResource) DiffOutputControl(liquibase.diff.output.DiffOutputControl) UnsupportedEncodingException(java.io.UnsupportedEncodingException) JsonChangeLogSerializer(liquibase.serializer.core.json.JsonChangeLogSerializer) StringChangeLogSerializer(liquibase.serializer.core.string.StringChangeLogSerializer) ChangeLogSerializer(liquibase.serializer.ChangeLogSerializer) YamlChangeLogSerializer(liquibase.serializer.core.yaml.YamlChangeLogSerializer) XMLChangeLogSerializer(liquibase.serializer.core.xml.XMLChangeLogSerializer) IOException(java.io.IOException) CatalogAndSchema(liquibase.CatalogAndSchema) ChangeLogOutputFile(liquibase.integration.ant.type.ChangeLogOutputFile) Liquibase(liquibase.Liquibase) Database(liquibase.database.Database) DiffToChangeLog(liquibase.diff.output.changelog.DiffToChangeLog) BuildException(org.apache.tools.ant.BuildException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DatabaseException(liquibase.exception.DatabaseException)

Example 33 with CatalogAndSchema

use of liquibase.CatalogAndSchema in project liquibase by liquibase.

the class CommandLineUtils method doGenerateChangeLog.

public static void doGenerateChangeLog(String changeLogFile, Database originalDatabase, CatalogAndSchema[] schemas, String snapshotTypes, String author, String context, String dataDir, DiffOutputControl diffOutputControl) throws DatabaseException, IOException, ParserConfigurationException, InvalidExampleException, LiquibaseException {
    CompareControl.SchemaComparison[] comparisons = new CompareControl.SchemaComparison[schemas.length];
    int i = 0;
    for (CatalogAndSchema schema : schemas) {
        comparisons[i++] = new CompareControl.SchemaComparison(schema, schema);
    }
    CompareControl compareControl = new CompareControl(comparisons, snapshotTypes);
    diffOutputControl.setDataDir(dataDir);
    GenerateChangeLogCommand command = (GenerateChangeLogCommand) CommandFactory.getInstance().getCommand("generateChangeLog");
    command.setReferenceDatabase(originalDatabase).setSnapshotTypes(snapshotTypes).setOutputStream(System.out).setCompareControl(compareControl);
    command.setChangeLogFile(changeLogFile).setDiffOutputControl(diffOutputControl);
    command.setAuthor(author).setContext(context);
    try {
        command.execute();
    } catch (CommandExecutionException e) {
        throw new LiquibaseException(e);
    }
}
Also used : GenerateChangeLogCommand(liquibase.command.core.GenerateChangeLogCommand) CompareControl(liquibase.diff.compare.CompareControl) CommandExecutionException(liquibase.command.CommandExecutionException) CatalogAndSchema(liquibase.CatalogAndSchema)

Example 34 with CatalogAndSchema

use of liquibase.CatalogAndSchema in project liquibase by liquibase.

the class FindForeignKeyConstraintsGeneratorMySQL method generateSql.

@Override
public Sql[] generateSql(FindForeignKeyConstraintsStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    CatalogAndSchema schema = new CatalogAndSchema(statement.getBaseTableCatalogName(), statement.getBaseTableSchemaName()).customize(database);
    StringBuilder sb = new StringBuilder();
    sb.append("SELECT ");
    sb.append("RC.TABLE_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_NAME).append(", ");
    sb.append("KCU.COLUMN_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_BASE_TABLE_COLUMN_NAME).append(", ");
    sb.append("RC.REFERENCED_TABLE_NAME ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_TABLE_NAME).append(", ");
    sb.append("KCU.REFERENCED_COLUMN_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_FOREIGN_COLUMN_NAME).append(", ");
    sb.append("RC.CONSTRAINT_NAME as ").append(FindForeignKeyConstraintsStatement.RESULT_COLUMN_CONSTRAINT_NAME).append(" ");
    sb.append("FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS RC,");
    sb.append("     INFORMATION_SCHEMA.KEY_COLUMN_USAGE KCU ");
    sb.append("WHERE RC.TABLE_NAME = KCU.TABLE_NAME ");
    sb.append("AND RC.CONSTRAINT_SCHEMA = KCU.CONSTRAINT_SCHEMA ");
    sb.append("AND RC.CONSTRAINT_NAME = KCU.CONSTRAINT_NAME ");
    sb.append("AND RC.TABLE_NAME = '").append(statement.getBaseTableName()).append("' ");
    sb.append("AND RC.CONSTRAINT_SCHEMA = '").append(schema.getCatalogName()).append("'");
    sb.append("AND KCU.TABLE_SCHEMA = '").append(schema.getCatalogName()).append("'");
    return new Sql[] { new UnparsedSql(sb.toString()) };
}
Also used : UnparsedSql(liquibase.sql.UnparsedSql) CatalogAndSchema(liquibase.CatalogAndSchema) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Example 35 with CatalogAndSchema

use of liquibase.CatalogAndSchema in project liquibase by liquibase.

the class AbstractIntegrationTest method setUp.

@Before
public void setUp() throws Exception {
    openConnection(url);
    if (database != null) {
        if (!database.getConnection().getAutoCommit()) {
            database.rollback();
        }
        SnapshotGeneratorFactory.resetAll();
        ExecutorService.getInstance().reset();
        LockServiceFactory.getInstance().resetAll();
        LockServiceFactory.getInstance().getLockService(database).init();
        ChangeLogHistoryServiceFactory.getInstance().resetAll();
        if (database.getConnection() != null) {
            ((JdbcConnection) database.getConnection()).getUnderlyingConnection().createStatement().executeUpdate("drop table " + database.getDatabaseChangeLogLockTableName());
            database.commit();
        }
        SnapshotGeneratorFactory.resetAll();
        LockService lockService = LockServiceFactory.getInstance().getLockService(database);
        database.dropDatabaseObjects(CatalogAndSchema.DEFAULT);
        if (database.supportsSchemas()) {
            database.dropDatabaseObjects(new CatalogAndSchema((String) null, DatabaseTestContext.ALT_SCHEMA));
        }
        if (supportsAltCatalogTests()) {
            if (database.supportsSchemas() && database.supportsCatalogs()) {
                database.dropDatabaseObjects(new CatalogAndSchema(DatabaseTestContext.ALT_CATALOG, DatabaseTestContext.ALT_SCHEMA));
            } else if (database.supportsCatalogs()) {
                database.dropDatabaseObjects(new CatalogAndSchema(DatabaseTestContext.ALT_SCHEMA, null));
            }
        }
        database.commit();
        SnapshotGeneratorFactory.resetAll();
    }
}
Also used : LockService(liquibase.lockservice.LockService) JdbcConnection(liquibase.database.jvm.JdbcConnection) CatalogAndSchema(liquibase.CatalogAndSchema) Before(org.junit.Before)

Aggregations

CatalogAndSchema (liquibase.CatalogAndSchema)36 Database (liquibase.database.Database)9 DatabaseException (liquibase.exception.DatabaseException)9 CompareControl (liquibase.diff.compare.CompareControl)8 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)7 Sql (liquibase.sql.Sql)7 UnparsedSql (liquibase.sql.UnparsedSql)7 Liquibase (liquibase.Liquibase)5 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)5 InvalidExampleException (liquibase.snapshot.InvalidExampleException)5 SQLException (java.sql.SQLException)4 ArrayList (java.util.ArrayList)4 ObjectQuotingStrategy (liquibase.database.ObjectQuotingStrategy)4 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 DiffOutputControl (liquibase.diff.output.DiffOutputControl)4 LiquibaseException (liquibase.exception.LiquibaseException)4 LockService (liquibase.lockservice.LockService)3 SnapshotControl (liquibase.snapshot.SnapshotControl)3 Schema (liquibase.structure.core.Schema)3 IOException (java.io.IOException)2