Search in sources :

Example 51 with Sql

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());
        }
    }
}
Also used : SybaseASADatabase(liquibase.database.core.SybaseASADatabase) AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) CreateTableStatement(liquibase.statement.core.CreateTableStatement) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) DerbyDatabase(liquibase.database.core.DerbyDatabase) DB2Database(liquibase.database.core.DB2Database) H2Database(liquibase.database.core.H2Database) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) SybaseDatabase(liquibase.database.core.SybaseDatabase) SybaseASADatabase(liquibase.database.core.SybaseASADatabase) Database(liquibase.database.Database) HsqlDatabase(liquibase.database.core.HsqlDatabase) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Example 52 with Sql

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());
        }
    }
}
Also used : AutoIncrementConstraint(liquibase.statement.AutoIncrementConstraint) CreateTableStatement(liquibase.statement.core.CreateTableStatement) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) DerbyDatabase(liquibase.database.core.DerbyDatabase) DB2Database(liquibase.database.core.DB2Database) H2Database(liquibase.database.core.H2Database) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) OracleDatabase(liquibase.database.core.OracleDatabase) SybaseDatabase(liquibase.database.core.SybaseDatabase) SybaseASADatabase(liquibase.database.core.SybaseASADatabase) Database(liquibase.database.Database) HsqlDatabase(liquibase.database.core.HsqlDatabase) MySQLDatabase(liquibase.database.core.MySQLDatabase) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Example 53 with Sql

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());
}
Also used : TreeSet(java.util.TreeSet) MockSqlStatement(liquibase.statement.core.MockSqlStatement) MockDatabase(liquibase.sdk.database.MockDatabase) Sql(liquibase.sql.Sql) Test(org.junit.Test)

Example 54 with Sql

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;
            }
        }
    }
}
Also used : SqlStatement(liquibase.statement.SqlStatement) Statement(java.sql.Statement) MockDatabase(liquibase.sdk.database.MockDatabase) UnsupportedDatabase(liquibase.database.core.UnsupportedDatabase) Database(liquibase.database.Database) ExampleCustomDatabase(liquibase.database.example.ExampleCustomDatabase) SQLException(java.sql.SQLException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) DatabaseException(liquibase.exception.DatabaseException) Sql(liquibase.sql.Sql)

Example 55 with Sql

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());
            }
        }
    }
}
Also used : SetSequenceStatement(liquibase.ext.opennms.setsequence.SetSequenceStatement) SetSequenceGenerator(liquibase.ext.opennms.setsequence.SetSequenceGenerator) PostgresDatabase(liquibase.database.core.PostgresDatabase) PostgresDatabase(liquibase.database.core.PostgresDatabase) Database(liquibase.database.Database) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Aggregations

Sql (liquibase.sql.Sql)95 Test (org.junit.Test)55 Database (liquibase.database.Database)42 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)40 AbstractSqlGeneratorTest (liquibase.sqlgenerator.AbstractSqlGeneratorTest)37 OracleDatabase (liquibase.database.core.OracleDatabase)36 PostgresDatabase (liquibase.database.core.PostgresDatabase)36 MySQLDatabase (liquibase.database.core.MySQLDatabase)35 DB2Database (liquibase.database.core.DB2Database)33 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)33 SybaseDatabase (liquibase.database.core.SybaseDatabase)33 AutoIncrementConstraint (liquibase.statement.AutoIncrementConstraint)33 HsqlDatabase (liquibase.database.core.HsqlDatabase)32 SybaseASADatabase (liquibase.database.core.SybaseASADatabase)32 CreateTableStatement (liquibase.statement.core.CreateTableStatement)32 DerbyDatabase (liquibase.database.core.DerbyDatabase)31 H2Database (liquibase.database.core.H2Database)31 UnparsedSql (liquibase.sql.UnparsedSql)31 ArrayList (java.util.ArrayList)11 CatalogAndSchema (liquibase.CatalogAndSchema)8