Search in sources :

Example 6 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class InsertDataChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    boolean needsPreparedStatement = false;
    for (ColumnConfig column : columns) {
        if (column.getValueBlobFile() != null) {
            needsPreparedStatement = true;
        }
        if (column.getValueClobFile() != null) {
            needsPreparedStatement = true;
        }
        if (LoadDataChange.LOAD_DATA_TYPE.BLOB.name().equalsIgnoreCase(column.getType())) {
            needsPreparedStatement = true;
        }
        if (LoadDataChange.LOAD_DATA_TYPE.CLOB.name().equalsIgnoreCase(column.getType())) {
            needsPreparedStatement = true;
        }
        if (!needsPreparedStatement && (database instanceof InformixDatabase)) {
            if (column.getValue() != null) {
                try {
                    Column snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(new Column(column), database);
                    if (snapshot != null) {
                        needsPreparedStatement = true;
                    }
                } catch (Exception ignore) {
                // assume it's not a clob
                }
            }
        }
    }
    if (needsPreparedStatement) {
        return new SqlStatement[] { new InsertExecutablePreparedStatement(database, catalogName, schemaName, tableName, columns, getChangeSet(), Scope.getCurrentScope().getResourceAccessor()) };
    }
    InsertStatement statement = new InsertStatement(getCatalogName(), getSchemaName(), getTableName());
    for (ColumnConfig column : columns) {
        if (database.supportsAutoIncrement() && (column.isAutoIncrement() != null) && column.isAutoIncrement()) {
            // skip auto increment columns as they will be generated by the database
            continue;
        }
        final Object valueObject = column.getValueObject();
        if (valueObject instanceof SequenceNextValueFunction) {
            ((SequenceNextValueFunction) valueObject).setSchemaName(this.getSchemaName());
        }
        if (valueObject instanceof SequenceCurrentValueFunction) {
            ((SequenceCurrentValueFunction) valueObject).setSchemaName(this.getSchemaName());
        }
        statement.addColumnValue(column.getName(), column.getValueObject());
    }
    return new SqlStatement[] { statement };
}
Also used : SequenceCurrentValueFunction(liquibase.statement.SequenceCurrentValueFunction) SqlStatement(liquibase.statement.SqlStatement) InformixDatabase(liquibase.database.core.InformixDatabase) Column(liquibase.structure.core.Column) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) InsertExecutablePreparedStatement(liquibase.statement.InsertExecutablePreparedStatement) InsertStatement(liquibase.statement.core.InsertStatement)

Example 7 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class RenameColumnChange method checkStatus.

@Override
public ChangeStatus checkStatus(Database database) {
    try {
        ChangeStatus changeStatus = new ChangeStatus();
        Column newColumn = SnapshotGeneratorFactory.getInstance().createSnapshot(new Column(Table.class, getCatalogName(), getSchemaName(), getTableName(), getNewColumnName()), database);
        Column oldColumn = SnapshotGeneratorFactory.getInstance().createSnapshot(new Column(Table.class, getCatalogName(), getSchemaName(), getTableName(), getOldColumnName()), database);
        if ((newColumn == null) && (oldColumn == null)) {
            return changeStatus.unknown("Neither column exists");
        }
        if ((newColumn != null) && (oldColumn != null)) {
            return changeStatus.unknown("Both columns exist");
        }
        changeStatus.assertComplete(newColumn != null, "New column does not exist");
        return changeStatus;
    } catch (Exception e) {
        return new ChangeStatus().unknown(e);
    }
}
Also used : Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column)

Example 8 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class AbstractDatabaseObject method load.

@Override
public void load(ParsedNode parsedNode, ResourceAccessor resourceAccessor) throws ParsedNodeException {
    for (ParsedNode child : parsedNode.getChildren()) {
        String name = child.getName();
        if ("snapshotId".equals(name)) {
            this.snapshotId = child.getValue(String.class);
            continue;
        }
        Class propertyType = ObjectUtil.getPropertyType(this, name);
        if ((propertyType != null) && Collection.class.isAssignableFrom(propertyType) && !(child.getValue() instanceof Collection)) {
            if (this.attributes.get(name) == null) {
                this.setAttribute(name, new ArrayList<Column>());
            }
            this.getAttribute(name, List.class).add(child.getValue());
        } else {
            Object childValue = child.getValue();
            if ((childValue != null) && (childValue instanceof String)) {
                Matcher matcher = Pattern.compile("(.*)!\\{(.*)\\}").matcher((String) childValue);
                if (matcher.matches()) {
                    String stringValue = matcher.group(1);
                    try {
                        Class<?> aClass = Class.forName(matcher.group(2));
                        if (Date.class.isAssignableFrom(aClass)) {
                            Date date = new ISODateFormat().parse(stringValue);
                            childValue = aClass.getConstructor(long.class).newInstance(date.getTime());
                        } else if (Enum.class.isAssignableFrom(aClass)) {
                            childValue = Enum.valueOf((Class<? extends Enum>) aClass, stringValue);
                        } else {
                            childValue = aClass.getConstructor(String.class).newInstance(stringValue);
                        }
                    } catch (Exception e) {
                        throw new UnexpectedLiquibaseException(e);
                    }
                }
            }
            this.attributes.put(name, childValue);
        }
    }
}
Also used : ParsedNode(liquibase.parser.core.ParsedNode) Matcher(java.util.regex.Matcher) ParsedNodeException(liquibase.parser.core.ParsedNodeException) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException) ISODateFormat(liquibase.util.ISODateFormat) Column(liquibase.structure.core.Column) UnexpectedLiquibaseException(liquibase.exception.UnexpectedLiquibaseException)

Example 9 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class MssqlIntegrationTest method dataTypesTest.

@Test
public void dataTypesTest() throws Exception {
    assumeNotNull(this.getDatabase());
    clearDatabase();
    Liquibase liquibase = createLiquibase("changelogs/mssql/issues/data.types.xml");
    liquibase.update((String) null);
    DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(CatalogAndSchema.DEFAULT, this.getDatabase(), new SnapshotControl(getDatabase()));
    for (Table table : snapshot.get(Table.class)) {
        if (getDatabase().isLiquibaseObject(table)) {
            continue;
        }
        for (Column column : table.getColumns()) {
            String expectedType = column.getName().split("_")[0];
            switch(expectedType.toUpperCase()) {
                // varchar(max), nvarchar(max) and varbinary(max).
                case "TEXT":
                    expectedType = "varchar";
                    break;
                case "NTEXT":
                    expectedType = "nvarchar";
                    break;
                case "IMAGE":
                    expectedType = "varbinary";
                    break;
                default:
            }
            String foundTypeDefinition = DataTypeFactory.getInstance().from(column.getType(), new MSSQLDatabase()).toDatabaseDataType(getDatabase()).toString();
            // [varbinary] -> varbinary
            foundTypeDefinition = foundTypeDefinition.replaceFirst("^\\[(.*?)\\]", "$1");
            String foundType = foundTypeDefinition.replaceFirst("\\(.*", "").trim();
            assertEquals("Wrong data type for " + table.getName() + "." + column.getName(), expectedType.toLowerCase(), foundType.toLowerCase());
            if ("varbinary".equalsIgnoreCase(expectedType)) {
                if (column.getName().endsWith("_MAX")) {
                    assertEquals("VARBINARY(MAX)", foundTypeDefinition.toUpperCase());
                } else {
                    assertEquals("VARBINARY(1)", foundTypeDefinition.toUpperCase());
                }
            }
        }
    }
}
Also used : Liquibase(liquibase.Liquibase) Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) Test(org.junit.Test)

Example 10 with Column

use of liquibase.structure.core.Column in project liquibase by liquibase.

the class CockroachDBIntegrationTest method descPrimaryKey.

@Test
public void descPrimaryKey() throws Exception {
    if (getDatabase() == null) {
        return;
    }
    final Executor executor = Scope.getCurrentScope().getSingleton(ExecutorService.class).getExecutor("jdbc", getDatabase());
    executor.execute(new RawSqlStatement("DROP TABLE IF EXISTS pk"));
    executor.execute(new RawSqlStatement("CREATE TABLE pk (\n" + "a INT8 NOT NULL,\n" + "b INT8 NOT NULL,\n" + "c INT8 NOT NULL,\n" + "d INT8 NOT NULL,\n" + "CONSTRAINT \"primary\" PRIMARY KEY (a ASC, b ASC, c DESC)\n" + ")"));
    DatabaseSnapshot snapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(getDatabase().getDefaultSchema(), getDatabase(), new SnapshotControl(getDatabase()));
    PrimaryKey pk = snapshot.get(new PrimaryKey().setTable(new Table().setName("pk")).setName("primary"));
    List<Column> columns = pk.getColumns();
    assertEquals("a", columns.get(0).getName());
    assertNull(columns.get(0).getDescending());
    assertEquals("b", columns.get(1).getName());
    assertNull(columns.get(1).getDescending());
    assertEquals("c", columns.get(2).getName());
    assertTrue(columns.get(2).getDescending());
}
Also used : RawSqlStatement(liquibase.statement.core.RawSqlStatement) Executor(liquibase.executor.Executor) Table(liquibase.structure.core.Table) Column(liquibase.structure.core.Column) ExecutorService(liquibase.executor.ExecutorService) PrimaryKey(liquibase.structure.core.PrimaryKey) DatabaseSnapshot(liquibase.snapshot.DatabaseSnapshot) SnapshotControl(liquibase.snapshot.SnapshotControl) Test(org.junit.Test) AbstractIntegrationTest(liquibase.dbtest.AbstractIntegrationTest)

Aggregations

Column (liquibase.structure.core.Column)43 Table (liquibase.structure.core.Table)22 Change (liquibase.change.Change)8 SnapshotControl (liquibase.snapshot.SnapshotControl)8 Index (liquibase.structure.core.Index)8 DatabaseSnapshot (liquibase.snapshot.DatabaseSnapshot)7 DatabaseObject (liquibase.structure.DatabaseObject)6 Test (org.junit.Test)6 DatabaseException (liquibase.exception.DatabaseException)5 SqlStatement (liquibase.statement.SqlStatement)5 Schema (liquibase.structure.core.Schema)5 UniqueConstraint (liquibase.structure.core.UniqueConstraint)5 ArrayList (java.util.ArrayList)4 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)4 UnexpectedLiquibaseException (liquibase.exception.UnexpectedLiquibaseException)4 CatalogAndSchema (liquibase.CatalogAndSchema)3 Liquibase (liquibase.Liquibase)3 AbstractIntegrationTest (liquibase.dbtest.AbstractIntegrationTest)3 RawSqlStatement (liquibase.statement.core.RawSqlStatement)3 PrimaryKey (liquibase.structure.core.PrimaryKey)3