Search in sources :

Example 71 with Sql

use of liquibase.sql.Sql 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);
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) ColumnConfig(liquibase.change.ColumnConfig) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) InsertStatement(liquibase.statement.core.InsertStatement) Sql(liquibase.sql.Sql) Test(org.junit.Test)

Example 72 with Sql

use of liquibase.sql.Sql in project liquibase by liquibase.

the class VerifyChangeClassesTest method minimumRequiredIsValidSql.

@Ignore
@Test
public void minimumRequiredIsValidSql() throws Exception {
    ChangeFactory changeFactory = ChangeFactory.getInstance();
    for (String changeName : changeFactory.getDefinedChanges()) {
        if (changeName.equals("addDefaultValue")) {
            //need to better handle strange "one of defaultValue* is required" logic
            continue;
        }
        if (changeName.equals("changeWithNestedTags") || changeName.equals("sampleChange")) {
            //not a real change
            continue;
        }
        for (Database database : DatabaseFactory.getInstance().getImplementedDatabases()) {
            if (database.getShortName() == null) {
                continue;
            }
            TestState state = new TestState(name.getMethodName(), changeName, database.getShortName(), TestState.Type.SQL);
            state.addComment("Database: " + database.getShortName());
            Change change = changeFactory.create(changeName);
            if (!change.supports(database)) {
                continue;
            }
            if (change.generateStatementsVolatile(database)) {
                continue;
            }
            ChangeMetaData changeMetaData = ChangeFactory.getInstance().getChangeMetaData(change);
            change.setResourceAccessor(new JUnitResourceAccessor());
            for (String paramName : new TreeSet<String>(changeMetaData.getRequiredParameters(database).keySet())) {
                ChangeParameterMetaData param = changeMetaData.getParameters().get(paramName);
                Object paramValue = param.getExampleValue(database);
                String serializedValue;
                serializedValue = formatParameter(paramValue);
                state.addComment("Change Parameter: " + param.getParameterName() + "=" + serializedValue);
                param.setValue(change, paramValue);
            }
            ValidationErrors errors = change.validate(database);
            assertFalse("Validation errors for " + changeMetaData.getName() + " on " + database.getShortName() + ": " + errors.toString(), errors.hasErrors());
            SqlStatement[] sqlStatements = change.generateStatements(database);
            for (SqlStatement statement : sqlStatements) {
                Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(statement, database);
                if (sql == null) {
                    System.out.println("Null sql for " + statement + " on " + database.getShortName());
                } else {
                    for (Sql line : sql) {
                        String sqlLine = line.toSql();
                        assertFalse("Change " + changeMetaData.getName() + " contains 'null' for " + database.getShortName() + ": " + sqlLine, sqlLine.contains(" null "));
                        state.addValue(sqlLine + ";");
                    }
                }
            }
            state.test();
        }
    }
}
Also used : ValidationErrors(liquibase.exception.ValidationErrors) JUnitResourceAccessor(liquibase.test.JUnitResourceAccessor) Change(liquibase.change.Change) Sql(liquibase.sql.Sql) SqlStatement(liquibase.statement.SqlStatement) ChangeFactory(liquibase.change.ChangeFactory) Database(liquibase.database.Database) ChangeParameterMetaData(liquibase.change.ChangeParameterMetaData) ChangeMetaData(liquibase.change.ChangeMetaData) Ignore(org.junit.Ignore) Test(org.junit.Test) AbstractVerifyTest(liquibase.verify.AbstractVerifyTest)

Example 73 with Sql

use of liquibase.sql.Sql in project liquibase by liquibase.

the class MarkChangeSetRanGeneratorTest method generateSql_markRan.

@Test
public void generateSql_markRan() {
    Sql[] sqls = new MarkChangeSetRanGenerator().generateSql(new MarkChangeSetRanStatement(new ChangeSet("1", "a", false, false, "c", null, null, null), ChangeSet.ExecType.MARK_RAN), new MockDatabase(), new MockSqlGeneratorChain());
    assertEquals(1, sqls.length);
    assertTrue(sqls[0].toSql(), sqls[0].toSql().contains("MARK_RAN"));
}
Also used : MarkChangeSetRanStatement(liquibase.statement.core.MarkChangeSetRanStatement) MockDatabase(liquibase.sdk.database.MockDatabase) ChangeSet(liquibase.changelog.ChangeSet) MockSqlGeneratorChain(liquibase.sqlgenerator.MockSqlGeneratorChain) Sql(liquibase.sql.Sql) AbstractSqlGeneratorTest(liquibase.sqlgenerator.AbstractSqlGeneratorTest) Test(org.junit.Test)

Example 74 with Sql

use of liquibase.sql.Sql in project liquibase by liquibase.

the class SetNullableGeneratorTest method testGenerateOracleNotNullSqlWithConstraintName.

@Test
public void testGenerateOracleNotNullSqlWithConstraintName() throws Exception {
    final Sql[] sqls = generator.generateSql(new SetNullableStatement(null, "schema_name", "table_name", "column_name", null, false, "constraint_name"), oracle, null);
    assertEquals(1, sqls.length);
    final Sql sql = sqls[0];
    assertEquals("ALTER TABLE schema_name.table_name MODIFY column_name CONSTRAINT constraint_name NOT NULL", sql.toSql());
}
Also used : SetNullableStatement(liquibase.statement.core.SetNullableStatement) Sql(liquibase.sql.Sql) Test(org.junit.Test)

Example 75 with Sql

use of liquibase.sql.Sql in project liquibase by liquibase.

the class TagDatabaseGeneratorTest method testMSSQL.

////    @Test
////    public void supports() throws Exception {
////        new DatabaseTestTemplate().testOnAllDatabases(new DatabaseTest() {
////            public void performTest(Database database) throws Exception {
////                assertTrue(createGeneratorUnderTest().supportsDatabase(database));
////            }
////        });
////    }
//
//    @Test
//    public void execute() throws Exception {
//        new DatabaseTestTemplate().testOnAvailableDatabases(
//                new SqlStatementDatabaseTest(null, new TagDatabaseStatement("TAG_NAME")) {
//                    protected void setup(Database database) throws Exception {
//                        new Liquibase("changelogs/common/common.tests.changelog.xml", new JUnitResourceAccessor(), database).update(null);
//                    }
//
//                    protected void preExecuteAssert(DatabaseSnapshotGenerator snapshot) throws DatabaseException {
//                        assertFalse(snapshot.getDatabase().doesTagExist("TAG_NAME"));
//                    }
//
//                    protected void postExecuteAssert(DatabaseSnapshotGenerator snapshot) throws DatabaseException {
//                        assertTrue(snapshot.getDatabase().doesTagExist("TAG_NAME"));
//                    }
//
//                });
//    }
@Test
public void testMSSQL() throws Exception {
    TagDatabaseStatement statement = new TagDatabaseStatement("v1.0");
    Sql[] sql = SqlGeneratorFactory.getInstance().generateSql(statement, new MSSQLDatabase());
    assertEquals(1, sql.length);
    assertEquals("UPDATE [changelog] " + "SET [TAG] = 'v1.0' " + "FROM [DATABASECHANGELOG] AS [changelog] " + "INNER JOIN (" + "SELECT TOP (1) [ID], [AUTHOR], [FILENAME] " + "FROM [DATABASECHANGELOG] " + "ORDER BY [DATEEXECUTED] DESC, [ORDEREXECUTED] DESC" + ") AS [latest] " + "ON [latest].[ID] = [changelog].[ID] " + "AND [latest].[AUTHOR] = [changelog].[AUTHOR] " + "AND [latest].[FILENAME] = [changelog].[FILENAME]", sql[0].toSql());
}
Also used : TagDatabaseStatement(liquibase.statement.core.TagDatabaseStatement) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Sql(liquibase.sql.Sql) 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