Search in sources :

Example 1 with SetNullableStatement

use of liquibase.statement.core.SetNullableStatement in project liquibase by liquibase.

the class AddAutoIncrementChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    if (database instanceof PostgresDatabase) {
        String sequenceName = (getTableName() + "_" + getColumnName() + "_seq");
        String escapedTableName = database.escapeObjectName(getTableName(), Table.class);
        String escapedColumnName = database.escapeObjectName(getColumnName(), Table.class);
        if (escapedTableName != null && escapedColumnName != null && !escapedTableName.startsWith("\"") && !escapedColumnName.startsWith("\"")) {
            sequenceName = sequenceName.toLowerCase();
        }
        String schemaPrefix;
        if (this.schemaName == null) {
            schemaPrefix = database.getDefaultSchemaName();
        } else {
            schemaPrefix = this.schemaName;
        }
        if (schemaPrefix == null) {
            schemaPrefix = "";
        } else {
            schemaPrefix = schemaPrefix + ".";
        }
        return new SqlStatement[] { new CreateSequenceStatement(catalogName, this.schemaName, sequenceName), new SetNullableStatement(catalogName, this.schemaName, getTableName(), getColumnName(), null, false), new AddDefaultValueStatement(catalogName, this.schemaName, getTableName(), getColumnName(), getColumnDataType(), new SequenceNextValueFunction(schemaPrefix + sequenceName)) };
    }
    return new SqlStatement[] { new AddAutoIncrementStatement(getCatalogName(), getSchemaName(), getTableName(), getColumnName(), getColumnDataType(), getStartWith(), getIncrementBy()) };
}
Also used : PostgresDatabase(liquibase.database.core.PostgresDatabase) SqlStatement(liquibase.statement.SqlStatement) SetNullableStatement(liquibase.statement.core.SetNullableStatement) CreateSequenceStatement(liquibase.statement.core.CreateSequenceStatement) SequenceNextValueFunction(liquibase.statement.SequenceNextValueFunction) AddAutoIncrementStatement(liquibase.statement.core.AddAutoIncrementStatement) AddDefaultValueStatement(liquibase.statement.core.AddDefaultValueStatement)

Example 2 with SetNullableStatement

use of liquibase.statement.core.SetNullableStatement in project liquibase by liquibase.

the class AddNotNullConstraintChange method generateStatements.

@Override
public SqlStatement[] generateStatements(Database database) {
    ////        if (database instanceof SQLiteDatabase) {
    //    		// return special statements for SQLite databases
    //    		return generateStatementsForSQLiteDatabase(database);
    //        }
    List<SqlStatement> statements = new ArrayList<SqlStatement>();
    if (defaultNullValue != null) {
        statements.add(new UpdateStatement(getCatalogName(), getSchemaName(), getTableName()).addNewColumnValue(getColumnName(), defaultNullValue).setWhereClause(database.escapeObjectName(getColumnName(), Column.class) + " IS NULL"));
    }
    statements.add(new SetNullableStatement(getCatalogName(), getSchemaName(), getTableName(), getColumnName(), getColumnDataType(), false, getConstraintName()));
    if (database instanceof DB2Database) {
        statements.add(new ReorganizeTableStatement(getCatalogName(), getSchemaName(), getTableName()));
    }
    return statements.toArray(new SqlStatement[statements.size()]);
}
Also used : DB2Database(liquibase.database.core.DB2Database) SqlStatement(liquibase.statement.SqlStatement) UpdateStatement(liquibase.statement.core.UpdateStatement) SetNullableStatement(liquibase.statement.core.SetNullableStatement) Column(liquibase.structure.core.Column) ReorganizeTableStatement(liquibase.statement.core.ReorganizeTableStatement) ArrayList(java.util.ArrayList)

Example 3 with SetNullableStatement

use of liquibase.statement.core.SetNullableStatement in project liquibase by liquibase.

the class SetNullableGeneratorTest method testGenerateOracleNotNullSql.

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

Example 4 with SetNullableStatement

use of liquibase.statement.core.SetNullableStatement in project opennms by OpenNMS.

the class AddNamedAutoIncrementChange method generateStatements.

@Override
public SqlStatement[] generateStatements(final Database database) {
    final List<SqlStatement> statements = new ArrayList<SqlStatement>();
    if (database instanceof PostgresDatabase) {
        String sequenceName = m_sequenceName;
        if (m_sequenceName == null) {
            sequenceName = (getTableName() + "_" + getColumnName() + "_seq").toLowerCase();
            statements.add(new CreateSequenceStatement(getSchemaName(), sequenceName));
        }
        statements.add(new SetNullableStatement(getSchemaName(), getTableName(), getColumnName(), null, false));
        statements.add(new AddDefaultValueStatement(getSchemaName(), getTableName(), getColumnName(), getColumnDataType(), new DatabaseFunction("NEXTVAL('" + sequenceName + "')")));
        return statements.toArray(new SqlStatement[0]);
    } else {
        return super.generateStatements(database);
    }
}
Also used : SqlStatement(liquibase.statement.SqlStatement) PostgresDatabase(liquibase.database.core.PostgresDatabase) SetNullableStatement(liquibase.statement.core.SetNullableStatement) DatabaseFunction(liquibase.statement.DatabaseFunction) CreateSequenceStatement(liquibase.statement.core.CreateSequenceStatement) ArrayList(java.util.ArrayList) AddDefaultValueStatement(liquibase.statement.core.AddDefaultValueStatement)

Example 5 with SetNullableStatement

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

Aggregations

SetNullableStatement (liquibase.statement.core.SetNullableStatement)5 SqlStatement (liquibase.statement.SqlStatement)3 ArrayList (java.util.ArrayList)2 PostgresDatabase (liquibase.database.core.PostgresDatabase)2 Sql (liquibase.sql.Sql)2 AddDefaultValueStatement (liquibase.statement.core.AddDefaultValueStatement)2 CreateSequenceStatement (liquibase.statement.core.CreateSequenceStatement)2 Test (org.junit.Test)2 DB2Database (liquibase.database.core.DB2Database)1 DatabaseFunction (liquibase.statement.DatabaseFunction)1 SequenceNextValueFunction (liquibase.statement.SequenceNextValueFunction)1 AddAutoIncrementStatement (liquibase.statement.core.AddAutoIncrementStatement)1 ReorganizeTableStatement (liquibase.statement.core.ReorganizeTableStatement)1 UpdateStatement (liquibase.statement.core.UpdateStatement)1 Column (liquibase.structure.core.Column)1