Search in sources :

Example 1 with SqlGeneratorChain

use of liquibase.sqlgenerator.SqlGeneratorChain in project liquibase by liquibase.

the class InsertOrUpdateGeneratorH2Test method testGetUpdateStatement_notOnlyUpdate.

// private static final String SEQUENCE_NAME = "my_sequence";
/**
 * Test method for {@link InsertOrUpdateGeneratorH2#getUpdateStatement(InsertOrUpdateStatement, Database, String, SqlGeneratorChain)}.
 * @throws LiquibaseException
 */
@Test
public void testGetUpdateStatement_notOnlyUpdate() throws LiquibaseException {
    final InsertOrUpdateGeneratorH2 generator = new InsertOrUpdateGeneratorH2();
    final InsertOrUpdateStatement insertOrUpdateStatement = new InsertOrUpdateStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME, "pk1");
    final Database database = new H2Database();
    final SqlGeneratorChain sqlGeneratorChain = null;
    ColumnConfig columnConfig;
    columnConfig = new ColumnConfig();
    columnConfig.setValue("value0");
    columnConfig.setName("col0");
    insertOrUpdateStatement.addColumn(columnConfig);
    columnConfig = new ColumnConfig();
    columnConfig.setValue("keyvalue1");
    columnConfig.setName("pk1");
    insertOrUpdateStatement.addColumn(columnConfig);
    final String whereClause = generator.getWhereClause(insertOrUpdateStatement, database);
    String result = generator.getUpdateStatement(insertOrUpdateStatement, database, whereClause, sqlGeneratorChain);
    assertEquals("", result);
}
Also used : InsertOrUpdateStatement(liquibase.statement.core.InsertOrUpdateStatement) ColumnConfig(liquibase.change.ColumnConfig) H2Database(liquibase.database.core.H2Database) Database(liquibase.database.Database) H2Database(liquibase.database.core.H2Database) SqlGeneratorChain(liquibase.sqlgenerator.SqlGeneratorChain) Test(org.junit.Test)

Example 2 with SqlGeneratorChain

use of liquibase.sqlgenerator.SqlGeneratorChain 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) SqlGenerator(liquibase.sqlgenerator.SqlGenerator) TreeSet(java.util.TreeSet) 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 3 with SqlGeneratorChain

use of liquibase.sqlgenerator.SqlGeneratorChain in project liquibase by liquibase.

the class InsertOrUpdateGeneratorMySQLTest method testGenerateSql_notOnlyUpdate.

/**
 * Test method for {@link liquibase.sqlgenerator.core.InsertOrUpdateGenerator#generateSql(liquibase.statement.core.InsertOrUpdateStatement, liquibase.database.Database, liquibase.sqlgenerator.SqlGeneratorChain)}.
 */
@Test
public void testGenerateSql_notOnlyUpdate() {
    final InsertOrUpdateGeneratorMySQL generator = new InsertOrUpdateGeneratorMySQL();
    final InsertOrUpdateStatement insertOrUpdateStatement = new InsertOrUpdateStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME, "pk1");
    final Database database = new MySQLDatabase();
    final SqlGeneratorChain sqlGeneratorChain = null;
    ColumnConfig columnConfig;
    columnConfig = new ColumnConfig();
    columnConfig.setValue("keyvalue1");
    columnConfig.setName("pk1");
    insertOrUpdateStatement.addColumn(columnConfig);
    columnConfig = new ColumnConfig();
    columnConfig.setValue("value0");
    columnConfig.setName("col0");
    insertOrUpdateStatement.addColumn(columnConfig);
    // final String whereClause = generator.getWhereClause(insertOrUpdateStatement, database);
    Sql[] results = generator.generateSql(insertOrUpdateStatement, database, sqlGeneratorChain);
    assertThat(results, is(arrayWithSize(1)));
    assertEquals(String.format("INSERT INTO %s.%s (%s, %s) VALUES ('%s', '%s')\nON DUPLICATE KEY UPDATE col0 = 'value0'", CATALOG_NAME, TABLE_NAME, "pk1", "col0", "keyvalue1", "value0"), results[0].toSql());
}
Also used : InsertOrUpdateStatement(liquibase.statement.core.InsertOrUpdateStatement) ColumnConfig(liquibase.change.ColumnConfig) MySQLDatabase(liquibase.database.core.MySQLDatabase) Database(liquibase.database.Database) MySQLDatabase(liquibase.database.core.MySQLDatabase) SqlGeneratorChain(liquibase.sqlgenerator.SqlGeneratorChain) Sql(liquibase.sql.Sql) Test(org.junit.Test)

Example 4 with SqlGeneratorChain

use of liquibase.sqlgenerator.SqlGeneratorChain in project liquibase by liquibase.

the class InsertOrUpdateGeneratorMySQLTest method testGetUpdateStatement_notOnlyUpdate.

// private static final String SEQUENCE_NAME = "my_sequence";
/**
 * Test method for {@link liquibase.sqlgenerator.core.InsertOrUpdateGeneratorMySQL#getUpdateStatement(liquibase.statement.core.InsertOrUpdateStatement, liquibase.database.Database, java.lang.String, liquibase.sqlgenerator.SqlGeneratorChain)}.
 * @throws LiquibaseException
 */
@Test
public void testGetUpdateStatement_notOnlyUpdate() throws LiquibaseException {
    final InsertOrUpdateGeneratorMySQL generator = new InsertOrUpdateGeneratorMySQL();
    final InsertOrUpdateStatement insertOrUpdateStatement = new InsertOrUpdateStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME, "pk1");
    final Database database = new MySQLDatabase();
    final SqlGeneratorChain sqlGeneratorChain = null;
    ColumnConfig columnConfig;
    columnConfig = new ColumnConfig();
    columnConfig.setValue("value0");
    columnConfig.setName("col0");
    insertOrUpdateStatement.addColumn(columnConfig);
    columnConfig = new ColumnConfig();
    columnConfig.setValue("keyvalue1");
    columnConfig.setName("pk1");
    insertOrUpdateStatement.addColumn(columnConfig);
    final String whereClause = generator.getWhereClause(insertOrUpdateStatement, database);
    String result = generator.getUpdateStatement(insertOrUpdateStatement, database, whereClause, sqlGeneratorChain);
    assertEquals("", result);
}
Also used : InsertOrUpdateStatement(liquibase.statement.core.InsertOrUpdateStatement) ColumnConfig(liquibase.change.ColumnConfig) MySQLDatabase(liquibase.database.core.MySQLDatabase) Database(liquibase.database.Database) MySQLDatabase(liquibase.database.core.MySQLDatabase) SqlGeneratorChain(liquibase.sqlgenerator.SqlGeneratorChain) Test(org.junit.Test)

Example 5 with SqlGeneratorChain

use of liquibase.sqlgenerator.SqlGeneratorChain in project liquibase by liquibase.

the class InsertOrUpdateGeneratorMySQLTest method testGetUpdateStatement_onlyUpdate.

/**
 * Test method for {@link liquibase.sqlgenerator.core.InsertOrUpdateGeneratorMySQL#getUpdateStatement(liquibase.statement.core.InsertOrUpdateStatement, liquibase.database.Database, java.lang.String, liquibase.sqlgenerator.SqlGeneratorChain)}.
 * @throws LiquibaseException
 */
@Test
public void testGetUpdateStatement_onlyUpdate() throws LiquibaseException {
    final InsertOrUpdateGeneratorMySQL generator = new InsertOrUpdateGeneratorMySQL();
    final InsertOrUpdateStatement insertOrUpdateStatement = new InsertOrUpdateStatement(CATALOG_NAME, SCHEMA_NAME, TABLE_NAME, "pk1", true);
    final Database database = new MySQLDatabase();
    final SqlGeneratorChain sqlGeneratorChain = null;
    ColumnConfig columnConfig;
    columnConfig = new ColumnConfig();
    columnConfig.setValue("value0");
    columnConfig.setName("col0");
    insertOrUpdateStatement.addColumn(columnConfig);
    columnConfig = new ColumnConfig();
    columnConfig.setValue("keyvalue1");
    columnConfig.setName("pk1");
    insertOrUpdateStatement.addColumn(columnConfig);
    final String whereClause = generator.getWhereClause(insertOrUpdateStatement, database);
    String result = generator.getUpdateStatement(insertOrUpdateStatement, database, whereClause, sqlGeneratorChain);
    assertEquals(String.format("UPDATE %s.%s SET %s = '%s' WHERE %s = '%s';\n", CATALOG_NAME, TABLE_NAME, "col0", "value0", "pk1", "keyvalue1"), result);
}
Also used : InsertOrUpdateStatement(liquibase.statement.core.InsertOrUpdateStatement) ColumnConfig(liquibase.change.ColumnConfig) MySQLDatabase(liquibase.database.core.MySQLDatabase) Database(liquibase.database.Database) MySQLDatabase(liquibase.database.core.MySQLDatabase) SqlGeneratorChain(liquibase.sqlgenerator.SqlGeneratorChain) Test(org.junit.Test)

Aggregations

SqlGeneratorChain (liquibase.sqlgenerator.SqlGeneratorChain)8 Test (org.junit.Test)8 Database (liquibase.database.Database)7 ColumnConfig (liquibase.change.ColumnConfig)6 InsertOrUpdateStatement (liquibase.statement.core.InsertOrUpdateStatement)6 Sql (liquibase.sql.Sql)4 H2Database (liquibase.database.core.H2Database)3 MySQLDatabase (liquibase.database.core.MySQLDatabase)3 TreeSet (java.util.TreeSet)2 SqlGenerator (liquibase.sqlgenerator.SqlGenerator)2 InformixDatabase (liquibase.database.core.InformixDatabase)1 PostgresDatabase (liquibase.database.core.PostgresDatabase)1 AddDefaultValueStatement (liquibase.statement.core.AddDefaultValueStatement)1 DropIndexStatement (liquibase.statement.core.DropIndexStatement)1