
org.flywaydb.database.redshift.RedshiftSchema Maven / Gradle / Ivy
/*-
* ========================LICENSE_START=================================
* flyway-database-redshift
* ========================================================================
* Copyright (C) 2010 - 2025 Red Gate Software Ltd
* ========================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =========================LICENSE_END==================================
*/
package org.flywaydb.database.redshift;
import org.flywaydb.core.internal.jdbc.JdbcTemplate;
import org.flywaydb.core.internal.database.base.Schema;
import org.flywaydb.core.internal.database.base.Table;
import org.flywaydb.core.internal.database.base.Type;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* PostgreSQL implementation of Schema.
*/
public class RedshiftSchema extends Schema {
/**
* Creates a new PostgreSQL schema.
*
* @param jdbcTemplate The Jdbc Template for communicating with the DB.
* @param database The database-specific support.
* @param name The name of the schema.
*/
RedshiftSchema(JdbcTemplate jdbcTemplate, RedshiftDatabase database, String name) {
super(jdbcTemplate, database, name);
}
@Override
protected boolean doExists() throws SQLException {
return jdbcTemplate.queryForInt("SELECT COUNT(*) FROM pg_namespace WHERE nspname=?", name) > 0;
}
@Override
protected boolean doEmpty() throws SQLException {
return !jdbcTemplate.queryForBoolean("SELECT EXISTS ( SELECT 1\n" +
" FROM pg_catalog.pg_class c\n" +
" JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace\n" +
" WHERE n.nspname = ?)", name);
}
@Override
protected void doCreate() throws SQLException {
jdbcTemplate.execute("CREATE SCHEMA " + database.quote(name));
}
@Override
protected void doDrop() throws SQLException {
jdbcTemplate.execute("DROP SCHEMA " + database.quote(name) + " CASCADE");
}
@Override
protected void doClean() throws SQLException {
for (String statement : generateDropStatementsForViews()) {
jdbcTemplate.execute(statement);
}
for (Table table : allTables()) {
table.drop();
}
for (String statement : generateDropStatementsForRoutines('a', "FUNCTION", " CASCADE")) {
jdbcTemplate.execute(statement);
}
for (String statement : generateDropStatementsForRoutines('f', "FUNCTION", " CASCADE")) {
jdbcTemplate.execute(statement);
}
for (String statement : generateDropStatementsForRoutines('p', "PROCEDURE", "")) {
jdbcTemplate.execute(statement);
}
}
/**
* Generates the statements for dropping the routines in this schema.
*
* @return The drop statements.
* @throws SQLException when the clean statements could not be generated.
* @kind The kind of object: f for functions, a for aggregate functions, p for procedures
* @objType The type of object for the DROP statement; FUNCTION or PROCEDURE
* @cascade CASCADE if required, blank if not.
*/
private List generateDropStatementsForRoutines(char kind, String objType, String cascade) throws SQLException {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy