Search in sources :

Example 1 with CreateSequenceStatement

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

use of liquibase.statement.core.CreateSequenceStatement 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 3 with CreateSequenceStatement

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

the class ChangeFactoryTest method supportStatement.

@Test
public void supportStatement() throws Exception {
    CreateSequenceStatement statement = new CreateSequenceStatement(null, null, "seq_my_table");
    MSSQLDatabase database10 = new MSSQLDatabase() {

        @Override
        public int getDatabaseMajorVersion() throws DatabaseException {
            return 10;
        }
    };
    MSSQLDatabase database11 = new MSSQLDatabase() {

        @Override
        public int getDatabaseMajorVersion() throws DatabaseException {
            return 11;
        }
    };
    //make sure there is no problem with SqlGeneratorFactory.generatorsByKey cache
    ChangeFactory.getInstance();
    assertFalse("unsupported create sequence", SqlGeneratorFactory.getInstance().supports(statement, database10));
    assertTrue("supported create sequence", SqlGeneratorFactory.getInstance().supports(statement, database11));
}
Also used : CreateSequenceStatement(liquibase.statement.core.CreateSequenceStatement) MSSQLDatabase(liquibase.database.core.MSSQLDatabase) Test(org.junit.Test)

Aggregations

CreateSequenceStatement (liquibase.statement.core.CreateSequenceStatement)3 PostgresDatabase (liquibase.database.core.PostgresDatabase)2 SqlStatement (liquibase.statement.SqlStatement)2 AddDefaultValueStatement (liquibase.statement.core.AddDefaultValueStatement)2 SetNullableStatement (liquibase.statement.core.SetNullableStatement)2 ArrayList (java.util.ArrayList)1 MSSQLDatabase (liquibase.database.core.MSSQLDatabase)1 DatabaseFunction (liquibase.statement.DatabaseFunction)1 SequenceNextValueFunction (liquibase.statement.SequenceNextValueFunction)1 AddAutoIncrementStatement (liquibase.statement.core.AddAutoIncrementStatement)1 Test (org.junit.Test)1