Search in sources :

Example 1 with AddAutoIncrementStatement

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

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

the class AddAutoIncrementExecuteTest method noSchema.

@SuppressWarnings("unchecked")
@Test
public void noSchema() throws Exception {
    this.statementUnderTest = new AddAutoIncrementStatement(null, null, TABLE_NAME, COLUMN_NAME, "int", null, null);
    assertCorrect("alter table [table_name] modify column_name serial", PostgresDatabase.class);
    assertCorrect("alter table table_name modify column_name int auto_increment", MySQLDatabase.class);
    assertCorrect("ALTER TABLE [table_name] ALTER COLUMN [column_name] SET GENERATED BY DEFAULT AS IDENTITY", DB2Database.class);
    assertCorrect("alter table table_name alter column column_name int generated by default as identity", HsqlDatabase.class, H2Database.class);
    assertCorrect("ALTER TABLE [table_name] MODIFY [column_name] serial", InformixDatabase.class);
    assertCorrect("ALTER TABLE [table_name] MODIFY [column_name] int default autoincrement", SybaseASADatabase.class);
    assertCorrect("ALTER TABLE [table_name] MODIFY [column_name] int identity", SybaseDatabase.class);
    assertCorrectOnRest("ALTER TABLE [table_name] MODIFY [column_name] int AUTO_INCREMENT");
}
Also used : AddAutoIncrementStatement(liquibase.statement.core.AddAutoIncrementStatement) Test(org.junit.Test)

Example 3 with AddAutoIncrementStatement

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

the class SqlGeneratorFactoryTest method getGenerators.

@Test
public void getGenerators() {
    SortedSet<SqlGenerator> allGenerators = SqlGeneratorFactory.getInstance().getGenerators(new AddAutoIncrementStatement(null, null, "person", "name", "varchar(255)", null, null), new H2Database());
    assertNotNull(allGenerators);
    assertEquals(1, allGenerators.size());
}
Also used : AddAutoIncrementStatement(liquibase.statement.core.AddAutoIncrementStatement) H2Database(liquibase.database.core.H2Database) Test(org.junit.Test)

Aggregations

AddAutoIncrementStatement (liquibase.statement.core.AddAutoIncrementStatement)3 Test (org.junit.Test)2 H2Database (liquibase.database.core.H2Database)1 PostgresDatabase (liquibase.database.core.PostgresDatabase)1 SequenceNextValueFunction (liquibase.statement.SequenceNextValueFunction)1 SqlStatement (liquibase.statement.SqlStatement)1 AddDefaultValueStatement (liquibase.statement.core.AddDefaultValueStatement)1 CreateSequenceStatement (liquibase.statement.core.CreateSequenceStatement)1 SetNullableStatement (liquibase.statement.core.SetNullableStatement)1