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