use of liquibase.sql.Sql in project liquibase by liquibase.
the class CreateTableGeneratorTest method testAutoIncrementStartWithIncrementBySybaseASADatabase.
@Test
public void testAutoIncrementStartWithIncrementBySybaseASADatabase() throws Exception {
for (Database database : TestContext.getInstance().getAllDatabases()) {
if (database instanceof SybaseASADatabase) {
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.valueOf(2), BigInteger.TEN));
Sql[] generatedSql = this.generatorUnderTest.generateSql(statement, database, null);
// start with and increment by not supported by SybaseASA
assertEquals("CREATE TABLE SCHEMA_NAME.TABLE_NAME (COLUMN1_NAME BIGINT DEFAULT AUTOINCREMENT NULL)", generatedSql[0].toSql());
}
}
}
use of liquibase.sql.Sql in project liquibase by liquibase.
the class CreateTableGeneratorTest method testAutoIncrementStartWithIncrementByMySQLDatabase.
@Test
public void testAutoIncrementStartWithIncrementByMySQLDatabase() throws Exception {
for (Database database : TestContext.getInstance().getAllDatabases()) {
if (database instanceof MySQLDatabase) {
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.valueOf(2), BigInteger.TEN));
Sql[] generatedSql = this.generatorUnderTest.generateSql(statement, database, null);
// increment by not supported by MySQL
assertEquals("CREATE TABLE CATALOG_NAME.TABLE_NAME (COLUMN1_NAME BIGINT AUTO_INCREMENT NULL) AUTO_INCREMENT=2", generatedSql[0].toSql());
}
}
}
use of liquibase.sql.Sql in project liquibase by liquibase.
the class SqlGeneratorChainTest method generateSql_threeGenerators.
@Test
public void generateSql_threeGenerators() {
SortedSet<SqlGenerator> generators = new TreeSet<SqlGenerator>(new SqlGeneratorComparator());
generators.add(new MockSqlGenerator(2, "B1", "B2"));
generators.add(new MockSqlGenerator(1, "A1", "A2"));
generators.add(new MockSqlGenerator(3, "C1", "C2"));
SqlGeneratorChain chain = new SqlGeneratorChain(generators);
Sql[] sql = chain.generateSql(new MockSqlStatement(), new MockDatabase());
assertEquals(6, sql.length);
assertEquals("C1", sql[0].toSql());
assertEquals("C2", sql[1].toSql());
assertEquals("B1", sql[2].toSql());
assertEquals("B2", sql[3].toSql());
assertEquals("A1", sql[4].toSql());
assertEquals("A2", sql[5].toSql());
}
use of liquibase.sql.Sql in project liquibase by liquibase.
the class AbstractExecuteTest method test.
private void test(String[] expectedSql, Class<? extends Database>[] includeDatabases, Class<? extends Database>[] excludeDatabases) throws Exception {
if (expectedSql != null) {
for (Database database : TestContext.getInstance().getAllDatabases()) {
if (shouldTestDatabase(database, includeDatabases, excludeDatabases)) {
testedDatabases.add(database.getClass());
if (database.getConnection() != null) {
ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).init();
LockServiceFactory.getInstance().getLockService(database).init();
}
Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(statementUnderTest, database);
assertNotNull("Null SQL for " + database, sql);
assertEquals("Unexpected number of SQL statements for " + database, expectedSql.length, sql.length);
int index = 0;
for (String convertedSql : expectedSql) {
convertedSql = replaceEscaping(convertedSql, database);
convertedSql = replaceDatabaseClauses(convertedSql, database);
convertedSql = replaceStandardTypes(convertedSql, database);
assertEquals("Incorrect SQL for " + database.getClass().getName(), convertedSql.toLowerCase().trim(), sql[index].toSql().toLowerCase());
index++;
}
}
}
}
resetAvailableDatabases();
for (Database availableDatabase : DatabaseTestContext.getInstance().getAvailableDatabases()) {
Statement statement = ((JdbcConnection) availableDatabase.getConnection()).getUnderlyingConnection().createStatement();
if (shouldTestDatabase(availableDatabase, includeDatabases, excludeDatabases)) {
String sqlToRun = SqlGeneratorFactory.getInstance().generateSql(statementUnderTest, availableDatabase)[0].toSql();
try {
statement.execute(sqlToRun);
} catch (Exception e) {
System.out.println("Failed to execute against " + availableDatabase.getShortName() + ": " + sqlToRun);
throw e;
}
}
}
}
use of liquibase.sql.Sql in project opennms by OpenNMS.
the class SetSequenceGeneratorTest method testWithMultipleTables.
@Test
public void testWithMultipleTables() {
for (final Database database : TestContext.getInstance().getAllDatabases()) {
if (database instanceof PostgresDatabase) {
final SetSequenceStatement statement = new SetSequenceStatement("SEQUENCE_NAME");
statement.addTable("TABLE1_NAME", "COLUMN1_NAME");
statement.addTable("TABLE2_NAME", "COLUMN2_NAME");
if (shouldBeImplementation(database)) {
final SqlGenerator<SetSequenceStatement> generator = this.generatorUnderTest;
final String tempTableName = ((SetSequenceGenerator) generator).getTempTableName();
final Sql[] sql = generator.generateSql(statement, database, null);
assertEquals("SELECT pg_catalog.setval('SEQUENCE_NAME',(SELECT max(" + tempTableName + ".id)+1 AS id FROM ((SELECT max(COLUMN1_NAME) AS id FROM TABLE1_NAME LIMIT 1) UNION (SELECT max(COLUMN2_NAME) AS id FROM TABLE2_NAME LIMIT 1)) AS " + tempTableName + " LIMIT 1),true);", sql[0].toSql());
}
}
}
}
Aggregations