Search in sources :

Example 1 with ModifyDataTypeChange

use of liquibase.change.core.ModifyDataTypeChange in project seed by euu-rocks.

the class EntityChangeLogBuilder method addModifyDataTypeChangeSet.

private void addModifyDataTypeChangeSet(Entity entity, EntityField field) {
    Assert.notNull(entity, C.ENTITY);
    Assert.notNull(field, C.FIELD);
    final ModifyDataTypeChange modifyDataTypeChange = new ModifyDataTypeChange();
    modifyDataTypeChange.setTableName(getTableName(entity));
    modifyDataTypeChange.setColumnName(getColumnName(field));
    modifyDataTypeChange.setNewDataType(getDBFieldType(field));
    addChange(modifyDataTypeChange);
}
Also used : ModifyDataTypeChange(liquibase.change.core.ModifyDataTypeChange)

Example 2 with ModifyDataTypeChange

use of liquibase.change.core.ModifyDataTypeChange in project meveo by meveo-org.

the class CustomTableCreatorService method updateField.

/**
 * Update a field of a db table. Creates a liquibase changeset to add a field to
 * a table and executes it
 *
 * @param dbTableName DB Table name
 * @param cft         Field definition
 */
public void updateField(String sqlConnectionCode, String dbTableName, CustomFieldTemplate cft) {
    String dbFieldname = cft.getDbFieldname();
    try (Session hibernateSession = sqlConnectionProvider.getSession(sqlConnectionCode)) {
        String columnExistsQueryStr = "SELECT EXISTS(\n" + "	SELECT column_name\n" + "	FROM information_schema.columns \n" + "	WHERE table_name=:tableName and column_name=:columnName\n" + ");";
        Query columnExistsQuery = hibernateSession.createNativeQuery(columnExistsQueryStr).setParameter("tableName", dbTableName).setParameter("columnName", dbFieldname);
        boolean columnExists = (boolean) columnExistsQuery.getSingleResult();
        if (!columnExists) {
            addField(sqlConnectionCode, dbTableName, cft);
            return;
        }
        DatabaseChangeLog dbLog = new DatabaseChangeLog("path");
        // Drop not null constraint and add again if needed - a better way would be to
        // check if valueRequired field value was changed
        ChangeSet changeSet = new ChangeSet(dbTableName + "_CT_" + dbFieldname + "_RNN_" + System.currentTimeMillis(), "Meveo", false, false, "meveo", "", "", dbLog);
        changeSet.setFailOnError(false);
        DropNotNullConstraintChange dropNotNullChange = new DropNotNullConstraintChange();
        dropNotNullChange.setTableName(dbTableName);
        dropNotNullChange.setColumnName(dbFieldname);
        String type;
        try {
            type = getColumnType(cft);
        } catch (ClassNotFoundException e1) {
            throw new IllegalArgumentException("Cannot get field type for entity with class or code " + cft.getEntityClazzCetCode(), e1);
        }
        dropNotNullChange.setColumnDataType(type);
        changeSet.addChange(dropNotNullChange);
        dbLog.addChangeSet(changeSet);
        // Add not null constraint if needed
        if (cft.isValueRequired()) {
            changeSet = new ChangeSet(dbTableName + "_CT_" + dbFieldname + "_ANN_" + System.currentTimeMillis(), "Meveo", false, false, "meveo", "", "", dbLog);
            AddNotNullConstraintChange addNotNullChange = new AddNotNullConstraintChange();
            addNotNullChange.setTableName(dbTableName);
            addNotNullChange.setColumnName(dbFieldname);
            addNotNullChange.setColumnDataType(type);
            changeSet.addChange(addNotNullChange);
            dbLog.addChangeSet(changeSet);
        }
        // Default value does not apply to date type field
        if (cft.getFieldType() != CustomFieldTypeEnum.DATE) {
            changeSet = new ChangeSet(dbTableName + "_CT_" + dbFieldname + "_RD_" + System.currentTimeMillis(), "Meveo", false, false, "meveo", "", "", dbLog);
            changeSet.setFailOnError(false);
            DropDefaultValueChange dropDefaultValueChange = new DropDefaultValueChange();
            dropDefaultValueChange.setTableName(dbTableName);
            dropDefaultValueChange.setColumnName(dbFieldname);
            dropDefaultValueChange.setColumnDataType(type);
            changeSet.addChange(dropDefaultValueChange);
            dbLog.addChangeSet(changeSet);
            // Add default value if needed
            if (cft.getDefaultValue() != null) {
                changeSet = new ChangeSet(dbTableName + "_CT_" + dbFieldname + "_AD_" + System.currentTimeMillis(), "Meveo", false, false, "meveo", "", "", dbLog);
                AddDefaultValueChange addDefaultValueChange = new AddDefaultValueChange();
                addDefaultValueChange.setTableName(dbTableName);
                addDefaultValueChange.setColumnName(dbFieldname);
                addDefaultValueChange.setColumnDataType(type);
                if (cft.getFieldType() == CustomFieldTypeEnum.DOUBLE) {
                    addDefaultValueChange.setDefaultValueNumeric(cft.getDefaultValue());
                } else if (cft.getFieldType() == CustomFieldTypeEnum.LONG) {
                    addDefaultValueChange.setDefaultValueNumeric(cft.getDefaultValue());
                } else if (cft.getFieldType() == CustomFieldTypeEnum.SECRET || cft.getFieldType() == CustomFieldTypeEnum.STRING || cft.getFieldType() == CustomFieldTypeEnum.LIST) {
                    addDefaultValueChange.setDefaultValue(cft.getDefaultValue());
                } else if (cft.getFieldType() == CustomFieldTypeEnum.BOOLEAN) {
                    addDefaultValueChange.setColumnDataType("int");
                    addDefaultValueChange.setDefaultValueNumeric("1".equals(cft.getDefaultValue()) || "true".equalsIgnoreCase(cft.getDefaultValue()) ? "1" : "0");
                }
                changeSet.addChange(addDefaultValueChange);
                dbLog.addChangeSet(changeSet);
            }
        }
        // Update field length for String type fields.
        if (cft.getFieldType() == CustomFieldTypeEnum.STRING || cft.getFieldType() == CustomFieldTypeEnum.LIST) {
            changeSet = new ChangeSet(dbTableName + "_CT_" + dbFieldname + "_M_" + System.currentTimeMillis(), "Meveo", false, false, "meveo", "", "", dbLog);
            changeSet.setFailOnError(false);
            ModifyDataTypeChange modifyDataTypeChange = new ModifyDataTypeChange();
            modifyDataTypeChange.setTableName(dbTableName);
            modifyDataTypeChange.setColumnName(dbFieldname);
            modifyDataTypeChange.setNewDataType(type);
            changeSet.addChange(modifyDataTypeChange);
            dbLog.addChangeSet(changeSet);
        }
        createOrUpdateUniqueField(dbTableName, cft, changeSet);
        hibernateSession.doWork(connection -> {
            Database database;
            try {
                database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
                setSchemaName(database);
                Liquibase liquibase = new Liquibase(dbLog, new ClassLoaderResourceAccessor(), database);
                liquibase.update(new Contexts(), new LabelExpression());
            } catch (Exception e) {
                log.error("Failed to update a field {} in a custom table {}", dbTableName, dbFieldname, e);
                throw new SQLException(e);
            }
        });
    }
}
Also used : Query(javax.persistence.Query) SQLException(java.sql.SQLException) AddDefaultValueChange(liquibase.change.core.AddDefaultValueChange) DropNotNullConstraintChange(liquibase.change.core.DropNotNullConstraintChange) ModifyDataTypeChange(liquibase.change.core.ModifyDataTypeChange) JdbcConnection(liquibase.database.jvm.JdbcConnection) DropDefaultValueChange(liquibase.change.core.DropDefaultValueChange) Contexts(liquibase.Contexts) DatabaseChangeLog(liquibase.changelog.DatabaseChangeLog) TimeoutException(java.util.concurrent.TimeoutException) MigrationFailedException(liquibase.exception.MigrationFailedException) BusinessException(org.meveo.admin.exception.BusinessException) HibernateException(org.hibernate.HibernateException) SQLException(java.sql.SQLException) DatabaseException(liquibase.exception.DatabaseException) ExecutionException(java.util.concurrent.ExecutionException) Liquibase(liquibase.Liquibase) AddNotNullConstraintChange(liquibase.change.core.AddNotNullConstraintChange) Database(liquibase.database.Database) LabelExpression(liquibase.LabelExpression) ClassLoaderResourceAccessor(liquibase.resource.ClassLoaderResourceAccessor) ChangeSet(liquibase.changelog.ChangeSet) Session(org.hibernate.Session)

Aggregations

ModifyDataTypeChange (liquibase.change.core.ModifyDataTypeChange)2 SQLException (java.sql.SQLException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 Query (javax.persistence.Query)1 Contexts (liquibase.Contexts)1 LabelExpression (liquibase.LabelExpression)1 Liquibase (liquibase.Liquibase)1 AddDefaultValueChange (liquibase.change.core.AddDefaultValueChange)1 AddNotNullConstraintChange (liquibase.change.core.AddNotNullConstraintChange)1 DropDefaultValueChange (liquibase.change.core.DropDefaultValueChange)1 DropNotNullConstraintChange (liquibase.change.core.DropNotNullConstraintChange)1 ChangeSet (liquibase.changelog.ChangeSet)1 DatabaseChangeLog (liquibase.changelog.DatabaseChangeLog)1 Database (liquibase.database.Database)1 JdbcConnection (liquibase.database.jvm.JdbcConnection)1 DatabaseException (liquibase.exception.DatabaseException)1 MigrationFailedException (liquibase.exception.MigrationFailedException)1 ClassLoaderResourceAccessor (liquibase.resource.ClassLoaderResourceAccessor)1 HibernateException (org.hibernate.HibernateException)1