Search in sources :

Example 21 with ColumnConfig

use of liquibase.change.ColumnConfig in project liquibase by liquibase.

the class XMLChangeLogSerializerTest method serialize_pretty_nestedNodeWithAttributes.

@Test
public void serialize_pretty_nestedNodeWithAttributes() {
    CreateTableChange change = new CreateTableChange();
    change.setCatalogName("a");
    change.setSchemaName("b");
    change.setTableName("c");
    change.addColumn(new ColumnConfig().setName("x").setDefaultValue("x1"));
    change.addColumn(new ColumnConfig().setName("y").setDefaultValue("y1"));
    String out = new XMLChangeLogSerializer().serialize(change, true);
    assertEquals("<createTable catalogName=\"a\"\n" + "        schemaName=\"b\"\n" + "        tableName=\"c\">\n" + "    <column defaultValue=\"x1\" name=\"x\"/>\n" + "    <column defaultValue=\"y1\" name=\"y\"/>\n" + "</createTable>", out);
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) AddColumnConfig(liquibase.change.AddColumnConfig) Test(org.junit.Test)

Example 22 with ColumnConfig

use of liquibase.change.ColumnConfig in project liquibase by liquibase.

the class XMLChangeLogSerializerTest method createNode_ColumnConfig.

@Test
public void createNode_ColumnConfig() throws Exception {
    ColumnConfig column = new ColumnConfig();
    column.setName("id");
    column.setType("varchar(255)");
    column.setDefaultValue("test Value");
    column.setValue("some value here");
    ConstraintsConfig constraints = new ConstraintsConfig();
    constraints.setDeferrable(Boolean.TRUE);
    constraints.setDeleteCascade(true);
    constraints.setForeignKeyName("FK_NAME");
    constraints.setInitiallyDeferred(true);
    constraints.setNullable(false);
    constraints.setPrimaryKey(true);
    constraints.setReferences("state(id)");
    constraints.setUnique(true);
    column.setConstraints(constraints);
    Element element = new XMLChangeLogSerializer(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()).createNode(column);
    assertEquals("column", element.getTagName());
    assertEquals("id", element.getAttribute("name"));
    assertEquals("varchar(255)", element.getAttribute("type"));
    assertEquals("test Value", element.getAttribute("defaultValue"));
    assertEquals("some value here", element.getAttribute("value"));
    Element constraintsElement = (Element) element.getChildNodes().item(0);
    assertEquals(8, constraintsElement.getAttributes().getLength());
    assertEquals("true", constraintsElement.getAttribute("deferrable"));
    assertEquals("true", constraintsElement.getAttribute("deleteCascade"));
    assertEquals("FK_NAME", constraintsElement.getAttribute("foreignKeyName"));
    assertEquals("true", constraintsElement.getAttribute("initiallyDeferred"));
    assertEquals("false", constraintsElement.getAttribute("nullable"));
    assertEquals("true", constraintsElement.getAttribute("primaryKey"));
    assertEquals("state(id)", constraintsElement.getAttribute("references"));
    assertEquals("true", constraintsElement.getAttribute("unique"));
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) AddColumnConfig(liquibase.change.AddColumnConfig) Element(org.w3c.dom.Element) ConstraintsConfig(liquibase.change.ConstraintsConfig) Test(org.junit.Test)

Example 23 with ColumnConfig

use of liquibase.change.ColumnConfig in project liquibase by liquibase.

the class CopyRowsGenerator method generateSql.

@Override
public Sql[] generateSql(CopyRowsStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
    StringBuffer sql = new StringBuffer();
    if (statement.getCopyColumns().size() == 0) {
        return new Sql[] {};
    }
    if (database instanceof SQLiteDatabase) {
        sql.append("INSERT INTO `").append(statement.getTargetTable()).append("` (");
        for (int i = 0; i < statement.getCopyColumns().size(); i++) {
            ColumnConfig column = statement.getCopyColumns().get(i);
            if (i > 0) {
                sql.append(",");
            }
            sql.append("`").append(column.getName()).append("`");
        }
        sql.append(") SELECT ");
        for (int i = 0; i < statement.getCopyColumns().size(); i++) {
            ColumnConfig column = statement.getCopyColumns().get(i);
            if (i > 0) {
                sql.append(",");
            }
            sql.append("`").append(column.getName()).append("`");
        }
        sql.append(" FROM `").append(statement.getSourceTable()).append("`");
    }
    return new Sql[] { new UnparsedSql(sql.toString(), getAffectedTable(statement)) };
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) SQLiteDatabase(liquibase.database.core.SQLiteDatabase) UnparsedSql(liquibase.sql.UnparsedSql) UnparsedSql(liquibase.sql.UnparsedSql) Sql(liquibase.sql.Sql)

Example 24 with ColumnConfig

use of liquibase.change.ColumnConfig in project liquibase by liquibase.

the class MissingTableChangeGenerator method fixMissing.

@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl control, Database referenceDatabase, Database comparisonDatabase, ChangeGeneratorChain chain) {
    Table missingTable = (Table) missingObject;
    PrimaryKey primaryKey = missingTable.getPrimaryKey();
    //        if (control.diffResult.getReferenceSnapshot().getDatabase().isLiquibaseTable(missingTable.getSchema().toCatalogAndSchema(), missingTable.getName())) {
    //            continue;
    //        }
    CreateTableChange change = createCreateTableChange();
    change.setTableName(missingTable.getName());
    if (control.getIncludeCatalog()) {
        change.setCatalogName(missingTable.getSchema().getCatalogName());
    }
    if (control.getIncludeSchema()) {
        change.setSchemaName(missingTable.getSchema().getName());
    }
    if (missingTable.getRemarks() != null) {
        change.setRemarks(missingTable.getRemarks());
    }
    for (Column column : missingTable.getColumns()) {
        ColumnConfig columnConfig = new ColumnConfig();
        columnConfig.setName(column.getName());
        LiquibaseDataType ldt = DataTypeFactory.getInstance().from(column.getType(), referenceDatabase);
        DatabaseDataType ddt = ldt.toDatabaseDataType(comparisonDatabase);
        String typeString = ddt.toString();
        if (comparisonDatabase instanceof MSSQLDatabase) {
            typeString = comparisonDatabase.unescapeDataTypeString(typeString);
        }
        columnConfig.setType(typeString);
        if (column.isAutoIncrement()) {
            columnConfig.setAutoIncrement(true);
        }
        ConstraintsConfig constraintsConfig = null;
        // In MySQL, the primary key must be specified at creation for an autoincrement column
        if (column.isAutoIncrement() && primaryKey != null && primaryKey.getColumns().size() == 1 && primaryKey.getColumnNamesAsList().contains(column.getName())) {
            if (referenceDatabase instanceof MSSQLDatabase && primaryKey.getBackingIndex() != null && primaryKey.getBackingIndex().getClustered() != null && !primaryKey.getBackingIndex().getClustered()) {
            // have to handle PK as a separate statement
            } else if (referenceDatabase instanceof PostgresDatabase && primaryKey.getBackingIndex() != null && primaryKey.getBackingIndex().getClustered() != null && primaryKey.getBackingIndex().getClustered()) {
            // have to handle PK as a separate statement
            } else {
                constraintsConfig = new ConstraintsConfig();
                if (shouldAddPrimarykeyToConstraints(missingObject, control, referenceDatabase, comparisonDatabase)) {
                    constraintsConfig.setPrimaryKey(true);
                    constraintsConfig.setPrimaryKeyTablespace(primaryKey.getTablespace());
                    // MySQL sets some primary key names as PRIMARY which is invalid
                    if (comparisonDatabase instanceof MySQLDatabase && "PRIMARY".equals(primaryKey.getName())) {
                        constraintsConfig.setPrimaryKeyName(null);
                    } else {
                        constraintsConfig.setPrimaryKeyName(primaryKey.getName());
                    }
                    control.setAlreadyHandledMissing(primaryKey);
                    control.setAlreadyHandledMissing(primaryKey.getBackingIndex());
                } else {
                    constraintsConfig.setNullable(false);
                }
            }
        } else if (column.isNullable() != null && !column.isNullable()) {
            constraintsConfig = new ConstraintsConfig();
            constraintsConfig.setNullable(false);
        }
        if (constraintsConfig != null) {
            columnConfig.setConstraints(constraintsConfig);
        }
        setDefaultValue(columnConfig, column, referenceDatabase);
        if (column.getRemarks() != null) {
            columnConfig.setRemarks(column.getRemarks());
        }
        if (column.getAutoIncrementInformation() != null) {
            BigInteger startWith = column.getAutoIncrementInformation().getStartWith();
            BigInteger incrementBy = column.getAutoIncrementInformation().getIncrementBy();
            if (startWith != null && !startWith.equals(BigInteger.ONE)) {
                columnConfig.setStartWith(startWith);
            }
            if (incrementBy != null && !incrementBy.equals(BigInteger.ONE)) {
                columnConfig.setIncrementBy(incrementBy);
            }
        }
        change.addColumn(columnConfig);
        control.setAlreadyHandledMissing(column);
    }
    return new Change[] { change };
}
Also used : Table(liquibase.structure.core.Table) ColumnConfig(liquibase.change.ColumnConfig) LiquibaseDataType(liquibase.datatype.LiquibaseDataType) PrimaryKey(liquibase.structure.core.PrimaryKey) MySQLDatabase(liquibase.database.core.MySQLDatabase) Change(liquibase.change.Change) CreateTableChange(liquibase.change.core.CreateTableChange) PostgresDatabase(liquibase.database.core.PostgresDatabase) DatabaseDataType(liquibase.datatype.DatabaseDataType) Column(liquibase.structure.core.Column) CreateTableChange(liquibase.change.core.CreateTableChange) ConstraintsConfig(liquibase.change.ConstraintsConfig) BigInteger(java.math.BigInteger) MSSQLDatabase(liquibase.database.core.MSSQLDatabase)

Example 25 with ColumnConfig

use of liquibase.change.ColumnConfig in project liquibase by liquibase.

the class SelectFromDatabaseChangeLogLockExecutorTest method generateSql_count.

@Test
public void generateSql_count() throws Exception {
    this.statementUnderTest = new SelectFromDatabaseChangeLogLockStatement(new ColumnConfig().setName("COUNT(*)", true));
    assertCorrect("select count(*) from [databasechangeloglock] where [id]=1", MSSQLDatabase.class, SybaseDatabase.class);
    assertCorrect("select count(*) from [databasechangeloglock] where [id]=1", MSSQLDatabase.class, SybaseASADatabase.class);
    assertCorrect("select count(*) from [databasechangeloglock] where [id]=1 for update", OracleDatabase.class);
    assertCorrectOnRest("select count(*) from [databasechangeloglock] where [id]=1");
}
Also used : ColumnConfig(liquibase.change.ColumnConfig) SelectFromDatabaseChangeLogLockStatement(liquibase.statement.core.SelectFromDatabaseChangeLogLockStatement) Test(org.junit.Test)

Aggregations

ColumnConfig (liquibase.change.ColumnConfig)28 Test (org.junit.Test)14 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)6 AddColumnConfig (liquibase.change.AddColumnConfig)5 CreateTableChange (liquibase.change.core.CreateTableChange)5 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)5 SQLiteDatabase (liquibase.database.core.SQLiteDatabase)5 DatabaseException (liquibase.exception.DatabaseException)5 Sql (liquibase.sql.Sql)5 PostgresDatabase (liquibase.database.core.PostgresDatabase)4 SqlStatement (liquibase.statement.SqlStatement)4 ArrayList (java.util.ArrayList)3 ConstraintsConfig (liquibase.change.ConstraintsConfig)3 Database (liquibase.database.Database)3 DB2Database (liquibase.database.core.DB2Database)3 MySQLDatabase (liquibase.database.core.MySQLDatabase)3 UnparsedSql (liquibase.sql.UnparsedSql)3 AddUniqueConstraintStatement (liquibase.statement.core.AddUniqueConstraintStatement)3 Column (liquibase.structure.core.Column)3 IOException (java.io.IOException)2