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