Search in sources :

Example 46 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class AddColumnChangeTest method generateStatements_multipleColumns.

@Test
public void generateStatements_multipleColumns() {
    AddColumnChange change = new AddColumnChange();
    AddColumnConfig column1 = new AddColumnConfig();
    column1.setName("column1");
    column1.setType("INT");
    change.addColumn(column1);
    AddColumnConfig column2 = new AddColumnConfig();
    column2.setName("column2");
    column2.setType("INT");
    change.addColumn(column2);
    SqlStatement[] statements = change.generateStatements(new MockDatabase());
    Assert.assertEquals(1, statements.length);
    Assert.assertTrue(statements[0] instanceof AddColumnStatement);
    AddColumnStatement stmt = (AddColumnStatement) statements[0];
    Assert.assertTrue(stmt.isMultiple());
    Assert.assertEquals(2, stmt.getColumns().size());
}
Also used : SqlStatement(liquibase.statement.SqlStatement) MockDatabase(liquibase.sdk.database.MockDatabase) AddColumnChange(liquibase.change.core.AddColumnChange) AddColumnStatement(liquibase.statement.core.AddColumnStatement) Test(org.junit.Test)

Example 47 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class CustomChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) throws CustomChangeException {
    SqlStatement[] st = new SqlStatement[1];
    InsertStatement is = new InsertStatement(null, null, "persons");
    is.addColumnValue("id", new Integer(1));
    is.addColumnValue("firstname", "joan");
    is.addColumnValue("lastname", "pujol");
    st[0] = is;
    return st;
}
Also used : SqlStatement(liquibase.statement.SqlStatement) InsertStatement(liquibase.statement.core.InsertStatement)

Example 48 with SqlStatement

use of liquibase.statement.SqlStatement in project liquibase by liquibase.

the class StandardLockService method listLocks.

@Override
public DatabaseChangeLogLock[] listLocks() throws LockException {
    try {
        if (!this.hasDatabaseChangeLogLockTable()) {
            return new DatabaseChangeLogLock[0];
        }
        List<DatabaseChangeLogLock> allLocks = new ArrayList<DatabaseChangeLogLock>();
        SqlStatement sqlStatement = new SelectFromDatabaseChangeLogLockStatement("ID", "LOCKED", "LOCKGRANTED", "LOCKEDBY");
        List<Map<String, ?>> rows = ExecutorService.getInstance().getExecutor(database).queryForList(sqlStatement);
        for (Map columnMap : rows) {
            Object lockedValue = columnMap.get("LOCKED");
            Boolean locked;
            if (lockedValue instanceof Number) {
                locked = ((Number) lockedValue).intValue() == 1;
            } else {
                locked = (Boolean) lockedValue;
            }
            if (locked != null && locked) {
                allLocks.add(new DatabaseChangeLogLock(((Number) columnMap.get("ID")).intValue(), (Date) columnMap.get("LOCKGRANTED"), (String) columnMap.get("LOCKEDBY")));
            }
        }
        return allLocks.toArray(new DatabaseChangeLogLock[allLocks.size()]);
    } catch (Exception e) {
        throw new LockException(e);
    }
}
Also used : ArrayList(java.util.ArrayList) Date(java.util.Date) LockException(liquibase.exception.LockException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) InvalidExampleException(liquibase.snapshot.InvalidExampleException) DatabaseException(liquibase.exception.DatabaseException) LiquibaseException(liquibase.exception.LiquibaseException) SqlStatement(liquibase.statement.SqlStatement) LockException(liquibase.exception.LockException) Map(java.util.Map)

Example 49 with SqlStatement

use of liquibase.statement.SqlStatement 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)

Aggregations

SqlStatement (liquibase.statement.SqlStatement)49 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)9 Column (liquibase.structure.core.Column)8 DB2Database (liquibase.database.core.DB2Database)7 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)7 ReorganizeTableStatement (liquibase.statement.core.ReorganizeTableStatement)7 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)6 DatabaseException (liquibase.exception.DatabaseException)6 Database (liquibase.database.Database)5 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)5 Sql (liquibase.sql.Sql)5 RawSqlStatement (liquibase.statement.core.RawSqlStatement)5 UpdateStatement (liquibase.statement.core.UpdateStatement)5 Index (liquibase.structure.core.Index)5 Change (liquibase.change.Change)4 ColumnConfig (liquibase.change.ColumnConfig)4 AddColumnChange (liquibase.change.core.AddColumnChange)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)3 AlterTableVisitor (liquibase.database.core.SQLiteDatabase.AlterTableVisitor)3