Search in sources :

Example 1 with Schema

use of liquibase.structure.core.Schema in project liquibase by liquibase.

the class CommandLineUtils method initializeDatabase.

/**
     * Executes RawSqlStatements particular to each database engine to set the default schema for the given Database
     *  
     * @param username            The username used for the connection. Used with MSSQL databases
     * @param defaultCatalogName  Catalog name and schema name are similar concepts. Used if defaultCatalogName is null. 
     * @param defaultSchemaName   Catalog name and schema name are similar concepts. Catalog is used with Oracle, DB2 and MySQL, and takes
     *                             precedence over the schema name.
     * @param database            Which Database object is affected by the initialization.
     * @throws DatabaseException
     */
public static void initializeDatabase(String username, String defaultCatalogName, String defaultSchemaName, Database database) throws DatabaseException {
    if ((defaultCatalogName != null || defaultSchemaName != null) && !(database.getConnection() instanceof OfflineConnection)) {
        if (database instanceof OracleDatabase) {
            String schema = defaultCatalogName;
            if (schema == null) {
                schema = defaultSchemaName;
            }
            ExecutorService.getInstance().getExecutor(database).execute(new RawSqlStatement("ALTER SESSION SET CURRENT_SCHEMA=" + database.escapeObjectName(schema, Schema.class)));
        } else if (database instanceof MSSQLDatabase && defaultSchemaName != null) {
            boolean sql2005OrLater = true;
            try {
                sql2005OrLater = database.getDatabaseMajorVersion() >= 9;
            } catch (DatabaseException e) {
            // Assume SQL Server 2005 or later
            }
            if (sql2005OrLater && username != null) {
                ExecutorService.getInstance().getExecutor(database).execute(new RawSqlStatement("IF USER_NAME() <> N'dbo'\r\n" + "BEGIN\r\n" + "	DECLARE @sql [nvarchar](MAX)\r\n" + "	SELECT @sql = N'ALTER USER ' + QUOTENAME(USER_NAME()) + N' WITH DEFAULT_SCHEMA = " + database.escapeStringForDatabase(database.escapeObjectName(username, DatabaseObject.class)) + "'\r\n" + "	EXEC sp_executesql @sql\r\n" + "END"));
            }
        } else if (database instanceof PostgresDatabase && defaultSchemaName != null) {
            ExecutorService.getInstance().getExecutor(database).execute(new RawSqlStatement("SET SEARCH_PATH TO " + database.escapeObjectName(defaultSchemaName, Schema.class)));
        } else if (database instanceof DB2Database) {
            String schema = defaultCatalogName;
            if (schema == null) {
                schema = defaultSchemaName;
            }
            ExecutorService.getInstance().getExecutor(database).execute(new RawSqlStatement("SET CURRENT SCHEMA " + schema));
        } else if (database instanceof MySQLDatabase) {
            String schema = defaultCatalogName;
            if (schema == null) {
                schema = defaultSchemaName;
            }
            ExecutorService.getInstance().getExecutor(database).execute(new RawSqlStatement("USE " + schema));
        }
    }
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) CatalogAndSchema(liquibase.CatalogAndSchema) Schema(liquibase.structure.core.Schema) OfflineConnection(liquibase.database.OfflineConnection)

Example 2 with Schema

use of liquibase.structure.core.Schema in project liquibase by liquibase.

the class ColumnExistsPrecondition method checkUsingSnapshot.

private void checkUsingSnapshot(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
    Column example = new Column();
    if (StringUtils.trimToNull(getTableName()) != null) {
        example.setRelation(new Table().setName(database.correctObjectName(getTableName(), Table.class)).setSchema(new Schema(getCatalogName(), getSchemaName())));
    }
    example.setName(database.correctObjectName(getColumnName(), Column.class));
    try {
        if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
            throw new PreconditionFailedException("Column '" + database.escapeColumnName(catalogName, schemaName, getTableName(), getColumnName()) + "' does not exist", changeLog, this);
        }
    } catch (LiquibaseException e) {
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) Schema(liquibase.structure.core.Schema)

Example 3 with Schema

use of liquibase.structure.core.Schema in project liquibase by liquibase.

the class IndexExistsPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
    try {
        Schema schema = new Schema(getCatalogName(), getSchemaName());
        Index example = new Index();
        String tableName = StringUtils.trimToNull(getTableName());
        if (tableName != null) {
            example.setTable((Table) new Table().setName(database.correctObjectName(getTableName(), Table.class)).setSchema(schema));
        }
        example.setName(database.correctObjectName(getIndexName(), Index.class));
        if (StringUtils.trimToNull(getColumnNames()) != null) {
            for (String column : getColumnNames().split("\\s*,\\s*")) {
                example.addColumn(new Column(database.correctObjectName(column, Column.class)));
            }
        }
        if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
            String name = "";
            if (getIndexName() != null) {
                name += database.escapeObjectName(getIndexName(), Index.class);
            }
            if (tableName != null) {
                name += " on " + database.escapeObjectName(getTableName(), Table.class);
                if (StringUtils.trimToNull(getColumnNames()) != null) {
                    name += " columns " + getColumnNames();
                }
            }
            throw new PreconditionFailedException("Index " + name + " does not exist", changeLog, this);
        }
    } catch (Exception e) {
        if (e instanceof PreconditionFailedException) {
            throw (((PreconditionFailedException) e));
        }
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) Schema(liquibase.structure.core.Schema) Index(liquibase.structure.core.Index)

Example 4 with Schema

use of liquibase.structure.core.Schema in project liquibase by liquibase.

the class PrimaryKeyExistsPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
    try {
        PrimaryKey example = new PrimaryKey();
        Table table = new Table();
        table.setSchema(new Schema(getCatalogName(), getSchemaName()));
        if (StringUtils.trimToNull(getTableName()) != null) {
            table.setName(getTableName());
        }
        example.setTable(table);
        example.setName(getPrimaryKeyName());
        if (!SnapshotGeneratorFactory.getInstance().has(example, database)) {
            if (tableName != null) {
                throw new PreconditionFailedException("Primary Key does not exist on " + database.escapeObjectName(getTableName(), Table.class), changeLog, this);
            } else {
                throw new PreconditionFailedException("Primary Key " + database.escapeObjectName(getPrimaryKeyName(), PrimaryKey.class) + " does not exist", changeLog, this);
            }
        }
    } catch (PreconditionFailedException e) {
        throw e;
    } catch (Exception e) {
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Table(liquibase.structure.core.Table) Schema(liquibase.structure.core.Schema) PrimaryKey(liquibase.structure.core.PrimaryKey)

Example 5 with Schema

use of liquibase.structure.core.Schema in project liquibase by liquibase.

the class SequenceExistsPrecondition method check.

@Override
public void check(Database database, DatabaseChangeLog changeLog, ChangeSet changeSet) throws PreconditionFailedException, PreconditionErrorException {
    DatabaseSnapshot snapshot;
    Schema schema = new Schema(getCatalogName(), getSchemaName());
    try {
        if (!SnapshotGeneratorFactory.getInstance().has(new Sequence().setName(getSequenceName()).setSchema(schema), database)) {
            throw new PreconditionFailedException("Sequence " + database.escapeSequenceName(getCatalogName(), getSchemaName(), getSequenceName()) + " does not exist", changeLog, this);
        }
    } catch (LiquibaseException e) {
        throw new PreconditionErrorException(e, changeLog, this);
    }
}
Also used : Schema(liquibase.structure.core.Schema) Sequence(liquibase.structure.core.Sequence) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot)

Aggregations

Schema (liquibase.structure.core.Schema)23 CatalogAndSchema (liquibase.CatalogAndSchema)9 Table (liquibase.structure.core.Table)6 Database (liquibase.database.Database)5 OracleDatabase (liquibase.database.core.OracleDatabase)4 SQLException (java.sql.SQLException)3 AbstractJdbcDatabase (liquibase.database.AbstractJdbcDatabase)3 DatabaseException (liquibase.exception.DatabaseException)3 RawSqlStatement (liquibase.statement.core.RawSqlStatement)3 Column (liquibase.structure.core.Column)3 View (liquibase.structure.core.View)3 InformixDatabase (liquibase.database.core.InformixDatabase)2 CachedRow (liquibase.snapshot.CachedRow)2 JdbcDatabaseSnapshot (liquibase.snapshot.JdbcDatabaseSnapshot)2 UnparsedSql (liquibase.sql.UnparsedSql)2 Catalog (liquibase.structure.core.Catalog)2 Index (liquibase.structure.core.Index)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1