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));
}
}
}
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);
}
}
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);
}
}
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);
}
}
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);
}
}
Aggregations