Search in sources :

Example 11 with PostgresDatabase

use of liquibase.database.core.PostgresDatabase in project liquibase by liquibase.

the class ColumnExistsPrecondition method checkFast.

private void checkFast(Database database, DatabaseChangeLog changeLog) throws PreconditionFailedException, PreconditionErrorException {
    Statement statement = null;
    try {
        statement = ((JdbcConnection) database.getConnection()).createStatement();
        String schemaName = getSchemaName();
        if (schemaName == null) {
            schemaName = database.getDefaultSchemaName();
        }
        String tableName = getTableName();
        String columnName = getColumnName();
        if (database instanceof PostgresDatabase) {
            String sql = "SELECT 1 FROM pg_attribute a WHERE EXISTS (SELECT 1 FROM pg_class JOIN pg_catalog.pg_namespace ns ON ns.oid = pg_class.relnamespace WHERE lower(ns.nspname)='" + schemaName.toLowerCase() + "' AND lower(relname) = lower('" + tableName + "') AND pg_class.oid = a.attrelid) AND lower(a.attname) = lower('" + columnName + "');";
            try {
                ResultSet rs = statement.executeQuery(sql);
                try {
                    if (rs.next()) {
                        return;
                    } else {
                        // column or table does not exist
                        throw new PreconditionFailedException(format("Column %s.%s.%s does not exist", schemaName, tableName, columnName), changeLog, this);
                    }
                } finally {
                    rs.close();
                }
            } catch (SQLException e) {
                throw new PreconditionErrorException(e, changeLog, this);
            }
        }
        try {
            String sql = format("select t.%s from %s.%s t where 0=1", database.escapeColumnNameList(columnName), database.escapeObjectName(schemaName, Schema.class), database.escapeObjectName(tableName, Table.class));
            statement.executeQuery(sql).close();
            // column exists
            return;
        } catch (SQLException e) {
            // column or table does not exist
            throw new PreconditionFailedException(format("Column %s.%s.%s does not exist", schemaName, tableName, columnName), changeLog, this);
        }
    } catch (DatabaseException e) {
        throw new PreconditionErrorException(e, changeLog, this);
    } finally {
        JdbcUtils.closeStatement(statement);
    }
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) Table(liquibase.structure.core.Table) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Schema(liquibase.structure.core.Schema) ResultSet(java.sql.ResultSet)

Example 12 with PostgresDatabase

use of liquibase.database.core.PostgresDatabase in project liquibase by liquibase.

the class DropIndexGeneratorTest method shouldDropIndexInPostgreSQL.

//    @Test
//    public void execute_defaultSchema() throws Exception {
//        new DatabaseTestTemplate().testOnAvailableDatabases(
//                new SqlStatementDatabaseTest(null, new DropIndexStatement(IDX_NAME, null, TABLE_NAME)) {
//
//                    protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
//                        assertNotNull(snapshot.getIndex(IDX_NAME));
//                    }
//
//                    protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
//                        assertNull(snapshot.getIndex(IDX_NAME));
//                    }
//
//                });
//    }
//
//    //todo: issues with schemas on some databases
////    @Test
////    public void execute_altSchema() throws Exception {
////        new DatabaseTestTemplate().testOnAvailableDatabases(
////                new SqlStatementDatabaseTest(TestContext.ALT_SCHEMA, new DropIndexStatement(ALT_IDX_NAME, TestContext.ALT_SCHEMA, TABLE_NAME)) {
////
////                    protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) {
////                        //todo: how do we assert indexes within a schema snapshot?
//////                        assertNotNull(snapshot.getIndex(ALT_IDX_NAME));
////                    }
////
////                    protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) {
////                        //todo: how do we assert indexes within a schema snapshot?
//////                        assertNull(snapshot.getIndex(ALT_IDX_NAME));
////                    }
////
////                });
////    }
@Test
public void shouldDropIndexInPostgreSQL() throws Exception {
    DropIndexGenerator dropIndexGenerator = new DropIndexGenerator();
    DropIndexStatement statement = new DropIndexStatement("indexName", "defaultCatalog", "defaultSchema", "aTable", null);
    Database database = new PostgresDatabase();
    SortedSet<SqlGenerator> sqlGenerators = new TreeSet<SqlGenerator>();
    SqlGeneratorChain sqlGenerationChain = new SqlGeneratorChain(sqlGenerators);
    Sql[] sqls = dropIndexGenerator.generateSql(statement, database, sqlGenerationChain);
    assertEquals("DROP INDEX \"defaultSchema\".\"indexName\"", sqls[0].toSql());
    statement = new DropIndexStatement("index_name", "default_catalog", "default_schema", "a_table", null);
    sqls = dropIndexGenerator.generateSql(statement, database, sqlGenerationChain);
    assertEquals("DROP INDEX default_schema.index_name", sqls[0].toSql());
    statement = new DropIndexStatement("index_name", null, null, "a_table", null);
    sqls = dropIndexGenerator.generateSql(statement, database, sqlGenerationChain);
    assertEquals("DROP INDEX index_name", sqls[0].toSql());
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) TreeSet(java.util.TreeSet) SqlGenerator(liquibase.sqlgenerator.SqlGenerator) PostgresDatabase(liquibase.database.core.PostgresDatabase) Database(liquibase.database.Database) SqlGeneratorChain(liquibase.sqlgenerator.SqlGeneratorChain) DropIndexStatement(liquibase.statement.core.DropIndexStatement) Sql(liquibase.sql.Sql) Test(org.junit.Test)

Example 13 with PostgresDatabase

use of liquibase.database.core.PostgresDatabase in project liquibase by liquibase.

the class InsertOrUpdateGeneratorPostgresTest method testInsertSequenceValWithSchema.

@Test
public void testInsertSequenceValWithSchema() {
    PostgresDatabase postgresDatabase = new PostgresDatabase();
    InsertGenerator generator = new InsertGenerator();
    InsertStatement statement = new InsertStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME);
    ColumnConfig columnConfig = new ColumnConfig();
    columnConfig.setValueSequenceNext(new SequenceNextValueFunction(SCHEMA_NAME + '.' + SEQUENCE_NAME));
    columnConfig.setName("col3");
    statement.addColumn(columnConfig);
    Sql[] sql = generator.generateSql(statement, postgresDatabase, null);
    String theSql = sql[0].toSql();
    assertEquals(String.format("INSERT INTO %s.%s (col3) VALUES (nextval('%s.%s'))", SCHEMA_NAME, TABLE_NAME, SCHEMA_NAME, SEQUENCE_NAME), theSql);
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) ColumnConfig(liquibase.change.ColumnConfig) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) InsertStatement(liquibase.statement.core.InsertStatement) Sql(liquibase.sql.Sql) Test(org.junit.Test)

Example 14 with PostgresDatabase

use of liquibase.database.core.PostgresDatabase in project liquibase by liquibase.

the class CreateTableGeneratorTest method testAutoIncrementStartWithIncrementByPostgresDatabase.

@Test
public void testAutoIncrementStartWithIncrementByPostgresDatabase() throws Exception {
    for (Database database : TestContext.getInstance().getAllDatabases()) {
        if (database instanceof PostgresDatabase) {
            CreateTableStatement statement = new CreateTableStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME);
            statement.addColumn(COLUMN_NAME1, DataTypeFactory.getInstance().fromDescription("BIGINT{autoIncrement:true}", database), new AutoIncrementConstraint(COLUMN_NAME1, BigInteger.ZERO, BigInteger.TEN));
            Sql[] generatedSql = this.generatorUnderTest.generateSql(statement, database, null);
            // start with and increment by supported over generated sequence
            assertEquals("CREATE TABLE SCHEMA_NAME.TABLE_NAME (COLUMN1_NAME BIGSERIAL)", generatedSql[0].toSql());
        }
    }
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) CreateTableStatement(liquibase.statement.core.CreateTableStatement) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) DerbyDatabase(liquibase.database.core.DerbyDatabase) DB2Database(liquibase.database.core.DB2Database) H2Database(liquibase.database.core.H2Database) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) SybaseDatabase(liquibase.database.core.SybaseDatabase) SybaseASADatabase(liquibase.database.core.SybaseASADatabase) Database(liquibase.database.Database) HsqlDatabase(liquibase.database.core.HsqlDatabase) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Example 15 with PostgresDatabase

use of liquibase.database.core.PostgresDatabase in project liquibase by liquibase.

the class CreateTableGeneratorTest method testAutoIncrementPostgresDatabase.

@Test
public void testAutoIncrementPostgresDatabase() throws Exception {
    for (Database database : TestContext.getInstance().getAllDatabases()) {
        if (database instanceof PostgresDatabase) {
            CreateTableStatement statement = new CreateTableStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME);
            statement.addColumn(COLUMN_NAME1, DataTypeFactory.getInstance().fromDescription("BIGINT{autoIncrement:true}", database), new AutoIncrementConstraint(COLUMN_NAME1));
            Sql[] generatedSql = this.generatorUnderTest.generateSql(statement, database, null);
            assertEquals("CREATE TABLE SCHEMA_NAME.TABLE_NAME (COLUMN1_NAME BIGSERIAL)", generatedSql[0].toSql());
        }
    }
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) CreateTableStatement(liquibase.statement.core.CreateTableStatement) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) DerbyDatabase(liquibase.database.core.DerbyDatabase) DB2Database(liquibase.database.core.DB2Database) H2Database(liquibase.database.core.H2Database) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) SybaseDatabase(liquibase.database.core.SybaseDatabase) SybaseASADatabase(liquibase.database.core.SybaseASADatabase) Database(liquibase.database.Database) HsqlDatabase(liquibase.database.core.HsqlDatabase) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Aggregations

PostgresDatabase (liquibase.database.core.PostgresDatabase)16 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)8 Sql (liquibase.sql.Sql)8 Test (org.junit.Test)8 Database (liquibase.database.Database)7 MySQLDatabase (liquibase.database.core.MySQLDatabase)6 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)6 DB2Database (liquibase.database.core.DB2Database)5 OracleDatabase (liquibase.database.core.OracleDatabase)5 DerbyDatabase (liquibase.database.core.DerbyDatabase)4 H2Database (liquibase.database.core.H2Database)4 HsqlDatabase (liquibase.database.core.HsqlDatabase)4 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)4 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)4 SybaseDatabase (liquibase.database.core.SybaseDatabase)4 CreateTableStatement (liquibase.statement.core.CreateTableStatement)4 ColumnConfig (liquibase.change.ColumnConfig)3 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)3 SqlStatement (liquibase.statement.SqlStatement)3 BigInteger (java.math.BigInteger)2