Search in sources :

Example 21 with PostgresDatabase

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

the class CreateSequenceGeneratorTest method postgresDatabaseSupportIfNotExistsByVersion.

@Test
public void postgresDatabaseSupportIfNotExistsByVersion() throws Exception {
    DatabaseConnection dbConnection = mock(DatabaseConnection.class);
    when(dbConnection.getDatabaseMajorVersion()).thenReturn(9);
    when(dbConnection.getDatabaseMinorVersion()).thenReturn(4);
    PostgresDatabase database = spy(new PostgresDatabase());
    database.setConnection(dbConnection);
    doReturn(SEQUENCE_NAME).when(database).escapeSequenceName(CATALOG_NAME, SCHEMA_NAME, SEQUENCE_NAME);
    CreateSequenceStatement createSequenceStatement = createSampleSqlStatement();
    createSequenceStatement.setStartValue(new BigInteger("1"));
    // verify that for version <= 9.4 no IF NOT EXISTS is not in the statement
    Sql[] sql = new CreateSequenceGenerator().generateSql(createSequenceStatement, database, new MockSqlGeneratorChain());
    assertThat(sql).isNotEmpty().hasSize(1);
    assertThat(sql[0].toSql()).doesNotContain("IF NOT EXISTS");
    // verify that if no version is available the optional no IF NOT EXISTS is not in the statement
    reset(dbConnection);
    when(dbConnection.getDatabaseMajorVersion()).thenThrow(DatabaseException.class);
    sql = new CreateSequenceGenerator().generateSql(createSequenceStatement, database, new MockSqlGeneratorChain());
    assertThat(sql).isNotEmpty().hasSize(1);
    assertThat(sql[0].toSql()).doesNotContain("IF NOT EXISTS");
    reset(dbConnection);
    when(dbConnection.getDatabaseMajorVersion()).thenReturn(9);
    when(dbConnection.getDatabaseMinorVersion()).thenReturn(5);
    sql = new CreateSequenceGenerator().generateSql(createSequenceStatement, database, new MockSqlGeneratorChain());
    assertThat(sql).isNotEmpty().hasSize(1);
    assertThat(sql[0].toSql()).contains("IF NOT EXISTS");
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) CreateSequenceStatement(liquibase.statement.core.CreateSequenceStatement) BigInteger(java.math.BigInteger) DatabaseConnection(liquibase.database.DatabaseConnection) Sql(liquibase.sql.Sql) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain) Test(org.junit.Test) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest)

Example 22 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 23 with PostgresDatabase

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

the class CharType method toDatabaseDataType.

@Override
public DatabaseDataType toDatabaseDataType(Database database) {
    if (database instanceof MSSQLDatabase) {
        Object[] parameters = getParameters();
        if (parameters.length > 0) {
            String param1 = parameters[0].toString();
            if (!param1.matches("\\d+") || (new BigInteger(param1).compareTo(BigInteger.valueOf(8000)) > 0)) {
                DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("char"), 8000);
                type.addAdditionalInformation(getAdditionalInformation());
                return type;
            }
        }
        if (parameters.length == 0) {
            parameters = new Object[] { 1 };
        } else if (parameters.length > 1) {
            parameters = Arrays.copyOfRange(parameters, 0, 1);
        }
        DatabaseDataType type = new DatabaseDataType(database.escapeDataTypeName("char"), parameters);
        type.addAdditionalInformation(getAdditionalInformation());
        return type;
    } else if (database instanceof PostgresDatabase) {
        if ((getParameters() != null) && (getParameters().length == 1) && "2147483647".equals(getParameters()[0].toString())) {
            DatabaseDataType type = new DatabaseDataType("CHARACTER");
            type.addAdditionalInformation("VARYING");
            return type;
        }
        return super.toDatabaseDataType(database);
    } else if (database instanceof H2Database) {
        if (getRawDefinition().toLowerCase(Locale.US).contains("large object")) {
            return new DatabaseDataType("CHARACTER LARGE OBJECT");
        }
    }
    return super.toDatabaseDataType(database);
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) DatabaseDataType(liquibase.datatype.DatabaseDataType) BigInteger(java.math.BigInteger) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) H2Database(liquibase.database.core.H2Database)

Example 24 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 {
        JdbcUtil.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 25 with PostgresDatabase

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

the class BooleanTypeTest method postgresqlBitString.

@Test
public void postgresqlBitString() {
    BooleanType bt = new BooleanType();
    bt.finishInitialization("bit(12)");
    String expected = "b'111111111111'::\"bit\"";
    assertEquals(expected, bt.objectToSql("'111111111111'", new PostgresDatabase()));
    assertEquals(expected, bt.objectToSql("b'111111111111'", new PostgresDatabase()));
    assertEquals(expected, bt.objectToSql("'111111111111'::bit", new PostgresDatabase()));
    assertEquals(expected, bt.objectToSql("b'111111111111'::bit", new PostgresDatabase()));
    assertEquals(expected, bt.objectToSql("'111111111111'::\"bit\"", new PostgresDatabase()));
    assertEquals(expected, bt.objectToSql("b'111111111111'::\"bit\"", new PostgresDatabase()));
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) BooleanType(liquibase.datatype.core.BooleanType) Test(org.junit.Test)

Aggregations

PostgresDatabase (liquibase.database.core.PostgresDatabase)33 Test (org.junit.Test)19 Sql (liquibase.sql.Sql)16 Database (liquibase.database.Database)11 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)11 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)11 MySQLDatabase (liquibase.database.core.MySQLDatabase)10 OracleDatabase (liquibase.database.core.OracleDatabase)10 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)9 AbstractDb2Database (liquibase.database.core.AbstractDb2Database)8 H2Database (liquibase.database.core.H2Database)7 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)7 SybaseDatabase (liquibase.database.core.SybaseDatabase)7 BigInteger (java.math.BigInteger)6 ColumnConfig (liquibase.change.ColumnConfig)6 DerbyDatabase (liquibase.database.core.DerbyDatabase)6 HsqlDatabase (liquibase.database.core.HsqlDatabase)6 CreateTableStatement (liquibase.statement.core.CreateTableStatement)6 MockDatabaseConnection (liquibase.database.MockDatabaseConnection)5 ArrayList (java.util.ArrayList)4